receiveDownloadRecordTableView.m 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839
  1. //
  2. // receiveDownloadRecordTableView.m
  3. // 双子星云手机
  4. //
  5. // Created by xd h on 2024/5/23.
  6. //
  7. #import "receiveDownloadRecordTableView.h"
  8. #import "boxDownloadFileRecordCell.h"
  9. #import "UIScrollView+EmptyDataSet.h"
  10. #import "uploadFileRecordTableViewHeadView.h"
  11. @interface receiveDownloadRecordTableView()<UITableViewDataSource,UITableViewDelegate,DZNEmptyDataSetSource, DZNEmptyDataSetDelegate>
  12. {
  13. }
  14. @property (nonatomic, strong) NSMutableArray *curDataArr;
  15. @end
  16. @implementation receiveDownloadRecordTableView
  17. - (id)initWithFrame:(CGRect)frame {
  18. self = [super initWithFrame:frame];
  19. if (self) {
  20. [self initCommon];
  21. [self initTableHeaderInSectionFun];
  22. _selectModelArr = [NSMutableArray new];
  23. _curDataArr = [NSMutableArray new];
  24. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(downloadTaskExeIng:) name:SGDownloadTaskExeing object:nil];
  25. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(downloadTaskExeEnd:) name:SGDownloadTaskExeEnd object:nil];
  26. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(downloadTaskExeError:) name:SGDownloadTaskExeError object:nil];
  27. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(downloadTaskExeSupend:) name:SGDownloadTaskExeSuspend object:nil];
  28. }
  29. return self;
  30. }
  31. - (void)initCommon {
  32. self.delegate = self;
  33. self.dataSource = self;
  34. self.showsVerticalScrollIndicator = NO;
  35. self.showsHorizontalScrollIndicator = NO;
  36. [self setSeparatorStyle:(UITableViewCellSeparatorStyleNone)];
  37. [self setSeparatorColor:[UIColor clearColor]];
  38. [self setBackgroundColor:[UIColor clearColor]];
  39. [self setTableFooterView:[UIView new]];
  40. [self setBounces:YES];
  41. if (@available(iOS 15.0, *)) {
  42. self.sectionHeaderTopPadding = 0;
  43. }
  44. //空数据引入第三方开源处理
  45. self.emptyDataSetSource = self;
  46. self.emptyDataSetDelegate = self;
  47. }
  48. - (void)setOutSideDataArr:(NSMutableArray *)outSideDataArr{
  49. if(!outSideDataArr || outSideDataArr.count != 3){
  50. return;
  51. }
  52. _outSideDataArr = outSideDataArr;
  53. _downloadingArr = [NSMutableArray arrayWithArray:outSideDataArr[0]];
  54. for (ShareFileDataModel*model in _downloadingArr) {
  55. model.downloadBoxStateType = downloadBoxStateDownloadloading;
  56. }
  57. _downloadDoneArr = [NSMutableArray arrayWithArray:outSideDataArr[1]];
  58. _downloadfailArr = [NSMutableArray arrayWithArray:outSideDataArr[2]];
  59. [self RefreshAllDataFun];
  60. }
  61. - (void)RefreshAllDataFun
  62. {
  63. NSMutableArray *totalArr = [NSMutableArray new];
  64. if(_downloadingArr.count>0){
  65. [totalArr addObject:_downloadingArr];
  66. }
  67. if(_downloadfailArr.count>0){
  68. [totalArr addObject:_downloadfailArr];
  69. }
  70. if(_downloadDoneArr.count>0){
  71. [totalArr addObject:_downloadDoneArr];
  72. }
  73. _curDataArr = totalArr;
  74. [self reloadDataFun];
  75. }
  76. #pragma mark - 列表委托
  77. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
  78. return _curDataArr.count;
  79. }
  80. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  81. if(section < _curDataArr.count){
  82. NSArray *curArr = _curDataArr[section];
  83. return curArr.count;
  84. }
  85. return 0;
  86. }
  87. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  88. NSInteger row = indexPath.row;
  89. NSInteger section = indexPath.section;
  90. static NSString *identifier = @"boxDownloadFileRecordCell";
  91. boxDownloadFileRecordCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];
  92. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  93. if (!cell){
  94. cell = [[boxDownloadFileRecordCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
  95. [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
  96. [cell setBackgroundColor:[UIColor clearColor]];
  97. [cell setAccessoryType:(UITableViewCellAccessoryNone)];
  98. }
  99. if(section < _curDataArr.count){
  100. NSMutableArray *curArr = _curDataArr[section];
  101. if(row < curArr.count){
  102. ShareFileDataModel *model = curArr[row];
  103. cell.curShareFileDataModel = model;
  104. cell.isEditType = _isEditType;
  105. if(_isEditType){
  106. if([_selectModelArr containsObject:model]){
  107. cell.isSelectType = YES;
  108. }
  109. else{
  110. cell.isSelectType = NO;
  111. }
  112. }
  113. KWeakSelf
  114. cell.didLongPressClick = ^{
  115. [weakSelf didLongPressClickFun];
  116. };
  117. cell.didClckSelectBut = ^(BOOL isSelect) {
  118. [weakSelf selectModelOneByOne:model BySelect:isSelect];
  119. };
  120. cell.didTapPressClick = ^{
  121. if(model.downloadBoxStateType == downloadBoxStateDownloadloading){
  122. model.downloadBoxStateType = downloadBoxStateSuspend;
  123. [weakSelf handleDownloadingStateTapFunBy:YES with:model];
  124. }
  125. else if(model.downloadBoxStateType == downloadBoxStateSuspend){
  126. model.downloadBoxStateType = downloadBoxStateDownloadloading;
  127. [weakSelf handleDownloadingStateTapFunBy:NO with:model];
  128. }
  129. else if(model.downloadBoxStateType == downloadBoxStateFail){
  130. [weakSelf handleUploadFailStateWith:model];
  131. }
  132. };
  133. }
  134. }
  135. return cell;
  136. }
  137. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  138. return 70;
  139. }
  140. - (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
  141. {
  142. NSInteger curType = 0;// 0 上传中 1 失败 2 完成
  143. if(section == 2){
  144. curType = 2;
  145. }
  146. else if(section == 1){
  147. if(_downloadingArr.count == 0){
  148. curType = 2;
  149. }
  150. else if(_downloadfailArr.count == 0){
  151. curType = 2;
  152. }
  153. else{
  154. curType = 1;
  155. }
  156. }
  157. else{
  158. if(_downloadingArr.count > 0){
  159. curType = 0;
  160. }
  161. else if(_downloadfailArr.count > 0){
  162. curType = 1;
  163. }
  164. else{
  165. curType = 2;
  166. }
  167. }
  168. [self RefresHeadUIFun];
  169. if(curType == 2){
  170. return _doneHeadView;
  171. }
  172. else if(curType == 1){
  173. return _failHeadView;
  174. }
  175. else{
  176. return _downloadingHeadView;
  177. }
  178. }
  179. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
  180. {
  181. return 40;
  182. }
  183. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  184. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  185. }
  186. #pragma mark 空数据
  187. - (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView {
  188. NSString *imageName = @"common_no_data_pic";
  189. return [UIImage imageNamed:imageName];
  190. }
  191. - (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView {
  192. NSString *text = NSLocalizedString(@"common_no_data_tip",nil);
  193. NSDictionary *attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:16.0f],
  194. NSForegroundColorAttributeName: HW999999Color};
  195. return [[NSAttributedString alloc] initWithString:text attributes:attributes];
  196. }
  197. //调整图片位置
  198. - (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView {
  199. return -150;
  200. }
  201. -(void)reloadDataFun{
  202. mainBlock(^{
  203. [self RefresHeadUIFun];
  204. [self reloadData];
  205. });
  206. }
  207. #pragma mark 初始化段头
  208. - (void)initTableHeaderInSectionFun
  209. {
  210. KWeakSelf
  211. if(!_downloadingHeadView){
  212. _downloadingHeadView = [[uploadFileRecordTableViewHeadView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, 40)];
  213. //headView.backgroundColor = [UIColor greenColor];
  214. NSString *leftStr = NSLocalizedString(@"File_upload_Record_downloading",nil);
  215. NSString *rightStr = NSLocalizedString(@"File_upload_Record_all_suspend",nil);
  216. NSString *rightSelectStr = NSLocalizedString(@"File_upload_Record_all_open",nil);
  217. NSString *titleStr = [[NSString alloc] initWithFormat:@"%@ (%ld)",leftStr,_downloadingArr.count];
  218. _downloadingHeadView.titleLabel.text = titleStr;
  219. [_downloadingHeadView.rightButton setTitle:rightStr forState:UIControlStateNormal];
  220. [_downloadingHeadView.rightButton setTitle:rightSelectStr forState:UIControlStateSelected];
  221. _downloadingHeadView.didClickButFun = ^{
  222. [weakSelf didClickRightButtonFun:0];
  223. };
  224. }
  225. if(!_failHeadView){
  226. _failHeadView = [[uploadFileRecordTableViewHeadView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, 40)];
  227. //headView.backgroundColor = [UIColor greenColor];
  228. NSString *leftStr = NSLocalizedString(@"File_upload_Record_download_fail",nil);
  229. NSString *rightStr = NSLocalizedString(@"File_upload_Record_clear_Record",nil);
  230. NSString *titleStr = [[NSString alloc] initWithFormat:@"%@ (%ld)",leftStr,_downloadfailArr.count];
  231. _failHeadView.titleLabel.text = titleStr;
  232. [_failHeadView.rightButton setTitle:rightStr forState:UIControlStateNormal];
  233. _failHeadView.didClickButFun = ^{
  234. [weakSelf didClickRightButtonFun:2];
  235. };
  236. }
  237. if(!_doneHeadView){
  238. _doneHeadView = [[uploadFileRecordTableViewHeadView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, 40)];
  239. //headView.backgroundColor = [UIColor greenColor];
  240. NSString *leftStr = NSLocalizedString(@"File_upload_Record_did_download",nil);
  241. NSString *rightStr = NSLocalizedString(@"File_upload_Record_clear_Record",nil);
  242. NSString *titleStr = [[NSString alloc] initWithFormat:@"%@ (%ld)",leftStr,_downloadDoneArr.count];
  243. _doneHeadView.titleLabel.text = titleStr;
  244. [_doneHeadView.rightButton setTitle:rightStr forState:UIControlStateNormal];
  245. _doneHeadView.didClickButFun = ^{
  246. [weakSelf didClickRightButtonFun:1];
  247. };
  248. }
  249. }
  250. #pragma mark 点击头部右边按钮
  251. - (void)didClickRightButtonFun:(NSInteger)section
  252. {
  253. if(_didClickSectionHeadViewRightButton){
  254. _didClickSectionHeadViewRightButton(section);
  255. }
  256. [self didClikRightButInUploadTableVieFunBy:section];
  257. }
  258. #pragma mark 下载文件列表的头部点击事件
  259. - (void)didClikRightButInUploadTableVieFunBy:(NSInteger)section
  260. {
  261. if(section == 0){
  262. [self didClickAllSuspendInLoadingFun];
  263. }
  264. else if(section == 1){//成功
  265. [self didClickClearRecordInDoneFun];
  266. }
  267. else if(section == 2){//失败
  268. [self didClickClearRecordInFailFun];
  269. }
  270. }
  271. #pragma mark 刷新头部数据
  272. -(void)RefresHeadUIFun
  273. {
  274. if(_downloadingHeadView){
  275. NSString *leftStr = NSLocalizedString(@"File_upload_Record_downloading",nil);
  276. NSString *rightStr = NSLocalizedString(@"File_upload_Record_all_suspend",nil);
  277. NSString *rightSelectStr = NSLocalizedString(@"File_upload_Record_all_open",nil);
  278. NSString *titleStr = [[NSString alloc] initWithFormat:@"%@ (%ld)",leftStr,_downloadingArr.count];
  279. _downloadingHeadView.titleLabel.text = titleStr;
  280. [_downloadingHeadView.rightButton setTitle:rightStr forState:UIControlStateNormal];
  281. [_downloadingHeadView.rightButton setTitle:rightSelectStr forState:UIControlStateSelected];
  282. if(_downloadingArr.count == 0){
  283. _downloadingHeadView.hidden = YES;
  284. }
  285. else{
  286. _downloadingHeadView.hidden = NO;
  287. }
  288. }
  289. if(_failHeadView){
  290. NSString *leftStr = NSLocalizedString(@"File_upload_Record_download_fail",nil);
  291. NSString *rightStr = NSLocalizedString(@"File_upload_Record_clear_Record",nil);
  292. NSString *titleStr = [[NSString alloc] initWithFormat:@"%@ (%ld)",leftStr,_downloadfailArr.count];
  293. _failHeadView.titleLabel.text = titleStr;
  294. [_failHeadView.rightButton setTitle:rightStr forState:UIControlStateNormal];
  295. if(_downloadfailArr.count == 0){
  296. _failHeadView.hidden = YES;
  297. }
  298. else{
  299. _failHeadView.hidden = NO;
  300. }
  301. }
  302. if(_doneHeadView){
  303. NSString *leftStr = NSLocalizedString(@"File_upload_Record_did_download",nil);
  304. NSString *rightStr = NSLocalizedString(@"File_upload_Record_clear_Record",nil);
  305. NSString *titleStr = [[NSString alloc] initWithFormat:@"%@ (%ld)",leftStr,_downloadDoneArr.count];
  306. _doneHeadView.titleLabel.text = titleStr;
  307. [_doneHeadView.rightButton setTitle:rightStr forState:UIControlStateNormal];
  308. if(_downloadDoneArr.count == 0){
  309. _doneHeadView.hidden = YES;
  310. }
  311. else{
  312. _doneHeadView.hidden = NO;
  313. }
  314. }
  315. }
  316. #pragma mark 点击全选
  317. - (void)setIsSelectAllType:(BOOL)isSelectAllType
  318. {
  319. //_selectModelArr = [NSMutableArray arrayWithArray:_curDataArr];
  320. _selectModelArr = [NSMutableArray new];
  321. [_selectModelArr addObjectsFromArray:_downloadingArr];
  322. [_selectModelArr addObjectsFromArray:_downloadfailArr];
  323. [_selectModelArr addObjectsFromArray:_downloadDoneArr];
  324. [self reloadDataFun];
  325. }
  326. -(void)setIsEditType:(BOOL)isEditType
  327. {
  328. _isEditType = isEditType;
  329. if(_isEditType){
  330. //_bgScrollV.scrollEnabled = NO;
  331. }
  332. else{
  333. //_bgScrollV.scrollEnabled = YES;
  334. [_selectModelArr removeAllObjects];
  335. }
  336. [self reloadDataFun];
  337. }
  338. #pragma mark cell长按时间
  339. - (void)didLongPressClickFun{
  340. self.isEditType = YES;
  341. if(self->_didLongPressClick){
  342. self->_didLongPressClick();
  343. }
  344. }
  345. #pragma mark 单个点击选中 取消
  346. - (void)selectModelOneByOne:(ShareFileDataModel*)model BySelect:(BOOL)isSelcet
  347. {
  348. if(isSelcet){
  349. [_selectModelArr addObject:model];
  350. }
  351. else{
  352. [_selectModelArr removeObject:model];
  353. }
  354. }
  355. #pragma mark 单个点击选中后删除
  356. - (void)deleteModelOneByOneFun{
  357. if(!_selectModelArr || _selectModelArr.count==0){
  358. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  359. [SVProgressHUD dismiss];
  360. });
  361. return;
  362. }
  363. [[boxDownloadFileManager shareInstance] deleteBoxSaveFileRecordBy:_selectModelArr];
  364. for (ShareFileDataModel *dataModel in _selectModelArr) {
  365. [self handleTotalArrByDelete:dataModel];
  366. }
  367. [_selectModelArr removeAllObjects];
  368. [self reloadDataFun];
  369. }
  370. - (void)handleTotalArrByDelete:(ShareFileDataModel*)delModel
  371. {
  372. if(delModel.downloadBoxStateType == downloadBoxStateDone)
  373. {
  374. NSMutableArray *arr = _downloadDoneArr;
  375. [self deteleModel:delModel inArr:arr];
  376. }
  377. else if(delModel.downloadBoxStateType == downloadBoxStateFail){
  378. NSMutableArray *arr = _downloadfailArr;
  379. [self deteleModel:delModel inArr:arr];
  380. }
  381. else{
  382. NSMutableArray *arr = _downloadingArr;
  383. [self deteleModel:delModel inArr:arr];
  384. }
  385. }
  386. #pragma mark 处理删除内存数据
  387. - (void)deteleModel:(ShareFileDataModel*)delModel inArr:(NSMutableArray*)delArr{
  388. for (ShareFileDataModel *dataModel in delArr) {
  389. if(delModel.bg_id.integerValue == dataModel.bg_id.integerValue){
  390. [delArr removeObject:dataModel];
  391. break;;
  392. }
  393. }
  394. }
  395. #pragma mark 处理上传中的 状态点击事件
  396. - (void)handleDownloadingStateTapFunBy:(BOOL)isSuspendType with:(ShareFileDataModel*)model
  397. {
  398. NSString *urlString = model.fileUrl;
  399. NSString *requestURLEncodedString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  400. if (isSuspendType) {
  401. [[SGDownloadManager shareManager] supendDownloadWithUrl:requestURLEncodedString];
  402. }
  403. else{
  404. [[SGDownloadManager shareManager] startDownLoadWithUrl:requestURLEncodedString];
  405. }
  406. [self reloadDataFun];
  407. }
  408. #pragma mark 处理上传中的 状态点击事件
  409. - (void)handleUploadFailStateWith:(ShareFileDataModel*)model{
  410. [_downloadfailArr removeObject:model];
  411. model.downloadBoxStateType = downloadBoxStateDownloadloading;
  412. [_downloadingArr addObject:model];
  413. [self RefreshAllDataFun];
  414. [[boxDownloadFileManager shareInstance] updataBoxSaveDataInFailBy:model];
  415. NSString *urlString = model.fileUrl;
  416. NSString *requestURLEncodedString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  417. [[SGDownloadManager shareManager] startDownLoadWithUrl:requestURLEncodedString];
  418. }
  419. #pragma mark 点击上传中的全部暂停
  420. - (void)didClickAllSuspendInLoadingFun
  421. {
  422. if(_downloadingHeadView.rightButton.selected){
  423. [[SGDownloadManager shareManager] suspendAllDownloadTask];
  424. }
  425. else{
  426. [[SGDownloadManager shareManager] startAllDownloadTask];
  427. }
  428. }
  429. #pragma mark 点击上传成功的清空记录
  430. - (void)didClickClearRecordInDoneFun
  431. {
  432. /*弹窗提示二次确认*/
  433. KWeakSelf
  434. ComontAlretViewController *nextVC = [[ComontAlretViewController alloc] initWithTiTle:NSLocalizedString(@"File_upload_Record_clear_Tip_title",nil)
  435. msg:NSLocalizedString(@"File_upload_Record_clear_done_Tip_msg",nil)
  436. imageStr:nil
  437. cancelTitle:NSLocalizedString(@"other_cancel",nil)
  438. okTitle:NSLocalizedString(@"other_comment_ok",nil) isOkBtnHighlight:YES
  439. didClickOk:^{
  440. [weakSelf ClearAllRecordInDoneFun];
  441. } didClickCancel:^{
  442. }];
  443. nextVC.modalPresentationStyle = UIModalPresentationCustom;
  444. [[iTools appRootViewController] presentViewController:nextVC animated:YES completion:^{
  445. nextVC.view.superview.backgroundColor = [UIColor clearColor];
  446. }];
  447. }
  448. - (void)ClearAllRecordInDoneFun
  449. {
  450. [[boxDownloadFileManager shareInstance] deleteBoxSaveFileRecordBy:_downloadDoneArr];
  451. [_downloadDoneArr removeAllObjects];
  452. [self RefreshAllDataFun];
  453. }
  454. #pragma mark 点击上传失败的清空记录
  455. - (void)didClickClearRecordInFailFun
  456. {
  457. /*弹窗提示二次确认*/
  458. KWeakSelf
  459. ComontAlretViewController *nextVC = [[ComontAlretViewController alloc] initWithTiTle:NSLocalizedString(@"File_upload_Record_clear_Tip_title",nil)
  460. msg:NSLocalizedString(@"File_upload_Record_clear_fail_Tip_msg",nil)
  461. imageStr:nil
  462. cancelTitle:NSLocalizedString(@"other_cancel",nil)
  463. okTitle:NSLocalizedString(@"other_comment_ok",nil) isOkBtnHighlight:YES
  464. didClickOk:^{
  465. [weakSelf ClearAllRecordInFailFun];
  466. } didClickCancel:^{
  467. }];
  468. nextVC.modalPresentationStyle = UIModalPresentationCustom;
  469. [[iTools appRootViewController] presentViewController:nextVC animated:YES completion:^{
  470. nextVC.view.superview.backgroundColor = [UIColor clearColor];
  471. }];
  472. }
  473. - (void)ClearAllRecordInFailFun
  474. {
  475. [[boxDownloadFileManager shareInstance] deleteBoxSaveFileRecordBy:_downloadfailArr];
  476. [_downloadfailArr removeAllObjects];
  477. [self RefreshAllDataFun];
  478. }
  479. #pragma mark 下载回调
  480. - (void)downloadTaskExeIng:(NSNotification *)notification
  481. {
  482. SGDownloadOperation *model = notification.userInfo.allValues.firstObject;
  483. HLog(@"下载中 %lld",model.currentSize);
  484. KWeakSelf
  485. mainBlock(^{
  486. [weakSelf handldDownloadingDataBy:model];
  487. });
  488. //[[SGDownloadManager shareManager] supendDownloadWithUrl:model.url];
  489. }
  490. - (void)downloadTaskExeSupend:(NSNotification *)notification
  491. {
  492. //SGDownloadOperation *model = notification.userInfo.allValues.firstObject;
  493. HLog(@"暂停/下载等待中");
  494. }
  495. - (void)downloadTaskExeEnd:(NSNotification *)notification
  496. {
  497. SGDownloadOperation *model = notification.userInfo.allValues.firstObject;
  498. KWeakSelf
  499. mainBlock(^{
  500. [weakSelf handldDownloadDoneDataBy:model];
  501. [weakSelf handldDownloadDoneToSaveBy:model];
  502. });
  503. HLog(@"下载成功");
  504. }
  505. - (void)downloadTaskExeError:(NSNotification *)notification
  506. {
  507. SGDownloadOperation *model = notification.userInfo.allValues.firstObject;
  508. KWeakSelf
  509. mainBlock(^{
  510. [weakSelf handldDownloadFailDataBy:model];
  511. });
  512. HLog(@"下载失败");
  513. }
  514. #pragma mark 第一次进来 检测重新下载
  515. - (void)reDownLoadAgainFun
  516. {
  517. if(_reDownLoadAgainType){
  518. return;
  519. }
  520. else{
  521. //_reDownLoadAgainType = YES;
  522. }
  523. //重新下载
  524. NSMutableArray *handleDownloadArr = [NSMutableArray new];
  525. for (ShareFileDataModel * curModel in _downloadingArr) {
  526. if(curModel.downloadBoxStateType != downloadBoxStateSuspend)
  527. {
  528. [handleDownloadArr addObject:curModel];
  529. }
  530. }
  531. if(handleDownloadArr.count >0){
  532. [[boxDownloadFileManager shareInstance] beginDownloadFilesByUrls:handleDownloadArr];
  533. }
  534. }
  535. #pragma mark 下载失败数据回调处理
  536. - (void)handldDownloadFailDataBy:(SGDownloadOperation*)model
  537. {
  538. //解码
  539. NSString * urlString = [model.url stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  540. for (ShareFileDataModel*dataModel in _downloadingArr) {
  541. //HLog(@"22222222 %@\n%@",urlString,dataModel.fileUrl);
  542. if([urlString isEqualToString:dataModel.fileUrl]){
  543. dataModel.totalSize = model.totalSize;
  544. dataModel.currentSize = model.currentSize;
  545. dataModel.downloadBoxStateType = downloadBoxStateFail;
  546. [_downloadfailArr insertObject:dataModel atIndex:0];
  547. [_downloadingArr removeObject:dataModel];
  548. break;
  549. }
  550. }
  551. [self RefreshAllDataFun];
  552. }
  553. #pragma mark 下载中数据回调处理
  554. - (void)handldDownloadingDataBy:(SGDownloadOperation*)model
  555. {
  556. BOOL canReloadData = YES;
  557. //解码
  558. NSString * urlString = [model.url stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  559. for (ShareFileDataModel*dataModel in _downloadingArr) {
  560. //HLog(@"22222222 %@\n%@",urlString,dataModel.fileUrl);
  561. if([urlString isEqualToString:dataModel.fileUrl]){
  562. dataModel.totalSize = model.totalSize;
  563. dataModel.currentSize = model.currentSize;
  564. dataModel.curTimeInterval = [[NSDate date] timeIntervalSince1970];
  565. if(dataModel.downloadBoxStateType == downloadBoxStateSuspend){
  566. canReloadData = NO;
  567. }
  568. break;
  569. }
  570. }
  571. if (canReloadData) {
  572. [self reloadData];
  573. }
  574. }
  575. #pragma mark 下载完成数据回调处理
  576. - (void)handldDownloadDoneDataBy:(SGDownloadOperation*)model
  577. {
  578. //解码
  579. NSString * urlString = [model.url stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  580. for (ShareFileDataModel*dataModel in _downloadingArr) {
  581. //HLog(@"22222222 %@\n%@",urlString,dataModel.fileUrl);
  582. if([urlString isEqualToString:dataModel.fileUrl]){
  583. dataModel.downloadBoxStateType = downloadBoxStateDone;
  584. [_downloadDoneArr insertObject:dataModel atIndex:0];
  585. [_downloadingArr removeObject:dataModel];
  586. break;
  587. }
  588. }
  589. [self RefreshAllDataFun];
  590. }
  591. #pragma mark 下载完后处理保持流程
  592. - (void)handldDownloadDoneToSaveBy:(SGDownloadOperation*)model{
  593. //解码
  594. NSString * urlString = [model.url stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  595. NSArray *nameArr= [urlString componentsSeparatedByString:@"."];
  596. NSString * pathStr= model.fullPath;
  597. //HLog(@"fileName: %@",curShareFileDataModel.fileName);
  598. if (nameArr.count >= 2) {
  599. NSString *lastName = nameArr.lastObject;
  600. lastName = [lastName lowercaseString];
  601. if ([lastName rangeOfString:@"jpg"].location != NSNotFound
  602. ||[lastName rangeOfString:@"jpeg"].location != NSNotFound
  603. ||[lastName rangeOfString:@"png"].location != NSNotFound
  604. ||[lastName rangeOfString:@"gif"].location != NSNotFound
  605. ||[lastName rangeOfString:@"bmp"].location != NSNotFound
  606. ||[lastName rangeOfString:@"svg"].location != NSNotFound
  607. ||[lastName rangeOfString:@"tiff"].location != NSNotFound
  608. ||[lastName rangeOfString:@"heic"].location != NSNotFound
  609. ) {//可以保持到相册
  610. UIImage *image = [UIImage imageWithContentsOfFile:pathStr];
  611. if(image){
  612. [self loadImageFinished:image with:pathStr];
  613. }
  614. }
  615. else if ([lastName rangeOfString:@"mp4"].location != NSNotFound
  616. ||[lastName rangeOfString:@"avi"].location != NSNotFound
  617. ||[lastName rangeOfString:@"flv"].location != NSNotFound
  618. ||[lastName rangeOfString:@"wmv"].location != NSNotFound
  619. ||[lastName rangeOfString:@"wkm"].location != NSNotFound
  620. ||[lastName rangeOfString:@"ts"].location != NSNotFound
  621. ||[lastName rangeOfString:@"mov"].location != NSNotFound
  622. ||[lastName rangeOfString:@"rmvb"].location != NSNotFound
  623. ) {//可以保持到相册
  624. [self loadVideoFinishedBy:pathStr];
  625. }
  626. else{//保存到文件
  627. [self loadOtherDataFinishedBy:pathStr];
  628. }
  629. }
  630. }
  631. - (void)loadImageFinished:(UIImage *)image with:(NSString*)fullPath
  632. {
  633. [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
  634. //写入图片到相册
  635. PHAssetChangeRequest *req = [PHAssetChangeRequest creationRequestForAssetFromImage:image];
  636. } completionHandler:^(BOOL success, NSError * _Nullable error) {
  637. //NSLog(@"success = %d, error = %@", success, error);
  638. if (success) {
  639. HLog(@"已将图片保存至相册");
  640. [[NSFileManager defaultManager] removeItemAtPath:fullPath error:nil];
  641. } else {
  642. HLog(@"未能将图片保存至相册");
  643. }
  644. }];
  645. }
  646. - (void)loadVideoFinishedBy:(NSString*)fullPath
  647. {
  648. NSString*pathStr = fullPath;
  649. PHPhotoLibrary *photoLibrary = [PHPhotoLibrary sharedPhotoLibrary];
  650. [photoLibrary performChanges:^{
  651. [PHAssetChangeRequest creationRequestForAssetFromVideoAtFileURL:[NSURL
  652. fileURLWithPath:pathStr]];
  653. } completionHandler:^(BOOL success, NSError * _Nullable error) {
  654. if (success) {
  655. HLog(@"已将视频保存至相册");
  656. [[NSFileManager defaultManager] removeItemAtPath:pathStr error:nil];
  657. } else {
  658. HLog(@"未能将视频保存至相册");
  659. }
  660. }];
  661. }
  662. //下载音频 文件等
  663. - (void)loadOtherDataFinishedBy:(NSString*)fullPath
  664. {
  665. [[NSNotificationCenter defaultCenter] postNotificationName:NotLoadOtherDataFinished object:fullPath userInfo:nil];
  666. }
  667. @end