Browse Source

【分包下载】添加取消分包下载功能

lizhe 1 year ago
parent
commit
58dc249989

+ 6 - 6
PDF Office/PDF Master.xcodeproj/project.pbxproj

@@ -1042,7 +1042,6 @@
 		9FBC48BF299E23B100CA39D7 /* NSViewController+DesignToken.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9FBC48BE299E23B100CA39D7 /* NSViewController+DesignToken.swift */; };
 		9FBC48C0299E23B100CA39D7 /* NSViewController+DesignToken.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9FBC48BE299E23B100CA39D7 /* NSViewController+DesignToken.swift */; };
 		9FBC48C1299E23B100CA39D7 /* NSViewController+DesignToken.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9FBC48BE299E23B100CA39D7 /* NSViewController+DesignToken.swift */; };
-		9FC444F72AA5F62200D7187C /* KMResourceDownload.m in Sources */ = {isa = PBXBuildFile; fileRef = 9FC444F42AA5E82700D7187C /* KMResourceDownload.m */; };
 		9FC444FA2AA61EDE00D7187C /* ZipArchive.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 9FC444F82AA5F7D600D7187C /* ZipArchive.framework */; };
 		9FC444FB2AA61EDE00D7187C /* ZipArchive.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 9FC444F82AA5F7D600D7187C /* ZipArchive.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
 		9FC444FD2AA7057B00D7187C /* DocumentAI.bundle in Resources */ = {isa = PBXBuildFile; fileRef = 9FC444FC2AA7057B00D7187C /* DocumentAI.bundle */; };
@@ -1545,6 +1544,7 @@
 		ADB2D6FD294882B70029D2B3 /* KMTextFieldStepperView.xib in Resources */ = {isa = PBXBuildFile; fileRef = ADB2D6FC294882B70029D2B3 /* KMTextFieldStepperView.xib */; };
 		ADB2D6FE294882B70029D2B3 /* KMTextFieldStepperView.xib in Resources */ = {isa = PBXBuildFile; fileRef = ADB2D6FC294882B70029D2B3 /* KMTextFieldStepperView.xib */; };
 		ADB2D6FF294882B70029D2B3 /* KMTextFieldStepperView.xib in Resources */ = {isa = PBXBuildFile; fileRef = ADB2D6FC294882B70029D2B3 /* KMTextFieldStepperView.xib */; };
+		ADB595BA2AA8543F00D778B7 /* KMResourceDownload.m in Sources */ = {isa = PBXBuildFile; fileRef = ADB595B82AA8543F00D778B7 /* KMResourceDownload.m */; };
 		ADB5E50E2A3703E6007110A8 /* KMInAppPurchaseManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = AD0E8AAF2A31B76300DBFD3C /* KMInAppPurchaseManager.swift */; };
 		ADB5E5122A371131007110A8 /* KMSubscribeWaterMarkWindowController.swift in Sources */ = {isa = PBXBuildFile; fileRef = ADB5E5102A371131007110A8 /* KMSubscribeWaterMarkWindowController.swift */; };
 		ADB5E5132A371131007110A8 /* KMSubscribeWaterMarkWindowController.swift in Sources */ = {isa = PBXBuildFile; fileRef = ADB5E5102A371131007110A8 /* KMSubscribeWaterMarkWindowController.swift */; };
@@ -4032,8 +4032,6 @@
 		9FBC48BE299E23B100CA39D7 /* NSViewController+DesignToken.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "NSViewController+DesignToken.swift"; sourceTree = "<group>"; };
 		9FBDA7282A4D32A500A972F3 /* KMWebsocket.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KMWebsocket.swift; sourceTree = "<group>"; };
 		9FBDA72C2A4D532700A972F3 /* KMWebsocketManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KMWebsocketManager.swift; sourceTree = "<group>"; };
-		9FC444F42AA5E82700D7187C /* KMResourceDownload.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = KMResourceDownload.m; sourceTree = "<group>"; };
-		9FC444F52AA5E82700D7187C /* KMResourceDownload.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = KMResourceDownload.h; sourceTree = "<group>"; };
 		9FC444F82AA5F7D600D7187C /* ZipArchive.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = ZipArchive.framework; sourceTree = "<group>"; };
 		9FC444FC2AA7057B00D7187C /* DocumentAI.bundle */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.plug-in"; path = DocumentAI.bundle; sourceTree = "<group>"; };
 		9FD0FA2729CD3ED400F2AB0D /* KMRightSideEmptyVC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KMRightSideEmptyVC.swift; sourceTree = "<group>"; };
@@ -4203,6 +4201,8 @@
 		ADB2D6F1294741720029D2B3 /* KMPrintPaperSetView.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = KMPrintPaperSetView.xib; sourceTree = "<group>"; };
 		ADB2D6F8294882AE0029D2B3 /* KMTextFieldStepperView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KMTextFieldStepperView.swift; sourceTree = "<group>"; };
 		ADB2D6FC294882B70029D2B3 /* KMTextFieldStepperView.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = KMTextFieldStepperView.xib; sourceTree = "<group>"; };
+		ADB595B82AA8543F00D778B7 /* KMResourceDownload.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = KMResourceDownload.m; sourceTree = "<group>"; };
+		ADB595B92AA8543F00D778B7 /* KMResourceDownload.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = KMResourceDownload.h; sourceTree = "<group>"; };
 		ADB5E5102A371131007110A8 /* KMSubscribeWaterMarkWindowController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KMSubscribeWaterMarkWindowController.swift; sourceTree = "<group>"; };
 		ADB5E5112A371131007110A8 /* KMSubscribeWaterMarkWindowController.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = KMSubscribeWaterMarkWindowController.xib; sourceTree = "<group>"; };
 		ADB5E5182A3711CB007110A8 /* KMSubscribeWaterMarkView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KMSubscribeWaterMarkView.swift; sourceTree = "<group>"; };
@@ -6122,8 +6122,8 @@
 		9FC444F32AA5E82700D7187C /* Resource Download */ = {
 			isa = PBXGroup;
 			children = (
-				9FC444F52AA5E82700D7187C /* KMResourceDownload.h */,
-				9FC444F42AA5E82700D7187C /* KMResourceDownload.m */,
+				ADB595B92AA8543F00D778B7 /* KMResourceDownload.h */,
+				ADB595B82AA8543F00D778B7 /* KMResourceDownload.m */,
 			);
 			name = "Resource Download";
 			path = "PDF Master/Class/Resource Download";
@@ -12442,7 +12442,6 @@
 				BB897260294C5DDA0045787C /* KMWatermarkPropertyInfoController.swift in Sources */,
 				BB0A55162A3052F800B6E84B /* KMCustomTextField.swift in Sources */,
 				F3732316292DF9410013862C /* CPDFTextAnnotation+PDFListView.m in Sources */,
-				9FC444F72AA5F62200D7187C /* KMResourceDownload.m in Sources */,
 				BBB1A3A029F6128200E54E47 /* KMSystemMenu.swift in Sources */,
 				9F0201882A1DA28B00C9B673 /* KMAILanguagePopVC.swift in Sources */,
 				F37322EC292DF9410013862C /* CPDFStampAnnotation+PDFListView.m in Sources */,
@@ -12502,6 +12501,7 @@
 				899700E128F3A9F2009AF911 /* KMHomeWindowController.swift in Sources */,
 				89D2D30529504E5E00BFF5FE /* KMSharingServicePicker+ESSSharingServicePickerMenu.m in Sources */,
 				BB9DCCA12A0A2B0A0024A6F1 /* KMConvertSettingView.swift in Sources */,
+				ADB595BA2AA8543F00D778B7 /* KMResourceDownload.m in Sources */,
 				BB8F4578295AFA2D0037EA22 /* KMHeaderFooterAdjectiveInfoBaseView.swift in Sources */,
 				F3F0B27529B8ACD000722957 /* CPDFListViewDragObject.m in Sources */,
 				F34BF9552953DA91002C25A2 /* NSGeometry+PDFListView.m in Sources */,

+ 3 - 3
PDF Office/PDF Master/Class/PDFTools/Convert/View/KMConvertOCRSettingItemView.swift

@@ -78,7 +78,7 @@ class KMConvertOCRSettingItemView: KMConvertBaseView {
     
     @objc func checkBoxAction(sender: NSButton) {
 #if VERSION_DMG
-        let isExist = KMResourceDownload().documentAIBundleIsExist()
+        let isExist = KMResourceDownload.manager().documentAIBundleIsExist()
         if isExist || self._checkVC?.state == .Checked {
             if let state = self._checkVC?.state {
                 self._checkVC?.state = state == .Checked ? .Norm : .Checked
@@ -97,7 +97,7 @@ class KMConvertOCRSettingItemView: KMConvertBaseView {
                         self.showProgressWindow()
                         self.progressController?.maxValue = Double(100)
                     }
-                    KMResourceDownload().downloadFrameworkProgress { progress in
+                    KMResourceDownload.manager().downloadFrameworkProgress { progress in
                         self.progressController?.doubleValue = Double(progress*100)
                     } result: { result in
                         if result {
@@ -150,7 +150,7 @@ class KMConvertOCRSettingItemView: KMConvertBaseView {
         progress.progressField.textColor = NSColor.white
         
         progress.closeBlock = { [unowned self] in
-            
+            KMResourceDownload.manager().cancel()
         }
         
         self.progressController = progress

+ 3 - 0
PDF Office/PDF Master/Class/Resource Download/KMResourceDownload.h

@@ -13,6 +13,7 @@ typedef void(^KMResourceDownloadResult)(BOOL isSuccessful);
 typedef void(^KMResourceDownloadProgress)(double progress);
 
 @interface KMResourceDownload : NSObject
++ (KMResourceDownload *)manager;
 
 @property(nonatomic, copy) KMResourceDownloadResult downloadResultBlock;
 
@@ -22,6 +23,8 @@ typedef void(^KMResourceDownloadProgress)(double progress);
 
 - (BOOL)documentAIBundleIsExist;
 
+- (void)cancelDownload;
+
 @end
 
 NS_ASSUME_NONNULL_END

+ 45 - 14
PDF Office/PDF Master/Class/Resource Download/KMResourceDownload.m

@@ -14,21 +14,38 @@
 //    #define HAS_DOCUMENTAI_FRAMEWORK 0
 //#endif
 
+@interface KMResourceDownload()
+@property (nonatomic, strong) NSURLSessionDownloadTask *downloadTask;
+@end
+
 @implementation KMResourceDownload
+static KMResourceDownload *_manager;
++ (KMResourceDownload *)manager {
+    static dispatch_once_t onceToken;
+    dispatch_once(&onceToken, ^{
+        _manager = [[KMResourceDownload alloc] init];
+    });
+    return _manager;
+}
 
 - (void)downloadFrameworkProgress:(KMResourceDownloadProgress)currentProgress result:(KMResourceDownloadResult)result {
     self.downloadResultBlock = result;
     self.progressBlock = currentProgress;
     
-    NSString *urlString = @"http://test-pdf-pro.kdan.cn:3021/downloads/DocumentAI.bundle.zip";
-    NSURL *url = [NSURL URLWithString:urlString];
-    
-    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
-    NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
-
-    NSURLSessionDownloadTask *downloadTask = [session downloadTaskWithURL:url];
-    [downloadTask resume];
-
+    if (self.downloadTask == nil) {
+        
+        NSString *urlString = @"http://test-pdf-pro.kdan.cn:3021/downloads/DocumentAI.bundle.zip";
+        NSURL *url = [NSURL URLWithString:urlString];
+        
+        NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
+        NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
+
+        NSURLSessionDownloadTask *downloadTask = [session downloadTaskWithURL:url];
+        [downloadTask resume];
+        self.downloadTask = downloadTask;
+    } else {
+        [self dealDownLoadResult:NO];
+    }
 }
 
 // 解压 framework 文件
@@ -52,6 +69,7 @@
 
     } else {
         NSLog(@"Failed to unzip file.");
+        [self dealDownLoadResult:NO];
     }
 }
 
@@ -62,6 +80,7 @@
     NSBundle *frameworkBundle = [NSBundle bundleWithPath:destinationPath];
     if (![frameworkBundle loadAndReturnError:&error]) {
         NSLog(@"Error loading bundle: %@", error);
+        [self dealDownLoadResult:NO];
         return;
     }
     
@@ -92,6 +111,21 @@
     }
 }
 
+- (void)cancelDownload {
+    [self.downloadTask cancel];
+    self.downloadTask = nil;
+    self.progressBlock = nil;
+    self.downloadResultBlock = nil;
+}
+
+- (void)dealDownLoadResult:(BOOL)isSuccess {
+    dispatch_async(dispatch_get_main_queue(), ^{
+        if (self.downloadResultBlock) {
+            self.downloadResultBlock(isSuccess);
+            [self cancelDownload];
+        }
+    });
+}
 #pragma mark - NSURLSessionDelegate
 
 - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite {
@@ -117,6 +151,7 @@
     [fileManager moveItemAtURL:location toURL:[NSURL fileURLWithPath:destinationPath] error:&moveError];
     if (moveError) {
         NSLog(@"Failed to move framework: %@", moveError);
+        [self dealDownLoadResult:NO];
         return;
     }
     
@@ -125,11 +160,7 @@
     // 解压 framework 文件
     [self unzipFrameworkAtURL:[NSURL fileURLWithPath:destinationPath] toDestination:resourcePath];
     
-    dispatch_async(dispatch_get_main_queue(), ^{
-        if (self.downloadResultBlock) {
-            self.downloadResultBlock(YES);
-        }
-    });
+    [self dealDownLoadResult:YES];
 }
 
 @end