downloadManager.m 11 KB

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