Skip to content

Commit

Permalink
catch error
Browse files Browse the repository at this point in the history
  • Loading branch information
kyleduo committed May 22, 2017
1 parent 99ef7d8 commit a606ae2
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 39 deletions.
1 change: 1 addition & 0 deletions source/tinypng4mac/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,7 @@
<outlet property="dropContainer" destination="FK5-1A-UoS" id="rq3-HY-lkJ"/>
<outlet property="icon" destination="Fq4-Bl-UgO" id="IIB-zH-pAt"/>
<outlet property="outputPathField" destination="fT1-QL-4Nq" id="lr2-ir-Isq"/>
<outlet property="outputPathSelectButton" destination="0K4-Mk-HkB" id="937-Tl-7dv"/>
<outlet property="replaceSwitch" destination="zRv-TO-bMw" id="YAz-sO-KJH"/>
<outlet property="taskTableView" destination="fWd-5L-B1V" id="4AE-2m-kM9"/>
<outlet property="totalReduce" destination="D8d-TS-a2j" id="uaD-e2-A6h"/>
Expand Down
4 changes: 2 additions & 2 deletions source/tinypng4mac/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>0.9.3</string>
<string>1.0.0</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>5</string>
<string>6</string>
<key>LSMinimumSystemVersion</key>
<string>$(MACOSX_DEPLOYMENT_TARGET)</string>
<key>NSHumanReadableCopyright</key>
Expand Down
16 changes: 8 additions & 8 deletions source/tinypng4mac/MainViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}

Expand Down
57 changes: 28 additions & 29 deletions source/tinypng4mac/tpclient/TPClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand All @@ -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))
Expand All @@ -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)
}

Expand Down
1 change: 1 addition & 0 deletions source/tinypng4mac/utils/TPConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,6 @@ class TPConfig {

static func removeKey() {
UserDefaults.standard.removeObject(forKey: KEY_API)
UserDefaults.standard.removeObject(forKey: KEY_REPLACE)
}
}

0 comments on commit a606ae2

Please sign in to comment.