downloadManager.m 11 KB

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