passforios/pass/Controllers/GeneralSettingsTableViewController.swift

250 lines
9.7 KiB
Swift
Raw Normal View History

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 passKit
import UIKit
2017-02-09 21:45:31 +08:00
class GeneralSettingsTableViewController: BasicStaticTableViewController {
let passwordStore = PasswordStore.shared
private lazy var hideUnknownSwitch: UISwitch = {
let uiSwitch = UISwitch()
uiSwitch.onTintColor = Colors.systemBlue
uiSwitch.sizeToFit()
uiSwitch.addTarget(self, action: #selector(hideUnknownSwitchAction), for: UIControl.Event.valueChanged)
return uiSwitch
}()
2018-12-09 16:59:07 -08:00
private lazy var hideOTPSwitch: UISwitch = {
let uiSwitch = UISwitch()
uiSwitch.onTintColor = Colors.systemBlue
uiSwitch.sizeToFit()
uiSwitch.addTarget(self, action: #selector(hideOTPSwitchAction), for: UIControl.Event.valueChanged)
return uiSwitch
}()
2018-12-09 16:59:07 -08:00
private lazy var autoCopyOTPSwitch: UISwitch = {
let uiSwitch = UISwitch()
uiSwitch.onTintColor = Colors.systemBlue
uiSwitch.sizeToFit()
uiSwitch.addTarget(self, action: #selector(autoCopyOTPSwitchAction), for: UIControl.Event.valueChanged)
return uiSwitch
}()
private lazy var rememberPGPPassphraseSwitch: UISwitch = {
let uiSwitch = UISwitch()
uiSwitch.onTintColor = Colors.systemBlue
uiSwitch.sizeToFit()
uiSwitch.addTarget(self, action: #selector(rememberPGPPassphraseSwitchAction), for: UIControl.Event.valueChanged)
uiSwitch.isOn = Defaults.isRememberPGPPassphraseOn
return uiSwitch
}()
2018-12-09 16:59:07 -08:00
private lazy var rememberGitCredentialPassphraseSwitch: UISwitch = {
let uiSwitch = UISwitch()
uiSwitch.onTintColor = Colors.systemBlue
uiSwitch.sizeToFit()
uiSwitch.addTarget(self, action: #selector(rememberGitCredentialPassphraseSwitchAction), for: UIControl.Event.valueChanged)
uiSwitch.isOn = Defaults.isRememberGitCredentialPassphraseOn
return uiSwitch
}()
2018-12-09 16:59:07 -08:00
private lazy var enableGPGIDSwitch: UISwitch = {
2021-01-07 21:58:38 -08:00
let uiSwitch = UISwitch()
uiSwitch.onTintColor = Colors.systemBlue
uiSwitch.sizeToFit()
uiSwitch.addTarget(self, action: #selector(enableGPGIDSwitchAction), for: UIControl.Event.valueChanged)
uiSwitch.isOn = Defaults.isEnableGPGIDOn
2021-01-07 21:58:38 -08:00
return uiSwitch
}()
private lazy var showFolderSwitch: UISwitch = {
let uiSwitch = UISwitch()
uiSwitch.onTintColor = Colors.systemBlue
uiSwitch.sizeToFit()
uiSwitch.addTarget(self, action: #selector(showFolderSwitchAction), for: UIControl.Event.valueChanged)
uiSwitch.isOn = Defaults.isShowFolderOn
return uiSwitch
}()
2017-02-09 21:45:31 +08:00
private lazy var hidePasswordImagesSwitch: UISwitch = {
let uiSwitch = UISwitch()
uiSwitch.onTintColor = Colors.systemBlue
uiSwitch.sizeToFit()
uiSwitch.addTarget(self, action: #selector(hidePasswordImagesSwitchAction), for: UIControl.Event.valueChanged)
uiSwitch.isOn = Defaults.isHidePasswordImagesOn
return uiSwitch
}()
2017-02-09 21:45:31 +08:00
override func viewDidLoad() {
2017-02-09 22:13:31 +08:00
tableData = [
// section 0
[[.title: "AboutRepository".localize(), .action: "segue", .link: "showAboutRepositorySegue"]],
2018-12-09 16:59:07 -08:00
// section 1
[
[.title: "RememberPgpKeyPassphrase".localize(), .action: "none"],
[.title: "RememberGitCredentialPassphrase".localize(), .action: "none"],
2017-03-02 16:02:35 +08:00
],
// section 2
2017-03-02 16:02:35 +08:00
[
[.title: "EnableGPGID".localize(), .action: "none"],
[.title: "ShowFolders".localize(), .action: "none"],
[.title: "HidePasswordImages".localize(), .action: "none"],
[.title: "HideUnknownFields".localize(), .action: "none"],
[.title: "HideOtpFields".localize(), .action: "none"],
[.title: "AutoCopyOTP".localize(), .action: "none"],
2017-03-02 16:02:35 +08:00
],
2017-02-09 22:13:31 +08:00
]
2017-02-09 21:45:31 +08:00
super.viewDidLoad()
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)
cell.accessoryType = .none
cell.selectionStyle = .none
switch cell.textLabel!.text! {
case "AboutRepository".localize():
cell.accessoryType = .disclosureIndicator
2019-01-14 20:57:45 +01:00
case "HideUnknownFields".localize():
hideUnknownSwitch.isOn = Defaults.isHideUnknownOn
addDetailButton(to: cell, for: hideUnknownSwitch, with: #selector(tapHideUnknownSwitchDetailButton))
2019-01-14 20:57:45 +01:00
case "HideOtpFields".localize():
hideOTPSwitch.isOn = Defaults.isHideOTPOn
addDetailButton(to: cell, for: hideOTPSwitch, with: #selector(tapHideOTPSwitchDetailButton))
2019-01-14 20:57:45 +01:00
case "RememberPgpKeyPassphrase".localize():
cell.accessoryView = rememberPGPPassphraseSwitch
2019-01-14 20:57:45 +01:00
case "RememberGitCredentialPassphrase".localize():
cell.accessoryView = rememberGitCredentialPassphraseSwitch
2019-01-14 20:57:45 +01:00
case "ShowFolders".localize():
cell.accessoryView = showFolderSwitch
case "EnableGPGID".localize():
cell.accessoryView = enableGPGIDSwitch
case "HidePasswordImages".localize():
hidePasswordImagesSwitch.isOn = Defaults.isHidePasswordImagesOn
addDetailButton(to: cell, for: hidePasswordImagesSwitch, with: #selector(tapHidePasswordImagesSwitchDetailButton))
case "AutoCopyOTP".localize():
autoCopyOTPSwitch.isOn = Defaults.autoCopyOTP
addDetailButton(to: cell, for: autoCopyOTPSwitch, with: #selector(tapAutoCopyOTPSwitchDetailButton))
default:
break
2017-02-14 11:16:30 +08:00
}
return cell
}
2018-12-09 16:59:07 -08:00
private func addDetailButton(to cell: UITableViewCell, for uiSwitch: UISwitch, with action: Selector) {
let detailButton = UIButton(type: .detailDisclosure)
uiSwitch.frame = CGRect(
x: detailButton.bounds.width + 10,
y: 0,
width: uiSwitch.bounds.width,
height: uiSwitch.bounds.height
)
detailButton.frame = CGRect(
x: 0,
y: 5,
width: detailButton.bounds.width,
height: detailButton.bounds.height
)
detailButton.addTarget(self, action: action, for: UIControl.Event.touchDown)
let accessoryViewFrame = CGRect(
x: 0,
y: 0,
width: detailButton.bounds.width + uiSwitch.bounds.width + 10,
height: cell.contentView.bounds.height
)
let accessoryView = UIView(frame: accessoryViewFrame)
accessoryView.addSubview(detailButton)
accessoryView.addSubview(uiSwitch)
uiSwitch.center.y = accessoryView.center.y
detailButton.center.y = accessoryView.center.y
cell.accessoryView = accessoryView
}
@objc
func tapHideUnknownSwitchDetailButton(_: 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)
}
2018-12-09 16:59:07 -08:00
@objc
func tapHideOTPSwitchDetailButton(_: 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()
Utils.alert(title: alertTitle, message: alertMessage, controller: self, completion: nil)
}
2018-12-09 16:59:07 -08:00
@objc
func tapAutoCopyOTPSwitchDetailButton(_: Any?) {
let alertMessage = "AutoCopyOTPExplanation.".localize()
let alertTitle = "AutoCopyOTP".localize()
Utils.alert(title: alertTitle, message: alertMessage, controller: self, completion: nil)
}
@objc
func tapHidePasswordImagesSwitchDetailButton(_: Any?) {
let alertMessage = "HidePasswordImagesExplanation.".localize()
let alertTitle = "HidePasswordImages".localize()
Utils.alert(title: alertTitle, message: alertMessage, controller: self, completion: nil)
}
@objc
func hideUnknownSwitchAction(_: Any?) {
Defaults.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
@objc
func hideOTPSwitchAction(_: Any?) {
Defaults.isHideOTPOn = hideOTPSwitch.isOn
2017-03-24 20:40:24 +08:00
NotificationCenter.default.post(name: .passwordDetailDisplaySettingChanged, object: nil)
}
2018-12-09 16:59:07 -08:00
@objc
func autoCopyOTPSwitchAction(_: Any?) {
Defaults.autoCopyOTP = autoCopyOTPSwitch.isOn
}
@objc
func rememberPGPPassphraseSwitchAction(_: Any?) {
Defaults.isRememberPGPPassphraseOn = rememberPGPPassphraseSwitch.isOn
if rememberPGPPassphraseSwitch.isOn == false {
AppKeychain.shared.removeAllContent(withPrefix: Globals.pgpKeyPassphrase)
}
}
2018-12-09 16:59:07 -08:00
@objc
func rememberGitCredentialPassphraseSwitchAction(_: Any?) {
Defaults.isRememberGitCredentialPassphraseOn = rememberGitCredentialPassphraseSwitch.isOn
if rememberGitCredentialPassphraseSwitch.isOn == false {
passwordStore.gitSSHPrivateKeyPassphrase = nil
passwordStore.gitPassword = nil
}
}
2018-12-09 16:59:07 -08:00
@objc
func showFolderSwitchAction(_: Any?) {
Defaults.isShowFolderOn = showFolderSwitch.isOn
2017-03-24 20:40:24 +08:00
NotificationCenter.default.post(name: .passwordDisplaySettingChanged, object: nil)
}
2018-12-09 16:59:07 -08:00
2021-01-07 21:58:38 -08:00
@objc
func enableGPGIDSwitchAction(_: Any?) {
Defaults.isEnableGPGIDOn = enableGPGIDSwitch.isOn
2021-01-07 21:58:38 -08:00
}
@objc
func hidePasswordImagesSwitchAction(_: Any?) {
Defaults.isHidePasswordImagesOn = hidePasswordImagesSwitch.isOn
NotificationCenter.default.post(name: .passwordDetailDisplaySettingChanged, object: nil)
}
2017-02-09 21:45:31 +08:00
}