// // downloadManager.m // 隐私保护 // // Created by xd h on 2024/1/9. // #import "downloadManager.h" @implementation downloadManager static downloadManager * cur_downloadManager = nil; +(downloadManager *)shareInstance; { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ cur_downloadManager = [[downloadManager alloc] init]; }); return cur_downloadManager; } - (id)init { self = [super init]; if (self) { //[self initManager]; } return self; } #pragma mark 读取数据库数据 - (void)getDataInDatabaseFun:(BOOL)isReGet complete:(custom_complete_Arr)complete { if(_databaseArr && _databaseArr.count == 3 && !isReGet){ complete(_databaseArr); return; } if(!_databaseArr) { _databaseArr = [NSMutableArray new]; } //KWeakSelf //dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{ [couldPhoneFileModel bg_findAsync:download_tableName limit:0 orderBy:nil desc:NO complete:^(NSArray * _Nullable array) { NSMutableArray *failArr = [NSMutableArray new]; NSMutableArray *doneArr = [NSMutableArray new]; NSMutableArray *otherArr = [NSMutableArray new]; for (couldPhoneFileModel * curModel in array) { if(curModel.curDownloadStateType == downloadStateFail){ [failArr addObject:curModel]; } else if(curModel.curDownloadStateType == downloadStateDone){ [doneArr addObject:curModel]; } else{ [otherArr addObject:curModel]; } } NSLock *lock = [NSLock new]; [lock lock]; NSMutableArray *newArr = [NSMutableArray new]; [newArr addObject:otherArr]; [newArr addObject:doneArr]; [newArr addObject:failArr]; self->_databaseArr = newArr; [lock unlock]; complete(self->_databaseArr); }]; //}); } - (void)handleCouldPhoneFileModelToDownloadFileDataFunBy:(NSMutableArray*)indexPathsForSelectedItems complete:(custom_complete_Arr)complete { if(!indexPathsForSelectedItems && indexPathsForSelectedItems.count == 0){ complete(nil); return; } for (couldPhoneFileModel*model in indexPathsForSelectedItems) { model.bg_tableName = download_tableName; } KWeakSelf [couldPhoneFileModel bg_saveOrUpdateArrayAsync:indexPathsForSelectedItems complete:^(BOOL isSuccess) { [weakSelf getDataInDatabaseFun:YES complete:^(NSMutableArray * _Nonnull Arr) { complete(Arr); [weakSelf planToDownloadFileFun]; }]; }]; } - (void)planToDownloadFileFun { if(!_databaseArr || _databaseArr.count != 3){ return; } _downLoadFileModelDataArr = _databaseArr[0]; [self beginDownloadFileFun]; } - (void)beginDownloadFileFun { if(_downLoadFileModelDataArr.count == 0) { return; } _isSuspendType = NO; _isDownloadIngType = YES; _curDownloadFileModel = _downLoadFileModelDataArr.lastObject; [[NSNotificationCenter defaultCenter] postNotificationName:downloadFileBeginNotification object:_curDownloadFileModel]; } - (void)DownloadFileDoneOneFileFun { _curDownloadFileModel.curDownloadStateType = downloadStateDone; [_downLoadFileModelDataArr removeObject:_curDownloadFileModel]; [self beginDownloadFileFun]; } @end