receiveDownloadRecordTableView.m 29 KB

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