-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathListDetailViewController.swift
121 lines (101 loc) · 4.04 KB
/
ListDetailViewController.swift
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
//
// ListDetailViewController.swift
// Lockdown
//
// Created by Aliaksandr Dvoineu on 29.03.23.
// Copyright © 2023 Confirmed Inc. All rights reserved.
//
import UIKit
protocol ListDetailViewControllerDelegate {
func changeListName(name: String)
}
final class ListDetailViewController: UIViewController {
var delegate: ListDetailViewControllerDelegate?
var listName = ""
// MARK: - Properties
private lazy var navigationView: ConfiguredNavigationView = {
let view = ConfiguredNavigationView()
view.titleLabel.text = NSLocalizedString("Name", comment: "")
view.leftNavButton.setTitle("BACK", for: .normal)
view.leftNavButton.setImage(UIImage(systemName: "chevron.left"), for: .normal)
view.leftNavButton.addTarget(self, action: #selector(returnBack), for: .touchUpInside)
view.rightNavButton.setTitle("DONE", for: .normal)
view.rightNavButton.addTarget(self, action: #selector(doneButtonClicked), for: .touchUpInside)
return view
}()
lazy var listNameTextField: UITextField = {
let view = UITextField()
view.text = listName
view.font = fontMedium17
view.textColor = .label
view.backgroundColor = .systemBackground
view.leftView = UIView(frame: CGRect(x: 0, y: 0, width: 15, height: .zero))
view.leftViewMode = .always
view.addTarget(self, action: #selector(handleTextChange), for: .editingChanged)
return view
}()
private lazy var validationPrompt: UILabel = {
let label = UILabel()
label.textColor = .red
label.font = fontRegular14
label.textAlignment = .left
label.numberOfLines = 0
return label
}()
// MARK: - Lifecycle
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .secondarySystemBackground
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(dismissKeyboard))
tap.cancelsTouchesInView = false
view.addGestureRecognizer(tap)
configureUI()
}
// MARK: - Configure UI
private func configureUI() {
view.addSubview(navigationView)
navigationView.anchors.leading.pin()
navigationView.anchors.trailing.pin()
navigationView.anchors.top.safeAreaPin()
view.addSubview(listNameTextField)
listNameTextField.anchors.top.spacing(12, to: navigationView.anchors.bottom)
listNameTextField.anchors.leading.marginsPin()
listNameTextField.anchors.trailing.marginsPin()
listNameTextField.anchors.height.equal(40)
view.addSubview(validationPrompt)
validationPrompt.anchors.top.spacing(8, to: listNameTextField.anchors.bottom)
validationPrompt.anchors.leading.marginsPin()
validationPrompt.anchors.trailing.marginsPin()
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
listNameTextField.layer.cornerRadius = 8
}
}
// MARK: - Functions
extension ListDetailViewController {
@objc func handleTextChange() {
guard let text = listNameTextField.text else { return }
if text.isValid(.listName) {
navigationView.rightNavButton.isEnabled = true
validationPrompt.text = ""
} else {
navigationView.rightNavButton.isEnabled = false
validationPrompt.text = "Invalid name. Please use only letters and digits. The maximum number of symbols is 20."
}
}
@objc func returnBack() {
navigationController?.popViewController(animated: true)
}
@objc func doneButtonClicked() {
guard let newListName = listNameTextField.text else { return }
delegate?.changeListName(name: newListName)
if listName != newListName {
changeBlockedListName(from: listName, to: newListName)
}
navigationController?.popViewController(animated: true)
}
@objc func dismissKeyboard() {
view.endEditing(true)
}
}