downloadFileRecordTableView.m 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. //
  2. // downloadFileRecordTableView.m
  3. // 隐私保护
  4. //
  5. // Created by xd h on 2024/1/10.
  6. //
  7. #import "downloadFileRecordTableView.h"
  8. #import "downloadFileRecordCell.h"
  9. #import "UIScrollView+EmptyDataSet.h"
  10. #import "uploadFileRecordTableViewHeadView.h"
  11. @interface downloadFileRecordTableView()<UITableViewDataSource,UITableViewDelegate,DZNEmptyDataSetSource, DZNEmptyDataSetDelegate>
  12. {
  13. }
  14. @property (nonatomic, strong) NSMutableArray *curDataArr;
  15. @end
  16. @implementation downloadFileRecordTableView
  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. }
  25. return self;
  26. }
  27. - (void)initCommon {
  28. self.delegate = self;
  29. self.dataSource = self;
  30. self.showsVerticalScrollIndicator = NO;
  31. self.showsHorizontalScrollIndicator = NO;
  32. [self setSeparatorStyle:(UITableViewCellSeparatorStyleNone)];
  33. [self setSeparatorColor:[UIColor clearColor]];
  34. [self setBackgroundColor:[UIColor clearColor]];
  35. [self setTableFooterView:[UIView new]];
  36. [self setBounces:YES];
  37. if (@available(iOS 15.0, *)) {
  38. self.sectionHeaderTopPadding = 0;
  39. }
  40. //空数据引入第三方开源处理
  41. self.emptyDataSetSource = self;
  42. self.emptyDataSetDelegate = self;
  43. }
  44. - (void)setOutSideDataArr:(NSMutableArray *)outSideDataArr{
  45. if(!outSideDataArr || outSideDataArr.count != 3){
  46. return;
  47. }
  48. _outSideDataArr = outSideDataArr;
  49. _downloadingArr = outSideDataArr[0];
  50. _downloadDoneArr = outSideDataArr[1];
  51. _downloadfailArr = outSideDataArr[2];
  52. [self RefreshAllDataFun];
  53. }
  54. - (void)RefreshAllDataFun
  55. {
  56. NSMutableArray *totalArr = [NSMutableArray new];
  57. if(_downloadingArr.count>0){
  58. [totalArr addObject:_downloadingArr];
  59. }
  60. if(_downloadfailArr.count>0){
  61. [totalArr addObject:_downloadfailArr];
  62. }
  63. if(_downloadDoneArr.count>0){
  64. [totalArr addObject:_downloadDoneArr];
  65. }
  66. _curDataArr = totalArr;
  67. [self reloadDataFun];
  68. }
  69. #pragma mark - 列表委托
  70. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
  71. return _curDataArr.count;
  72. }
  73. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  74. if(section < _curDataArr.count){
  75. NSArray *curArr = _curDataArr[section];
  76. return curArr.count;
  77. }
  78. return 0;
  79. }
  80. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  81. NSInteger row = indexPath.row;
  82. NSInteger section = indexPath.section;
  83. static NSString *identifier = @"downloadFileRecordCell";
  84. downloadFileRecordCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];
  85. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  86. if (!cell){
  87. cell = [[downloadFileRecordCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
  88. [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
  89. [cell setBackgroundColor:[UIColor clearColor]];
  90. [cell setAccessoryType:(UITableViewCellAccessoryNone)];
  91. }
  92. if(section < _curDataArr.count){
  93. NSMutableArray *curArr = _curDataArr[section];
  94. if(row < curArr.count){
  95. couldPhoneFileModel *model = curArr[row];
  96. cell.curCouldPhoneFileModel = model;
  97. cell.isEditType = _isEditType;
  98. if(_isEditType){
  99. if([_selectModelArr containsObject:model]){
  100. cell.isSelectType = YES;
  101. }
  102. else{
  103. cell.isSelectType = NO;
  104. }
  105. }
  106. KWeakSelf
  107. cell.didLongPressClick = ^{
  108. [weakSelf didLongPressClickFun];
  109. };
  110. cell.didClckSelectBut = ^(BOOL isSelect) {
  111. [weakSelf selectModelOneByOne:model BySelect:isSelect];
  112. };
  113. cell.didTapPressClick = ^{
  114. if(model.curDownloadStateType == downloadStateUploading){
  115. model.curDownloadStateType = downloadStateSuspend;
  116. [weakSelf handleDownloadingStateTapFunBy:YES with:model];
  117. }
  118. else if(model.curDownloadStateType == downloadStateSuspend){
  119. model.curDownloadStateType = downloadStateUploading;
  120. [weakSelf handleDownloadingStateTapFunBy:NO with:model];
  121. }
  122. else if(model.curDownloadStateType == downloadStateFail){
  123. [weakSelf handleUploadFailStateWith:model];
  124. }
  125. };
  126. }
  127. }
  128. return cell;
  129. }
  130. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  131. return 70;
  132. }
  133. - (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
  134. {
  135. NSInteger curType = 0;// 0 上传中 1 失败 2 完成
  136. if(section == 2){
  137. curType = 2;
  138. }
  139. else if(section == 1){
  140. if(_downloadingArr.count == 0){
  141. curType = 2;
  142. }
  143. else if(_downloadfailArr.count == 0){
  144. curType = 2;
  145. }
  146. else{
  147. curType = 1;
  148. }
  149. }
  150. else{
  151. if(_downloadingArr.count > 0){
  152. curType = 0;
  153. }
  154. else if(_downloadfailArr.count > 0){
  155. curType = 1;
  156. }
  157. else{
  158. curType = 2;
  159. }
  160. }
  161. [self RefresHeadUIFun];
  162. if(curType == 2){
  163. return _doneHeadView;
  164. }
  165. else if(curType == 1){
  166. return _failHeadView;
  167. }
  168. else{
  169. return _downloadingHeadView;
  170. }
  171. }
  172. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
  173. {
  174. return 40;
  175. }
  176. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  177. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  178. }
  179. #pragma mark 空数据
  180. - (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView {
  181. NSString *imageName = @"uploadFile_noData";
  182. if(self.tag == 101){
  183. imageName = @"uploadFile_noData";
  184. }
  185. else if(self.tag == 102){
  186. imageName = @"uploadFile_noData";
  187. }
  188. return [UIImage imageNamed:imageName];
  189. }
  190. - (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView {
  191. NSString *text = NSLocalizedString(@"File_upload_Record_no_data",nil);
  192. if(self.tag == 101){
  193. text = NSLocalizedString(@"File_download_Record_no_data",nil);
  194. }
  195. else if(self.tag == 102){
  196. text = NSLocalizedString(@"File_backups_Record_no_data",nil);
  197. }
  198. NSDictionary *attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:16.0f],
  199. NSForegroundColorAttributeName: HW999999Color};
  200. return [[NSAttributedString alloc] initWithString:text attributes:attributes];
  201. }
  202. //调整图片位置
  203. - (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView {
  204. return -150;
  205. }
  206. -(void)reloadDataFun{
  207. mainBlock(^{
  208. [self RefresHeadUIFun];
  209. [self reloadData];
  210. });
  211. }
  212. #pragma mark 初始化段头
  213. - (void)initTableHeaderInSectionFun
  214. {
  215. KWeakSelf
  216. if(!_downloadingHeadView){
  217. _downloadingHeadView = [[uploadFileRecordTableViewHeadView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, 40)];
  218. //headView.backgroundColor = [UIColor greenColor];
  219. NSString *leftStr = NSLocalizedString(@"File_upload_Record_downloading",nil);
  220. NSString *rightStr = NSLocalizedString(@"File_upload_Record_all_suspend",nil);
  221. NSString *rightSelectStr = NSLocalizedString(@"File_upload_Record_all_open",nil);
  222. NSString *titleStr = [[NSString alloc] initWithFormat:@"%@ (%ld)",leftStr,_downloadingArr.count];
  223. _downloadingHeadView.titleLabel.text = titleStr;
  224. [_downloadingHeadView.rightButton setTitle:rightStr forState:UIControlStateNormal];
  225. [_downloadingHeadView.rightButton setTitle:rightSelectStr forState:UIControlStateSelected];
  226. _downloadingHeadView.didClickButFun = ^{
  227. [weakSelf didClickRightButtonFun:0];
  228. };
  229. }
  230. if(!_failHeadView){
  231. _failHeadView = [[uploadFileRecordTableViewHeadView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, 40)];
  232. //headView.backgroundColor = [UIColor greenColor];
  233. NSString *leftStr = NSLocalizedString(@"File_upload_Record_download_fail",nil);
  234. NSString *rightStr = NSLocalizedString(@"File_upload_Record_clear_Record",nil);
  235. NSString *titleStr = [[NSString alloc] initWithFormat:@"%@ (%ld)",leftStr,_downloadfailArr.count];
  236. _failHeadView.titleLabel.text = titleStr;
  237. [_failHeadView.rightButton setTitle:rightStr forState:UIControlStateNormal];
  238. _failHeadView.didClickButFun = ^{
  239. [weakSelf didClickRightButtonFun:2];
  240. };
  241. }
  242. if(!_doneHeadView){
  243. _doneHeadView = [[uploadFileRecordTableViewHeadView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, 40)];
  244. //headView.backgroundColor = [UIColor greenColor];
  245. NSString *leftStr = NSLocalizedString(@"File_upload_Record_did_download",nil);
  246. NSString *rightStr = NSLocalizedString(@"File_upload_Record_clear_Record",nil);
  247. NSString *titleStr = [[NSString alloc] initWithFormat:@"%@ (%ld)",leftStr,_downloadDoneArr.count];
  248. _doneHeadView.titleLabel.text = titleStr;
  249. [_doneHeadView.rightButton setTitle:rightStr forState:UIControlStateNormal];
  250. _doneHeadView.didClickButFun = ^{
  251. [weakSelf didClickRightButtonFun:1];
  252. };
  253. }
  254. }
  255. #pragma mark 点击头部右边按钮
  256. - (void)didClickRightButtonFun:(NSInteger)section
  257. {
  258. if(_didClickSectionHeadViewRightButton){
  259. _didClickSectionHeadViewRightButton(section);
  260. }
  261. }
  262. #pragma mark 刷新头部数据
  263. -(void)RefresHeadUIFun
  264. {
  265. if(_downloadingHeadView){
  266. NSString *leftStr = NSLocalizedString(@"File_upload_Record_downloading",nil);
  267. NSString *rightStr = NSLocalizedString(@"File_upload_Record_all_suspend",nil);
  268. NSString *rightSelectStr = NSLocalizedString(@"File_upload_Record_all_open",nil);
  269. NSString *titleStr = [[NSString alloc] initWithFormat:@"%@ (%ld)",leftStr,_downloadingArr.count];
  270. _downloadingHeadView.titleLabel.text = titleStr;
  271. [_downloadingHeadView.rightButton setTitle:rightStr forState:UIControlStateNormal];
  272. [_downloadingHeadView.rightButton setTitle:rightSelectStr forState:UIControlStateSelected];
  273. if(_downloadingArr.count == 0){
  274. _downloadingHeadView.hidden = YES;
  275. }
  276. else{
  277. _downloadingHeadView.hidden = NO;
  278. }
  279. }
  280. if(_failHeadView){
  281. NSString *leftStr = NSLocalizedString(@"File_upload_Record_download_fail",nil);
  282. NSString *rightStr = NSLocalizedString(@"File_upload_Record_clear_Record",nil);
  283. NSString *titleStr = [[NSString alloc] initWithFormat:@"%@ (%ld)",leftStr,_downloadfailArr.count];
  284. _failHeadView.titleLabel.text = titleStr;
  285. [_failHeadView.rightButton setTitle:rightStr forState:UIControlStateNormal];
  286. if(_downloadfailArr.count == 0){
  287. _failHeadView.hidden = YES;
  288. }
  289. else{
  290. _failHeadView.hidden = NO;
  291. }
  292. }
  293. if(_doneHeadView){
  294. NSString *leftStr = NSLocalizedString(@"File_upload_Record_did_download",nil);
  295. NSString *rightStr = NSLocalizedString(@"File_upload_Record_clear_Record",nil);
  296. NSString *titleStr = [[NSString alloc] initWithFormat:@"%@ (%ld)",leftStr,_downloadDoneArr.count];
  297. _doneHeadView.titleLabel.text = titleStr;
  298. [_doneHeadView.rightButton setTitle:rightStr forState:UIControlStateNormal];
  299. if(_downloadDoneArr.count == 0){
  300. _doneHeadView.hidden = YES;
  301. }
  302. else{
  303. _doneHeadView.hidden = NO;
  304. }
  305. }
  306. }
  307. #pragma mark 点击全选
  308. - (void)setIsSelectAllType:(BOOL)isSelectAllType
  309. {
  310. //_selectModelArr = [NSMutableArray arrayWithArray:_curDataArr];
  311. _selectModelArr = [NSMutableArray new];
  312. if(isSelectAllType){
  313. [_selectModelArr addObjectsFromArray:_downloadingArr];
  314. [_selectModelArr addObjectsFromArray:_downloadfailArr];
  315. [_selectModelArr addObjectsFromArray:_downloadDoneArr];
  316. }
  317. [self reloadDataFun];
  318. }
  319. -(void)setIsEditType:(BOOL)isEditType
  320. {
  321. _isEditType = isEditType;
  322. if(_isEditType){
  323. //_bgScrollV.scrollEnabled = NO;
  324. }
  325. else{
  326. //_bgScrollV.scrollEnabled = YES;
  327. [_selectModelArr removeAllObjects];
  328. }
  329. [self reloadDataFun];
  330. }
  331. #pragma mark cell长按时间
  332. - (void)didLongPressClickFun{
  333. self.isEditType = YES;
  334. if(self->_didLongPressClick){
  335. self->_didLongPressClick();
  336. }
  337. }
  338. #pragma mark 单个点击选中 取消
  339. - (void)selectModelOneByOne:(couldPhoneFileModel*)model BySelect:(BOOL)isSelcet
  340. {
  341. if(isSelcet){
  342. [_selectModelArr addObject:model];
  343. }
  344. else{
  345. [_selectModelArr removeObject:model];
  346. }
  347. }
  348. #pragma mark 单个点击选中后删除
  349. - (void)deleteModelOneByOneFun{
  350. if(!_selectModelArr || _selectModelArr.count==0){
  351. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  352. [SVProgressHUD dismiss];
  353. });
  354. return;
  355. }
  356. BOOL isDelAllType = NO;
  357. if(_selectModelArr.count >= _downloadingArr.count){
  358. isDelAllType = YES;
  359. }
  360. [[nasDownloadFileManager shareInstance] deleteDownloadFileRecordBy:_selectModelArr withDelCache:YES withAll:isDelAllType];
  361. //[[nasDownloadFileManager shareInstance] deleteDownloadFileRecordBy:_selectModelArr withDelCache:YES];
  362. [_selectModelArr removeAllObjects];
  363. //重新拿数据
  364. [self refreshGetDatabaseFun];
  365. }
  366. #pragma mark 删除后重新拿数据
  367. - (void)refreshGetDatabaseFun
  368. {
  369. if(_didNeedRefreshGetDatabaseFun){
  370. _didNeedRefreshGetDatabaseFun();
  371. }
  372. }
  373. #pragma mark 处理上传中的 状态点击事件
  374. - (void)handleDownloadingStateTapFunBy:(BOOL)isSuspendType with:(couldPhoneFileModel*)model
  375. {
  376. if (isSuspendType) {
  377. [[nasDownloadFileManager shareInstance] suspendDownloadFileFun:NO withModel:model];
  378. }
  379. else{
  380. NSMutableArray*arr = [NSMutableArray new];
  381. [arr addObject:model];
  382. [[nasDownloadFileManager shareInstance] reDownloadFileFunBy:arr withAll:NO];
  383. }
  384. [self reloadDataFun];
  385. }
  386. #pragma mark 处理上传中的 状态点击事件
  387. - (void)handleUploadFailStateWith:(couldPhoneFileModel*)model{
  388. [_downloadfailArr removeObject:model];
  389. [_downloadingArr addObject:model];
  390. [self RefreshAllDataFun];
  391. if(_didClickReUploadBlock){
  392. _didClickReUploadBlock(model);
  393. }
  394. NSMutableArray*arr = [NSMutableArray new];
  395. model.curDownloadStateType = downloadStateWait;
  396. [arr addObject:model];
  397. [[nasDownloadFileManager shareInstance] reDownloadFileFunBy:arr withAll:NO];
  398. }
  399. @end