passforios/pass/Controllers/AdvancedSettingsTableViewController.swift

96 lines
4.8 KiB
Swift
Raw Normal View History

2017-02-07 16:45:14 +08:00
//
// AdvancedSettingsTableViewController.swift
// pass
//
// Created by Mingshen Sun on 7/2/2017.
// Copyright © 2017 Bob Sun. All rights reserved.
//
import UIKit
2017-02-07 16:52:07 +08:00
import SVProgressHUD
import passKit
2017-02-07 16:45:14 +08:00
class AdvancedSettingsTableViewController: UITableViewController {
@IBOutlet weak var encryptInASCIIArmoredTableViewCell: UITableViewCell!
2017-04-10 23:02:42 +08:00
@IBOutlet weak var gitSignatureTableViewCell: UITableViewCell!
2017-02-07 16:45:14 +08:00
@IBOutlet weak var eraseDataTableViewCell: UITableViewCell!
@IBOutlet weak var discardChangesTableViewCell: UITableViewCell!
let passwordStore = PasswordStore.shared
2018-12-09 16:59:07 -08:00
let encryptInASCIIArmoredSwitch: UISwitch = {
let uiSwitch = UISwitch()
uiSwitch.onTintColor = Colors.systemBlue
uiSwitch.sizeToFit()
2019-05-01 17:49:27 +02:00
uiSwitch.addTarget(self, action: #selector(encryptInASCIIArmoredAction(_:)), for: UIControl.Event.valueChanged)
return uiSwitch
}()
2017-02-07 16:45:14 +08:00
override func viewDidLoad() {
super.viewDidLoad()
encryptInASCIIArmoredSwitch.isOn = Defaults.encryptInArmored
encryptInASCIIArmoredTableViewCell.accessoryView = encryptInASCIIArmoredSwitch
encryptInASCIIArmoredTableViewCell.selectionStyle = .none
2017-04-27 21:48:10 -07:00
setGitSignatureText()
}
2018-12-09 16:59:07 -08:00
2017-04-27 21:48:10 -07:00
private func setGitSignatureText() {
2019-06-09 22:18:54 -07:00
let gitSignatureName = passwordStore.gitSignatureForNow?.name ?? ""
let gitSignatureEmail = passwordStore.gitSignatureForNow?.email ?? ""
2019-11-17 11:44:35 -08:00
self.gitSignatureTableViewCell.detailTextLabel?.font = UIFont.preferredFont(forTextStyle: .footnote)
2017-04-27 22:48:11 -07:00
self.gitSignatureTableViewCell.detailTextLabel?.text = "\(gitSignatureName) <\(gitSignatureEmail)>"
if Defaults.gitSignatureName == nil && Defaults.gitSignatureEmail == nil {
2019-11-17 11:44:35 -08:00
self.gitSignatureTableViewCell.detailTextLabel?.font = UIFont.preferredFont(forTextStyle: .body)
2019-01-14 20:57:45 +01:00
gitSignatureTableViewCell.detailTextLabel?.text = "NotSet".localize()
2017-04-10 23:02:42 +08:00
}
2017-02-07 16:45:14 +08:00
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
2017-02-07 16:45:14 +08:00
if tableView.cellForRow(at: indexPath) == eraseDataTableViewCell {
2019-05-01 17:49:27 +02:00
let alert = UIAlertController(title: "ErasePasswordStoreData?".localize(), message: "EraseExplanation.".localize(), preferredStyle: UIAlertController.Style.alert)
alert.addAction(UIAlertAction(title: "ErasePasswordStoreData".localize(), style: UIAlertAction.Style.destructive, handler: {[unowned self] (action) -> Void in
2019-01-14 20:57:45 +01:00
SVProgressHUD.show(withStatus: "Erasing...".localize())
self.passwordStore.erase()
self.navigationController!.popViewController(animated: true)
2019-01-14 20:57:45 +01:00
SVProgressHUD.showSuccess(withStatus: "Done".localize())
SVProgressHUD.dismiss(withDelay: 1)
2017-02-07 16:45:14 +08:00
}))
2019-05-01 17:49:27 +02:00
alert.addAction(UIAlertAction(title: "Dismiss".localize(), style: UIAlertAction.Style.cancel, handler:nil))
2017-02-07 16:45:14 +08:00
self.present(alert, animated: true, completion: nil)
} else if tableView.cellForRow(at: indexPath) == discardChangesTableViewCell {
2019-05-01 17:49:27 +02:00
let alert = UIAlertController(title: "DiscardAllLocalChanges?".localize(), message: "DiscardExplanation.".localize(), preferredStyle: UIAlertController.Style.alert)
alert.addAction(UIAlertAction(title: "DiscardAllLocalChanges".localize(), style: UIAlertAction.Style.destructive, handler: {[unowned self] (action) -> Void in
2019-01-14 20:57:45 +01:00
SVProgressHUD.show(withStatus: "Resetting...".localize())
do {
let numberDiscarded = try self.passwordStore.reset()
self.navigationController!.popViewController(animated: true)
2019-01-14 20:57:45 +01:00
SVProgressHUD.showSuccess(withStatus: "DiscardedCommits(%d)".localize(numberDiscarded))
SVProgressHUD.dismiss(withDelay: 1)
} catch {
2019-01-14 20:57:45 +01:00
Utils.alert(title: "Error".localize(), message: error.localizedDescription, controller: self, completion: nil)
}
2018-12-09 16:59:07 -08:00
}))
2019-05-01 17:49:27 +02:00
alert.addAction(UIAlertAction(title: "Dismiss".localize(), style: UIAlertAction.Style.cancel, handler:nil))
self.present(alert, animated: true, completion: nil)
2017-02-07 16:45:14 +08:00
}
}
2018-12-09 16:59:07 -08:00
@objc func encryptInASCIIArmoredAction(_ sender: Any?) {
Defaults.encryptInArmored = encryptInASCIIArmoredSwitch.isOn
}
2018-12-09 16:59:07 -08:00
2017-04-10 23:02:42 +08:00
@IBAction func saveGitConfigSetting(segue: UIStoryboardSegue) {
if let controller = segue.source as? GitConfigSettingsTableViewController {
2017-04-27 22:48:11 -07:00
if let gitSignatureName = controller.nameTextField.text,
let gitSignatureEmail = controller.emailTextField.text {
Defaults.gitSignatureName = gitSignatureName.isEmpty ? nil : gitSignatureName
Defaults.gitSignatureEmail = gitSignatureEmail.isEmpty ? nil : gitSignatureEmail
2017-04-10 23:02:42 +08:00
}
2017-04-27 21:48:10 -07:00
setGitSignatureText()
2017-04-10 23:02:42 +08:00
}
}
2017-02-07 16:45:14 +08:00
}