Polish the logic of clone
- pop the current view only after a successful clone so that editing the previous incorrect git setting is possible
This commit is contained in:
parent
d86ee90eb7
commit
0542733f86
2 changed files with 59 additions and 67 deletions
|
|
@ -73,15 +73,56 @@ class GitServerSettingTableViewController: UITableViewController {
|
||||||
view.endEditing(true)
|
view.endEditing(true)
|
||||||
}
|
}
|
||||||
|
|
||||||
override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
|
private func cloneAndSegueIfSuccess() {
|
||||||
if identifier == "saveGitServerSettingSegue" {
|
// try to clone
|
||||||
guard let _ = URL(string: gitURLTextField.text!) else {
|
let gitRepostiroyURL = gitURLTextField.text!
|
||||||
Utils.alert(title: "Cannot Save", message: "Git Server is not set.", controller: self, completion: nil)
|
let username = usernameTextField.text!
|
||||||
return false
|
let auth = authenticationMethod
|
||||||
}
|
|
||||||
|
SVProgressHUD.setDefaultMaskType(.black)
|
||||||
|
SVProgressHUD.setDefaultStyle(.light)
|
||||||
|
SVProgressHUD.show(withStatus: "Prepare Repository")
|
||||||
|
var gitCredential: GitCredential
|
||||||
|
if auth == "Password" {
|
||||||
|
gitCredential = GitCredential(credential: GitCredential.Credential.http(userName: username, controller: self))
|
||||||
|
} else {
|
||||||
|
gitCredential = GitCredential(
|
||||||
|
credential: GitCredential.Credential.ssh(
|
||||||
|
userName: username,
|
||||||
|
privateKeyFile: Globals.gitSSHPrivateKeyURL,
|
||||||
|
controller: self
|
||||||
|
)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
return true
|
let dispatchQueue = DispatchQueue.global(qos: .userInitiated)
|
||||||
}
|
dispatchQueue.async {
|
||||||
|
do {
|
||||||
|
try self.passwordStore.cloneRepository(remoteRepoURL: URL(string: gitRepostiroyURL)!,
|
||||||
|
credential: gitCredential,
|
||||||
|
transferProgressBlock: { (git_transfer_progress, stop) in
|
||||||
|
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 {
|
||||||
|
SVProgressHUD.showProgress(Float(completedSteps)/Float(totalSteps), status: "Checkout Master Branch")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
DispatchQueue.main.async {
|
||||||
|
Defaults[.gitURL] = URL(string: gitRepostiroyURL)
|
||||||
|
Defaults[.gitUsername] = username
|
||||||
|
Defaults[.gitAuthenticationMethod] = auth
|
||||||
|
SVProgressHUD.showSuccess(withStatus: "Done")
|
||||||
|
SVProgressHUD.dismiss(withDelay: 1)
|
||||||
|
self.performSegue(withIdentifier: "saveGitServerSettingSegue", sender: self)
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
DispatchQueue.main.async {
|
||||||
|
Utils.alert(title: "Error", message: error.localizedDescription, controller: self, completion: nil)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} }
|
||||||
|
|
||||||
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
||||||
let cell = tableView.cellForRow(at: indexPath)
|
let cell = tableView.cellForRow(at: indexPath)
|
||||||
|
|
@ -100,22 +141,23 @@ class GitServerSettingTableViewController: UITableViewController {
|
||||||
tableView.deselectRow(at: indexPath, animated: true)
|
tableView.deselectRow(at: indexPath, animated: true)
|
||||||
}
|
}
|
||||||
|
|
||||||
private func doClone() {
|
|
||||||
if self.shouldPerformSegue(withIdentifier: "saveGitServerSettingSegue", sender: self) {
|
|
||||||
self.performSegue(withIdentifier: "saveGitServerSettingSegue", sender: self)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@IBAction func save(_ sender: Any) {
|
@IBAction func save(_ sender: Any) {
|
||||||
|
guard let _ = URL(string: gitURLTextField.text!) else {
|
||||||
|
Utils.alert(title: "Cannot Save", message: "Git Server is not set.", controller: self, completion: nil)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if passwordStore.repositoryExisted() {
|
if passwordStore.repositoryExisted() {
|
||||||
let alert = UIAlertController(title: "Erase Current Password Store Data?", message: "A cloned password store exists. This operation will erase all local data. Data on your remote server will not be affected.", preferredStyle: UIAlertControllerStyle.alert)
|
let alert = UIAlertController(title: "Erase Current Password Store Data?", message: "A cloned password store exists. This operation will erase all local data. Data on your remote server will not be affected.", preferredStyle: UIAlertControllerStyle.alert)
|
||||||
alert.addAction(UIAlertAction(title: "Erase", style: UIAlertActionStyle.destructive, handler: { _ in
|
alert.addAction(UIAlertAction(title: "Erase", style: UIAlertActionStyle.destructive, handler: { _ in
|
||||||
self.doClone()
|
// perform segue only after a successful clone
|
||||||
|
self.cloneAndSegueIfSuccess()
|
||||||
}))
|
}))
|
||||||
alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel, handler: nil))
|
alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel, handler: nil))
|
||||||
self.present(alert, animated: true, completion: nil)
|
self.present(alert, animated: true, completion: nil)
|
||||||
} else {
|
} else {
|
||||||
doClone()
|
// perform segue only after a successful clone
|
||||||
|
cloneAndSegueIfSuccess()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -96,57 +96,7 @@ class SettingsTableViewController: UITableViewController {
|
||||||
}
|
}
|
||||||
|
|
||||||
@IBAction func saveGitServerSetting(segue: UIStoryboardSegue) {
|
@IBAction func saveGitServerSetting(segue: UIStoryboardSegue) {
|
||||||
if let controller = segue.source as? GitServerSettingTableViewController {
|
self.passwordRepositoryTableViewCell.detailTextLabel?.text = Defaults[.gitURL]?.host
|
||||||
let gitRepostiroyURL = controller.gitURLTextField.text!
|
|
||||||
let username = controller.usernameTextField.text!
|
|
||||||
let auth = controller.authenticationMethod
|
|
||||||
|
|
||||||
SVProgressHUD.setDefaultMaskType(.black)
|
|
||||||
SVProgressHUD.setDefaultStyle(.light)
|
|
||||||
SVProgressHUD.show(withStatus: "Prepare Repository")
|
|
||||||
var gitCredential: GitCredential
|
|
||||||
if auth == "Password" {
|
|
||||||
gitCredential = GitCredential(credential: GitCredential.Credential.http(userName: username, controller: self))
|
|
||||||
} else {
|
|
||||||
gitCredential = GitCredential(
|
|
||||||
credential: GitCredential.Credential.ssh(
|
|
||||||
userName: username,
|
|
||||||
privateKeyFile: Globals.gitSSHPrivateKeyURL,
|
|
||||||
controller: self
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
let dispatchQueue = DispatchQueue.global(qos: .userInitiated)
|
|
||||||
dispatchQueue.async {
|
|
||||||
do {
|
|
||||||
try self.passwordStore.cloneRepository(remoteRepoURL: URL(string: gitRepostiroyURL)!,
|
|
||||||
credential: gitCredential,
|
|
||||||
transferProgressBlock:{ (git_transfer_progress, stop) in
|
|
||||||
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 {
|
|
||||||
SVProgressHUD.showProgress(Float(completedSteps)/Float(totalSteps), status: "Checkout Master Branch")
|
|
||||||
}
|
|
||||||
})
|
|
||||||
DispatchQueue.main.async {
|
|
||||||
Defaults[.gitURL] = URL(string: gitRepostiroyURL)
|
|
||||||
Defaults[.gitUsername] = username
|
|
||||||
Defaults[.gitAuthenticationMethod] = auth
|
|
||||||
self.passwordRepositoryTableViewCell.detailTextLabel?.text = Defaults[.gitURL]?.host
|
|
||||||
SVProgressHUD.showSuccess(withStatus: "Done")
|
|
||||||
SVProgressHUD.dismiss(withDelay: 1)
|
|
||||||
}
|
|
||||||
} catch {
|
|
||||||
DispatchQueue.main.async {
|
|
||||||
Utils.alert(title: "Error", message: error.localizedDescription, controller: self, completion: nil)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue