passforios/pass/Controllers/AdvancedSettingsTableViewController.swift

119 lines
5.6 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 AuthenticationServices
import passKit
import SVProgressHUD
import UIKit
2017-02-07 16:45:14 +08:00
class AdvancedSettingsTableViewController: UITableViewController {
@IBOutlet var encryptInASCIIArmoredTableViewCell: UITableViewCell!
@IBOutlet var gitSignatureTableViewCell: UITableViewCell!
@IBOutlet var eraseDataTableViewCell: UITableViewCell!
@IBOutlet var discardChangesTableViewCell: UITableViewCell!
@IBOutlet var clearSuggestionsTableViewCell: UITableViewCell!
let passwordStore = PasswordStore.shared
2018-12-09 16:59:07 -08:00
private lazy var encryptInASCIIArmoredSwitch: UISwitch = {
let uiSwitch = UISwitch()
uiSwitch.onTintColor = Colors.systemBlue
uiSwitch.sizeToFit()
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 ?? ""
gitSignatureTableViewCell.detailTextLabel?.font = UIFont.preferredFont(forTextStyle: .footnote)
gitSignatureTableViewCell.detailTextLabel?.text = "\(gitSignatureName) <\(gitSignatureEmail)>"
if Defaults.gitSignatureName == nil, Defaults.gitSignatureEmail == nil {
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) { [unowned self] _ in
SVProgressHUD.show(withStatus: "Erasing...".localize())
passwordStore.erase()
navigationController!.popViewController(animated: true)
SVProgressHUD.showSuccess(withStatus: "Done".localize())
SVProgressHUD.dismiss(withDelay: 1)
}
)
alert.addAction(UIAlertAction.dismiss())
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) { [unowned self] _ in
SVProgressHUD.show(withStatus: "Resetting...".localize())
do {
let numberDiscarded = try passwordStore.reset()
navigationController!.popViewController(animated: true)
SVProgressHUD.showSuccess(withStatus: "DiscardedCommits(%d)".localize(numberDiscarded))
SVProgressHUD.dismiss(withDelay: 1)
} catch {
Utils.alert(title: "Error".localize(), message: error.localizedDescription, controller: self, completion: nil)
}
}
)
alert.addAction(UIAlertAction.dismiss())
present(alert, animated: true, completion: nil)
} else if tableView.cellForRow(at: indexPath) == clearSuggestionsTableViewCell {
ASCredentialIdentityStore.shared.removeAllCredentialIdentities { _, error in
if let error {
SVProgressHUD.showError(withStatus: "FailedToClearQuickTypeSuggestions".localize(error))
SVProgressHUD.dismiss(withDelay: 1)
} else {
SVProgressHUD.showSuccess(withStatus: "Done".localize())
SVProgressHUD.dismiss(withDelay: 1)
}
}
2017-02-07 16:45:14 +08:00
}
}
2018-12-09 16:59:07 -08:00
2021-01-31 13:17:37 +01:00
override func tableView(_: UITableView, heightForRowAt _: IndexPath) -> CGFloat {
2021-01-05 23:27:35 -08:00
UITableView.automaticDimension
}
2021-01-31 13:17:37 +01:00
override func tableView(_: UITableView, estimatedHeightForRowAt _: IndexPath) -> CGFloat {
2021-01-05 23:27:35 -08:00
UITableView.automaticDimension
}
@objc
func encryptInASCIIArmoredAction(_: Any?) {
Defaults.encryptInArmored = encryptInASCIIArmoredSwitch.isOn
}
2018-12-09 16:59:07 -08:00
@IBAction
private 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,
2020-11-07 12:06:28 +01:00
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
}