Refactor GitServerSettingTableViewController

This commit is contained in:
Mingshen Sun 2019-11-30 16:42:53 -08:00
parent 258906fdbb
commit 9b304433fb
No known key found for this signature in database
GPG key ID: 1F86BA2052FED3B4

View file

@ -20,12 +20,16 @@ class GitServerSettingTableViewController: UITableViewController {
@IBOutlet weak var authPasswordCell: UITableViewCell! @IBOutlet weak var authPasswordCell: UITableViewCell!
@IBOutlet weak var gitURLCell: UITableViewCell! @IBOutlet weak var gitURLCell: UITableViewCell!
@IBOutlet weak var gitRepositoryURLTabelViewCell: UITableViewCell! @IBOutlet weak var gitRepositoryURLTabelViewCell: UITableViewCell!
private let passwordStore = PasswordStore.shared private let passwordStore = PasswordStore.shared
private var sshLabel: UILabel? = nil private var sshLabel: UILabel? = nil
private var gitAuthenticationMethod: GitAuthenticationMethod { private var gitAuthenticationMethod: GitAuthenticationMethod {
get { SharedDefaults[.gitAuthenticationMethod] } get { SharedDefaults[.gitAuthenticationMethod] }
set { SharedDefaults[.gitAuthenticationMethod] = newValue } set {
SharedDefaults[.gitAuthenticationMethod] = newValue
updateAuthenticationMethodCheckView(for: newValue)
}
} }
private var gitUrl: URL { private var gitUrl: URL {
get { SharedDefaults[.gitURL] } get { SharedDefaults[.gitURL] }
@ -51,11 +55,11 @@ class GitServerSettingTableViewController: UITableViewController {
} }
} }
private func checkAuthenticationMethod() { private func updateAuthenticationMethodCheckView(for method: GitAuthenticationMethod) {
let passwordCheckView = authPasswordCell.viewWithTag(1001)! let passwordCheckView = authPasswordCell.viewWithTag(1001)!
let sshKeyCheckView = authSSHKeyCell.viewWithTag(1001)! let sshKeyCheckView = authSSHKeyCell.viewWithTag(1001)!
switch self.gitAuthenticationMethod { switch method {
case .password: case .password:
passwordCheckView.isHidden = false passwordCheckView.isHidden = false
sshKeyCheckView.isHidden = true sshKeyCheckView.isHidden = true
@ -68,9 +72,7 @@ class GitServerSettingTableViewController: UITableViewController {
override func viewWillAppear(_ animated: Bool) { override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated) super.viewWillAppear(animated)
// Grey out ssh option if ssh_key is not present // Grey out ssh option if ssh_key is not present
if let sshLabel = sshLabel { sshLabel?.isEnabled = AppKeychain.shared.contains(key: SshKey.PRIVATE.getKeychainKey())
sshLabel.isEnabled = AppKeychain.shared.contains(key: SshKey.PRIVATE.getKeychainKey())
}
} }
override func viewDidLoad() { override func viewDidLoad() {
super.viewDidLoad() super.viewDidLoad()
@ -78,7 +80,7 @@ class GitServerSettingTableViewController: UITableViewController {
usernameTextField.text = self.gitUsername usernameTextField.text = self.gitUsername
branchNameTextField.text = self.gitBranchName branchNameTextField.text = self.gitBranchName
sshLabel = authSSHKeyCell.subviews[0].subviews[0] as? UILabel sshLabel = authSSHKeyCell.subviews[0].subviews[0] as? UILabel
checkAuthenticationMethod() updateAuthenticationMethodCheckView(for: .password)
authSSHKeyCell.accessoryType = .detailButton authSSHKeyCell.accessoryType = .detailButton
} }
@ -92,9 +94,7 @@ class GitServerSettingTableViewController: UITableViewController {
} }
private func showGitURLFormatHelp() { private func showGitURLFormatHelp() {
let alert = UIAlertController(title: "Git URL Format", message: "https://example.com[:port]/project.git\nssh://[user@]server[:port]/project.git\n[user@]server:project.git (no scheme)", preferredStyle: .alert) Utils.alert(title: "Git URL Format", message: "https://example.com[:port]/project.git\nssh://[user@]server[:port]/project.git\n[user@]server:project.git (no scheme)", controller: self)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { _ in }))
self.present(alert, animated: true, completion: nil)
} }
override func viewDidAppear(_ animated: Bool) { override func viewDidAppear(_ animated: Bool) {
@ -110,53 +110,50 @@ class GitServerSettingTableViewController: UITableViewController {
private func cloneAndSegueIfSuccess() { private func cloneAndSegueIfSuccess() {
// Remember git credential password/passphrase temporarily, ask whether users want this after a successful clone. // Remember git credential password/passphrase temporarily, ask whether users want this after a successful clone.
SharedDefaults[.isRememberGitCredentialPassphraseOn] = true SharedDefaults[.isRememberGitCredentialPassphraseOn] = true
let group = DispatchGroup() DispatchQueue.global(qos: .userInitiated).async() {
let dispatchQueue = DispatchQueue.global(qos: .userInitiated)
dispatchQueue.async(group: group) {
do { do {
let transferProgressBlock: (UnsafePointer<git_transfer_progress>, UnsafeMutablePointer<ObjCBool>) -> Void = { (git_transfer_progress, _) in
let progress = Float(git_transfer_progress.pointee.received_objects) / Float(git_transfer_progress.pointee.total_objects)
SVProgressHUD.showProgress(progress, status: "Cloning Remote Repository")
}
let checkoutProgressBlock: (String?, UInt, UInt) -> Void = { (_, completedSteps, totalSteps) in
let progress = Float(completedSteps) / Float(totalSteps)
SVProgressHUD.showProgress(progress, status: "CheckingOutBranch".localize(self.gitBranchName))
}
try self.passwordStore.cloneRepository(remoteRepoURL: self.gitUrl, try self.passwordStore.cloneRepository(remoteRepoURL: self.gitUrl,
credential: self.gitCredential, credential: self.gitCredential,
branchName: self.gitBranchName, branchName: self.gitBranchName,
requestGitPassword: self.requestGitPassword, requestGitPassword: self.requestGitPassword,
transferProgressBlock: { (git_transfer_progress, stop) in transferProgressBlock: transferProgressBlock,
DispatchQueue.main.async { checkoutProgressBlock: checkoutProgressBlock)
SVProgressHUD.showProgress(Float(git_transfer_progress.pointee.received_objects)/Float(git_transfer_progress.pointee.total_objects), status: "Clone Remote Repository")
} SVProgressHUD.dismiss() {
}, let savePassphraseAlert: UIAlertController = {
checkoutProgressBlock: { (path, completedSteps, totalSteps) in let alert = UIAlertController(title: "Done".localize(), message: "WantToSaveGitCredential?".localize(), preferredStyle: .alert)
DispatchQueue.main.async { SVProgressHUD.showProgress(Float(completedSteps)/Float(totalSteps), status: "CheckingOutBranch".localize(self.gitBranchName)) alert.addAction(UIAlertAction(title: "No".localize(), style: .default) { _ in
} SharedDefaults[.isRememberGitCredentialPassphraseOn] = false
}) self.passwordStore.gitPassword = nil
} catch { self.passwordStore.gitSSHPrivateKeyPassphrase = nil
DispatchQueue.main.async { self.performSegue(withIdentifier: "saveGitServerSettingSegue", sender: self)
let error = error as NSError })
var message = error.localizedDescription alert.addAction(UIAlertAction(title: "Yes".localize(), style: .destructive) {_ in
if let underlyingError = error.userInfo[NSUnderlyingErrorKey] as? NSError { SharedDefaults[.isRememberGitCredentialPassphraseOn] = true
message = "\(message)\n\("UnderlyingError".localize(underlyingError.localizedDescription))" self.performSegue(withIdentifier: "saveGitServerSettingSegue", sender: self)
} })
Utils.alert(title: "Error".localize(), message: message, controller: self, completion: nil) return alert
} }()
}
}
group.notify(queue: dispatchQueue) {
NSLog("clone done")
DispatchQueue.main.async {
SVProgressHUD.dismiss {
let savePassphraseAlert = UIAlertController(title: "Done".localize(), message: "WantToSaveGitCredential?".localize(), preferredStyle: UIAlertController.Style.alert)
// no
savePassphraseAlert.addAction(UIAlertAction(title: "No".localize(), style: UIAlertAction.Style.default) { _ in
SharedDefaults[.isRememberGitCredentialPassphraseOn] = false
self.passwordStore.gitPassword = nil
self.passwordStore.gitSSHPrivateKeyPassphrase = nil
self.performSegue(withIdentifier: "saveGitServerSettingSegue", sender: self)
})
// yes
savePassphraseAlert.addAction(UIAlertAction(title: "Yes".localize(), style: UIAlertAction.Style.destructive) {_ in
SharedDefaults[.isRememberGitCredentialPassphraseOn] = true
self.performSegue(withIdentifier: "saveGitServerSettingSegue", sender: self)
})
self.present(savePassphraseAlert, animated: true, completion: nil) self.present(savePassphraseAlert, animated: true, completion: nil)
} }
} catch {
SVProgressHUD.dismiss()
let error = error as NSError
var message = error.localizedDescription
if let underlyingError = error.userInfo[NSUnderlyingErrorKey] as? NSError {
message = "\(message)\n\("UnderlyingError".localize(underlyingError.localizedDescription))"
}
Utils.alert(title: "Error".localize(), message: message, controller: self, completion: nil)
} }
} }
} }
@ -166,7 +163,6 @@ class GitServerSettingTableViewController: UITableViewController {
if cell == authPasswordCell { if cell == authPasswordCell {
self.gitAuthenticationMethod = .password self.gitAuthenticationMethod = .password
} else if cell == authSSHKeyCell { } else if cell == authSSHKeyCell {
if !AppKeychain.shared.contains(key: SshKey.PRIVATE.getKeychainKey()) { if !AppKeychain.shared.contains(key: SshKey.PRIVATE.getKeychainKey()) {
Utils.alert(title: "CannotSelectSshKey".localize(), message: "PleaseSetupSshKeyFirst.".localize(), controller: self, completion: nil) Utils.alert(title: "CannotSelectSshKey".localize(), message: "PleaseSetupSshKeyFirst.".localize(), controller: self, completion: nil)
gitAuthenticationMethod = .password gitAuthenticationMethod = .password
@ -174,13 +170,11 @@ class GitServerSettingTableViewController: UITableViewController {
gitAuthenticationMethod = .key gitAuthenticationMethod = .key
} }
} }
checkAuthenticationMethod()
tableView.deselectRow(at: indexPath, animated: true) tableView.deselectRow(at: indexPath, animated: true)
} }
@IBAction func save(_ sender: Any) { @IBAction func save(_ sender: Any) {
guard let gitURLTextFieldText = gitURLTextField.text, let gitURL = URL(string: gitURLTextFieldText.trimmed) else {
guard let gitURLTextFieldText = gitURLTextField.text, let gitURL = URL(string: gitURLTextFieldText) else {
Utils.alert(title: "CannotSave".localize(), message: "SetGitRepositoryUrl".localize(), controller: self, completion: nil) Utils.alert(title: "CannotSave".localize(), message: "SetGitRepositoryUrl".localize(), controller: self, completion: nil)
return return
} }
@ -190,20 +184,18 @@ class GitServerSettingTableViewController: UITableViewController {
return return
} }
func checkUsername() {
if gitURL.user == nil && usernameTextField.text == nil {
Utils.alert(title: "CannotSave".localize(), message: "CannotFindUsername.".localize(), controller: self, completion: nil)
return
}
if let urlUsername = gitURL.user, let textFieldUsername = usernameTextField.text, urlUsername != textFieldUsername.trimmed {
Utils.alert(title: "CannotSave".localize(), message: "CheckEnteredUsername.".localize(), controller: self, completion: nil)
return
}
}
if let scheme = gitURL.scheme { if let scheme = gitURL.scheme {
switch scheme { switch scheme {
case "ssh", "http", "https", "file": checkUsername() case "ssh", "http", "https":
if gitURL.user == nil && usernameTextField.text == nil {
Utils.alert(title: "CannotSave".localize(), message: "CannotFindUsername.".localize(), controller: self, completion: nil)
return
}
if let urlUsername = gitURL.user, let textFieldUsername = usernameTextField.text, urlUsername != textFieldUsername.trimmed {
Utils.alert(title: "CannotSave".localize(), message: "CheckEnteredUsername.".localize(), controller: self, completion: nil)
return
}
case "file": break
default: default:
Utils.alert(title: "CannotSave".localize(), message: "Protocol is not supported", controller: self, completion: nil) Utils.alert(title: "CannotSave".localize(), message: "Protocol is not supported", controller: self, completion: nil)
return return
@ -211,19 +203,20 @@ class GitServerSettingTableViewController: UITableViewController {
} }
self.gitUrl = gitURL self.gitUrl = gitURL
self.gitBranchName = branchName self.gitBranchName = branchName.trimmed
self.gitUsername = (gitURL.user ?? usernameTextField.text ?? "git").trimmed self.gitUsername = (gitURL.user ?? usernameTextField.text ?? "git").trimmed
if passwordStore.repositoryExisted() { if passwordStore.repositoryExisted() {
let alert = UIAlertController(title: "Overwrite?".localize(), message: "OperationWillOverwriteData.".localize(), preferredStyle: UIAlertController.Style.alert) let overwriteAlert: UIAlertController = {
alert.addAction(UIAlertAction(title: "Overwrite".localize(), style: UIAlertAction.Style.destructive, handler: { _ in let alert = UIAlertController(title: "Overwrite?".localize(), message: "OperationWillOverwriteData.".localize(), preferredStyle: .alert)
// perform segue only after a successful clone alert.addAction(UIAlertAction(title: "Overwrite".localize(), style: .destructive) { _ in
self.cloneAndSegueIfSuccess() self.cloneAndSegueIfSuccess()
})) })
alert.addAction(UIAlertAction(title: "Cancel".localize(), style: UIAlertAction.Style.cancel, handler: nil)) alert.addAction(UIAlertAction(title: "Cancel".localize(), style: .cancel, handler: nil))
self.present(alert, animated: true, completion: nil) return alert
}()
self.present(overwriteAlert, animated: true, completion: nil)
} else { } else {
// perform segue only after a successful clone
cloneAndSegueIfSuccess() cloneAndSegueIfSuccess()
} }
} }
@ -280,10 +273,8 @@ class GitServerSettingTableViewController: UITableViewController {
let deleteAction = UIAlertAction(title: "RemoveSShKeys".localize(), style: .destructive) { _ in let deleteAction = UIAlertAction(title: "RemoveSShKeys".localize(), style: .destructive) { _ in
self.passwordStore.removeGitSSHKeys() self.passwordStore.removeGitSSHKeys()
SharedDefaults[.gitSSHKeySource] = nil SharedDefaults[.gitSSHKeySource] = nil
if let sshLabel = self.sshLabel { self.sshLabel?.isEnabled = false
sshLabel.isEnabled = false self.updateAuthenticationMethodCheckView(for: .password)
self.checkAuthenticationMethod()
}
} }
optionMenu.addAction(deleteAction) optionMenu.addAction(deleteAction)
} }
@ -296,22 +287,23 @@ class GitServerSettingTableViewController: UITableViewController {
private func requestGitPassword(credential: GitCredential.Credential, lastPassword: String?) -> String? { private func requestGitPassword(credential: GitCredential.Credential, lastPassword: String?) -> String? {
let sem = DispatchSemaphore(value: 0) let sem = DispatchSemaphore(value: 0)
var password: String? var password: String?
var message = "" let message: String = {
switch credential { switch credential {
case .http: case .http:
message = "FillInGitAccountPassword.".localize() return "FillInGitAccountPassword.".localize()
case .ssh: case .ssh:
message = "FillInSshKeyPassphrase.".localize() return "FillInSshKeyPassphrase.".localize()
} }
}()
DispatchQueue.main.async { DispatchQueue.main.async {
SVProgressHUD.dismiss() SVProgressHUD.dismiss()
let alert = UIAlertController(title: "Password".localize(), message: message, preferredStyle: UIAlertController.Style.alert) let alert = UIAlertController(title: "Password".localize(), message: message, preferredStyle: .alert)
alert.addTextField(configurationHandler: {(textField: UITextField!) in alert.addTextField(configurationHandler: {(textField: UITextField!) in
textField.text = lastPassword ?? "" textField.text = lastPassword ?? ""
textField.isSecureTextEntry = true textField.isSecureTextEntry = true
}) })
alert.addAction(UIAlertAction(title: "Ok".localize(), style: UIAlertAction.Style.default, handler: {_ in alert.addAction(UIAlertAction(title: "Ok".localize(), style: .default, handler: {_ in
password = alert.textFields!.first!.text password = alert.textFields!.first!.text
sem.signal() sem.signal()
})) }))