passforios/pass/SettingsTableViewController.swift

112 lines
5.6 KiB
Swift
Raw Normal View History

2017-01-19 21:15:47 +08:00
//
// SettingsTableViewController.swift
// pass
//
// Created by Mingshen Sun on 18/1/2017.
// Copyright © 2017 Bob Sun. All rights reserved.
//
import UIKit
import SVProgressHUD
import CoreData
import SwiftyUserDefaults
2017-01-19 21:15:47 +08:00
class SettingsTableViewController: UITableViewController {
@IBOutlet weak var pgpKeyTableViewCell: UITableViewCell!
2017-01-19 21:15:47 +08:00
@IBAction func cancel(segue: UIStoryboardSegue) {
}
@IBAction func save(segue: UIStoryboardSegue) {
if let controller = segue.source as? GitServerSettingTableViewController {
let gitRepostiroyURL = controller.gitRepositoryURLTextField.text!
let username = controller.usernameTextField.text!
let password = controller.passwordTextField.text!
2017-01-31 22:00:50 +08:00
let auth = controller.authenticationMethod
2017-01-25 18:28:37 +08:00
if Defaults[.gitRepositoryURL] == nil || gitRepostiroyURL != Defaults[.gitRepositoryURL]!.absoluteString {
SVProgressHUD.setDefaultMaskType(.black)
2017-01-23 17:36:10 +08:00
SVProgressHUD.show(withStatus: "Prepare Repository")
2017-01-31 22:00:50 +08:00
var gitCredential: GitCredential
2017-02-02 15:02:25 +08:00
if auth == "Password" {
2017-01-31 22:00:50 +08:00
gitCredential = GitCredential(credential: GitCredential.Credential.http(userName: username, password: password))
} else {
gitCredential = GitCredential(credential: GitCredential.Credential.ssh(userName: username, password: Defaults[.gitRepositorySSHPrivateKeyPassphrase]!, publicKeyFile: Globals.shared.sshPublicKeyPath, privateKeyFile: Globals.shared.sshPrivateKeyPath))
2017-01-31 22:00:50 +08:00
}
2017-01-19 21:15:47 +08:00
DispatchQueue.global(qos: .userInitiated).async {
2017-02-04 14:24:59 +08:00
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 {
SVProgressHUD.showSuccess(withStatus: "Done")
SVProgressHUD.dismiss(withDelay: 1)
Defaults[.gitRepositoryURL] = URL(string: gitRepostiroyURL)
Defaults[.gitRepositoryUsername] = username
Defaults[.gitRepositoryPassword] = password
Defaults[.gitRepositoryAuthenticationMethod] = auth
NotificationCenter.default.post(Notification(name: Notification.Name("passwordUpdated")))
2017-02-04 14:24:59 +08:00
}
} catch {
DispatchQueue.main.async {
print(error)
SVProgressHUD.showError(withStatus: error.localizedDescription)
SVProgressHUD.dismiss(withDelay: 3)
2017-01-19 21:15:47 +08:00
}
}
2017-02-04 14:24:59 +08:00
2017-01-19 21:15:47 +08:00
}
}
} else if let controller = segue.source as? PGPKeySettingTableViewController {
if Defaults[.pgpKeyURL] != URL(string: controller.pgpKeyURLTextField.text!) {
Defaults[.pgpKeyURL] = URL(string: controller.pgpKeyURLTextField.text!)
Defaults[.pgpKeyPassphrase] = controller.pgpKeyPassphraseTextField.text!
2017-01-19 21:15:47 +08:00
SVProgressHUD.setDefaultMaskType(.black)
SVProgressHUD.show(withStatus: "Fetching PGP Key")
DispatchQueue.global(qos: .userInitiated).async {
2017-02-06 00:59:27 +08:00
do {
try PasswordStore.shared.initPGP(pgpKeyURL: Defaults[.pgpKeyURL]!, pgpKeyLocalPath: Globals.shared.secringPath)
DispatchQueue.main.async {
SVProgressHUD.showSuccess(withStatus: "Success")
2017-02-06 00:59:27 +08:00
SVProgressHUD.dismiss(withDelay: 1)
}
} catch {
DispatchQueue.main.async {
SVProgressHUD.showError(withStatus: error.localizedDescription)
SVProgressHUD.dismiss(withDelay: 3)
}
}
}
2017-01-19 21:15:47 +08:00
}
2017-01-19 21:15:47 +08:00
}
}
override func viewDidLoad() {
super.viewDidLoad()
if Defaults[.pgpKeyID] == "" {
pgpKeyTableViewCell.detailTextLabel?.text = "Not Set"
2017-01-19 21:15:47 +08:00
} else {
pgpKeyTableViewCell.detailTextLabel?.text = Defaults[.pgpKeyID]
2017-01-19 21:15:47 +08:00
}
}
}