-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
CreatePasswordTableViewController.swift
298 lines (235 loc) · 9.68 KB
/
CreatePasswordTableViewController.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
import Library
import Prelude
import ReactiveSwift
import UIKit
enum CreatePasswordRow: CaseIterable {
case newPassword
case confirmNewPassword
}
extension CreatePasswordRow {
var placeholder: String {
return Strings.signup_input_fields_password()
}
var returnKeyType: UIReturnKeyType {
switch self {
case .newPassword: return .next
case .confirmNewPassword: return .done
}
}
var title: String {
switch self {
case .newPassword: return Strings.New_password()
case .confirmNewPassword: return Strings.Confirm_password()
}
}
}
@objc protocol CreatePasswordTableViewControllerType {
@objc optional func newPasswordTextFieldChanged(_ sender: UITextField)
@objc optional func newPasswordTextFieldDidReturn(_ sender: UITextField)
@objc optional func newPasswordConfirmationTextFieldChanged(_ sender: UITextField)
@objc optional func newPasswordConfirmationTextFieldDidReturn(_ sender: UITextField)
}
protocol CreatePasswordTableViewControllerDelegate: AnyObject {
func createPasswordTableViewController(
_ viewController: CreatePasswordTableViewController,
setSaveButtonIsEnabled isEnabled: Bool
)
func createPasswordTableViewControllerStartAnimatingSaveButton(
_ viewController: CreatePasswordTableViewController)
func createPasswordTableViewControllerStopAnimatingSaveButton(
_ viewController: CreatePasswordTableViewController)
func createPasswordTableViewController(
_ viewController: CreatePasswordTableViewController,
showErrorMessage message: String
)
}
final class CreatePasswordTableViewController: UITableViewController {
// MARK: - Properties
private let viewModel: CreatePasswordViewModelType = CreatePasswordViewModel()
weak var delegate: CreatePasswordTableViewControllerDelegate?
private weak var newPasswordTextField: UITextField?
private weak var newPasswordConfirmationTextField: UITextField?
private weak var groupedFooterView: SettingsGroupedFooterView?
// MARK: - Lifecycle
static func instantiate() -> CreatePasswordTableViewController {
return CreatePasswordTableViewController(style: .grouped)
}
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.registerHeaderFooterClass(SettingsGroupedHeaderView.self)
self.tableView.registerHeaderFooterClass(SettingsGroupedFooterView.self)
self.tableView.registerCellClass(SettingsTextInputCell.self)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
self.viewModel.inputs.viewDidAppear()
}
// MARK: - Styles
override func bindStyles() {
super.bindStyles()
_ = self.tableView
|> settingsGroupedTableViewStyle
|> \.allowsSelection .~ true
}
// MARK: - View model
override func bindViewModel() {
super.bindViewModel()
self.viewModel.outputs.activityIndicatorShouldShow
.observeForUI()
.observeValues { [weak self] shouldShow in
guard let self = self else { return }
if shouldShow {
self.delegate?.createPasswordTableViewControllerStartAnimatingSaveButton(self)
} else {
self.delegate?.createPasswordTableViewControllerStopAnimatingSaveButton(self)
}
}
self.viewModel.outputs.accessibilityFocusValidationLabel
.observeForUI()
.observeValues { [weak self] _ in
UIAccessibility.post(notification: .layoutChanged, argument: self?.groupedFooterView?.label)
}
self.viewModel.outputs.cellAtIndexPathDidBecomeFirstResponder
.observeForUI()
.observeValues { [weak self] indexPath in
guard let cell = self?.tableView.cellForRow(at: indexPath) as? SettingsTextInputCell else { return }
cell.textField.becomeFirstResponder()
}
self.viewModel.outputs.createPasswordFailure
.observeForControllerAction()
.observeValues { [weak self] errorMessage in
guard let self = self else { return }
self.delegate?.createPasswordTableViewController(self, showErrorMessage: errorMessage)
}
self.viewModel.outputs.createPasswordSuccess
.observeForControllerAction()
.observeValues { [weak self] in
guard let self = self else { return }
logoutAndDismiss(viewController: self)
}
self.viewModel.outputs.dismissKeyboard
.observeForControllerAction()
.observeValues { [weak self] in
self?.tableView.endEditing(true)
}
self.viewModel.outputs.newPasswordTextFieldDidBecomeFirstResponder
.observeForUI()
.observeValues { [weak self] in
self?.newPasswordTextField?.becomeFirstResponder()
}
self.viewModel.outputs.newPasswordConfirmationTextFieldDidBecomeFirstResponder
.observeForUI()
.observeValues { [weak self] in
self?.newPasswordConfirmationTextField?.becomeFirstResponder()
}
self.viewModel.outputs.newPasswordConfirmationTextFieldDidResignFirstResponder
.observeForUI()
.observeValues { [weak self] in
self?.newPasswordConfirmationTextField?.resignFirstResponder()
}
self.viewModel.outputs.saveButtonIsEnabled
.observeForUI()
.observeValues { [weak self] isEnabled in
guard let self = self else { return }
self.delegate?.createPasswordTableViewController(self, setSaveButtonIsEnabled: isEnabled)
}
self.viewModel.outputs.validationLabelIsHidden
.observeForUI()
.observeValues { [weak self] isHidden in
self?.groupedFooterView?.isHidden = isHidden
}
self.viewModel.outputs.validationLabelText
.observeForUI()
.observeValues { [weak self] text in
UIView.performWithoutAnimation {
self?.tableView.beginUpdates()
_ = self?.groupedFooterView?.label ?|> \.text .~ text
self?.tableView.endUpdates()
}
}
}
// MARK: - Actions
@objc func saveButtonTapped(_: Any) {
self.viewModel.inputs.saveButtonTapped()
}
// MARK: - UITableViewDataSource
override func tableView(_: UITableView, numberOfRowsInSection _: Int) -> Int {
return CreatePasswordRow.allCases.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withClass: SettingsTextInputCell.self, for: indexPath)
guard let textInputCell = cell as? SettingsTextInputCell else { return cell }
let row = CreatePasswordRow.allCases[indexPath.row]
textInputCell.configure(with: row.placeholder, returnKeyType: row.returnKeyType, title: row.title)
textInputCell.configure(with: textFieldTargetActions(for: self, row: row))
return textInputCell
}
// MARK: - UITableViewDelegate
override func tableView(_: UITableView, didSelectRowAt indexPath: IndexPath) {
self.viewModel.inputs.cellAtIndexPathShouldBecomeFirstResponder(indexPath)
}
override func tableView(_ tableView: UITableView, viewForHeaderInSection _: Int) -> UIView? {
let className = SettingsGroupedHeaderView.self
guard let headerView = tableView.dequeueReusableHeaderFooterView(withClass: className)
as? SettingsGroupedHeaderView else { return nil }
let text = Strings.Well_ask_you_to_sign_back_into_the_Kickstarter_app_once_youve_changed_your_password()
_ = headerView.label
|> \.text .~ text
return headerView
}
override func tableView(_ tableView: UITableView, viewForFooterInSection _: Int) -> UIView? {
let className = SettingsGroupedFooterView.self
guard let footerView = tableView.dequeueReusableHeaderFooterView(withClass: className)
as? SettingsGroupedFooterView else { return nil }
_ = footerView.label
|> \.text .~ self.viewModel.outputs.currentValidationLabelText()
return footerView
}
override func tableView(_: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
guard let textInputCell = cell as? SettingsTextInputCell else { return }
let row = CreatePasswordRow.allCases[indexPath.row]
switch row {
case .newPassword:
self.newPasswordTextField = textInputCell.textField
case .confirmNewPassword:
self.newPasswordConfirmationTextField = textInputCell.textField
}
}
override func tableView(_: UITableView, willDisplayFooterView view: UIView, forSection _: Int) {
guard let groupedFooterView = view as? SettingsGroupedFooterView else { return }
self.groupedFooterView = groupedFooterView
}
}
extension CreatePasswordTableViewController: CreatePasswordTableViewControllerType {
@objc func newPasswordTextFieldChanged(_ sender: UITextField) {
self.viewModel.inputs.newPasswordTextFieldChanged(text: sender.text)
}
@objc func newPasswordTextFieldDidReturn(_: UITextField) {
self.viewModel.inputs.newPasswordTextFieldDidReturn()
}
@objc func newPasswordConfirmationTextFieldChanged(_ sender: UITextField) {
self.viewModel.inputs.newPasswordConfirmationTextFieldChanged(text: sender.text)
}
@objc func newPasswordConfirmationTextFieldDidReturn(_: UITextField) {
self.viewModel.inputs.newPasswordConfirmationTextFieldDidReturn()
}
}
func textFieldTargetActions(
for controller: CreatePasswordTableViewControllerType,
row: CreatePasswordRow
) -> [TextFieldTargetAction] {
let changedSelector: Selector
let didReturnSelector: Selector
switch row {
case .newPassword:
changedSelector = #selector(controller.newPasswordTextFieldChanged(_:))
didReturnSelector = #selector(controller.newPasswordTextFieldDidReturn(_:))
case .confirmNewPassword:
changedSelector = #selector(controller.newPasswordConfirmationTextFieldChanged(_:))
didReturnSelector = #selector(controller.newPasswordConfirmationTextFieldDidReturn(_:))
}
return [
(controller.self, changedSelector, .editingChanged),
(controller.self, didReturnSelector, .editingDidEndOnExit)
]
}