Create password, enter with password

This commit is contained in:
Ivan Grachyov 2021-12-09 15:09:11 +03:00
parent 42959173b2
commit 3e0e4f8e68
3 changed files with 63 additions and 3 deletions

View File

@ -57,5 +57,6 @@ struct Strings {
static let addAccount = "Add Account"
static let createNew = "Create New"
static let importExisting = "💼 Import Existing"
static let passwordDoesNotMatch = "Password does not match"
}

View File

@ -14,4 +14,11 @@ extension UIViewController {
dismiss(animated: true)
}
func showMessageAlert(text: String) {
let alert = UIAlertController(title: text, message: nil, preferredStyle: .alert)
let okAction = UIAlertAction.init(title: Strings.ok, style: .default)
alert.addAction(okAction)
present(alert, animated: true)
}
}

View File

@ -4,6 +4,14 @@ import UIKit
class PasswordViewController: UIViewController {
enum Mode {
case create, repeatAfterCreate, enter
}
private let keychain = Keychain.shared
private var mode = Mode.create
var passwordToRepeat: String?
@IBOutlet weak var passwordTextField: UITextField! {
didSet {
passwordTextField.delegate = self
@ -15,12 +23,36 @@ class PasswordViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.title = "Enter password"
navigationController?.navigationBar.prefersLargeTitles = true
navigationItem.largeTitleDisplayMode = .always
navigationItem.backButtonDisplayMode = .minimal
if passwordToRepeat != nil {
switchToMode(.repeatAfterCreate)
} else if keychain.password != nil {
switchToMode(.enter)
} else {
switchToMode(.create)
}
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
passwordTextField.becomeFirstResponder()
}
func switchToMode(_ mode: Mode) {
self.mode = mode
switch mode {
case .create:
navigationItem.title = Strings.createPassword
case .repeatAfterCreate:
navigationItem.title = Strings.repeatPassword
case .enter:
navigationItem.title = Strings.enterPassword
}
}
@IBAction func okButtonTapped(_ sender: Any) {
proceedIfPossible()
}
@ -33,7 +65,25 @@ class PasswordViewController: UIViewController {
}
private func proceedIfPossible() {
showAccountsList()
switch mode {
case .create:
let passwordViewController = instantiate(PasswordViewController.self, from: .main)
passwordViewController.passwordToRepeat = passwordTextField.text
navigationController?.pushViewController(passwordViewController, animated: true)
case .repeatAfterCreate:
if let password = passwordTextField.text, !password.isEmpty, password == passwordToRepeat {
keychain.save(password: password)
showAccountsList()
} else {
showMessageAlert(text: Strings.passwordDoesNotMatch)
}
case .enter:
if passwordTextField.text == keychain.password {
showAccountsList()
} else {
showMessageAlert(text: Strings.passwordDoesNotMatch)
}
}
}
private func showAccountsList() {
@ -54,7 +104,9 @@ extension PasswordViewController: UITextFieldDelegate {
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
proceedIfPossible()
if passwordTextField.text?.isOkAsPassword == true {
proceedIfPossible()
}
return true
}