simplify password repository setting

This commit is contained in:
Bob Sun 2017-02-21 13:07:14 +08:00
parent 2829e9145d
commit 87dd4f96a3
No known key found for this signature in database
GPG key ID: 1F86BA2052FED3B4
6 changed files with 153 additions and 169 deletions

View file

@ -19,7 +19,22 @@ class GitServerSettingTableViewController: UITableViewController {
var authenticationMethod = Defaults[.gitRepositoryAuthenticationMethod]
private func checkAuthenticationMethod(method: String) {
let passwordCheckView = authPasswordCell.viewWithTag(1001)!
let sshKeyCheckView = authSSHKeyCell.viewWithTag(1001)!
switch method {
case "Password":
passwordCheckView.isHidden = false
sshKeyCheckView.isHidden = true
case "SSH Key":
passwordCheckView.isHidden = true
sshKeyCheckView.isHidden = false
default:
passwordCheckView.isHidden = false
sshKeyCheckView.isHidden = true
}
}
override func viewDidLoad() {
super.viewDidLoad()
if let url = Defaults[.gitRepositoryURL] {
@ -27,18 +42,18 @@ class GitServerSettingTableViewController: UITableViewController {
}
usernameTextField.text = Defaults[.gitRepositoryUsername]
password = PasswordStore.shared.gitRepositoryPassword
authenticationMethod = Defaults[.gitRepositoryAuthenticationMethod]
if authenticationMethod == nil {
authPasswordCell.accessoryType = .checkmark
authenticationMethod = "Password"
} else {
switch authenticationMethod! {
case "Password":
authPasswordCell.accessoryType = .checkmark
case "SSH Key":
authSSHKeyCell.accessoryType = .checkmark
default:
authPasswordCell.accessoryType = .checkmark
}
}
checkAuthenticationMethod(method: authenticationMethod!)
authSSHKeyCell.accessoryType = .detailButton
}
override func tableView(_ tableView: UITableView, accessoryButtonTappedForRowWith indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath)
if cell == authSSHKeyCell {
performSegue(withIdentifier: "showSSHKeySettingSegue", sender: self)
}
}
@ -72,21 +87,16 @@ class GitServerSettingTableViewController: UITableViewController {
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath)
if cell == authPasswordCell {
authPasswordCell.accessoryType = .checkmark
authSSHKeyCell.accessoryType = .none
authenticationMethod = "Password"
} else if cell == authSSHKeyCell {
authPasswordCell.accessoryType = .none
authSSHKeyCell.accessoryType = .checkmark
if Defaults[.gitRepositorySSHPublicKeyURL] == nil && Defaults[.gitRepositorySSHPrivateKeyURL] == nil {
Utils.alert(title: "Cannot Select SSH Key", message: "Please setup SSH key first.", controller: self, completion: nil)
authenticationMethod = "Password"
authSSHKeyCell.accessoryType = .none
authPasswordCell.accessoryType = .checkmark
} else {
authenticationMethod = "SSH Key"
}
}
checkAuthenticationMethod(method: authenticationMethod!)
tableView.deselectRow(at: indexPath, animated: true)
}

View file

@ -1,102 +0,0 @@
//
// PasswordRepositorySettingsTableViewController.swift
// pass
//
// Created by Mingshen Sun on 9/2/2017.
// Copyright © 2017 Bob Sun. All rights reserved.
//
import UIKit
import SwiftyUserDefaults
import SVProgressHUD
class PasswordRepositorySettingsTableViewController: BasicStaticTableViewController {
override func viewDidLoad() {
let url = Defaults[.gitRepositoryURL]?.host
tableData = [
[[.style: CellDataStyle.value1, .title: "Git Server", .action: "segue", .link: "showGitServerSettingSegue", .detailText: url ?? ""],
[.style: CellDataStyle.value1, .title: "SSH Key", .action: "segue", .link: "showSSHKeySettingSegue", .detailText: "Not Set"],],
]
navigationItemTitle = "Repository"
super.viewDidLoad()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if let url = Defaults[.gitRepositoryURL] {
if let cell = tableView.cellForRow(at: IndexPath(row: 0, section: 0)) {
cell.detailTextLabel!.text = url.host
}
}
if Defaults[.gitRepositorySSHPublicKeyURL] != nil {
if let cell = tableView.cellForRow(at: IndexPath(row: 1, section: 0)) {
cell.detailTextLabel!.text = "Set"
}
}
}
@IBAction func cancelGitServerSetting(segue: UIStoryboardSegue) {
}
@IBAction func saveGitServerSetting(segue: UIStoryboardSegue) {
if let controller = segue.source as? GitServerSettingTableViewController {
let gitRepostiroyURL = controller.gitRepositoryURLTextField.text!
let username = controller.usernameTextField.text!
let password = controller.password
let auth = controller.authenticationMethod
if Defaults[.gitRepositoryURL] == nil ||
Defaults[.gitRepositoryURL]!.absoluteString != gitRepostiroyURL ||
auth != Defaults[.gitRepositoryAuthenticationMethod] ||
username != Defaults[.gitRepositoryUsername] ||
password != PasswordStore.shared.gitRepositoryPassword {
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, password: password!))
} else {
gitCredential = GitCredential(credential: GitCredential.Credential.ssh(userName: username, password: Defaults[.gitRepositorySSHPrivateKeyPassphrase]!, publicKeyFile: Globals.sshPublicKeyURL, privateKeyFile: Globals.sshPrivateKeyURL))
}
let dispatchQueue = DispatchQueue.global(qos: .userInitiated)
dispatchQueue.async {
do {
try PasswordStore.shared.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 {
PasswordStore.shared.updatePasswordEntityCoreData()
Defaults[.lastUpdatedTime] = Date()
NotificationCenter.default.post(Notification(name: Notification.Name("passwordUpdated")))
Defaults[.gitRepositoryURL] = URL(string: gitRepostiroyURL)
Defaults[.gitRepositoryUsername] = username
Defaults[.gitRepositoryAuthenticationMethod] = auth
Defaults[.gitRepositoryPasswordAttempts] = 0
SVProgressHUD.showSuccess(withStatus: "Done")
SVProgressHUD.dismiss(withDelay: 1)
}
} catch {
DispatchQueue.main.async {
print(error)
SVProgressHUD.showError(withStatus: error.localizedDescription)
SVProgressHUD.dismiss(withDelay: 1)
}
}
}
}
}
}
}

View file

@ -19,6 +19,7 @@ class SettingsTableViewController: UITableViewController {
@IBOutlet weak var pgpKeyTableViewCell: UITableViewCell!
@IBOutlet weak var touchIDTableViewCell: UITableViewCell!
@IBOutlet weak var passcodeTableViewCell: UITableViewCell!
@IBOutlet weak var passwordRepositoryTableViewCell: UITableViewCell!
@IBAction func cancelPGPKey(segue: UIStoryboardSegue) {
}
@ -89,6 +90,71 @@ class SettingsTableViewController: UITableViewController {
}
}
@IBAction func cancelGitServerSetting(segue: UIStoryboardSegue) {
}
@IBAction func saveGitServerSetting(segue: UIStoryboardSegue) {
if let controller = segue.source as? GitServerSettingTableViewController {
let gitRepostiroyURL = controller.gitRepositoryURLTextField.text!
let username = controller.usernameTextField.text!
let password = controller.password
let auth = controller.authenticationMethod
if Defaults[.gitRepositoryURL] == nil ||
Defaults[.gitRepositoryURL]!.absoluteString != gitRepostiroyURL ||
auth != Defaults[.gitRepositoryAuthenticationMethod] ||
username != Defaults[.gitRepositoryUsername] ||
password != PasswordStore.shared.gitRepositoryPassword {
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, password: password!))
} else {
gitCredential = GitCredential(credential: GitCredential.Credential.ssh(userName: username, password: Defaults[.gitRepositorySSHPrivateKeyPassphrase]!, publicKeyFile: Globals.sshPublicKeyURL, privateKeyFile: Globals.sshPrivateKeyURL))
}
let dispatchQueue = DispatchQueue.global(qos: .userInitiated)
dispatchQueue.async {
do {
try PasswordStore.shared.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 {
PasswordStore.shared.updatePasswordEntityCoreData()
Defaults[.lastUpdatedTime] = Date()
NotificationCenter.default.post(Notification(name: Notification.Name("passwordUpdated")))
Defaults[.gitRepositoryURL] = URL(string: gitRepostiroyURL)
Defaults[.gitRepositoryUsername] = username
Defaults[.gitRepositoryAuthenticationMethod] = auth
Defaults[.gitRepositoryPasswordAttempts] = 0
self.passwordRepositoryTableViewCell.detailTextLabel?.text = Defaults[.gitRepositoryURL]?.host
SVProgressHUD.showSuccess(withStatus: "Done")
SVProgressHUD.dismiss(withDelay: 1)
}
} catch {
DispatchQueue.main.async {
print(error)
SVProgressHUD.showError(withStatus: error.localizedDescription)
SVProgressHUD.dismiss(withDelay: 1)
}
}
}
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(SettingsTableViewController.actOnPasswordStoreErasedNotification), name: NSNotification.Name(rawValue: "passwordStoreErased"), object: nil)