123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- //
- // mixDownloadManager.m
- // 双子星云手机
- //
- // Created by xd h on 2024/6/27.
- //
- #import "mixDownloadManager.h"
- #import "mixDownloadCacheManager.h"
- #import "mixDownloadSession.h"
- @interface mixDownloadManager ()
- @property(nonatomic,strong) mixDownloadSession *downloadSession;
- @end
- @implementation mixDownloadManager
- + (instancetype)shareManager {
- static mixDownloadManager *_instance;
- static dispatch_once_t onceToken;
- dispatch_once(&onceToken, ^{
- _instance = [[self alloc] init];
- });
- return _instance;
- }
- - (NSString*)uid{
- if(!_uid || _uid.length == 0){
- return @"mixDownload";
- }
-
- return _uid;
- }
- - (NSMutableArray *)getAllOperation {
- NSMutableDictionary *allOperationDict = [mixDownloadCacheManager getDownloadList];
- //HLog(@"%@---%@", allOperationDict,allOperationDict.allKeys);
- NSMutableArray *allOperationArray = [NSMutableArray array];
- for (NSDictionary *dict in allOperationDict.allValues) {
- mixDownloadOperation *operation = [mixDownloadOperation mj_objectWithKeyValues:dict];
- [allOperationArray addObject:operation];
- }
- return allOperationArray;
- }
- #pragma mark - 外界交互
- - (void)downloadWithURL:(NSURL *)url complete:(void(^)(NSDictionary *,NSError *))complete{
- [self downloadWithURL:url begin:nil progress:nil complete:complete];
- }
- - (void)downloadWithURL:(NSURL *)url begin:(void(^)(NSString *))begin progress:(void(^)(NSInteger,NSInteger))progress complete:(void(^)(NSDictionary *,NSError *))complete {
-
- if (![url isKindOfClass:NSURL.class]) {
- if ([url isKindOfClass:NSString.class]) {
- url = [NSURL URLWithString:(NSString *)url];
- }else {
- // 失败回调
- complete(nil,nil);
- return;
- }
- }
- // 开启异步 操作
- dispatch_async(dispatch_get_global_queue(0, 0), ^{
- // 本地查找
- NSDictionary *fileInfo = [mixDownloadCacheManager queryFileInfoWithUrl:url.absoluteString];
-
- // 本地存在直接返回
- if (fileInfo && [fileInfo[@"currentSize"] integerValue] == [fileInfo[@"totalSize"] integerValue]
- && [fileInfo[@"totalSize"] integerValue] != 0) {
-
- mixDownloadOperation *operation = [mixDownloadOperation mj_objectWithKeyValues:fileInfo];
- [[NSNotificationCenter defaultCenter] postNotificationName:mixDownloadTaskExeEnd object:nil userInfo:@{@"operation" : operation}];
-
- dispatch_async(dispatch_get_main_queue(), ^{
- !complete ? : complete(fileInfo,nil);
- });
- return;
- }
-
-
- // 交给downloader下载
- [self.downloadSession downloadWithURL:url begin:begin progress:progress complete:complete];
- });
-
- }
- #pragma mark - 队列中的任务进行操作
- - (void)startDownLoadWithUrl:(NSString *)url {
- // 开始下载
- [self.downloadSession startDownLoadWithUrl:url];
- }
- - (void)supendDownloadWithUrl:(NSString *)url {
- // 暂停下载
- [self.downloadSession supendDownloadWithUrl:url];
- }
- - (void)cancelDownloadWithUrl:(NSString *)url {
- // 取消下载
- [self.downloadSession cancelDownloadWithUrl:url];
- }
- /** 暂停当前所有的下载任务 下载任务不会从列队中删除 */
- - (void)suspendAllDownloadTask {
- [self.downloadSession suspendAllDownloads];
- }
- /** 开启当前列队中所有被暂停的下载任务 */
- - (void)startAllDownloadTask {
- [self.downloadSession startAllDownloads];
- }
- /** 停止当前所有的下载任务 调用此方法会清空所有列队下载任务 */
- - (void)stopAllDownloads {
- [self.downloadSession cancelAllDownloads];
- self.downloadSession = nil;
- }
- #pragma mark - lazy load
- - (mixDownloadSession *)downloadSession {
- if (!_downloadSession) {
- _downloadSession = [[mixDownloadSession alloc] init];
-
- }
- return _downloadSession;
- }
- @end
|