-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
ChangeEmailView.swift
235 lines (216 loc) · 7.44 KB
/
ChangeEmailView.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
import Combine
import Library
import SwiftUI
enum FocusField {
case newEmail
case currentPassword
}
@available(iOS 15.0, *)
struct ChangeEmailView: View {
@SwiftUI.Environment(\.defaultMinListRowHeight) var minListRow
@FocusState private var focusField: FocusField?
private let contentPadding = 12.0
@ObservedObject private var reactiveViewModel = ChangeEmailViewModel_SwiftUIIntegrationTest()
@State private var retrievedEmailText = ""
@State private var newEmailText = ""
@State private var newPasswordText = ""
@State private var saveEnabled = false
@State private var saveTriggered = false
@State private var showLoading = false
@State private var showBannerMessage = false
@State private var bannerMessage: MessageBannerViewViewModel?
var body: some View {
GeometryReader { proxy in
List {
Color(.ksr_support_100)
.frame(maxWidth: .infinity, maxHeight: minListRow, alignment: .center)
.listRowSeparator(.hidden)
.listRowInsets(EdgeInsets())
VStack(alignment: .center, spacing: 0) {
InputFieldView(
titleText: Strings.Current_email(),
secureField: false,
placeholderText: "",
contentPadding: contentPadding,
valueText: $retrievedEmailText
)
.currentEmail()
.onReceive(reactiveViewModel.retrievedEmailText) { newValue in
retrievedEmailText = newValue
}
Color(.ksr_cell_separator).frame(maxWidth: .infinity, maxHeight: 1)
}
.listRowSeparator(.hidden)
.listRowInsets(EdgeInsets())
if !reactiveViewModel.hideMessageLabel {
warningLabel(
text: reactiveViewModel.warningMessageWithAlert.0,
reactiveViewModel.warningMessageWithAlert.1
)
.frame(maxWidth: .infinity, maxHeight: minListRow, alignment: .leading)
.background(Color(.ksr_support_100))
.listRowSeparator(.hidden)
.listRowInsets(EdgeInsets())
}
if !reactiveViewModel.hideVerifyView {
VStack(alignment: .leading, spacing: 0) {
Button(reactiveViewModel.verifyEmailButtonTitle) {
reactiveViewModel.inputs.resendVerificationEmailButtonTapped()
}
.font(Font(UIFont.ksr_body()))
.foregroundColor(Color(.ksr_create_700))
.padding(contentPadding)
.disabled(showLoading)
Color(.ksr_cell_separator).frame(maxWidth: .infinity, maxHeight: 1)
}
.listRowSeparator(.hidden)
.listRowInsets(EdgeInsets())
}
Color(.ksr_support_100)
.frame(maxWidth: .infinity, maxHeight: minListRow, alignment: .center)
.listRowSeparator(.hidden)
.listRowInsets(EdgeInsets())
VStack(alignment: .center, spacing: 0) {
InputFieldView(
titleText: Strings.New_email(),
secureField: false,
placeholderText: Strings.login_placeholder_email(),
contentPadding: contentPadding,
valueText: $newEmailText
)
.onReceive(reactiveViewModel.resetEditableText) { newValue in
if newValue {
newEmailText = ""
}
}
.onChange(of: newEmailText) { newValue in
reactiveViewModel.newEmailText.send(newValue)
}
.newEmail(editable: !showLoading)
.focused($focusField, equals: .newEmail)
.onSubmit {
focusField = .currentPassword
}
InputFieldView(
titleText: Strings.Current_password(),
secureField: true,
placeholderText: Strings.login_placeholder_password(),
contentPadding: contentPadding,
valueText: $newPasswordText
)
.onChange(of: newPasswordText) { newValue in
reactiveViewModel.currentPasswordText.send(newValue)
}
.currentPassword(editable: !showLoading)
.focused($focusField, equals: .currentPassword)
.onReceive(reactiveViewModel.resetEditableText) { resetFlag in
if resetFlag {
newPasswordText = ""
}
}
// FIXME: So "Done" on keyboard doesn't trigger Save --> in the future we might want to add this (was in the old `ChangeEmailViewController`)
Color(.ksr_cell_separator).frame(maxWidth: .infinity, maxHeight: 1)
}
.listRowSeparator(.hidden)
.listRowInsets(EdgeInsets())
}
.navigationTitle(Strings.Change_email())
.background(Color(.ksr_support_100))
.listStyle(.plain)
.toolbar {
ToolbarItem(placement: .navigationBarTrailing) {
LoadingBarButtonItem(
saveEnabled: $saveEnabled,
saveTriggered: $saveTriggered,
showLoading: $showLoading,
titleText: Strings.Save()
)
.onReceive(reactiveViewModel.saveButtonEnabled) { newValue in
saveEnabled = newValue
}
.onReceive(reactiveViewModel.resetEditableText) { newValue in
showLoading = !newValue
}
.onChange(of: saveTriggered) { newValue in
focusField = nil
reactiveViewModel.saveTriggered.send(newValue)
}
}
}
.overlay(alignment: .bottom) {
MessageBannerView(viewModel: $bannerMessage)
.frame(
minWidth: proxy.size.width,
idealWidth: proxy.size.width,
maxHeight: proxy.size.height / 5,
alignment: .bottom
)
.animation(.easeInOut)
}
.onReceive(reactiveViewModel.bannerMessage) { newValue in
bannerMessage = newValue
}
.onAppear {
reactiveViewModel.inputs.viewDidLoad()
}
}
}
private struct InputFieldView: View {
var titleText: String
var secureField: Bool
var placeholderText: String
var contentPadding: CGFloat
var valueText: Binding<String>
var body: some View {
HStack {
Text(titleText)
.frame(
maxWidth: .infinity,
alignment: .leading
)
.font(Font(UIFont.ksr_body()))
.foregroundColor(Color(.ksr_support_700))
Spacer()
InputFieldUserInputView(
secureField: secureField,
placeholderText: placeholderText,
valueText: valueText
)
}
.padding(contentPadding)
.accessibilityElement(children: .combine)
.accessibilityLabel(titleText)
}
}
private struct InputFieldUserInputView: View {
var secureField: Bool
var placeholderText: String
var valueText: Binding<String>
var body: some View {
if secureField {
SecureField(
"",
text: valueText,
prompt: Text(placeholderText).foregroundColor(Color(.ksr_support_400))
)
} else {
TextField(
"",
text: valueText,
prompt:
Text(placeholderText).foregroundColor(Color(.ksr_support_400))
)
}
}
}
@ViewBuilder
private func warningLabel(text: String, _ alert: Bool) -> some View {
let textColor = alert ? Color(.ksr_alert) : Color(.ksr_support_400)
Label(text, systemImage: "exclamationmark.triangle.fill")
.labelStyle(.titleOnly)
.font(Font(UIFont.ksr_body(size: 13)))
.lineLimit(nil)
.padding([.leading, .trailing], self.contentPadding)
.foregroundColor(textColor)
}
}