receiveDownloadRecordTableView.m 30 KB

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