Browse Source

【fix】【Crash】用户反馈

tangchao 10 months ago
parent
commit
09b2e1f75c

+ 31 - 26
PDF Office/PDF Master/Class/DigtalSignature/ViewControllers/DSignatureFileListViewController.swift

@@ -23,9 +23,9 @@ import Cocoa
     @IBOutlet weak var progressIndicator: NSProgressIndicator!
     
     var _isFileList: Bool = false
-    var currentModel: KMDSignatureModel!
+    var currentModel: KMDSignatureModel?
     
-    private var signatureArrays: NSArray!
+    private var signatureArrays: NSArray?
     
     var actionBlock: ((_ fileListVC: DSignatureFileListViewController, _ type: DSignatureActionType)->Void)?
     var viewCertDetailBlock: ((_ fileListVC: DSignatureFileListViewController, _ certificate: CPDFSignatureCertificate, _ isDigitalFile: Bool)->Void)?
@@ -105,9 +105,9 @@ import Cocoa
     func reloadData() {
         self.signatureArrays = KMDSignatureManager.default().signatures
         self.tableView.reloadData()
-        if self.signatureArrays.count > 0 {
+        if let cnt = self.signatureArrays?.count, cnt > 0 {
             self.tableView.selectRowIndexes([0], byExtendingSelection: true)
-            self.currentModel = self.signatureArrays.firstObject as? KMDSignatureModel
+            self.currentModel = self.signatureArrays?.firstObject as? KMDSignatureModel
             self.continueButton.isEnabled = true
         } else {
             self.continueButton.isEnabled = false
@@ -119,11 +119,11 @@ import Cocoa
         
         for index in 0...self.tableView.numberOfRows-1 {
             let rowView = self.tableView.rowView(atRow: index, makeIfNecessary: true)
-            let cellView: DSignatureFileListCellView = rowView?.view(atColumn: 0) as! DSignatureFileListCellView
+            let cellView = rowView?.view(atColumn: 0) as? DSignatureFileListCellView
             if (index == selectRow) {
-                cellView.radioBtn.state = .on
+                cellView?.radioBtn.state = .on
             } else {
-                cellView.radioBtn.state = .off
+                cellView?.radioBtn.state = .off
             }
         }
     }
@@ -131,10 +131,12 @@ import Cocoa
     func menuItemClick_Delete() {
         let row = self.tableView.clickedRow
         
-        if row > -1 && row < self.signatureArrays.count {
-            let model = self.signatureArrays[row] as! KMDSignatureModel
-            if self.isFileList == false && model.isFormKeyChain == false {
-                KMDSignatureManager.default().removeSignatureCertPath(model.filePath)
+        let cnt = self.signatureArrays?.count ?? 0
+        if row > -1 && row < cnt {
+            let model = self.signatureArrays?[row] as? KMDSignatureModel
+            let isFormKeyChain = model?.isFormKeyChain ?? true
+            if self.isFileList == false && isFormKeyChain == false {
+                KMDSignatureManager.default().removeSignatureCertPath(model?.filePath ?? "")
             }
             self.reloadData()
         }
@@ -204,8 +206,9 @@ import Cocoa
     func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
         let cellView = DSignatureFileListCellView()
         cellView.itemIndex = row
-        if (self.signatureArrays.count > row) {
-            let model = self.signatureArrays[row] as! KMDSignatureModel
+        let cnt = self.signatureArrays?.count ?? 0
+        if (cnt > row) {
+            let model = self.signatureArrays?[row] as? KMDSignatureModel
             cellView.signModel = model
         }
         if (cellView.signModel == self.currentModel) {
@@ -217,13 +220,13 @@ import Cocoa
         weak var weakSelf = self
         cellView.radioActionHandle = {_ in
             if weakSelf!.tableView.numberOfRows > cellView.itemIndex {
-                weakSelf!.tableView.selectRowIndexes([cellView.itemIndex], byExtendingSelection: false)
+                weakSelf?.tableView.selectRowIndexes([cellView.itemIndex], byExtendingSelection: false)
             }
-            weakSelf!.currentModel = weakSelf!.signatureArrays[weakSelf!.tableView.selectedRow] as? KMDSignatureModel
-            weakSelf!.refreshSelectedState()
+            weakSelf?.currentModel = weakSelf!.signatureArrays?[weakSelf!.tableView.selectedRow] as? KMDSignatureModel
+            weakSelf?.refreshSelectedState()
         }
         cellView.viewDetailBlock = {cellView in
-            var filePath: NSString!
+            var filePath: NSString?
             var save: Bool = true
             var password: String = ""
             
@@ -240,7 +243,7 @@ import Cocoa
                 cellView.signModel?.filePath = filePath as? String
             } else {
                 if cellView.signModel?.password != nil {
-                    password = (cellView.signModel?.password)!
+                    password = (cellView.signModel?.password) ?? ""
                 }
                 if cellView.signModel?.filePath != nil {
                     filePath = cellView.signModel?.filePath as? NSString
@@ -248,13 +251,13 @@ import Cocoa
             }
             
             if save {
-                var certificate: CPDFSignatureCertificate!
+                var certificate: CPDFSignatureCertificate?
                 certificate = CPDFSignatureCertificate(pkcs12Path: filePath as String?, password: password as String?)
                 if certificate != nil {
                     guard let callBack = self.viewCertDetailBlock else {
                         return
                     }
-                    callBack(self, certificate, !cellView.signModel!.isFormKeyChain)
+                    callBack(self, certificate!, !(cellView.signModel?.isFormKeyChain ?? false))
                 }
             } else {
                 let alert = NSAlert.init()
@@ -267,9 +270,9 @@ import Cocoa
     }
     
     func tableViewSelectionDidChange(_ notification: Notification) {
-        
-        if (self.signatureArrays.count > self.tableView.selectedRow) {
-            self.currentModel = self.signatureArrays[self.tableView.selectedRow] as? KMDSignatureModel
+        let cnt = self.signatureArrays?.count ?? 0
+        if (cnt > self.tableView.selectedRow) {
+            self.currentModel = self.signatureArrays?[self.tableView.selectedRow] as? KMDSignatureModel
             
             self.refreshSelectedState()
         }
@@ -280,9 +283,11 @@ import Cocoa
         menu.removeAllItems()
         
         let row = self.tableView.clickedRow
-        if row > -1 && row < self.signatureArrays.count {
-            let model = self.signatureArrays[row] as! KMDSignatureModel
-            if self.isFileList == false && model.isFormKeyChain == false {
+        let cnt = self.signatureArrays?.count ?? 0
+        if row > -1 && row < cnt {
+            let model = self.signatureArrays?[row] as? KMDSignatureModel
+            let isFormKeyChain = model?.isFormKeyChain ?? true
+            if self.isFileList == false && isFormKeyChain == false {
                 menu.addItem(withTitle: NSLocalizedString("Delete", comment: ""), action: #selector(menuItemClick_Delete), keyEquivalent: "")
             }
         }

+ 15 - 14
PDF Office/PDF Master/Class/DigtalSignature/ViewControllers/Views/DSignatureFileListCellView.swift

@@ -21,12 +21,12 @@ class DSignatureFileListCellView: NSTableCellView {
     @IBOutlet weak var viewDetailBtn: NSButton!
     
     var itemIndex: Int = 0
-    var _signModel: KMDSignatureModel!
+    var _signModel: KMDSignatureModel?
     
     var radioActionHandle: ((DSignatureFileListCellView)->Void)?
     typealias refreshHandle = (_ cellView: DSignatureFileListCellView) ->Void
     
-    var viewDetailBlock: refreshHandle!
+    var viewDetailBlock: refreshHandle?
     
     // MARK: 初始化
     override init(frame frameRect: NSRect) {
@@ -64,14 +64,15 @@ class DSignatureFileListCellView: NSTableCellView {
         
         self.viewDetailBtn.title = NSLocalizedString("View Details", comment: "")
         
-        let cell: NSButtonCell = self.viewDetailBtn.cell as! NSButtonCell
-        let txtColor = KMAppearance.Interactive.a0Color()
-
-        let attrStr1 = cell.attributedTitle
-        let myAttr = NSMutableAttributedString.init(attributedString: attrStr1)
-        myAttr.addAttributes([NSAttributedString.Key.foregroundColor : txtColor], range: NSRange(location: 0, length: attrStr1.string.count))
-        cell.attributedTitle = myAttr
-        self.viewDetailBtn.updateCell(cell)
+        if let cell = self.viewDetailBtn.cell as? NSButtonCell {
+            let txtColor = KMAppearance.Interactive.a0Color()
+            
+            let attrStr1 = cell.attributedTitle
+            let myAttr = NSMutableAttributedString.init(attributedString: attrStr1)
+            myAttr.addAttributes([NSAttributedString.Key.foregroundColor : txtColor], range: NSRange(location: 0, length: attrStr1.string.count))
+            cell.attributedTitle = myAttr
+            self.viewDetailBtn.updateCell(cell)
+        }
         
         self.nameLabel.textColor = KMAppearance.Layout.h0Color()
         self.sourceLabel.textColor = KMAppearance.Layout.h0Color()
@@ -84,9 +85,9 @@ class DSignatureFileListCellView: NSTableCellView {
         set {
             _signModel = newValue
             
-            self.nameLabel.stringValue = _signModel.name ?? ""
-            self.authorLabel.stringValue = NSLocalizedString("Issued by:", comment: "") + _signModel.issusName
-            if (_signModel.isFormKeyChain) {
+            self.nameLabel.stringValue = _signModel?.name ?? ""
+            self.authorLabel.stringValue = NSLocalizedString("Issued by:", comment: "") + (_signModel?.issusName ?? "")
+            if let data = _signModel?.isFormKeyChain, data {
                 self.sourceLabel.stringValue = NSLocalizedString("(Keychain Digital ID)", comment: "");
                 self.signImgView.image = NSImage(named: "ImageNameDSignatSaveKechain")
             } else {
@@ -96,7 +97,7 @@ class DSignatureFileListCellView: NSTableCellView {
             
             let dateFormatter = DateFormatter()
             dateFormatter.dateFormat = "yyyy-MM-dd"
-            self.dateLabel.stringValue = NSLocalizedString("Expires", comment: "") + ":" + dateFormatter.string(from: _signModel.expiresDate)
+            self.dateLabel.stringValue = NSLocalizedString("Expires", comment: "") + ":" + dateFormatter.string(from: (_signModel?.expiresDate ?? Date()))
                                   
             self.nameLabel.toolTip = self.nameLabel.stringValue
             self.authorLabel.toolTip = self.authorLabel.stringValue