tokenary/Encrypted Ink/Screens/PasswordViewController.swift

98 lines
2.9 KiB
Swift
Raw Normal View History

2021-06-19 00:38:05 +03:00
// Copyright © 2021 Encrypted Ink. All rights reserved.
import Cocoa
class PasswordViewController: NSViewController {
static func with(mode: Mode, reason: String? = nil, completion: ((Bool) -> Void)?) -> PasswordViewController {
2021-06-19 00:38:05 +03:00
let new = instantiate(PasswordViewController.self)
new.mode = mode
new.reason = reason
2021-06-19 00:38:05 +03:00
new.completion = completion
return new
}
enum Mode {
case create, repeatAfterCreate, enter
}
private var mode = Mode.create
private var reason: String?
2021-06-19 00:38:05 +03:00
private var passwordToRepeat: String?
private var completion: ((Bool) -> Void)?
2021-06-19 19:34:55 +03:00
@IBOutlet weak var reasonLabel: NSTextField!
2021-06-19 00:38:05 +03:00
@IBOutlet weak var cancelButton: NSButton!
@IBOutlet weak var okButton: NSButton!
@IBOutlet weak var titleLabel: NSTextField!
@IBOutlet weak var passwordTextField: NSSecureTextField! {
didSet {
passwordTextField.delegate = self
}
}
override func viewDidLoad() {
super.viewDidLoad()
switchToMode(mode)
// TODO: use enum for reason to avoid strings comparison
if let reason = reason, reason != "Start" {
2021-06-19 19:34:55 +03:00
reasonLabel.stringValue = "to " + reason.lowercased()
} else {
reasonLabel.stringValue = ""
}
2021-06-20 11:00:58 +03:00
}
2021-06-19 00:38:05 +03:00
func switchToMode(_ mode: Mode) {
self.mode = mode
switch mode {
case .create:
titleLabel.stringValue = "Create Password"
passwordToRepeat = nil
case .repeatAfterCreate:
titleLabel.stringValue = "Repeat Password"
passwordToRepeat = passwordTextField.stringValue
case .enter:
titleLabel.stringValue = "Enter Password"
}
passwordTextField.stringValue = ""
okButton.isEnabled = false
}
@IBAction func actionButtonTapped(_ sender: Any) {
switch mode {
case .create:
switchToMode(.repeatAfterCreate)
case .repeatAfterCreate:
let repeated = passwordTextField.stringValue
if repeated == passwordToRepeat {
Keychain.save(password: repeated)
completion?(true)
}
case .enter:
if Keychain.password == passwordTextField.stringValue {
completion?(true)
}
2021-06-19 00:38:05 +03:00
}
}
@IBAction func cancelButtonTapped(_ sender: NSButton) {
switch mode {
case .create:
view.window?.contentViewController = WelcomeViewController.new(completion: completion)
case .repeatAfterCreate:
switchToMode(.create)
case .enter:
completion?(false)
2021-06-19 00:38:05 +03:00
}
}
}
extension PasswordViewController: NSTextFieldDelegate {
func controlTextDidChange(_ obj: Notification) {
okButton.isEnabled = passwordTextField.stringValue.count >= 4
}
}