downloadManager.m 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  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. #import "AFNetworkReachabilityManager.h"
  10. @interface downloadManager ()
  11. /**检测任务10s后 无反馈重新开始 */
  12. @property (nonatomic, assign) NSInteger taskRenewTime; //
  13. @end
  14. @implementation downloadManager
  15. static downloadManager * cur_downloadManager = nil;
  16. +(downloadManager *)shareInstance;
  17. {
  18. static dispatch_once_t onceToken;
  19. dispatch_once(&onceToken, ^{
  20. cur_downloadManager = [[downloadManager alloc] init];
  21. });
  22. return cur_downloadManager;
  23. }
  24. - (id)init
  25. {
  26. self = [super init];
  27. if (self) {
  28. //[self initManager];
  29. }
  30. return self;
  31. }
  32. #pragma mark 读取数据库数据
  33. - (void)getDataInDatabaseFun:(BOOL)isReGet complete:(custom_complete_Arr)complete
  34. {
  35. if(_databaseArr && _databaseArr.count == 3 && !isReGet){
  36. complete(_databaseArr);
  37. return;
  38. }
  39. if(!_databaseArr)
  40. {
  41. _databaseArr = [NSMutableArray new];
  42. }
  43. //KWeakSelf
  44. //dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{
  45. // NSLock *lock = [NSLock new];
  46. // [lock lock];
  47. [couldPhoneFileModel bg_findAsync:download_tableName limit:0 orderBy:nil desc:YES complete:^(NSArray * _Nullable array) {
  48. NSMutableArray *failArr = [NSMutableArray new];
  49. NSMutableArray *doneArr = [NSMutableArray new];
  50. NSMutableArray *otherArr = [NSMutableArray new];
  51. if(array && array.count >0){
  52. for (couldPhoneFileModel * curModel in array) {
  53. if(curModel.curDownloadStateType == downloadStateFail){
  54. [failArr addObject:curModel];
  55. }
  56. else if(curModel.curDownloadStateType == downloadStateDone){
  57. [doneArr addObject:curModel];
  58. }
  59. else{
  60. [otherArr addObject:curModel];
  61. }
  62. }
  63. }
  64. NSLock *lock = [NSLock new];
  65. [lock lock];
  66. NSMutableArray *newArr = [NSMutableArray new];
  67. [newArr addObject:otherArr];
  68. [newArr addObject:doneArr];
  69. [newArr addObject:failArr];
  70. self->_databaseArr = newArr;
  71. [lock unlock];
  72. complete(self->_databaseArr);
  73. }];
  74. // [lock unlock];
  75. //});
  76. }
  77. -(void)handleCouldPhoneFileModelToDownloadFileDataFunBy:(NSMutableArray*)indexPathsForSelectedItems complete:(custom_complete_Arr)complete
  78. {
  79. if(!indexPathsForSelectedItems && indexPathsForSelectedItems.count == 0){
  80. complete(nil);
  81. return;
  82. }
  83. if(ksharedAppDelegate.DisabledFileTransferType){
  84. if(ksharedAppDelegate.isImageNewFor130){
  85. [[iToast makeText:NSLocalizedString(@"File_Transfer_Disable_tip",nil)] show];
  86. }
  87. else{
  88. [[iToast makeText:NSLocalizedString(@"File_Transfer_Disable_tip2",nil)] show];
  89. }
  90. complete(_databaseArr);
  91. return;
  92. }
  93. for (couldPhoneFileModel*model in indexPathsForSelectedItems) {
  94. model.bg_tableName = download_tableName;
  95. }
  96. _isNewAddTaskType = YES;
  97. KWeakSelf
  98. //查
  99. [self getDataInDatabaseFun:YES complete:^(NSMutableArray * _Nonnull Arr) {
  100. //排重
  101. NSMutableArray *newDownArr = [weakSelf checkDownloadTaskRepeatFun:indexPathsForSelectedItems];
  102. if(newDownArr.count>0){
  103. //存
  104. [couldPhoneFileModel bg_saveOrUpdateArrayAsync:newDownArr complete:^(BOOL isSuccess) {
  105. //再查
  106. [weakSelf getDataInDatabaseFun:YES complete:^(NSMutableArray * _Nonnull Arr) {
  107. complete(Arr);
  108. [weakSelf planToDownloadFileFun];
  109. }];
  110. }];
  111. }
  112. else{
  113. complete(Arr);
  114. [weakSelf planToDownloadFileFun];
  115. }
  116. }];
  117. }
  118. #pragma mark 检测是否有下载任务重复
  119. - (NSMutableArray*)checkDownloadTaskRepeatFun:(NSMutableArray*)indexPathsForSelectedItems
  120. {
  121. if(!_databaseArr || _databaseArr.count != 3){
  122. return indexPathsForSelectedItems;
  123. }
  124. NSMutableArray *downloadArr = _databaseArr[0];
  125. NSMutableArray *newTaskArr = [NSMutableArray new];
  126. for (couldPhoneFileModel*model in indexPathsForSelectedItems) {
  127. NSString *fileName = [model getFileNameFun];
  128. BOOL isNeedAddType = YES;
  129. for (couldPhoneFileModel* hadModel in downloadArr) {
  130. NSString *hadFileName = [hadModel getFileNameFun];
  131. if([fileName isEqualToString:hadFileName]){
  132. isNeedAddType = NO;
  133. break;
  134. }
  135. }
  136. if(isNeedAddType){
  137. [newTaskArr addObject:model];
  138. }
  139. }
  140. return newTaskArr;
  141. }
  142. - (void)planToDownloadFileFun
  143. {
  144. if(!_databaseArr || _databaseArr.count != 3){
  145. return;
  146. }
  147. _downLoadFileModelDataArr = [NSMutableArray arrayWithArray:_databaseArr[0]];
  148. [self beginDownloadFileFun];
  149. }
  150. - (void)beginDownloadFileFun
  151. {
  152. if(_downLoadFileModelDataArr.count == 0)
  153. {
  154. [self didDownloadAllTaskDoneFun];
  155. return;
  156. }
  157. BOOL isCanUseCellular = [HWDataManager getBoolWithKey:stringKeyAddSn(Const_file_Transfe_canUse_Cellular_all)];
  158. if(!isCanUseCellular){//不允许流量上传
  159. //
  160. if([AFNetworkReachabilityManager sharedManager].networkReachabilityStatus == AFNetworkReachabilityStatusReachableViaWWAN){
  161. mainBlock(^{
  162. [[NSNotificationCenter defaultCenter] postNotificationName:downloadFileSuspendAllNotification object:nil];
  163. [[iToast makeText:NSLocalizedString(@"File_Transfer_By_Cellular_tip",nil)] show];
  164. });
  165. return;
  166. }
  167. }
  168. if(ksharedAppDelegate.DisabledFileTransferType){
  169. if(ksharedAppDelegate.isImageNewFor130){
  170. [[iToast makeText:NSLocalizedString(@"File_Transfer_Disable_tip",nil)] show];
  171. }
  172. else{
  173. [[iToast makeText:NSLocalizedString(@"File_Transfer_Disable_tip2",nil)] show];
  174. }
  175. return;
  176. }
  177. self.taskRenewTime = [iTools getNowTimeStamp];
  178. _isSuspendType = NO;
  179. _isDownloadIngType = YES;
  180. if(_reDownloadIngSelectIndex > 0
  181. && _reDownloadIngSelectIndex < _downLoadFileModelDataArr.count){
  182. _curDownloadFileModel = _downLoadFileModelDataArr[_reDownloadIngSelectIndex];
  183. }
  184. else
  185. {
  186. _curDownloadFileModel = _downLoadFileModelDataArr.firstObject;
  187. }
  188. if(!_curDownloadFileModel){
  189. return;
  190. }
  191. // //做个异常处理
  192. // if(_databaseArr.count == 3 && _isNewAddTaskType){
  193. // NSMutableArray *downloadIngArr = _databaseArr[0];
  194. // if(_downLoadFileModelDataArr.count != downloadIngArr.count){
  195. // _databaseArr[0] = [NSMutableArray arrayWithArray:_downLoadFileModelDataArr];
  196. // }
  197. // }
  198. if(![iTools checkFreeDiskSpaceInBytesIsOK:_curDownloadFileModel.length]){
  199. mainBlock(^{
  200. [[iToast makeText:NSLocalizedString(@"phone_space_Insufficient",nil)] show];
  201. });
  202. return;
  203. }
  204. _curDownloadFileModel.curDownloadStateType = downloadStateUploading;
  205. [[webSocketManager shareInstance] begindownloadFileFunBy:_curDownloadFileModel];
  206. }
  207. - (void)DownloadFileDoneOneFileFun
  208. {
  209. self.taskRenewTime = [iTools getNowTimeStamp];
  210. _curDownloadFileModel.curDownloadStateType = downloadStateDone;
  211. // NSString *fileName = [_curDownloadFileModel getFileNameFun];
  212. // [cachesFileManager removeItemAtPath:fileName type:DownLoadFileType error:nil];
  213. [self DownloadFileChangeingOneFileFun];
  214. if(_isSuspendType){
  215. return;
  216. }
  217. [self beginDownloadFileFun];
  218. }
  219. - (void)DownloadFileFailOneFileFun
  220. {
  221. self.taskRenewTime = [iTools getNowTimeStamp];
  222. _curDownloadFileModel.curDownloadStateType = downloadStateFail;
  223. [self DownloadFileChangeingOneFileFun];
  224. if(_isSuspendType){
  225. return;
  226. }
  227. [self beginDownloadFileFun];
  228. }
  229. - (void)DownloadFileChangeingOneFileFun
  230. {
  231. self.taskRenewTime = [iTools getNowTimeStamp];
  232. if(_curDownloadFileModel.curDownloadStateType == downloadStateDone){
  233. [_downLoadFileModelDataArr removeObject:_curDownloadFileModel];
  234. [self handleDatabaseArrDeleteObjectInDownloading:_curDownloadFileModel];
  235. [self handleDatabaseArrAddObjectInDone:_curDownloadFileModel];
  236. }
  237. else if(_curDownloadFileModel.curDownloadStateType == uploadStateFail){
  238. [_downLoadFileModelDataArr removeObject:_curDownloadFileModel];
  239. [self handleDatabaseArrDeleteObjectInDownloading:_curDownloadFileModel];
  240. [self handleDatabaseArrAddObjectInFail:_curDownloadFileModel];
  241. }
  242. [_curDownloadFileModel bg_saveOrUpdateAsync:^(BOOL isSuccess) {
  243. //HLog(@"%@ 写入 %@", model.filename, isSuccess ? @"成功":@"失败");
  244. }];
  245. if(!_isSuspendType || _curDownloadFileModel.curDownloadStateType != uploadStateSuspend){
  246. [[NSNotificationCenter defaultCenter] postNotificationName:downloadFileRefreshNotification object:_curDownloadFileModel];
  247. }
  248. }
  249. - (void)handleDatabaseArrDeleteObjectInDownloading:(couldPhoneFileModel*)model
  250. {
  251. NSLock *lock = [NSLock new];
  252. [lock lock];
  253. if(_databaseArr && _databaseArr.count == 3 ){
  254. NSMutableArray *ingArr = _databaseArr[0];
  255. NSInteger count = ingArr.count;
  256. [ingArr removeObject:model];
  257. NSInteger atferDelCount = ingArr.count;
  258. if(count == atferDelCount){
  259. for (couldPhoneFileModel*preModel in ingArr) {
  260. if(preModel.bg_id.integerValue == model.bg_id.integerValue){
  261. [ingArr removeObject:preModel];
  262. break;
  263. }
  264. }
  265. }
  266. NSInteger atferDelCount2 = ingArr.count;
  267. if(count == atferDelCount2){
  268. // HLog(@"\n\n\nerror:下载中的任务没有删除掉database bgid:%@\n\n\n\n",model.bg_id);
  269. }
  270. }
  271. [lock unlock];
  272. }
  273. - (void)handleDatabaseArrAddObjectInDone:(couldPhoneFileModel*)model
  274. {
  275. NSLock *lock = [NSLock new];
  276. [lock lock];
  277. if(_databaseArr && _databaseArr.count == 3 ){
  278. NSMutableArray *doneArr = _databaseArr[1];
  279. if(model){
  280. [doneArr insertObject:model atIndex:0];
  281. }
  282. }
  283. [lock unlock];
  284. }
  285. - (void)handleDatabaseArrAddObjectInFail:(couldPhoneFileModel*)model
  286. {
  287. NSLock *lock = [NSLock new];
  288. [lock lock];
  289. if(_databaseArr && _databaseArr.count == 3 ){
  290. NSMutableArray *failArr = _databaseArr[2];
  291. if(model){
  292. [failArr insertObject:model atIndex:0];
  293. }
  294. }
  295. [lock unlock];
  296. }
  297. - (void)suspendDownloadFileFun:(BOOL)isSuspendAll withModel:(couldPhoneFileModel*)model
  298. {
  299. if(isSuspendAll){
  300. _isSuspendType = YES;
  301. [[NSNotificationCenter defaultCenter] postNotificationName:downloadFileSuspendAllNotification object:nil];
  302. // for (couldPhoneFileModel *couldPhoneFileMod in _downLoadFileModelDataArr) {
  303. // couldPhoneFileMod.curDownloadStateType = downloadStateSuspend;
  304. // }
  305. [self changeDownloadState:downloadStateSuspend withModelArr:_downLoadFileModelDataArr complete:^(BOOL isSuccess) {
  306. [self->_downLoadFileModelDataArr removeAllObjects];
  307. }];
  308. }
  309. else{
  310. couldPhoneFileModel *curModel = _curDownloadFileModel;
  311. if(model){
  312. curModel = model;
  313. }
  314. else{
  315. _isSuspendType = YES;
  316. }
  317. [self changeDownloadState:downloadStateSuspend withModelArr:@[curModel] complete:^(BOOL isSuccess) {
  318. [self->_downLoadFileModelDataArr removeObject:model];
  319. if(curModel.bg_id.integerValue == self->_curDownloadFileModel.bg_id.integerValue){
  320. //开启下一个任务
  321. _reDownloadIngSelectIndex = -1;
  322. [self beginDownloadFileFun];
  323. }
  324. }];
  325. }
  326. }
  327. - (void)reDownloadFileFunBy:(NSMutableArray*)Arr withAll:(BOOL)isAllType
  328. {
  329. if(!_downLoadFileModelDataArr){
  330. _downLoadFileModelDataArr = [NSMutableArray new];
  331. }
  332. if(isAllType)
  333. {
  334. _isNewAddTaskType = YES;
  335. _downLoadFileModelDataArr = [NSMutableArray arrayWithArray:Arr];
  336. _reDownloadIngSelectIndex = -1;
  337. }
  338. else{
  339. _isNewAddTaskType = NO;
  340. _reDownloadIngSelectIndex = 0;
  341. NSMutableArray *curArr = [NSMutableArray arrayWithArray:Arr];
  342. for (uploadFileDataModel*addModel in curArr){
  343. [_downLoadFileModelDataArr insertObject:addModel atIndex:0];
  344. }
  345. }
  346. [self changeDownloadState:downloadStateUploading withModelArr:_downLoadFileModelDataArr complete:^(BOOL isSuccess) {
  347. }];
  348. [self beginDownloadFileFun];
  349. }
  350. - (void)handleDatabaseArrByDelete:(couldPhoneFileModel*)delModel
  351. {
  352. if(_databaseArr && _databaseArr.count == 3 ){
  353. if(delModel.curDownloadStateType == downloadStateDone)
  354. {
  355. NSMutableArray *arr = _databaseArr[1];
  356. [arr removeObject:delModel];
  357. }
  358. else if(delModel.curDownloadStateType == downloadStateFail){
  359. NSMutableArray *arr = _databaseArr[2];
  360. [arr removeObject:delModel];
  361. }
  362. else{
  363. NSMutableArray *arr = _databaseArr[0];
  364. [arr removeObject:delModel];
  365. }
  366. }
  367. }
  368. - (void)deleteDownloadFileRecordBy:(NSMutableArray *)delArr withDelCache:(BOOL)isDelCache
  369. {
  370. //逻辑待优化
  371. BOOL isSuc = false;
  372. BOOL isDelUploadingModel = false;
  373. NSMutableArray *curDelArr = [NSMutableArray arrayWithArray:delArr];
  374. for (couldPhoneFileModel *couldPhoneFileMod in curDelArr) {
  375. NSMutableString* where = [[NSMutableString alloc] initWithString:@"where "];
  376. NSString *curStr = [NSString stringWithFormat:@"%@=%@ ",bg_sqlKey(@"bg_id"),bg_sqlValue(couldPhoneFileMod.bg_id)];
  377. [where appendString:curStr];
  378. [couldPhoneFileModel bg_deleteAsync:download_tableName where:where complete:^(BOOL isSuccess) {
  379. }];
  380. //isSuc = [couldPhoneFileModel bg_delete:download_tableName where:where];
  381. [self handleDatabaseArrByDelete:couldPhoneFileMod];
  382. if(couldPhoneFileMod.bg_id.integerValue == _curDownloadFileModel.bg_id.integerValue){
  383. isDelUploadingModel = YES;
  384. [self suspendDownloadFileFun:NO withModel:_curDownloadFileModel];
  385. }
  386. }
  387. if(isDelUploadingModel){
  388. _isDownloadIngType = NO;
  389. if(_downLoadFileModelDataArr > 0){
  390. [self beginDownloadFileFun];
  391. }
  392. }
  393. [[NSNotificationCenter defaultCenter] postNotificationName:downloadFileRefreshNotification object:_curDownloadFileModel];
  394. }
  395. //修改文件上传的状态
  396. - (void)changeDownloadState:(downloadStateType)curDownloadStateType withModelArr:(NSArray*)modelArr complete:(custom_complete_B)complete
  397. {
  398. for (couldPhoneFileModel * model in modelArr) {
  399. model.curDownloadStateType = curDownloadStateType;
  400. }
  401. if(modelArr.count == 0){
  402. return;
  403. }
  404. [couldPhoneFileModel bg_saveOrUpdateArrayAsync:modelArr complete:^(BOOL isSuccess) {
  405. complete(YES);
  406. }];
  407. }
  408. //检查是否需要重新下载
  409. - (void)checkReDownloadFileFun
  410. {
  411. if(!_curDownloadFileModel){
  412. return;
  413. }
  414. if(_isSuspendType){
  415. return;
  416. }
  417. NSInteger curTime = [iTools getNowTimeStamp];
  418. if(self.taskRenewTime == 0){
  419. return;
  420. }
  421. if(curTime - self.taskRenewTime < 10){
  422. return;
  423. }
  424. if(![iTools checkFreeDiskSpaceInBytesIsOK:_curDownloadFileModel.length]){
  425. mainBlock(^{
  426. [[iToast makeText:NSLocalizedString(@"phone_space_Insufficient",nil)] show];
  427. });
  428. _curDownloadFileModel = nil;
  429. return;
  430. }
  431. [[webSocketManager shareInstance] begindownloadFileFunBy:_curDownloadFileModel];
  432. }
  433. - (BOOL)checkDownloadingFun
  434. {
  435. if(!_curDownloadFileModel){
  436. return NO;
  437. }
  438. // if(_curDownloadFileModel.curDownloadStateType == downloadStateDone
  439. // ||_curDownloadFileModel.curDownloadStateType == downloadStateFail
  440. // ||_curDownloadFileModel.curDownloadStateType == downloadStateSuspend){
  441. // return NO;
  442. // }
  443. if(_isSuspendType){
  444. return NO;
  445. }
  446. return YES;
  447. }
  448. - (void)didDownloadAllTaskDoneFun
  449. {
  450. _curDownloadFileModel = nil;
  451. [[NSNotificationCenter defaultCenter] postNotificationName:downloadFileAllTaskDoneNotification object:nil];
  452. }
  453. - (void)checkHadDownloadTaskWithComplete:(custom_complete_B)complete
  454. {
  455. [self getDataInDatabaseFun:NO complete:^(NSMutableArray * _Nonnull Arr) {
  456. mainBlock(^{
  457. if(!Arr || Arr.count != 3){
  458. complete(NO);
  459. }
  460. else{
  461. NSArray *firstArr = Arr[0];
  462. if(firstArr.count >0){
  463. complete(YES);
  464. }
  465. else{
  466. complete(NO);
  467. }
  468. }
  469. });
  470. }];
  471. }
  472. @end