2017-02-09 21:45:31 +08:00
|
|
|
//
|
|
|
|
|
// GeneralSettingsTableViewController.swift
|
|
|
|
|
// pass
|
|
|
|
|
//
|
|
|
|
|
// Created by Mingshen Sun on 9/2/2017.
|
|
|
|
|
// Copyright © 2017 Bob Sun. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import UIKit
|
2017-06-13 11:42:49 +08:00
|
|
|
import passKit
|
2017-02-09 21:45:31 +08:00
|
|
|
|
|
|
|
|
class GeneralSettingsTableViewController: BasicStaticTableViewController {
|
2017-03-16 22:39:03 -07:00
|
|
|
let passwordStore = PasswordStore.shared
|
|
|
|
|
|
2017-02-28 12:25:52 +08:00
|
|
|
let hideUnknownSwitch: UISwitch = {
|
|
|
|
|
let uiSwitch = UISwitch()
|
|
|
|
|
uiSwitch.onTintColor = Globals.blue
|
|
|
|
|
uiSwitch.sizeToFit()
|
|
|
|
|
uiSwitch.addTarget(self, action: #selector(hideUnknownSwitchAction(_:)), for: UIControlEvents.valueChanged)
|
|
|
|
|
return uiSwitch
|
|
|
|
|
}()
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-03-03 17:12:25 +08:00
|
|
|
let hideOTPSwitch: UISwitch = {
|
|
|
|
|
let uiSwitch = UISwitch()
|
|
|
|
|
uiSwitch.onTintColor = Globals.blue
|
|
|
|
|
uiSwitch.sizeToFit()
|
|
|
|
|
uiSwitch.addTarget(self, action: #selector(hideOTPSwitchAction(_:)), for: UIControlEvents.valueChanged)
|
|
|
|
|
return uiSwitch
|
|
|
|
|
}()
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-10-08 21:37:58 +08:00
|
|
|
let rememberPGPPassphraseSwitch: UISwitch = {
|
2017-02-28 12:25:52 +08:00
|
|
|
let uiSwitch = UISwitch()
|
|
|
|
|
uiSwitch.onTintColor = Globals.blue
|
|
|
|
|
uiSwitch.sizeToFit()
|
2017-10-08 21:37:58 +08:00
|
|
|
uiSwitch.addTarget(self, action: #selector(rememberPGPPassphraseSwitchAction(_:)), for: UIControlEvents.valueChanged)
|
|
|
|
|
uiSwitch.isOn = SharedDefaults[.isRememberPGPPassphraseOn]
|
|
|
|
|
return uiSwitch
|
|
|
|
|
}()
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-10-08 21:37:58 +08:00
|
|
|
let rememberGitCredentialPassphraseSwitch: UISwitch = {
|
|
|
|
|
let uiSwitch = UISwitch()
|
|
|
|
|
uiSwitch.onTintColor = Globals.blue
|
|
|
|
|
uiSwitch.sizeToFit()
|
|
|
|
|
uiSwitch.addTarget(self, action: #selector(rememberGitCredentialPassphraseSwitchAction(_:)), for: UIControlEvents.valueChanged)
|
|
|
|
|
uiSwitch.isOn = SharedDefaults[.isRememberGitCredentialPassphraseOn]
|
2017-02-28 12:25:52 +08:00
|
|
|
return uiSwitch
|
|
|
|
|
}()
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-03-02 15:01:52 +08:00
|
|
|
let showFolderSwitch: UISwitch = {
|
|
|
|
|
let uiSwitch = UISwitch()
|
|
|
|
|
uiSwitch.onTintColor = Globals.blue
|
|
|
|
|
uiSwitch.sizeToFit()
|
|
|
|
|
uiSwitch.addTarget(self, action: #selector(showFolderSwitchAction(_:)), for: UIControlEvents.valueChanged)
|
2017-06-13 11:42:49 +08:00
|
|
|
uiSwitch.isOn = SharedDefaults[.isShowFolderOn]
|
2017-03-02 15:01:52 +08:00
|
|
|
return uiSwitch
|
|
|
|
|
}()
|
2017-02-09 21:45:31 +08:00
|
|
|
|
|
|
|
|
override func viewDidLoad() {
|
2017-02-09 22:13:31 +08:00
|
|
|
tableData = [
|
|
|
|
|
// section 0
|
2019-01-14 20:57:45 +01:00
|
|
|
[[.title: "AboutRepository".localize(), .action: "segue", .link: "showAboutRepositorySegue"],],
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-02-25 11:40:12 +08:00
|
|
|
// section 1
|
2017-03-08 00:57:49 -08:00
|
|
|
[
|
2019-01-14 20:57:45 +01:00
|
|
|
[.title: "PasswordGeneratorFlavor".localize(), .action: "none", .style: CellDataStyle.value1],
|
2017-03-08 00:57:49 -08:00
|
|
|
],
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-03-08 00:57:49 -08:00
|
|
|
// section 2
|
2017-02-28 12:25:52 +08:00
|
|
|
[
|
2019-01-14 20:57:45 +01:00
|
|
|
[.title: "RememberPgpKeyPassphrase".localize(), .action: "none",],
|
|
|
|
|
[.title: "RememberGitCredentialPassphrase".localize(), .action: "none",],
|
2017-03-02 16:02:35 +08:00
|
|
|
],
|
|
|
|
|
[
|
2019-01-14 20:57:45 +01:00
|
|
|
[.title: "ShowFolders".localize(), .action: "none",],
|
|
|
|
|
[.title: "HideUnknownFields".localize(), .action: "none",],
|
|
|
|
|
[.title: "HideOtpFields".localize(), .action: "none",],
|
2017-03-02 16:02:35 +08:00
|
|
|
],
|
2017-02-14 11:16:30 +08:00
|
|
|
|
2017-02-09 22:13:31 +08:00
|
|
|
]
|
2017-02-09 21:45:31 +08:00
|
|
|
super.viewDidLoad()
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-02-14 11:16:30 +08:00
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-02-14 11:16:30 +08:00
|
|
|
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
|
|
|
|
let cell = super.tableView(tableView, cellForRowAt: indexPath)
|
2017-03-02 15:01:52 +08:00
|
|
|
switch cell.textLabel!.text! {
|
2019-01-14 20:57:45 +01:00
|
|
|
case "HideUnknownFields".localize():
|
2017-02-14 11:16:30 +08:00
|
|
|
cell.accessoryType = .none
|
2017-02-14 18:08:31 +08:00
|
|
|
let detailButton = UIButton(type: .detailDisclosure)
|
2017-02-14 19:19:47 +08:00
|
|
|
hideUnknownSwitch.frame = CGRect(x: detailButton.bounds.width+10, y: 0, width: hideUnknownSwitch.bounds.width, height: hideUnknownSwitch.bounds.height)
|
|
|
|
|
detailButton.frame = CGRect(x: 0, y: 5, width: detailButton.bounds.width, height: detailButton.bounds.height)
|
2017-02-14 18:08:31 +08:00
|
|
|
detailButton.addTarget(self, action: #selector(GeneralSettingsTableViewController.tapHideUnknownSwitchDetailButton(_:)), for: UIControlEvents.touchDown)
|
|
|
|
|
let accessoryView = UIView(frame: CGRect(x: 0, y: 0, width: detailButton.bounds.width + hideUnknownSwitch.bounds.width+10, height: hideUnknownSwitch.bounds.height))
|
|
|
|
|
accessoryView.addSubview(detailButton)
|
|
|
|
|
accessoryView.addSubview(hideUnknownSwitch)
|
|
|
|
|
cell.accessoryView = accessoryView
|
2017-02-14 11:16:30 +08:00
|
|
|
cell.selectionStyle = .none
|
2017-06-13 11:42:49 +08:00
|
|
|
hideUnknownSwitch.isOn = SharedDefaults[.isHideUnknownOn]
|
2019-01-14 20:57:45 +01:00
|
|
|
case "HideOtpFields".localize():
|
2017-03-03 17:12:25 +08:00
|
|
|
cell.accessoryType = .none
|
|
|
|
|
let detailButton = UIButton(type: .detailDisclosure)
|
|
|
|
|
hideOTPSwitch.frame = CGRect(x: detailButton.bounds.width+10, y: 0, width: hideOTPSwitch.bounds.width, height: hideOTPSwitch.bounds.height)
|
|
|
|
|
detailButton.frame = CGRect(x: 0, y: 5, width: detailButton.bounds.width, height: detailButton.bounds.height)
|
|
|
|
|
detailButton.addTarget(self, action: #selector(GeneralSettingsTableViewController.tapHideOTPSwitchDetailButton(_:)), for: UIControlEvents.touchDown)
|
|
|
|
|
let accessoryView = UIView(frame: CGRect(x: 0, y: 0, width: detailButton.bounds.width + hideOTPSwitch.bounds.width+10, height: hideOTPSwitch.bounds.height))
|
|
|
|
|
accessoryView.addSubview(detailButton)
|
|
|
|
|
accessoryView.addSubview(hideOTPSwitch)
|
|
|
|
|
cell.accessoryView = accessoryView
|
|
|
|
|
cell.selectionStyle = .none
|
2017-06-13 11:42:49 +08:00
|
|
|
hideOTPSwitch.isOn = SharedDefaults[.isHideOTPOn]
|
2019-01-14 20:57:45 +01:00
|
|
|
case "RememberPgpKeyPassphrase".localize():
|
2017-02-28 12:25:52 +08:00
|
|
|
cell.accessoryType = .none
|
|
|
|
|
cell.selectionStyle = .none
|
2017-10-08 21:37:58 +08:00
|
|
|
cell.accessoryView = rememberPGPPassphraseSwitch
|
2019-01-14 20:57:45 +01:00
|
|
|
case "RememberGitCredentialPassphrase".localize():
|
2017-10-08 21:37:58 +08:00
|
|
|
cell.accessoryType = .none
|
|
|
|
|
cell.selectionStyle = .none
|
|
|
|
|
cell.accessoryView = rememberGitCredentialPassphraseSwitch
|
2019-01-14 20:57:45 +01:00
|
|
|
case "ShowFolders".localize():
|
2017-03-02 15:01:52 +08:00
|
|
|
cell.accessoryType = .none
|
|
|
|
|
cell.selectionStyle = .none
|
|
|
|
|
cell.accessoryView = showFolderSwitch
|
2019-01-14 20:57:45 +01:00
|
|
|
case "PasswordGeneratorFlavor".localize():
|
2017-03-08 00:57:49 -08:00
|
|
|
cell.accessoryType = .disclosureIndicator
|
2019-01-14 20:24:56 +01:00
|
|
|
cell.detailTextLabel?.text = PasswordGeneratorFlavour.from(SharedDefaults[.passwordGeneratorFlavor]).name
|
2017-03-02 15:01:52 +08:00
|
|
|
default: break
|
2017-02-14 11:16:30 +08:00
|
|
|
}
|
|
|
|
|
return cell
|
|
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-03-08 00:57:49 -08:00
|
|
|
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
|
|
|
|
super.tableView(tableView, didSelectRowAt: indexPath)
|
|
|
|
|
let cell = tableView.cellForRow(at: indexPath)!
|
2019-01-14 20:57:45 +01:00
|
|
|
if cell.textLabel!.text! == "PasswordGeneratorFlavor".localize() {
|
2017-03-08 00:57:49 -08:00
|
|
|
tableView.deselectRow(at: indexPath, animated: true)
|
|
|
|
|
showPasswordGeneratorFlavorActionSheet(sourceCell: cell)
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-03-08 00:57:49 -08:00
|
|
|
func showPasswordGeneratorFlavorActionSheet(sourceCell: UITableViewCell) {
|
|
|
|
|
let optionMenu = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
|
|
|
|
|
var randomFlavorActionTitle = ""
|
|
|
|
|
var appleFlavorActionTitle = ""
|
2019-01-14 20:24:56 +01:00
|
|
|
if SharedDefaults[.passwordGeneratorFlavor] == PasswordGeneratorFlavour.RANDOM.rawValue {
|
2019-01-14 20:57:45 +01:00
|
|
|
randomFlavorActionTitle = "✓ " + "RandomString".localize()
|
|
|
|
|
appleFlavorActionTitle = "ApplesKeychainStyle".localize()
|
2017-03-08 00:57:49 -08:00
|
|
|
} else {
|
2019-01-14 20:57:45 +01:00
|
|
|
randomFlavorActionTitle = "RandomString".localize()
|
|
|
|
|
appleFlavorActionTitle = "✓ " + "ApplesKeychainStyle".localize()
|
2017-03-08 00:57:49 -08:00
|
|
|
}
|
|
|
|
|
let randomFlavorAction = UIAlertAction(title: randomFlavorActionTitle, style: .default) { _ in
|
2019-01-14 20:24:56 +01:00
|
|
|
SharedDefaults[.passwordGeneratorFlavor] = PasswordGeneratorFlavour.RANDOM.rawValue
|
|
|
|
|
sourceCell.detailTextLabel?.text = PasswordGeneratorFlavour.RANDOM.name
|
2017-03-08 00:57:49 -08:00
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-03-08 00:57:49 -08:00
|
|
|
let appleFlavorAction = UIAlertAction(title: appleFlavorActionTitle, style: .default) { _ in
|
2019-01-14 20:24:56 +01:00
|
|
|
SharedDefaults[.passwordGeneratorFlavor] = PasswordGeneratorFlavour.APPLE.rawValue
|
|
|
|
|
sourceCell.detailTextLabel?.text = PasswordGeneratorFlavour.APPLE.name
|
2017-03-08 00:57:49 -08:00
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2019-01-14 20:57:45 +01:00
|
|
|
let cancelAction = UIAlertAction(title: "Cancel".localize(), style: .cancel, handler: nil)
|
2017-03-08 00:57:49 -08:00
|
|
|
optionMenu.addAction(randomFlavorAction)
|
|
|
|
|
optionMenu.addAction(appleFlavorAction)
|
|
|
|
|
optionMenu.addAction(cancelAction)
|
|
|
|
|
optionMenu.popoverPresentationController?.sourceView = sourceCell
|
|
|
|
|
optionMenu.popoverPresentationController?.sourceRect = sourceCell.bounds
|
|
|
|
|
self.present(optionMenu, animated: true, completion: nil)
|
|
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-09-23 16:29:03 +08:00
|
|
|
@objc func tapHideUnknownSwitchDetailButton(_ sender: Any?) {
|
2019-01-14 20:57:45 +01:00
|
|
|
let alertMessage = "HideUnknownFieldsExplanation.".localize()
|
|
|
|
|
let alertTitle = "HideUnknownFields".localize()
|
2017-02-16 00:54:42 +08:00
|
|
|
Utils.alert(title: alertTitle, message: alertMessage, controller: self, completion: nil)
|
2017-02-14 18:08:31 +08:00
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-09-23 16:29:03 +08:00
|
|
|
@objc func tapHideOTPSwitchDetailButton(_ sender: Any?) {
|
2019-01-14 20:57:45 +01:00
|
|
|
let keywordsString = Constants.OTP_KEYWORDS.joined(separator: ", ")
|
|
|
|
|
let alertMessage = "HideOtpFieldsExplanation.".localize(keywordsString)
|
|
|
|
|
let alertTitle = "HideOtpFields".localize()
|
2017-03-03 17:12:25 +08:00
|
|
|
Utils.alert(title: alertTitle, message: alertMessage, controller: self, completion: nil)
|
|
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-09-23 16:29:03 +08:00
|
|
|
@objc func hideUnknownSwitchAction(_ sender: Any?) {
|
2017-06-13 11:42:49 +08:00
|
|
|
SharedDefaults[.isHideUnknownOn] = hideUnknownSwitch.isOn
|
2017-03-24 20:40:24 +08:00
|
|
|
NotificationCenter.default.post(name: .passwordDetailDisplaySettingChanged, object: nil)
|
2017-02-09 21:45:31 +08:00
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-09-23 16:29:03 +08:00
|
|
|
@objc func hideOTPSwitchAction(_ sender: Any?) {
|
2017-06-13 11:42:49 +08:00
|
|
|
SharedDefaults[.isHideOTPOn] = hideOTPSwitch.isOn
|
2017-03-24 20:40:24 +08:00
|
|
|
NotificationCenter.default.post(name: .passwordDetailDisplaySettingChanged, object: nil)
|
2017-03-03 17:12:25 +08:00
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-10-08 21:37:58 +08:00
|
|
|
@objc func rememberPGPPassphraseSwitchAction(_ sender: Any?) {
|
|
|
|
|
SharedDefaults[.isRememberPGPPassphraseOn] = rememberPGPPassphraseSwitch.isOn
|
|
|
|
|
if rememberPGPPassphraseSwitch.isOn == false {
|
2017-03-16 22:39:03 -07:00
|
|
|
passwordStore.pgpKeyPassphrase = nil
|
2017-02-28 12:25:52 +08:00
|
|
|
}
|
|
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-10-08 21:37:58 +08:00
|
|
|
@objc func rememberGitCredentialPassphraseSwitchAction(_ sender: Any?) {
|
|
|
|
|
SharedDefaults[.isRememberGitCredentialPassphraseOn] = rememberGitCredentialPassphraseSwitch.isOn
|
|
|
|
|
if rememberGitCredentialPassphraseSwitch.isOn == false {
|
|
|
|
|
passwordStore.gitSSHPrivateKeyPassphrase = nil
|
|
|
|
|
passwordStore.gitPassword = nil
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-09-23 16:29:03 +08:00
|
|
|
@objc func showFolderSwitchAction(_ sender: Any?) {
|
2017-06-13 11:42:49 +08:00
|
|
|
SharedDefaults[.isShowFolderOn] = showFolderSwitch.isOn
|
2017-03-24 20:40:24 +08:00
|
|
|
NotificationCenter.default.post(name: .passwordDisplaySettingChanged, object: nil)
|
2017-03-02 15:01:52 +08:00
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-02-09 21:45:31 +08:00
|
|
|
}
|