ソースを参照

Demo 功能 - 转档过程中支持【取消当前文档的转档任务】

zhudongyong 1 年間 前
コミット
c519d4c3cd

+ 8 - 0
KdanAutoTest/KdanAuto/Class/Norrmal/AutoTestAdvanceSettingView.swift

@@ -442,6 +442,7 @@ class AutoTestAdvanceSettingView : NSView, NSTableViewDataSource, NSTableViewDel
         let originDirectory = _autoTestObj?.originFileDirectory()
         let path = NSString(string: originDirectory!).appendingPathComponent(fileName);
         
+        FileConverter.shared().cancelTaskForSrcPath(path);
         try? FileManager.default.removeItem(atPath: path);
         
         let index = _files.firstIndex(of: fileName)
@@ -451,4 +452,11 @@ class AutoTestAdvanceSettingView : NSView, NSTableViewDataSource, NSTableViewDel
             _fileList.reloadData()
         }
     }
+    
+    func fileCellNeedCancelConvertTask(_ cell:TestFileCellView, fileName:String) {
+        let originDirectory = _autoTestObj?.originFileDirectory()
+        let path = NSString(string: originDirectory!).appendingPathComponent(fileName);
+        
+        FileConverter.shared().cancelTaskForSrcPath(path);
+    }
 }

+ 16 - 1
KdanAutoTest/KdanAuto/Class/Norrmal/Cell/TestFileCellView.swift

@@ -16,6 +16,8 @@ protocol TestFileCellViewDelegate : NSObjectProtocol {
     func fileCellNeedShowInFinder(_ cell:TestFileCellView, fileName:String)
     
     func fileCellNeedDelete(_ cell:TestFileCellView, fileName:String)
+    
+    func fileCellNeedCancelConvertTask(_ cell:TestFileCellView, fileName:String)
 }
 
 class TestFileCellView : NSTableCellView, NSMenuDelegate {
@@ -29,7 +31,7 @@ class TestFileCellView : NSTableCellView, NSMenuDelegate {
     var _title : String!
     var _isExpad: Bool!
     var _typeInfo : NSDictionary!
-    
+    var _cancelMenuItem : NSMenuItem!
     var _isActivting : Bool = false;
     
     var _degree : Double = 0
@@ -61,6 +63,10 @@ class TestFileCellView : NSTableCellView, NSMenuDelegate {
         let item1 = NSMenuItem.init(title: "删除", action: #selector(self.deleteActions(_:)), keyEquivalent: "")
         _menu.addItem(item1)
         item1.target = self;
+        _cancelMenuItem = NSMenuItem.init(title: "取消转档任务", action: #selector(self.cancelAction(_:)), keyEquivalent: "")
+        _cancelMenuItem.isEnabled = false;
+        _menu.addItem(_cancelMenuItem)
+        _cancelMenuItem.target = self;
         self.menu = _menu
         self.menu?.delegate = self;
         
@@ -95,8 +101,10 @@ class TestFileCellView : NSTableCellView, NSMenuDelegate {
         
         if (_isActivting) {
             _progressView.startAnimation(nil);
+            _cancelMenuItem.isEnabled = true;
         }else {
             _progressView.stopAnimation(nil);
+            _cancelMenuItem.isEnabled = false;
         }
         _progressView.isHidden = !_isActivting
     }
@@ -163,4 +171,11 @@ class TestFileCellView : NSTableCellView, NSMenuDelegate {
             _delegate?.fileCellNeedDelete(self, fileName: _title)
         }
     }
+    
+    @IBAction func cancelAction(_ sender:NSMenuItem) {
+        if (_delegate != nil) {
+            _cancelMenuItem.isEnabled = false;
+            _delegate?.fileCellNeedCancelConvertTask(self, fileName: _title);
+        }
+    }
 }

+ 8 - 2
KdanAutoTest/KdanAuto/Class/Tools/FileConveter/FileConverter.swift

@@ -327,7 +327,7 @@ class FileConverter : NSObject, CPDFConverterDelegate, CPDFConverterFPDelegate {
     
     
     /// Cancel
-    func cancel() {
+    func cancelTask() {
         if (self.fpConverter != nil) {
             self.fpConverter!.stopConvertsionIfNeed()
             self.fpConverter = nil;
@@ -335,15 +335,21 @@ class FileConverter : NSObject, CPDFConverterDelegate, CPDFConverterFPDelegate {
             Thread.sleep(forTimeInterval: 0.3)
         }
         if (self.pdfConverter != nil) {
+            self.pdfConverter?.delegate = nil
             if self.pdfConverter?.isConverting == true {
                 self.pdfConverter?.cancel()
             }
+            
+            self.pdfConverter = nil
+            
+            Thread.sleep(forTimeInterval: 0.3)
+            self.complention(didSuccess)
         }
     }
     
     func cancelTaskForSrcPath(_ srcPath:String) {
         if (NSString(string: srcPath).isEqual(to: self.originSrcPath)) {
-            cancel();
+            cancelTask();
         }
     }
 }

+ 0 - 13
KdanAutoTest/KdanAuto/ViewController.swift

@@ -181,7 +181,6 @@ class ViewController : NSViewController, SettingViewControllerDelegate, AutoTest
                 NSLog("[KdanAuto]Auto Test \(objectIndex)")
                 if (objectIndex >= objects.count) {
                     // 所有自动化测试对象均执行完成
-                    NSLog("[KdanAuto]6");
                     do {
                         let rtfData = try? report.data(from: .init(location: 0, length: report.length),
                                                       documentAttributes: [.documentType: NSAttributedString.DocumentType.rtf])
@@ -190,12 +189,9 @@ class ViewController : NSViewController, SettingViewControllerDelegate, AutoTest
                         try? FileManager.default.removeItem(atPath: path);
                         try? rtfData?.write(to: NSURL.fileURL(withPath: path))
                     } catch {
-                        NSLog("[KdanAuto]7");
                     }
                     
-                    NSLog("[KdanAuto]8");
                     DispatchQueue.main.async {
-                        NSLog("[KdanAuto]9");
                         self.view.window?.title = "KdanAuto:"+CPDFConvertKit.sharedInstance().versionString
                         
                         self._isProcessing = false
@@ -208,7 +204,6 @@ class ViewController : NSViewController, SettingViewControllerDelegate, AutoTest
                         self.advanceBtn.isEnabled = true;
                         
                         TestDegreeManager.shared().saveInfo()
-                        NSLog("[KdanAuto]10");
                     }
                     return
                 }
@@ -221,15 +216,12 @@ class ViewController : NSViewController, SettingViewControllerDelegate, AutoTest
                 var complention:(_ object:AutoTest, _ report:NSAttributedString?) -> () = {(object, inReport) in
                 };
                 complention = {(object, inReport) in
-                    NSLog("[KdanAuto]0");
                     if inReport != nil {
                         // 如果有自动化测试报告,则拼接在后面
                         report.append(inReport!)
                     }
                     
-                    NSLog("[KdanAuto]1");
                     if (Thread.isMainThread) {
-                        NSLog("[KdanAuto]3");
                         // 更新当前自动化测试对象状态及相关 UI 状态
                         object.setStatus(.Finished)
                         
@@ -239,10 +231,8 @@ class ViewController : NSViewController, SettingViewControllerDelegate, AutoTest
                             object.isEqual(to: self.advanceView.getAutoTestObj())) {
                             self.advanceView.setAutoTestObj(self.advanceView.getAutoTestObj());
                         }
-                        NSLog("[KdanAuto]4");
                     }else {
                         DispatchQueue.main.sync {
-                            NSLog("[KdanAuto]3");
                             // 更新当前自动化测试对象状态及相关 UI 状态
                             object.setStatus(.Finished)
                             
@@ -252,17 +242,14 @@ class ViewController : NSViewController, SettingViewControllerDelegate, AutoTest
                                 object.isEqual(to: self.advanceView.getAutoTestObj())) {
                                 self.advanceView.setAutoTestObj(self.advanceView.getAutoTestObj());
                             }
-                            NSLog("[KdanAuto]4");
                         }
                     }
                     
-                    NSLog("[KdanAuto]2");
                     DispatchQueue.global().asyncAfter(deadline: DispatchTime.init(uptimeNanoseconds: 300)) {
                         autoreleasepool {
                             // 触发下一个自动化测试对象进入自动化测试
                             objectIndex += 1
                             self.autotestBlock(objects)
-                            NSLog("[KdanAuto]5");
                         }
                     }
                 };