downloadManager.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. //
  2. // downloadManager.m
  3. // 隐私保护
  4. //
  5. // Created by xd h on 2024/1/9.
  6. //
  7. #import "downloadManager.h"
  8. #import "cachesFileManager.h"
  9. @implementation downloadManager
  10. static downloadManager * cur_downloadManager = nil;
  11. +(downloadManager *)shareInstance;
  12. {
  13. static dispatch_once_t onceToken;
  14. dispatch_once(&onceToken, ^{
  15. cur_downloadManager = [[downloadManager alloc] init];
  16. });
  17. return cur_downloadManager;
  18. }
  19. - (id)init
  20. {
  21. self = [super init];
  22. if (self) {
  23. //[self initManager];
  24. }
  25. return self;
  26. }
  27. #pragma mark 读取数据库数据
  28. - (void)getDataInDatabaseFun:(BOOL)isReGet complete:(custom_complete_Arr)complete
  29. {
  30. if(_databaseArr && _databaseArr.count == 3 && !isReGet){
  31. complete(_databaseArr);
  32. return;
  33. }
  34. if(!_databaseArr)
  35. {
  36. _databaseArr = [NSMutableArray new];
  37. }
  38. //KWeakSelf
  39. //dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{
  40. [couldPhoneFileModel bg_findAsync:download_tableName limit:0 orderBy:nil desc:NO complete:^(NSArray * _Nullable array) {
  41. NSMutableArray *failArr = [NSMutableArray new];
  42. NSMutableArray *doneArr = [NSMutableArray new];
  43. NSMutableArray *otherArr = [NSMutableArray new];
  44. for (couldPhoneFileModel * curModel in array) {
  45. if(curModel.curDownloadStateType == downloadStateFail){
  46. [failArr addObject:curModel];
  47. }
  48. else if(curModel.curDownloadStateType == downloadStateDone){
  49. [doneArr addObject:curModel];
  50. }
  51. else{
  52. [otherArr addObject:curModel];
  53. }
  54. }
  55. NSLock *lock = [NSLock new];
  56. [lock lock];
  57. NSMutableArray *newArr = [NSMutableArray new];
  58. [newArr addObject:otherArr];
  59. [newArr addObject:doneArr];
  60. [newArr addObject:failArr];
  61. self->_databaseArr = newArr;
  62. [lock unlock];
  63. complete(self->_databaseArr);
  64. }];
  65. //});
  66. }
  67. -(void)handleCouldPhoneFileModelToDownloadFileDataFunBy:(NSMutableArray*)indexPathsForSelectedItems complete:(custom_complete_Arr)complete
  68. {
  69. if(!indexPathsForSelectedItems && indexPathsForSelectedItems.count == 0){
  70. complete(nil);
  71. return;
  72. }
  73. for (couldPhoneFileModel*model in indexPathsForSelectedItems) {
  74. model.bg_tableName = download_tableName;
  75. }
  76. KWeakSelf
  77. //查
  78. [self getDataInDatabaseFun:YES complete:^(NSMutableArray * _Nonnull Arr) {
  79. //排重
  80. NSMutableArray *newDownArr = [weakSelf checkDownloadTaskRepeatFun:indexPathsForSelectedItems];
  81. if(newDownArr.count>0){
  82. //存
  83. [couldPhoneFileModel bg_saveOrUpdateArrayAsync:newDownArr complete:^(BOOL isSuccess) {
  84. //再查
  85. [weakSelf getDataInDatabaseFun:YES complete:^(NSMutableArray * _Nonnull Arr) {
  86. complete(Arr);
  87. [weakSelf planToDownloadFileFun];
  88. }];
  89. }];
  90. }
  91. else{
  92. complete(Arr);
  93. [weakSelf planToDownloadFileFun];
  94. }
  95. }];
  96. }
  97. #pragma mark 检测是否有下载任务重复
  98. - (NSMutableArray*)checkDownloadTaskRepeatFun:(NSMutableArray*)indexPathsForSelectedItems
  99. {
  100. if(!_databaseArr || _databaseArr.count != 3){
  101. return indexPathsForSelectedItems;
  102. }
  103. NSMutableArray *downloadArr = _databaseArr[0];
  104. NSMutableArray *newTaskArr = [NSMutableArray new];
  105. for (couldPhoneFileModel*model in indexPathsForSelectedItems) {
  106. NSString *fileName = [model getFileNameFun];
  107. BOOL isNeedAddType = YES;
  108. for (couldPhoneFileModel* hadModel in downloadArr) {
  109. NSString *hadFileName = [hadModel getFileNameFun];
  110. if([fileName isEqualToString:hadFileName]){
  111. isNeedAddType = NO;
  112. break;
  113. }
  114. }
  115. if(isNeedAddType){
  116. [newTaskArr addObject:model];
  117. }
  118. }
  119. return newTaskArr;
  120. }
  121. - (void)planToDownloadFileFun
  122. {
  123. if(!_databaseArr || _databaseArr.count != 3){
  124. return;
  125. }
  126. _downLoadFileModelDataArr = _databaseArr[0];
  127. [self beginDownloadFileFun];
  128. }
  129. - (void)beginDownloadFileFun
  130. {
  131. if(_downLoadFileModelDataArr.count == 0)
  132. {
  133. return;
  134. }
  135. _isSuspendType = NO;
  136. _isDownloadIngType = YES;
  137. if(_reDownloadIngSelectIndex > 0
  138. && _reDownloadIngSelectIndex < _downLoadFileModelDataArr.count){
  139. _curDownloadFileModel = _downLoadFileModelDataArr[_reDownloadIngSelectIndex];
  140. }
  141. else
  142. {
  143. _curDownloadFileModel = _downLoadFileModelDataArr.firstObject;
  144. }
  145. if(!_curDownloadFileModel){
  146. return;
  147. }
  148. _curDownloadFileModel.curDownloadStateType = downloadStateUploading;
  149. [[NSNotificationCenter defaultCenter] postNotificationName:downloadFileBeginNotification object:_curDownloadFileModel];
  150. }
  151. - (void)DownloadFileDoneOneFileFun
  152. {
  153. _curDownloadFileModel.curDownloadStateType = downloadStateDone;
  154. // NSString *fileName = [_curDownloadFileModel getFileNameFun];
  155. // [cachesFileManager removeItemAtPath:fileName type:DownLoadFileType error:nil];
  156. [self DownloadFileChangeingOneFileFun];
  157. if(_isSuspendType){
  158. return;
  159. }
  160. [self beginDownloadFileFun];
  161. }
  162. - (void)DownloadFileChangeingOneFileFun
  163. {
  164. if(_curDownloadFileModel.curDownloadStateType == downloadStateDone){
  165. [_downLoadFileModelDataArr removeObject:_curDownloadFileModel];
  166. [self handleDatabaseArrAddObjectInDone:_curDownloadFileModel];
  167. }
  168. else if(_curDownloadFileModel.curDownloadStateType == uploadStateFail){
  169. [self handleDatabaseArrDeleteObjectInDownloading:_curDownloadFileModel];
  170. [self handleDatabaseArrAddObjectInFail:_curDownloadFileModel];
  171. }
  172. [_curDownloadFileModel bg_saveOrUpdateAsync:^(BOOL isSuccess) {
  173. //HLog(@"%@ 写入 %@", model.filename, isSuccess ? @"成功":@"失败");
  174. }];
  175. if(!_isSuspendType || _curDownloadFileModel.curDownloadStateType != uploadStateSuspend){
  176. [[NSNotificationCenter defaultCenter] postNotificationName:downloadFileRefreshNotification object:_curDownloadFileModel];
  177. }
  178. }
  179. - (void)handleDatabaseArrDeleteObjectInDownloading:(couldPhoneFileModel*)model
  180. {
  181. if(_databaseArr && _databaseArr.count == 3 ){
  182. NSMutableArray *ingArr = _databaseArr[0];
  183. [ingArr removeObject:model];
  184. }
  185. }
  186. - (void)handleDatabaseArrAddObjectInDone:(couldPhoneFileModel*)model
  187. {
  188. if(_databaseArr && _databaseArr.count == 3 ){
  189. NSMutableArray *doneArr = _databaseArr[1];
  190. [doneArr insertObject:model atIndex:0];
  191. }
  192. }
  193. - (void)handleDatabaseArrAddObjectInFail:(couldPhoneFileModel*)model
  194. {
  195. if(_databaseArr && _databaseArr.count == 3 ){
  196. NSMutableArray *failArr = _databaseArr[2];
  197. [failArr insertObject:model atIndex:0];
  198. }
  199. }
  200. - (void)suspendDownloadFileFun:(BOOL)isSuspendAll
  201. {
  202. _isSuspendType = YES;
  203. if(isSuspendAll){
  204. for (couldPhoneFileModel *couldPhoneFileMod in _downLoadFileModelDataArr) {
  205. couldPhoneFileMod.curDownloadStateType = downloadStateSuspend;
  206. }
  207. }
  208. }
  209. - (void)reDownloadFileFunBy:(NSMutableArray*)Arr
  210. {
  211. if(!_downLoadFileModelDataArr){
  212. _downLoadFileModelDataArr = [NSMutableArray new];
  213. }
  214. _reDownloadIngSelectIndex = 0;
  215. for (uploadFileDataModel*addModel in Arr) {
  216. BOOL needAddModel = YES;
  217. for (int i=0;i< _downLoadFileModelDataArr.count;i++)
  218. {
  219. couldPhoneFileModel*preModel = _downLoadFileModelDataArr[i];
  220. if(addModel.bg_id.integerValue == preModel.bg_id.integerValue)
  221. {
  222. needAddModel = NO;
  223. if(_reDownloadIngSelectIndex == 0){
  224. _reDownloadIngSelectIndex = i;
  225. if(i==0){
  226. _reDownloadIngSelectIndex = -1;
  227. }
  228. }
  229. break;
  230. }
  231. }
  232. if(needAddModel){
  233. [_downLoadFileModelDataArr addObject:addModel];
  234. _reDownloadIngSelectIndex = _downLoadFileModelDataArr.count -1;
  235. }
  236. }
  237. [self beginDownloadFileFun];
  238. }
  239. - (void)handleDatabaseArrByDelete:(couldPhoneFileModel*)delModel
  240. {
  241. if(_databaseArr && _databaseArr.count == 3 ){
  242. if(delModel.curDownloadStateType == downloadStateDone)
  243. {
  244. NSMutableArray *arr = _databaseArr[1];
  245. [arr removeObject:delModel];
  246. }
  247. else if(delModel.curDownloadStateType == downloadStateFail){
  248. NSMutableArray *arr = _databaseArr[2];
  249. [arr removeObject:delModel];
  250. }
  251. else{
  252. NSMutableArray *arr = _databaseArr[0];
  253. [arr removeObject:delModel];
  254. }
  255. }
  256. }
  257. - (void)deleteDownloadFileRecordBy:(NSMutableArray *)delArr withDelCache:(BOOL)isDelCache
  258. {
  259. //逻辑待优化
  260. BOOL isSuc = false;
  261. BOOL isDelUploadingModel = false;
  262. NSMutableArray *curDelArr = [NSMutableArray arrayWithArray:delArr];
  263. for (couldPhoneFileModel *couldPhoneFileMod in curDelArr) {
  264. NSMutableString* where = [[NSMutableString alloc] initWithString:@"where "];
  265. NSString *curStr = [NSString stringWithFormat:@"%@=%@ ",bg_sqlKey(@"bg_id"),bg_sqlValue(couldPhoneFileMod.bg_id)];
  266. [where appendString:curStr];
  267. [couldPhoneFileModel bg_deleteAsync:download_tableName where:where complete:^(BOOL isSuccess) {
  268. }];
  269. //isSuc = [couldPhoneFileModel bg_delete:download_tableName where:where];
  270. [self handleDatabaseArrByDelete:couldPhoneFileMod];
  271. if(couldPhoneFileMod.bg_id.integerValue == _curDownloadFileModel.bg_id.integerValue){
  272. isDelUploadingModel = YES;
  273. [self suspendDownloadFileFun:NO];
  274. }
  275. }
  276. if(isDelUploadingModel){
  277. _isDownloadIngType = NO;
  278. if(_downLoadFileModelDataArr > 0){
  279. [self beginDownloadFileFun];
  280. }
  281. }
  282. [[NSNotificationCenter defaultCenter] postNotificationName:downloadFileRefreshNotification object:_curDownloadFileModel];
  283. }
  284. @end