This repository was archived by the owner on Oct 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLoginViewController.swift
More file actions
74 lines (52 loc) · 1.63 KB
/
LoginViewController.swift
File metadata and controls
74 lines (52 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import InterfaceBacked
import UIKit
protocol LoginViewDelegate: class {
func submitTapped(user: User)
}
final class LoginViewController: UIViewController, StoryboardBacked {
// MARK: - Properties
@IBOutlet weak var emailField: UITextField!
@IBOutlet weak var passwordField: UITextField!
@IBOutlet weak var submitButton: UIButton!
weak var delegate: LoginViewDelegate?
// MARK: - UIViewController Methods
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
emailField.becomeFirstResponder()
validateSubmitButton()
}
// MARK: - User Actions
@IBAction func submitTapped() {
guard let rawEmail = emailField.text, let password = passwordField.text else { return }
let email = Email(rawEmail)
let user = User(email: email, password: password)
delegate?.submitTapped(user: user)
let shortcuts = QuickAction.Shortcut.self
UIApplication.shared.shortcutItems = [shortcuts.new, shortcuts.duplicate]
}
@IBAction func textFieldChanged() {
validateSubmitButton()
}
// MARK: - Private
private func validateSubmitButton() {
if let email = emailField.text, email.isNotEmpty, let password = passwordField.text, password.isNotEmpty {
submitButton.isEnabled = true
} else {
submitButton.isEnabled = false
}
}
}
// MARK: - Extension
extension LoginViewController: UITextFieldDelegate {
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
if textField === emailField {
textField.resignFirstResponder()
passwordField.becomeFirstResponder()
}
if textField === passwordField {
passwordField.resignFirstResponder()
submitTapped()
}
return true
}
}