From a606ae2f98bac7fd814c6d1bb7b0a266400008ed Mon Sep 17 00:00:00 2001 From: kyleduo Date: Tue, 23 May 2017 00:34:40 +0800 Subject: [PATCH] catch error --- source/tinypng4mac/Base.lproj/Main.storyboard | 1 + source/tinypng4mac/Info.plist | 4 +- source/tinypng4mac/MainViewController.swift | 16 +++--- source/tinypng4mac/tpclient/TPClient.swift | 57 +++++++++---------- source/tinypng4mac/utils/TPConfig.swift | 1 + 5 files changed, 40 insertions(+), 39 deletions(-) diff --git a/source/tinypng4mac/Base.lproj/Main.storyboard b/source/tinypng4mac/Base.lproj/Main.storyboard index 9fa446c..6656d2d 100644 --- a/source/tinypng4mac/Base.lproj/Main.storyboard +++ b/source/tinypng4mac/Base.lproj/Main.storyboard @@ -483,6 +483,7 @@ + diff --git a/source/tinypng4mac/Info.plist b/source/tinypng4mac/Info.plist index 041cddb..d3a560d 100644 --- a/source/tinypng4mac/Info.plist +++ b/source/tinypng4mac/Info.plist @@ -17,11 +17,11 @@ CFBundlePackageType APPL CFBundleShortVersionString - 0.9.3 + 1.0.0 CFBundleSignature ???? CFBundleVersion - 5 + 6 LSMinimumSystemVersion $(MACOSX_DEPLOYMENT_TARGET) NSHumanReadableCopyright diff --git a/source/tinypng4mac/MainViewController.swift b/source/tinypng4mac/MainViewController.swift index 5ed6a4e..6cea36a 100644 --- a/source/tinypng4mac/MainViewController.swift +++ b/source/tinypng4mac/MainViewController.swift @@ -12,6 +12,7 @@ class MainViewController: NSViewController, NSOpenSavePanelDelegate, NSTableView @IBOutlet weak var apiKey: NSTextField! @IBOutlet weak var outputPathField: NSTextField! + @IBOutlet weak var outputPathSelectButton: NSButton! @IBOutlet weak var taskTableView: NSTableView! @IBOutlet weak var dropContainer: DragContainer! @IBOutlet weak var totalReduce: NSTextField! @@ -101,15 +102,19 @@ class MainViewController: NSViewController, NSOpenSavePanelDelegate, NSTableView }) } - func lockTextField() { + func lockUI() { apiKey.isEditable = false outputPathField.isEditable = false + replaceSwitch.isEnabled = false + outputPathSelectButton.isEnabled = false } func unlockUI() { apiKey.isEditable = true - outputPathField.isEditable = true replaceSwitch.isEnabled = true + outputPathSelectButton.isEnabled = true + + outputPathField.isEditable = !TPConfig.shouldReplace() } // MARK: - tpclient callback @@ -167,9 +172,6 @@ class MainViewController: NSViewController, NSOpenSavePanelDelegate, NSTableView } @IBAction func clickSettings(_ sender: AnyObject) { - if TPClient.sharedClient.runningTasks > 0 { - return - } let window = NSApplication.shared().windows.first! let height = window.frame.height changePanel(height == 320, animated: true) @@ -230,9 +232,7 @@ class MainViewController: NSViewController, NSOpenSavePanelDelegate, NSTableView icon.animator().alphaValue = 0 desc.animator().alphaValue = 0 - apiKey.isEditable = false - outputPathField.isEditable = false - replaceSwitch.isEnabled = false + lockUI() changePanel(false, animated: true) } diff --git a/source/tinypng4mac/tpclient/TPClient.swift b/source/tinypng4mac/tpclient/TPClient.swift index 57b923b..d3ac648 100644 --- a/source/tinypng4mac/tpclient/TPClient.swift +++ b/source/tinypng4mac/tpclient/TPClient.swift @@ -86,34 +86,34 @@ class TPClient { } }) .responseJSON(completionHandler: { (response) in - let json = JSON(response.result.value!) - if json != JSON.null { - if let error = json["error"].string { - debugPrint("error: " + task.fileName + error) - task.errorMessage = json["message"].string - self.updateStatus(task, newStatus: .error) - return - } - let output = json["output"] - if output != JSON.null { - let resultUrl = output["url"] - task.resultUrl = String(describing: resultUrl) - task.resultSize = output["size"].doubleValue - task.compressRate = task.resultSize / task.originSize - self.onUploadFinish(task) + if let jsonstr = response.result.value { + let json = JSON(jsonstr) + if json != JSON.null { + if let error = json["error"].string { + debugPrint("error: " + task.fileName + error) + self.markError(task, errorMessage: json["message"].string) + return + } + let output = json["output"] + if output != JSON.null { + let resultUrl = output["url"] + task.resultUrl = String(describing: resultUrl) + task.resultSize = output["size"].doubleValue + task.compressRate = task.resultSize / task.originSize + self.onUploadFinish(task) + } else { + self.markError(task, errorMessage: "response data error") + } } else { - task.errorMessage = "error response" - self.updateStatus(task, newStatus: .error) + self.markError(task, errorMessage: "response format error") } } else { - task.errorMessage = "error response" - self.updateStatus(task, newStatus: .error) + self.markError(task, errorMessage: response.result.description) } }) return true } catch { - task.errorMessage = "error execution" - self.updateStatus(task, newStatus: .error) + self.markError(task, errorMessage: "execute error") return false } } @@ -139,12 +139,10 @@ class TPClient { .downloadProgress(closure: { (progress) in self.updateStatus(task, newStatus: .downloading, progress: progress) }) - .response { - response in + .response { response in let error = response.error if (error != nil) { - task.errorMessage = "error" //error?.description - self.updateStatus(task, newStatus: .error) + self.markError(task, errorMessage: "download error") } else { self.updateStatus(task, newStatus: .finish) debugPrint("finish: " + task.fileName + " tasks: " + String(self.runningTasks)) @@ -154,16 +152,17 @@ class TPClient { } } + fileprivate func markError(_ task: TPTaskInfo, errorMessage: String?) { + task.errorMessage = errorMessage + updateStatus(task, newStatus: .error) + } + fileprivate func updateStatus(_ task: TPTaskInfo, newStatus: TPTaskStatus, progress: Progress) { - print(task.fileName + " old:" + String(describing: task.status) + " -> new: " + String(describing: newStatus)) task.status = newStatus task.progress = progress if newStatus == .error || newStatus == .finish { self.runningTasks -= 1 } -// OperationQueue.main.addOperation { -// NotificationCenter.default.post(name: Notification.Name(rawValue: "statusChanged"), object: task) -// } callback.taskStatusChanged(task: task) } diff --git a/source/tinypng4mac/utils/TPConfig.swift b/source/tinypng4mac/utils/TPConfig.swift index 4a7f818..dc139f3 100644 --- a/source/tinypng4mac/utils/TPConfig.swift +++ b/source/tinypng4mac/utils/TPConfig.swift @@ -44,5 +44,6 @@ class TPConfig { static func removeKey() { UserDefaults.standard.removeObject(forKey: KEY_API) + UserDefaults.standard.removeObject(forKey: KEY_REPLACE) } }