add error handling

This commit is contained in:
Bob Sun 2017-02-04 14:24:59 +08:00
parent 88dcd8370a
commit 529e170704
No known key found for this signature in database
GPG key ID: 1F86BA2052FED3B4
3 changed files with 61 additions and 62 deletions

View file

@ -90,7 +90,7 @@ class PasswordStore {
func cloneRepository(remoteRepoURL: URL, func cloneRepository(remoteRepoURL: URL,
credential: GitCredential, credential: GitCredential,
transferProgressBlock: @escaping (UnsafePointer<git_transfer_progress>, UnsafeMutablePointer<ObjCBool>) -> Void, transferProgressBlock: @escaping (UnsafePointer<git_transfer_progress>, UnsafeMutablePointer<ObjCBool>) -> Void,
checkoutProgressBlock: @escaping (String?, UInt, UInt) -> Void) -> Bool { checkoutProgressBlock: @escaping (String?, UInt, UInt) -> Void) throws {
print("start cloning remote repo: \(remoteRepoURL)") print("start cloning remote repo: \(remoteRepoURL)")
let fm = FileManager.default let fm = FileManager.default
if (storeRepository != nil) { if (storeRepository != nil) {
@ -101,39 +101,27 @@ class PasswordStore {
print(error.debugDescription) print(error.debugDescription)
} }
} }
do { print("start cloning...")
print("start cloning...") let credentialProvider = try credential.credentialProvider()
let credentialProvider = try credential.credentialProvider() let options: [String: Any] = [
let options: [String: Any] = [ GTRepositoryCloneOptionsCredentialProvider: credentialProvider,
GTRepositoryCloneOptionsCredentialProvider: credentialProvider, ]
] storeRepository = try GTRepository.clone(from: remoteRepoURL, toWorkingDirectory: storeURL, options: options, transferProgressBlock:transferProgressBlock, checkoutProgressBlock: checkoutProgressBlock)
storeRepository = try GTRepository.clone(from: remoteRepoURL, toWorkingDirectory: storeURL, options: options, transferProgressBlock:transferProgressBlock, checkoutProgressBlock: checkoutProgressBlock) print("clone finish")
print("clone finish") updatePasswordEntityCoreData()
updatePasswordEntityCoreData() gitCredential = credential
gitCredential = credential
return true
} catch {
print(error)
return false
}
} }
func pullRepository(transferProgressBlock: @escaping (UnsafePointer<git_transfer_progress>, UnsafeMutablePointer<ObjCBool>) -> Void) -> Bool { func pullRepository(transferProgressBlock: @escaping (UnsafePointer<git_transfer_progress>, UnsafeMutablePointer<ObjCBool>) -> Void) throws {
print("pullRepoisitory") print("pullRepoisitory")
do { print("start pulling...")
print("start pulling...") let credentialProvider = try gitCredential!.credentialProvider()
let credentialProvider = try gitCredential!.credentialProvider() let options: [String: Any] = [
let options: [String: Any] = [ GTRepositoryRemoteOptionsCredentialProvider: credentialProvider
GTRepositoryRemoteOptionsCredentialProvider: credentialProvider ]
] let remote = try GTRemote(name: "origin", in: storeRepository!)
let remote = try GTRemote(name: "origin", in: storeRepository!) try storeRepository?.pull((storeRepository?.currentBranch())!, from: remote, withOptions: options, progress: transferProgressBlock)
try storeRepository?.pull((storeRepository?.currentBranch())!, from: remote, withOptions: options, progress: transferProgressBlock) updatePasswordEntityCoreData()
updatePasswordEntityCoreData()
return true
} catch {
print(error)
return false
}
} }
func updatePasswordEntityCoreData() { func updatePasswordEntityCoreData() {

View file

@ -34,11 +34,12 @@ class PasswordsViewController: UIViewController, UITableViewDataSource, UITableV
SVProgressHUD.setDefaultMaskType(.black) SVProgressHUD.setDefaultMaskType(.black)
SVProgressHUD.show(withStatus: "Sync Passwords") SVProgressHUD.show(withStatus: "Sync Passwords")
DispatchQueue.global(qos: .userInitiated).async { [unowned self] in DispatchQueue.global(qos: .userInitiated).async { [unowned self] in
if PasswordStore.shared.pullRepository(transferProgressBlock: {(git_transfer_progress, stop) in do {
DispatchQueue.main.async { try PasswordStore.shared.pullRepository(transferProgressBlock: {(git_transfer_progress, stop) in
SVProgressHUD.showProgress(Float(git_transfer_progress.pointee.received_objects)/Float(git_transfer_progress.pointee.total_objects), status: "Pull Remote Repository") DispatchQueue.main.async {
} SVProgressHUD.showProgress(Float(git_transfer_progress.pointee.received_objects)/Float(git_transfer_progress.pointee.total_objects), status: "Pull Remote Repository")
}) { }
})
DispatchQueue.main.async { DispatchQueue.main.async {
SVProgressHUD.showSuccess(withStatus: "Done") SVProgressHUD.showSuccess(withStatus: "Done")
SVProgressHUD.dismiss(withDelay: 1) SVProgressHUD.dismiss(withDelay: 1)
@ -46,6 +47,11 @@ class PasswordsViewController: UIViewController, UITableViewDataSource, UITableV
print("pull success") print("pull success")
self.passwordEntities = PasswordStore.shared.fetchPasswordEntityCoreData() self.passwordEntities = PasswordStore.shared.fetchPasswordEntityCoreData()
self.reloadTableView(data: self.passwordEntities!) self.reloadTableView(data: self.passwordEntities!)
} catch {
DispatchQueue.main.async {
SVProgressHUD.showError(withStatus: error.localizedDescription)
SVProgressHUD.dismiss(withDelay: 3)
}
} }
} }
} }

View file

@ -38,35 +38,40 @@ class SettingsTableViewController: UITableViewController {
} }
DispatchQueue.global(qos: .userInitiated).async { DispatchQueue.global(qos: .userInitiated).async {
let ret = PasswordStore.shared.cloneRepository(remoteRepoURL: URL(string: gitRepostiroyURL)!, do {
credential: gitCredential, try PasswordStore.shared.cloneRepository(remoteRepoURL: URL(string: gitRepostiroyURL)!,
transferProgressBlock:{ (git_transfer_progress, stop) in credential: gitCredential,
DispatchQueue.main.async { transferProgressBlock:{ (git_transfer_progress, stop) in
SVProgressHUD.showProgress(Float(git_transfer_progress.pointee.received_objects)/Float(git_transfer_progress.pointee.total_objects), status: "Clone Remote Repository") DispatchQueue.main.async {
} SVProgressHUD.showProgress(Float(git_transfer_progress.pointee.received_objects)/Float(git_transfer_progress.pointee.total_objects), status: "Clone Remote Repository")
}, }
checkoutProgressBlock: { (path, completedSteps, totalSteps) in },
DispatchQueue.main.async { checkoutProgressBlock: { (path, completedSteps, totalSteps) in
SVProgressHUD.showProgress(Float(completedSteps)/Float(totalSteps), status: "Checkout Master Branch") DispatchQueue.main.async {
} SVProgressHUD.showProgress(Float(completedSteps)/Float(totalSteps), status: "Checkout Master Branch")
}) }
})
DispatchQueue.main.async {
if ret { DispatchQueue.main.async {
SVProgressHUD.showSuccess(withStatus: "Done") SVProgressHUD.showSuccess(withStatus: "Done")
SVProgressHUD.dismiss(withDelay: 1) SVProgressHUD.dismiss(withDelay: 1)
Defaults[.gitRepositoryURL] = URL(string: gitRepostiroyURL) Defaults[.gitRepositoryURL] = URL(string: gitRepostiroyURL)
Defaults[.gitRepositoryUsername] = username Defaults[.gitRepositoryUsername] = username
Defaults[.gitRepositoryPassword] = password Defaults[.gitRepositoryPassword] = password
Defaults[.gitRepositoryAuthenticationMethod] = auth Defaults[.gitRepositoryAuthenticationMethod] = auth
NotificationCenter.default.post(Notification(name: Notification.Name("passwordUpdated")))
NotificationCenter.default.post(Notification(name: Notification.Name("passwordUpdated"))) }
} else { } catch {
SVProgressHUD.showError(withStatus: "Error") DispatchQueue.main.async {
SVProgressHUD.dismiss(withDelay: 1) print(error)
SVProgressHUD.showError(withStatus: error.localizedDescription)
SVProgressHUD.dismiss(withDelay: 3)
} }
} }
} }
} }
} else if let controller = segue.source as? PGPKeySettingTableViewController { } else if let controller = segue.source as? PGPKeySettingTableViewController {