mixDownloadManager.m 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. //
  2. // mixDownloadManager.m
  3. // 双子星云手机
  4. //
  5. // Created by xd h on 2024/6/27.
  6. //
  7. #import "mixDownloadManager.h"
  8. #import "mixDownloadCacheManager.h"
  9. #import "mixDownloadSession.h"
  10. @interface mixDownloadManager ()
  11. @property(nonatomic,strong) mixDownloadSession *downloadSession;
  12. @end
  13. @implementation mixDownloadManager
  14. + (instancetype)shareManager {
  15. static mixDownloadManager *_instance;
  16. static dispatch_once_t onceToken;
  17. dispatch_once(&onceToken, ^{
  18. _instance = [[self alloc] init];
  19. });
  20. return _instance;
  21. }
  22. - (NSString*)uid{
  23. if(!_uid || _uid.length == 0){
  24. return @"mixDownload";
  25. }
  26. return _uid;
  27. }
  28. - (NSMutableArray *)getAllOperation {
  29. NSMutableDictionary *allOperationDict = [mixDownloadCacheManager getDownloadList];
  30. //HLog(@"%@---%@", allOperationDict,allOperationDict.allKeys);
  31. NSMutableArray *allOperationArray = [NSMutableArray array];
  32. for (NSDictionary *dict in allOperationDict.allValues) {
  33. mixDownloadOperation *operation = [mixDownloadOperation mj_objectWithKeyValues:dict];
  34. [allOperationArray addObject:operation];
  35. }
  36. return allOperationArray;
  37. }
  38. #pragma mark - 外界交互
  39. - (void)downloadWithURL:(NSURL *)url complete:(void(^)(NSDictionary *,NSError *))complete{
  40. [self downloadWithURL:url begin:nil progress:nil complete:complete];
  41. }
  42. - (void)downloadWithURL:(NSURL *)url begin:(void(^)(NSString *))begin progress:(void(^)(NSInteger,NSInteger))progress complete:(void(^)(NSDictionary *,NSError *))complete {
  43. if (![url isKindOfClass:NSURL.class]) {
  44. if ([url isKindOfClass:NSString.class]) {
  45. url = [NSURL URLWithString:(NSString *)url];
  46. }else {
  47. // 失败回调
  48. complete(nil,nil);
  49. return;
  50. }
  51. }
  52. // 开启异步 操作
  53. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  54. // 本地查找
  55. NSDictionary *fileInfo = [mixDownloadCacheManager queryFileInfoWithUrl:url.absoluteString];
  56. // 本地存在直接返回
  57. if (fileInfo && [fileInfo[@"currentSize"] integerValue] == [fileInfo[@"totalSize"] integerValue]
  58. && [fileInfo[@"totalSize"] integerValue] != 0) {
  59. mixDownloadOperation *operation = [mixDownloadOperation mj_objectWithKeyValues:fileInfo];
  60. [[NSNotificationCenter defaultCenter] postNotificationName:mixDownloadTaskExeEnd object:nil userInfo:@{@"operation" : operation}];
  61. dispatch_async(dispatch_get_main_queue(), ^{
  62. !complete ? : complete(fileInfo,nil);
  63. });
  64. return;
  65. }
  66. // 交给downloader下载
  67. [self.downloadSession downloadWithURL:url begin:begin progress:progress complete:complete];
  68. });
  69. }
  70. #pragma mark - 队列中的任务进行操作
  71. - (void)startDownLoadWithUrl:(NSString *)url {
  72. // 开始下载
  73. [self.downloadSession startDownLoadWithUrl:url];
  74. }
  75. - (void)supendDownloadWithUrl:(NSString *)url {
  76. // 暂停下载
  77. [self.downloadSession supendDownloadWithUrl:url];
  78. }
  79. - (void)cancelDownloadWithUrl:(NSString *)url {
  80. // 取消下载
  81. [self.downloadSession cancelDownloadWithUrl:url];
  82. }
  83. /** 暂停当前所有的下载任务 下载任务不会从列队中删除 */
  84. - (void)suspendAllDownloadTask {
  85. [self.downloadSession suspendAllDownloads];
  86. }
  87. /** 开启当前列队中所有被暂停的下载任务 */
  88. - (void)startAllDownloadTask {
  89. [self.downloadSession startAllDownloads];
  90. }
  91. /** 停止当前所有的下载任务 调用此方法会清空所有列队下载任务 */
  92. - (void)stopAllDownloads {
  93. [self.downloadSession cancelAllDownloads];
  94. self.downloadSession = nil;
  95. }
  96. #pragma mark - lazy load
  97. - (mixDownloadSession *)downloadSession {
  98. if (!_downloadSession) {
  99. _downloadSession = [[mixDownloadSession alloc] init];
  100. }
  101. return _downloadSession;
  102. }
  103. @end