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
|
2017-01-22 01:42:36 +08:00
|
|
|
import SwiftyUserDefaults
|
2017-02-07 20:24:58 +08:00
|
|
|
import PasscodeLock
|
2017-01-19 21:15:47 +08:00
|
|
|
|
|
|
|
|
class SettingsTableViewController: UITableViewController {
|
2017-02-07 20:24:58 +08:00
|
|
|
|
2017-02-07 20:57:06 +08:00
|
|
|
let touchIDSwitch = UISwitch(frame: CGRect.zero)
|
|
|
|
|
|
2017-01-22 01:42:36 +08:00
|
|
|
@IBOutlet weak var pgpKeyTableViewCell: UITableViewCell!
|
2017-02-07 17:56:31 +08:00
|
|
|
@IBOutlet weak var touchIDTableViewCell: UITableViewCell!
|
|
|
|
|
@IBOutlet weak var passcodeTableViewCell: UITableViewCell!
|
2017-01-19 21:15:47 +08:00
|
|
|
@IBAction func cancel(segue: UIStoryboardSegue) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@IBAction func save(segue: UIStoryboardSegue) {
|
2017-01-22 01:42:36 +08:00
|
|
|
if let controller = segue.source as? GitServerSettingTableViewController {
|
2017-01-24 01:49:55 +08:00
|
|
|
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
|
|
|
|
2017-01-24 01:49:55 +08:00
|
|
|
if Defaults[.gitRepositoryURL] == nil || gitRepostiroyURL != Defaults[.gitRepositoryURL]!.absoluteString {
|
2017-01-22 01:42:36 +08:00
|
|
|
SVProgressHUD.setDefaultMaskType(.black)
|
2017-02-08 01:29:00 +08:00
|
|
|
SVProgressHUD.setDefaultStyle(.light)
|
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 {
|
2017-02-08 19:31:42 +08:00
|
|
|
gitCredential = GitCredential(credential: GitCredential.Credential.ssh(userName: username, password: Defaults[.gitRepositorySSHPrivateKeyPassphrase]!, publicKeyFile: Globals.sshPublicKeyURL, privateKeyFile: Globals.sshPrivateKeyURL))
|
2017-01-31 22:00:50 +08:00
|
|
|
}
|
2017-01-24 01:49:55 +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)
|
2017-02-06 19:13:33 +08:00
|
|
|
|
|
|
|
|
Defaults[.lastUpdatedTime] = Date()
|
2017-02-04 14:24:59 +08:00
|
|
|
|
|
|
|
|
NotificationCenter.default.post(Notification(name: Notification.Name("passwordUpdated")))
|
2017-01-24 01:49:55 +08:00
|
|
|
|
2017-02-04 14:24:59 +08:00
|
|
|
}
|
|
|
|
|
} catch {
|
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
|
print(error)
|
|
|
|
|
SVProgressHUD.showError(withStatus: error.localizedDescription)
|
2017-02-09 11:17:36 +08:00
|
|
|
SVProgressHUD.dismiss(withDelay: 1)
|
2017-01-19 21:15:47 +08:00
|
|
|
}
|
|
|
|
|
}
|
2017-02-04 14:24:59 +08:00
|
|
|
|
2017-01-19 21:15:47 +08:00
|
|
|
}
|
2017-01-22 01:42:36 +08:00
|
|
|
}
|
2017-02-07 15:09:35 +08:00
|
|
|
|
|
|
|
|
Defaults[.gitRepositoryURL] = URL(string: gitRepostiroyURL)
|
|
|
|
|
Defaults[.gitRepositoryUsername] = username
|
|
|
|
|
Defaults[.gitRepositoryPassword] = password
|
|
|
|
|
Defaults[.gitRepositoryAuthenticationMethod] = auth
|
2017-01-22 01:42:36 +08:00
|
|
|
} else if let controller = segue.source as? PGPKeySettingTableViewController {
|
2017-02-06 13:25:27 +08:00
|
|
|
|
2017-02-06 13:11:44 +08:00
|
|
|
if Defaults[.pgpKeyURL] != URL(string: controller.pgpKeyURLTextField.text!) ||
|
2017-02-06 13:25:27 +08:00
|
|
|
Defaults[.pgpKeyPassphrase] != controller.pgpKeyPassphraseTextField.text! {
|
2017-01-22 01:42:36 +08:00
|
|
|
Defaults[.pgpKeyURL] = URL(string: controller.pgpKeyURLTextField.text!)
|
|
|
|
|
Defaults[.pgpKeyPassphrase] = controller.pgpKeyPassphraseTextField.text!
|
2017-01-19 21:15:47 +08:00
|
|
|
|
2017-01-22 01:42:36 +08:00
|
|
|
SVProgressHUD.setDefaultMaskType(.black)
|
2017-02-08 01:29:00 +08:00
|
|
|
SVProgressHUD.setDefaultStyle(.light)
|
2017-01-22 01:42:36 +08:00
|
|
|
SVProgressHUD.show(withStatus: "Fetching PGP Key")
|
2017-02-07 11:47:39 +08:00
|
|
|
DispatchQueue.global(qos: .userInitiated).async { [unowned self] in
|
2017-02-06 00:59:27 +08:00
|
|
|
do {
|
2017-02-08 10:15:38 +08:00
|
|
|
try PasswordStore.shared.initPGP(pgpKeyURL: Defaults[.pgpKeyURL]!, pgpKeyLocalPath: Globals.secringPath)
|
2017-02-06 00:59:27 +08:00
|
|
|
DispatchQueue.main.async {
|
2017-02-07 11:47:39 +08:00
|
|
|
self.pgpKeyTableViewCell.detailTextLabel?.text = Defaults[.pgpKeyID]
|
2017-02-06 14:33:54 +08:00
|
|
|
SVProgressHUD.showSuccess(withStatus: "Success. Remember to remove the key from the server.")
|
2017-02-06 00:59:27 +08:00
|
|
|
SVProgressHUD.dismiss(withDelay: 1)
|
|
|
|
|
}
|
|
|
|
|
} catch {
|
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
|
SVProgressHUD.showError(withStatus: error.localizedDescription)
|
2017-02-09 11:17:36 +08:00
|
|
|
SVProgressHUD.dismiss(withDelay: 1)
|
2017-02-06 00:59:27 +08:00
|
|
|
}
|
2017-01-22 01:42:36 +08:00
|
|
|
}
|
|
|
|
|
}
|
2017-01-19 21:15:47 +08:00
|
|
|
}
|
2017-01-22 01:42:36 +08:00
|
|
|
|
2017-01-19 21:15:47 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override func viewDidLoad() {
|
|
|
|
|
super.viewDidLoad()
|
2017-02-09 11:31:49 +08:00
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(SettingsTableViewController.actOnPasswordStoreErasedNotification), name: NSNotification.Name(rawValue: "passwordStoreErased"), object: nil)
|
2017-02-08 00:43:59 +08:00
|
|
|
touchIDSwitch.onTintColor = UIColor(displayP3Red: 0, green: 122.0/255, blue: 1, alpha: 1)
|
2017-02-07 17:56:31 +08:00
|
|
|
touchIDTableViewCell.accessoryView = touchIDSwitch
|
|
|
|
|
touchIDSwitch.addTarget(self, action: #selector(touchIDSwitchAction), for: UIControlEvents.valueChanged)
|
2017-02-09 11:31:49 +08:00
|
|
|
if Defaults[.pgpKeyID] == "" {
|
|
|
|
|
pgpKeyTableViewCell.detailTextLabel?.text = "Not Set"
|
|
|
|
|
} else {
|
|
|
|
|
pgpKeyTableViewCell.detailTextLabel?.text = Defaults[.pgpKeyID]
|
|
|
|
|
}
|
2017-02-07 20:24:58 +08:00
|
|
|
if Defaults[.isTouchIDOn] {
|
|
|
|
|
touchIDSwitch.isOn = true
|
|
|
|
|
} else {
|
|
|
|
|
touchIDSwitch.isOn = false
|
|
|
|
|
}
|
2017-02-07 20:57:06 +08:00
|
|
|
if PasscodeLockRepository().hasPasscode {
|
2017-02-07 20:24:58 +08:00
|
|
|
self.passcodeTableViewCell.detailTextLabel?.text = "On"
|
|
|
|
|
} else {
|
|
|
|
|
self.passcodeTableViewCell.detailTextLabel?.text = "Off"
|
2017-02-07 20:57:06 +08:00
|
|
|
touchIDSwitch.isEnabled = false
|
2017-02-07 20:24:58 +08:00
|
|
|
}
|
2017-02-07 17:56:31 +08:00
|
|
|
}
|
|
|
|
|
|
2017-02-09 11:31:49 +08:00
|
|
|
func actOnPasswordStoreErasedNotification() {
|
|
|
|
|
pgpKeyTableViewCell.detailTextLabel?.text = "Not Set"
|
|
|
|
|
touchIDSwitch.isOn = false
|
|
|
|
|
self.passcodeTableViewCell.detailTextLabel?.text = "Off"
|
|
|
|
|
Globals.passcodeConfiguration.isTouchIDAllowed = false
|
|
|
|
|
|
|
|
|
|
let appDelegate = UIApplication.shared.delegate as! AppDelegate
|
|
|
|
|
appDelegate.passcodeLockPresenter = PasscodeLockPresenter(mainWindow: appDelegate.window, configuration: Globals.passcodeConfiguration)
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-07 17:56:31 +08:00
|
|
|
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
|
|
|
|
if tableView.cellForRow(at: indexPath) == passcodeTableViewCell {
|
2017-02-07 20:24:58 +08:00
|
|
|
if Defaults[.passcodeKey] != nil{
|
|
|
|
|
showPasscodeActionSheet()
|
|
|
|
|
} else {
|
|
|
|
|
setPasscodeLock()
|
|
|
|
|
}
|
2017-02-07 17:56:31 +08:00
|
|
|
tableView.deselectRow(at: indexPath, animated: true)
|
|
|
|
|
}
|
2017-02-07 16:45:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override func viewWillAppear(_ animated: Bool) {
|
|
|
|
|
super.viewWillAppear(animated)
|
2017-01-19 21:15:47 +08:00
|
|
|
}
|
2017-02-07 17:56:31 +08:00
|
|
|
|
|
|
|
|
func touchIDSwitchAction(uiSwitch: UISwitch) {
|
|
|
|
|
if uiSwitch.isOn {
|
2017-02-07 20:57:06 +08:00
|
|
|
Defaults[.isTouchIDOn] = true
|
2017-02-08 10:15:38 +08:00
|
|
|
Globals.passcodeConfiguration.isTouchIDAllowed = true
|
2017-02-07 17:56:31 +08:00
|
|
|
} else {
|
2017-02-07 20:57:06 +08:00
|
|
|
Defaults[.isTouchIDOn] = false
|
2017-02-08 10:15:38 +08:00
|
|
|
Globals.passcodeConfiguration.isTouchIDAllowed = false
|
2017-02-07 17:56:31 +08:00
|
|
|
}
|
2017-02-08 14:53:07 +08:00
|
|
|
let appDelegate = UIApplication.shared.delegate as! AppDelegate
|
|
|
|
|
appDelegate.passcodeLockPresenter = PasscodeLockPresenter(mainWindow: appDelegate.window, configuration: Globals.passcodeConfiguration)
|
2017-02-07 17:56:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func showPasscodeActionSheet() {
|
2017-02-08 10:15:38 +08:00
|
|
|
let passcodeChangeViewController = PasscodeLockViewController(state: .change, configuration: Globals.passcodeConfiguration)
|
|
|
|
|
let passcodeRemoveViewController = PasscodeLockViewController(state: .remove, configuration: Globals.passcodeConfiguration)
|
2017-02-07 20:24:58 +08:00
|
|
|
|
2017-02-07 17:56:31 +08:00
|
|
|
let optionMenu = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
|
2017-02-07 20:24:58 +08:00
|
|
|
let removePasscodeAction = UIAlertAction(title: "Remove Passcode", style: .destructive) { [unowned self] _ in
|
|
|
|
|
passcodeRemoveViewController.successCallback = { _ in
|
|
|
|
|
self.passcodeTableViewCell.detailTextLabel?.text = "Off"
|
2017-02-07 20:57:06 +08:00
|
|
|
self.touchIDSwitch.isEnabled = false
|
2017-02-08 16:29:03 +08:00
|
|
|
let appDelegate = UIApplication.shared.delegate as! AppDelegate
|
|
|
|
|
appDelegate.passcodeLockPresenter = PasscodeLockPresenter(mainWindow: appDelegate.window, configuration: Globals.passcodeConfiguration)
|
2017-02-07 20:24:58 +08:00
|
|
|
}
|
|
|
|
|
self.present(passcodeRemoveViewController, animated: true, completion: nil)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let changePasscodeAction = UIAlertAction(title: "Change Passcode", style: .default) { [unowned self] _ in
|
|
|
|
|
self.present(passcodeChangeViewController, animated: true, completion: nil)
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-07 17:56:31 +08:00
|
|
|
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
|
|
|
|
|
optionMenu.addAction(removePasscodeAction)
|
|
|
|
|
optionMenu.addAction(changePasscodeAction)
|
|
|
|
|
optionMenu.addAction(cancelAction)
|
|
|
|
|
self.present(optionMenu, animated: true, completion: nil)
|
|
|
|
|
}
|
2017-02-07 20:24:58 +08:00
|
|
|
|
|
|
|
|
func setPasscodeLock() {
|
2017-02-08 10:15:38 +08:00
|
|
|
let passcodeSetViewController = PasscodeLockViewController(state: .set, configuration: Globals.passcodeConfiguration)
|
2017-02-07 20:24:58 +08:00
|
|
|
passcodeSetViewController.successCallback = { _ in
|
|
|
|
|
self.passcodeTableViewCell.detailTextLabel?.text = "On"
|
2017-02-07 20:57:06 +08:00
|
|
|
self.touchIDSwitch.isEnabled = true
|
2017-02-07 20:24:58 +08:00
|
|
|
}
|
|
|
|
|
present(passcodeSetViewController, animated: true, completion: nil)
|
|
|
|
|
}
|
2017-01-19 21:15:47 +08:00
|
|
|
}
|