downloadManager.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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. return;
  138. }
  139. _isSuspendType = NO;
  140. _isDownloadIngType = YES;
  141. if(_reDownloadIngSelectIndex > 0
  142. && _reDownloadIngSelectIndex < _downLoadFileModelDataArr.count){
  143. _curDownloadFileModel = _downLoadFileModelDataArr[_reDownloadIngSelectIndex];
  144. }
  145. else
  146. {
  147. _curDownloadFileModel = _downLoadFileModelDataArr.firstObject;
  148. }
  149. if(!_curDownloadFileModel){
  150. return;
  151. }
  152. _curDownloadFileModel.curDownloadStateType = downloadStateUploading;
  153. [[NSNotificationCenter defaultCenter] postNotificationName:downloadFileBeginNotification object:_curDownloadFileModel];
  154. }
  155. - (void)DownloadFileDoneOneFileFun
  156. {
  157. self.taskRenewTime = [iTools getNowTimeStamp];
  158. _curDownloadFileModel.curDownloadStateType = downloadStateDone;
  159. // NSString *fileName = [_curDownloadFileModel getFileNameFun];
  160. // [cachesFileManager removeItemAtPath:fileName type:DownLoadFileType error:nil];
  161. [self DownloadFileChangeingOneFileFun];
  162. if(_isSuspendType){
  163. return;
  164. }
  165. [self beginDownloadFileFun];
  166. }
  167. - (void)DownloadFileChangeingOneFileFun
  168. {
  169. self.taskRenewTime = [iTools getNowTimeStamp];
  170. if(_curDownloadFileModel.curDownloadStateType == downloadStateDone){
  171. [_downLoadFileModelDataArr removeObject:_curDownloadFileModel];
  172. [self handleDatabaseArrDeleteObjectInDownloading:_curDownloadFileModel];
  173. [self handleDatabaseArrAddObjectInDone:_curDownloadFileModel];
  174. }
  175. else if(_curDownloadFileModel.curDownloadStateType == uploadStateFail){
  176. [_downLoadFileModelDataArr removeObject:_curDownloadFileModel];
  177. [self handleDatabaseArrDeleteObjectInDownloading:_curDownloadFileModel];
  178. [self handleDatabaseArrAddObjectInFail:_curDownloadFileModel];
  179. }
  180. [_curDownloadFileModel bg_saveOrUpdateAsync:^(BOOL isSuccess) {
  181. //HLog(@"%@ 写入 %@", model.filename, isSuccess ? @"成功":@"失败");
  182. }];
  183. if(!_isSuspendType || _curDownloadFileModel.curDownloadStateType != uploadStateSuspend){
  184. [[NSNotificationCenter defaultCenter] postNotificationName:downloadFileRefreshNotification object:_curDownloadFileModel];
  185. }
  186. }
  187. - (void)handleDatabaseArrDeleteObjectInDownloading:(couldPhoneFileModel*)model
  188. {
  189. if(_databaseArr && _databaseArr.count == 3 ){
  190. NSMutableArray *ingArr = _databaseArr[0];
  191. [ingArr removeObject:model];
  192. }
  193. }
  194. - (void)handleDatabaseArrAddObjectInDone:(couldPhoneFileModel*)model
  195. {
  196. if(_databaseArr && _databaseArr.count == 3 ){
  197. NSMutableArray *doneArr = _databaseArr[1];
  198. [doneArr insertObject:model atIndex:0];
  199. }
  200. }
  201. - (void)handleDatabaseArrAddObjectInFail:(couldPhoneFileModel*)model
  202. {
  203. if(_databaseArr && _databaseArr.count == 3 ){
  204. NSMutableArray *failArr = _databaseArr[2];
  205. [failArr insertObject:model atIndex:0];
  206. }
  207. }
  208. - (void)suspendDownloadFileFun:(BOOL)isSuspendAll
  209. {
  210. _isSuspendType = YES;
  211. if(isSuspendAll){
  212. for (couldPhoneFileModel *couldPhoneFileMod in _downLoadFileModelDataArr) {
  213. couldPhoneFileMod.curDownloadStateType = downloadStateSuspend;
  214. }
  215. }
  216. }
  217. - (void)reDownloadFileFunBy:(NSMutableArray*)Arr
  218. {
  219. if(!_downLoadFileModelDataArr){
  220. _downLoadFileModelDataArr = [NSMutableArray new];
  221. }
  222. _reDownloadIngSelectIndex = 0;
  223. for (uploadFileDataModel*addModel in Arr) {
  224. BOOL needAddModel = YES;
  225. for (int i=0;i< _downLoadFileModelDataArr.count;i++)
  226. {
  227. couldPhoneFileModel*preModel = _downLoadFileModelDataArr[i];
  228. if(addModel.bg_id.integerValue == preModel.bg_id.integerValue)
  229. {
  230. needAddModel = NO;
  231. if(_reDownloadIngSelectIndex == 0){
  232. _reDownloadIngSelectIndex = i;
  233. if(i==0){
  234. _reDownloadIngSelectIndex = -1;
  235. }
  236. }
  237. break;
  238. }
  239. }
  240. if(needAddModel){
  241. [_downLoadFileModelDataArr addObject:addModel];
  242. _reDownloadIngSelectIndex = _downLoadFileModelDataArr.count -1;
  243. }
  244. }
  245. [self beginDownloadFileFun];
  246. }
  247. - (void)handleDatabaseArrByDelete:(couldPhoneFileModel*)delModel
  248. {
  249. if(_databaseArr && _databaseArr.count == 3 ){
  250. if(delModel.curDownloadStateType == downloadStateDone)
  251. {
  252. NSMutableArray *arr = _databaseArr[1];
  253. [arr removeObject:delModel];
  254. }
  255. else if(delModel.curDownloadStateType == downloadStateFail){
  256. NSMutableArray *arr = _databaseArr[2];
  257. [arr removeObject:delModel];
  258. }
  259. else{
  260. NSMutableArray *arr = _databaseArr[0];
  261. [arr removeObject:delModel];
  262. }
  263. }
  264. }
  265. - (void)deleteDownloadFileRecordBy:(NSMutableArray *)delArr withDelCache:(BOOL)isDelCache
  266. {
  267. //逻辑待优化
  268. BOOL isSuc = false;
  269. BOOL isDelUploadingModel = false;
  270. NSMutableArray *curDelArr = [NSMutableArray arrayWithArray:delArr];
  271. for (couldPhoneFileModel *couldPhoneFileMod in curDelArr) {
  272. NSMutableString* where = [[NSMutableString alloc] initWithString:@"where "];
  273. NSString *curStr = [NSString stringWithFormat:@"%@=%@ ",bg_sqlKey(@"bg_id"),bg_sqlValue(couldPhoneFileMod.bg_id)];
  274. [where appendString:curStr];
  275. [couldPhoneFileModel bg_deleteAsync:download_tableName where:where complete:^(BOOL isSuccess) {
  276. }];
  277. //isSuc = [couldPhoneFileModel bg_delete:download_tableName where:where];
  278. [self handleDatabaseArrByDelete:couldPhoneFileMod];
  279. if(couldPhoneFileMod.bg_id.integerValue == _curDownloadFileModel.bg_id.integerValue){
  280. isDelUploadingModel = YES;
  281. [self suspendDownloadFileFun:NO];
  282. }
  283. }
  284. if(isDelUploadingModel){
  285. _isDownloadIngType = NO;
  286. if(_downLoadFileModelDataArr > 0){
  287. [self beginDownloadFileFun];
  288. }
  289. }
  290. [[NSNotificationCenter defaultCenter] postNotificationName:downloadFileRefreshNotification object:_curDownloadFileModel];
  291. }
  292. //检查是否需要重新下载
  293. - (void)checkReDownloadFileFun
  294. {
  295. if(!_curDownloadFileModel){
  296. return;
  297. }
  298. if(_curDownloadFileModel.curDownloadStateType != downloadStateUploading){
  299. return;
  300. }
  301. NSInteger curTime = [iTools getNowTimeStamp];
  302. if(self.taskRenewTime == 0){
  303. return;
  304. }
  305. if(curTime - self.taskRenewTime < 10){
  306. return;
  307. }
  308. [[NSNotificationCenter defaultCenter] postNotificationName:downloadFileBeginNotification object:_curDownloadFileModel];
  309. }
  310. @end