downloadManager.m 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. //
  2. // downloadManager.m
  3. // 隐私保护
  4. //
  5. // Created by xd h on 2024/1/9.
  6. //
  7. #import "downloadManager.h"
  8. @implementation downloadManager
  9. static downloadManager * cur_downloadManager = nil;
  10. +(downloadManager *)shareInstance;
  11. {
  12. static dispatch_once_t onceToken;
  13. dispatch_once(&onceToken, ^{
  14. cur_downloadManager = [[downloadManager alloc] init];
  15. });
  16. return cur_downloadManager;
  17. }
  18. - (id)init
  19. {
  20. self = [super init];
  21. if (self) {
  22. //[self initManager];
  23. }
  24. return self;
  25. }
  26. #pragma mark 读取数据库数据
  27. - (void)getDataInDatabaseFun:(BOOL)isReGet complete:(custom_complete_Arr)complete
  28. {
  29. if(_databaseArr && _databaseArr.count == 3 && !isReGet){
  30. complete(_databaseArr);
  31. return;
  32. }
  33. if(!_databaseArr)
  34. {
  35. _databaseArr = [NSMutableArray new];
  36. }
  37. //KWeakSelf
  38. //dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{
  39. [couldPhoneFileModel bg_findAsync:download_tableName limit:0 orderBy:nil desc:NO complete:^(NSArray * _Nullable array) {
  40. NSMutableArray *failArr = [NSMutableArray new];
  41. NSMutableArray *doneArr = [NSMutableArray new];
  42. NSMutableArray *otherArr = [NSMutableArray new];
  43. for (couldPhoneFileModel * curModel in array) {
  44. if(curModel.curDownloadStateType == downloadStateFail){
  45. [failArr addObject:curModel];
  46. }
  47. else if(curModel.curDownloadStateType == downloadStateDone){
  48. [doneArr addObject:curModel];
  49. }
  50. else{
  51. [otherArr addObject:curModel];
  52. }
  53. }
  54. NSLock *lock = [NSLock new];
  55. [lock lock];
  56. NSMutableArray *newArr = [NSMutableArray new];
  57. [newArr addObject:otherArr];
  58. [newArr addObject:doneArr];
  59. [newArr addObject:failArr];
  60. self->_databaseArr = newArr;
  61. [lock unlock];
  62. complete(self->_databaseArr);
  63. }];
  64. //});
  65. }
  66. - (void)handleCouldPhoneFileModelToDownloadFileDataFunBy:(NSMutableArray*)indexPathsForSelectedItems complete:(custom_complete_Arr)complete
  67. {
  68. if(!indexPathsForSelectedItems && indexPathsForSelectedItems.count == 0){
  69. complete(nil);
  70. return;
  71. }
  72. for (couldPhoneFileModel*model in indexPathsForSelectedItems) {
  73. model.bg_tableName = download_tableName;
  74. }
  75. KWeakSelf
  76. [couldPhoneFileModel bg_saveOrUpdateArrayAsync:indexPathsForSelectedItems complete:^(BOOL isSuccess) {
  77. [weakSelf getDataInDatabaseFun:YES complete:^(NSMutableArray * _Nonnull Arr) {
  78. complete(Arr);
  79. [weakSelf planToDownloadFileFun];
  80. }];
  81. }];
  82. }
  83. - (void)planToDownloadFileFun
  84. {
  85. if(!_databaseArr || _databaseArr.count != 3){
  86. return;
  87. }
  88. _downLoadFileModelDataArr = _databaseArr[0];
  89. [self beginDownloadFileFun];
  90. }
  91. - (void)beginDownloadFileFun
  92. {
  93. if(_downLoadFileModelDataArr.count == 0)
  94. {
  95. return;
  96. }
  97. _isSuspendType = NO;
  98. _isDownloadIngType = YES;
  99. _curDownloadFileModel = _downLoadFileModelDataArr.lastObject;
  100. [[NSNotificationCenter defaultCenter] postNotificationName:downloadFileBeginNotification object:_curDownloadFileModel];
  101. }
  102. - (void)DownloadFileDoneOneFileFun
  103. {
  104. _curDownloadFileModel.curDownloadStateType = downloadStateDone;
  105. [_downLoadFileModelDataArr removeObject:_curDownloadFileModel];
  106. [self beginDownloadFileFun];
  107. }
  108. @end