receiveDownloadRecordTableView.m 27 KB

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