Просмотр исходного кода

AI - 首页文件翻译补充Loading、补充错误信息显示

wanjun 1 год назад
Родитель
Сommit
af4731cc6a

+ 1 - 0
PDF Office/PDF Master/Class/Home/ViewController/KMAIRewritingVC.swift

@@ -307,6 +307,7 @@ class KMAIRewritingVC: NSViewController {
         progress.progressField.textColor = NSColor.white
         
         progress.closeBlock = { [unowned self] in
+            
         }
         
         self.progressController = progress

+ 74 - 5
PDF Office/PDF Master/Class/Home/ViewController/KMAITranslationVC.swift

@@ -25,12 +25,16 @@ class KMAITranslationVC: NSViewController {
     @IBOutlet weak var selectYourFilesImageView: NSImageView!
     @IBOutlet weak var orDropFileHereLabel: NSTextField!
     
+    @IBOutlet weak var errorView: NSView!
+    @IBOutlet weak var errorLabel: NSTextField!
+    
     var fromStr: String = "auto"
     var toStr: String = "en"
     var fromLanguages: [String] = ["Automatic", "English", "Simplified Chinese", "Traditional Chinese", "Japanese", "Korean", "French", "Spanish", "Italian", "German", "Portuguese", "Russian", "Vietnamese", "Thai", "Arabic", "Greek", "Bulgarian", "Finnish", "Slovene", "Dutch", "Czech", "Swedish", "Polish", "Danish", "Romanian", "Hungarian"]
     var toLanguages: [String] = ["English", "Simplified Chinese", "Traditional Chinese", "Japanese", "Korean", "French", "Spanish", "Italian", "German", "Portuguese", "Russian", "Vietnamese", "Thai", "Arabic", "Greek", "Bulgarian", "Finnish", "Slovene", "Dutch", "Czech", "Swedish", "Polish", "Danish", "Romanian", "Hungarian"]
 
     var popover: NSPopover?
+    var progressController: SKProgressController?
 
     override func viewDidLoad() {
         super.viewDidLoad()
@@ -40,6 +44,12 @@ class KMAITranslationVC: NSViewController {
         self.initializeUI()
     }
     
+    override func viewDidAppear() {
+        super.viewDidAppear()
+        
+        self.errorView.isHidden = true
+    }
+    
     // MARK: initialize
     
     func initializeUI() -> Void {
@@ -94,6 +104,8 @@ class KMAITranslationVC: NSViewController {
         }
         self.selectYourFilesBox.downCallback = { [unowned self](downEntered: Bool, mouseBox: KMBox, event) -> Void in
             if downEntered {
+                self.errorView.isHidden = true
+
                 let openPanel = NSOpenPanel()
                 openPanel.allowedFileTypes = ["pdf", "PDF", "docx", "doc"]
                 openPanel.allowsMultipleSelection = false
@@ -106,10 +118,16 @@ class KMAITranslationVC: NSViewController {
                                 alert.messageText = NSLocalizedString("An error occurred while opening this document. The file is damaged and could not be repaired.", comment: "")
                                 alert.runModal()
                             } else {
+                                DispatchQueue.main.async {
+                                    self.showProgressWindow()
+//                                    self.progressController?.message = NSLocalizedString("Uploading...", comment: "")
+                                }
+                                
                                 let infoDictionary = Bundle .main.infoDictionary!
                                 let majorVersion = infoDictionary["CFBundleShortVersionString"]
 
                                 KMRequestServerManager.manager.aiTranslationFileUpload(file: url.path, version: "1.0.1") { [unowned self] success, result in
+//                                    self.hiddenProgressWindow()
                                     if success {
                                         let result: NSDictionary = result!.result
 
@@ -120,7 +138,13 @@ class KMAITranslationVC: NSViewController {
                                             self.fileTranslateHandle(fileKey as! String)
                                         }
                                     } else {
-                                        
+                                        let result: String = result!.message
+
+                                        DispatchQueue.main.async {
+                                            self.hiddenProgressWindow()
+                                            self.errorView.isHidden = false
+                                            self.errorLabel.stringValue = result
+                                        }
                                     }
                                 }
                             }
@@ -130,6 +154,13 @@ class KMAITranslationVC: NSViewController {
 
             }
         }
+        
+        self.errorView.isHidden = true
+        self.errorView.wantsLayer = true
+        self.errorView.layer?.backgroundColor = NSColor(hex: "##36383B").cgColor
+        self.errorView.layer?.cornerRadius = 4.0
+        self.errorLabel.textColor = NSColor(hex: "#FFFFFF")
+        self.errorLabel.font = NSFont.SFProTextRegular(14.0)
     }
     
     func initLocalization() -> Void {
@@ -148,7 +179,10 @@ class KMAITranslationVC: NSViewController {
     func fileTranslateHandle(_ fileKey: String) -> Void {
         let infoDictionary = Bundle .main.infoDictionary!
         let majorVersion = infoDictionary["CFBundleShortVersionString"]
-        
+//        DispatchQueue.main.async {
+//            self.showProgressWindow()
+//        }
+
         KMRequestServerManager.manager.aiTranslationFileTranslateHandle(fileKey: fileKey, from: self.fromStr, to: self.toStr, version: "1.0.1") { success, result in
             if success {
                 let result: NSDictionary = result!.result
@@ -162,6 +196,15 @@ class KMAITranslationVC: NSViewController {
                 let to: String = result["to"] as! String
                 
                 self.downloadFile(filePath: ossDownUrl, downFileName: downFileName)
+            } else {
+                let result: String = result!.message
+
+                DispatchQueue.main.async {
+                    self.hiddenProgressWindow()
+                    
+                    self.errorView.isHidden = false
+                    self.errorLabel.stringValue = result
+                }
             }
         }
     }
@@ -204,11 +247,14 @@ class KMAITranslationVC: NSViewController {
 
                 return
             }
-
-
+            
+            DispatchQueue.main.async {
+                self.hiddenProgressWindow()
+            }
+            
             do {
                 try FileManager.default.moveItem(at: tempLocalURL, to: destinationURL)
-                
+
                 NSDocumentController.shared.openDocument(withContentsOf: destinationURL, display: true) { document, documentWasAlreadyOpen, error in
                     if error != nil {
                         NSApp.presentError(error!)
@@ -283,6 +329,29 @@ class KMAITranslationVC: NSViewController {
         return "auto"
     }
     
+    func showProgressWindow() {
+        let progress = SKProgressController()
+        progress.message = NSLocalizedString("translation...", comment: "")
+        progress.window?.backgroundColor = NSColor(hex: "#36383B")
+        progress.window?.contentView?.wantsLayer = true
+        progress.window?.contentView?.layer?.backgroundColor = NSColor(hex: "#36383B").cgColor
+        progress.progressField.textColor = NSColor.white
+        
+        progress.closeBlock = { [unowned self] in
+            
+        }
+        
+        self.progressController = progress
+        self.view.window?.beginSheet(progress.window!)
+    }
+    
+    func hiddenProgressWindow() {
+        if (self.progressController != nil) {
+            self.view.window?.endSheet((self.progressController?.window)!)
+            self.progressController = nil
+        }
+    }
+    
     // MARK: Action Methods
     
     func languageAction(_ isFromLanguage: Bool) -> Void {

+ 34 - 0
PDF Office/PDF Master/Class/Home/ViewController/KMAITranslationVC.xib

@@ -11,6 +11,8 @@
                 <outlet property="aiTranslationView" destination="8ax-UJ-9Yb" id="DQF-hv-9GS"/>
                 <outlet property="automaticBox" destination="0oh-UB-dwx" id="rep-xx-EBj"/>
                 <outlet property="automaticLabel" destination="QWm-C6-Sfz" id="OqH-PR-KVZ"/>
+                <outlet property="errorLabel" destination="QSA-QC-2cE" id="ppQ-wJ-llV"/>
+                <outlet property="errorView" destination="xq4-gK-6AB" id="5gF-KE-Fwu"/>
                 <outlet property="languageBox" destination="gs4-sG-7XS" id="nLe-05-u3W"/>
                 <outlet property="languageImageView" destination="UhD-tL-SCP" id="Vwv-gm-TtR"/>
                 <outlet property="languageLabel" destination="Bu3-gS-M5Y" id="IaM-FA-bAo"/>
@@ -269,6 +271,35 @@
                         <constraint firstItem="tCA-C4-nrI" firstAttribute="centerX" secondItem="8ax-UJ-9Yb" secondAttribute="centerX" id="vgs-LB-9o5"/>
                     </constraints>
                 </customView>
+                <customView translatesAutoresizingMaskIntoConstraints="NO" id="xq4-gK-6AB">
+                    <rect key="frame" x="458" y="457" width="89" height="40"/>
+                    <subviews>
+                        <imageView horizontalHuggingPriority="251" verticalHuggingPriority="251" translatesAutoresizingMaskIntoConstraints="NO" id="Zsv-ud-4nL">
+                            <rect key="frame" x="16" y="12" width="16" height="16"/>
+                            <constraints>
+                                <constraint firstAttribute="width" constant="16" id="lk3-O6-ef4"/>
+                                <constraint firstAttribute="height" constant="16" id="sjl-kB-UMh"/>
+                            </constraints>
+                            <imageCell key="cell" refusesFirstResponder="YES" alignment="left" imageScaling="proportionallyDown" image="icon_error" id="fnv-5l-c7j"/>
+                        </imageView>
+                        <textField horizontalHuggingPriority="251" verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="QSA-QC-2cE">
+                            <rect key="frame" x="38" y="12" width="37" height="16"/>
+                            <textFieldCell key="cell" lineBreakMode="clipping" title="Label" id="nM2-YC-0lf">
+                                <font key="font" metaFont="system"/>
+                                <color key="textColor" name="labelColor" catalog="System" colorSpace="catalog"/>
+                                <color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
+                            </textFieldCell>
+                        </textField>
+                    </subviews>
+                    <constraints>
+                        <constraint firstAttribute="height" constant="40" id="Afs-kl-9Ht"/>
+                        <constraint firstItem="QSA-QC-2cE" firstAttribute="leading" secondItem="Zsv-ud-4nL" secondAttribute="trailing" constant="8" id="K0A-7h-Rjd"/>
+                        <constraint firstItem="Zsv-ud-4nL" firstAttribute="centerY" secondItem="xq4-gK-6AB" secondAttribute="centerY" id="RUT-4q-gMM"/>
+                        <constraint firstItem="QSA-QC-2cE" firstAttribute="centerY" secondItem="Zsv-ud-4nL" secondAttribute="centerY" id="Tlt-wN-piF"/>
+                        <constraint firstItem="Zsv-ud-4nL" firstAttribute="leading" secondItem="xq4-gK-6AB" secondAttribute="leading" constant="16" id="c3A-h3-bn1"/>
+                        <constraint firstAttribute="trailing" secondItem="QSA-QC-2cE" secondAttribute="trailing" constant="16" id="ghk-IJ-BT6"/>
+                    </constraints>
+                </customView>
             </subviews>
             <constraints>
                 <constraint firstAttribute="trailing" secondItem="8ax-UJ-9Yb" secondAttribute="trailing" constant="32" id="5m9-lH-FE7"/>
@@ -276,12 +307,15 @@
                 <constraint firstItem="ED8-nx-ye3" firstAttribute="leading" secondItem="Hz6-mo-xeY" secondAttribute="leading" constant="32" id="CSa-OT-xq6"/>
                 <constraint firstItem="8ax-UJ-9Yb" firstAttribute="top" secondItem="ED8-nx-ye3" secondAttribute="bottom" constant="16" id="GfY-Ls-poL"/>
                 <constraint firstAttribute="trailing" secondItem="ED8-nx-ye3" secondAttribute="trailing" constant="32" id="Pv7-dW-axv"/>
+                <constraint firstItem="xq4-gK-6AB" firstAttribute="centerX" secondItem="Hz6-mo-xeY" secondAttribute="centerX" id="bTF-2c-uNY"/>
                 <constraint firstItem="ED8-nx-ye3" firstAttribute="top" secondItem="Hz6-mo-xeY" secondAttribute="top" constant="40" id="iMO-Kj-SwU"/>
+                <constraint firstItem="xq4-gK-6AB" firstAttribute="top" secondItem="Hz6-mo-xeY" secondAttribute="top" constant="24" id="mnT-27-dxL"/>
             </constraints>
             <point key="canvasLocation" x="401.5" y="264.5"/>
         </customView>
     </objects>
     <resources>
         <image name="icon_btn_arrow_gray_down_s_norm_false" width="12" height="12"/>
+        <image name="icon_error" width="16" height="16"/>
     </resources>
 </document>

+ 43 - 13
PDF Office/PDF Master/Class/Home/ViewController/KMHomeViewController+Action.swift

@@ -598,23 +598,40 @@ extension KMHomeViewController {
     }
     
     func aiTranslation(withFilePath path: String) -> Void {
-        let infoDictionary = Bundle .main.infoDictionary!
-         let majorVersion = infoDictionary["CFBundleShortVersionString"]
+        if !path.isPDFValid() {
+            let alert = NSAlert()
+            alert.alertStyle = .critical
+            alert.messageText = NSLocalizedString("An error occurred while opening this document. The file is damaged and could not be repaired.", comment: "")
+            alert.runModal()
+        } else {
+            let infoDictionary = Bundle .main.infoDictionary!
+             let majorVersion = infoDictionary["CFBundleShortVersionString"]
+            DispatchQueue.main.async {
+                self.showProgressWindow()
+            }
+            
+             KMRequestServerManager.manager.aiTranslationFileUpload(file: path, version: "1.0.1") { [unowned self] success, result in
+                 if success {
+                     let result: NSDictionary = result!.result
 
-         KMRequestServerManager.manager.aiTranslationFileUpload(file: path, version: "1.0.1") { [unowned self] success, result in
-             if success {
-                 let result: NSDictionary = result!.result
+                     let fileKey = result["fileKey"]
+                     let fileName = result["fileName"]
+                     let pageCount = result["pageCount"]
+                     if fileKey != nil {
+                         self.fileTranslateHandle(fileKey as! String)
+                     }
+                 } else {
+                     let result: String = result!.message
 
-                 let fileKey = result["fileKey"]
-                 let fileName = result["fileName"]
-                 let pageCount = result["pageCount"]
-                 if fileKey != nil {
-                     self.fileTranslateHandle(fileKey as! String)
+                     DispatchQueue.main.async {
+                         self.hiddenProgressWindow()
+                         self.aiTranslationViewController.errorView.isHidden = false
+                         self.aiTranslationViewController.errorLabel.stringValue = result
+
+                     }
                  }
-             } else {
-                 
              }
-         }
+        }
     }
     
     func fileTranslateHandle(_ fileKey: String) -> Void {
@@ -634,6 +651,15 @@ extension KMHomeViewController {
                 let to: String = result["to"] as! String
                 
                 self.downloadFile(filePath: ossDownUrl, downFileName: downFileName)
+            } else {
+                let result: String = result!.message
+
+                DispatchQueue.main.async {
+                    self.hiddenProgressWindow()
+                    
+                    self.aiTranslationViewController.errorView.isHidden = false
+                    self.aiTranslationViewController.errorLabel.stringValue = result
+                }
             }
         }
     }
@@ -676,6 +702,10 @@ extension KMHomeViewController {
 
                 return
             }
+            
+            DispatchQueue.main.async {
+                self.hiddenProgressWindow()
+            }
 
             do {
                 try FileManager.default.moveItem(at: tempLocalURL, to: destinationURL)

+ 24 - 0
PDF Office/PDF Master/Class/Home/ViewController/KMHomeViewController.swift

@@ -119,6 +119,7 @@ import KMAdvertisement
     var pdfOthersButtonVC: KMTextImageButtonVC!
     var popover: NSPopover?
     var deviceBrowserWC: KMDeviceBrowserWindowController?
+    var progressController: SKProgressController?
 
     override func viewWillAppear() {
         super.viewWillAppear()
@@ -592,6 +593,29 @@ import KMAdvertisement
         
         return resultFilePath;
     }
+    
+    func showProgressWindow() {
+        let progress = SKProgressController()
+        progress.message = NSLocalizedString("translation...", comment: "")
+        progress.window?.backgroundColor = NSColor(hex: "#36383B")
+        progress.window?.contentView?.wantsLayer = true
+        progress.window?.contentView?.layer?.backgroundColor = NSColor(hex: "#36383B").cgColor
+        progress.progressField.textColor = NSColor.white
+        
+        progress.closeBlock = { [unowned self] in
+            
+        }
+        
+        self.progressController = progress
+        self.view.window?.beginSheet(progress.window!)
+    }
+    
+    func hiddenProgressWindow() {
+        if (self.progressController != nil) {
+            self.view.window?.endSheet((self.progressController?.window)!)
+            self.progressController = nil
+        }
+    }
 
     // MARK: Common methods