-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathCreatePasswordViewModel.swift
194 lines (157 loc) · 7.34 KB
/
CreatePasswordViewModel.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
import Foundation
import KsApi
import Prelude
import ReactiveSwift
public protocol CreatePasswordViewModelInputs {
func cellAtIndexPathShouldBecomeFirstResponder(_ indexPath: IndexPath?)
func newPasswordTextFieldChanged(text: String?)
func newPasswordTextFieldDidReturn()
func newPasswordConfirmationTextFieldChanged(text: String?)
func newPasswordConfirmationTextFieldDidReturn()
func saveButtonTapped()
func viewDidAppear()
}
public protocol CreatePasswordViewModelOutputs {
var accessibilityFocusValidationLabel: Signal<Void, Never> { get }
var activityIndicatorShouldShow: Signal<Bool, Never> { get }
var cellAtIndexPathDidBecomeFirstResponder: Signal<IndexPath, Never> { get }
var createPasswordFailure: Signal<String, Never> { get }
var createPasswordSuccess: Signal<Void, Never> { get }
var dismissKeyboard: Signal<Void, Never> { get }
var newPasswordTextFieldDidBecomeFirstResponder: Signal<Void, Never> { get }
var newPasswordConfirmationTextFieldDidBecomeFirstResponder: Signal<Void, Never> { get }
var newPasswordConfirmationTextFieldDidResignFirstResponder: Signal<Void, Never> { get }
var saveButtonIsEnabled: Signal<Bool, Never> { get }
var validationLabelIsHidden: Signal<Bool, Never> { get }
var validationLabelText: Signal<String?, Never> { get }
func currentValidationLabelText() -> String?
}
public protocol CreatePasswordViewModelType {
var inputs: CreatePasswordViewModelInputs { get }
var outputs: CreatePasswordViewModelOutputs { get }
}
public class CreatePasswordViewModel: CreatePasswordViewModelType,
CreatePasswordViewModelInputs, CreatePasswordViewModelOutputs {
public init() {
self.newPasswordTextFieldDidBecomeFirstResponder = self.viewDidAppearProperty.signal
self.newPasswordConfirmationTextFieldDidBecomeFirstResponder = self.newPasswordDidReturnProperty.signal
self.newPasswordConfirmationTextFieldDidResignFirstResponder =
self.newPasswordConfirmationDidReturnProperty.signal
let combinedPasswords = Signal.combineLatest(
self.newPasswordChangedProperty.signal.skipNil(),
self.newPasswordConfirmationChangedProperty.signal.skipNil()
)
let fieldsMatch = combinedPasswords.map(==)
let fieldLengthIsValid = self.newPasswordChangedProperty.signal.skipNil().map(passwordLengthValid)
let fieldsNotEmpty = combinedPasswords.map(formFieldsNotEmpty)
let formIsValid = Signal.combineLatest(fieldsNotEmpty, fieldsMatch, fieldLengthIsValid)
.map(passwordFormValid)
.skipRepeats()
let newPasswordValidationText = fieldLengthIsValid
.map(passwordValidationText)
.skipRepeats()
let newPasswordAndConfirmationValidationText = Signal.combineLatest(fieldLengthIsValid, fieldsMatch)
.map(passwordValidationText)
.skipRepeats()
self.validationLabelText = Signal.merge(
self.viewDidAppearProperty.signal.mapConst(nil),
newPasswordValidationText,
newPasswordAndConfirmationValidationText
)
self.currentValidationLabelTextProperty <~ self.validationLabelText
let validationLabelTextIsNil = self.validationLabelText
.map(isNil)
let inputsChanged = Signal.merge(
self.newPasswordChangedProperty.signal, self.newPasswordConfirmationChangedProperty.signal
)
self.accessibilityFocusValidationLabel = validationLabelTextIsNil
.takeWhen(inputsChanged)
.filter { _ in AppEnvironment.current.isVoiceOverRunning() }
.filter(isFalse)
.ignoreValues()
self.saveButtonIsEnabled = formIsValid
let submitFormEvent = Signal.merge(
self.newPasswordConfirmationDidReturnProperty.signal,
self.saveButtonTappedProperty.signal
)
let saveAction = formIsValid
.takeWhen(submitFormEvent)
.filter(isTrue)
.ignoreValues()
let createPasswordEvent = combinedPasswords
.takeWhen(saveAction)
.map { CreatePasswordInput(password: $0.0, passwordConfirmation: $0.1) }
.switchMap { input in
AppEnvironment.current.apiService.createPassword(input: input)
.ksr_delay(AppEnvironment.current.apiDelayInterval, on: AppEnvironment.current.scheduler)
.materialize()
}
self.createPasswordFailure = createPasswordEvent.errors().map { $0.localizedDescription }
self.createPasswordSuccess = createPasswordEvent.values().ignoreValues()
self.activityIndicatorShouldShow = Signal.merge(
saveAction.signal.mapConst(true),
self.createPasswordFailure.mapConst(false),
self.createPasswordSuccess.mapConst(false)
)
self.dismissKeyboard = submitFormEvent
self.cellAtIndexPathDidBecomeFirstResponder = Signal.combineLatest(
self.viewDidAppearProperty.signal,
self.cellAtIndexPathShouldBecomeFirstResponderProperty.signal.skipNil()
).map { $0.1 }
self.validationLabelIsHidden = validationLabelTextIsNil
}
private var newPasswordChangedProperty = MutableProperty<String?>(nil)
public func newPasswordTextFieldChanged(text: String?) {
self.newPasswordChangedProperty.value = text
}
private var newPasswordDidReturnProperty = MutableProperty(())
public func newPasswordTextFieldDidReturn() {
self.newPasswordDidReturnProperty.value = ()
}
private var newPasswordConfirmationChangedProperty = MutableProperty<String?>(nil)
public func newPasswordConfirmationTextFieldChanged(text: String?) {
self.newPasswordConfirmationChangedProperty.value = text
}
private var newPasswordConfirmationDidReturnProperty = MutableProperty(())
public func newPasswordConfirmationTextFieldDidReturn() {
self.newPasswordConfirmationDidReturnProperty.value = ()
}
private var saveButtonTappedProperty = MutableProperty(())
public func saveButtonTapped() {
self.saveButtonTappedProperty.value = ()
}
private var cellAtIndexPathShouldBecomeFirstResponderProperty = MutableProperty<IndexPath?>(nil)
public func cellAtIndexPathShouldBecomeFirstResponder(_ indexPath: IndexPath?) {
self.cellAtIndexPathShouldBecomeFirstResponderProperty.value = indexPath
}
public let accessibilityFocusValidationLabel: Signal<Void, Never>
public let activityIndicatorShouldShow: Signal<Bool, Never>
public let cellAtIndexPathDidBecomeFirstResponder: Signal<IndexPath, Never>
public let createPasswordFailure: Signal<String, Never>
public let createPasswordSuccess: Signal<Void, Never>
public let dismissKeyboard: Signal<Void, Never>
public let newPasswordTextFieldDidBecomeFirstResponder: Signal<Void, Never>
public let newPasswordConfirmationTextFieldDidBecomeFirstResponder: Signal<Void, Never>
public let newPasswordConfirmationTextFieldDidResignFirstResponder: Signal<Void, Never>
public let saveButtonIsEnabled: Signal<Bool, Never>
public let validationLabelIsHidden: Signal<Bool, Never>
public let validationLabelText: Signal<String?, Never>
private let currentValidationLabelTextProperty = MutableProperty<String?>(nil)
public func currentValidationLabelText() -> String? {
return self.currentValidationLabelTextProperty.value
}
private let viewDidAppearProperty = MutableProperty(())
public func viewDidAppear() {
self.viewDidAppearProperty.value = ()
}
public var inputs: CreatePasswordViewModelInputs {
return self
}
public var outputs: CreatePasswordViewModelOutputs {
return self
}
}
// MARK: - Functions
private func formFieldsNotEmpty(_ pwds: (first: String, second: String)) -> Bool {
return !pwds.first.isEmpty && !pwds.second.isEmpty
}