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
import SwiftUI
#if DEBUG
#Preview("Sign in") {
PreviewHost(signedIn: false) { _ in SignInView() }
}
#endif
struct SignInView: View {
@Environment(AppModel.self) private var model
@Environment(\.accessibilityReduceMotion) private var reduceMotion
@State private var handle = ""
@State private var appPassword = ""
@State private var authFactorToken = ""
@FocusState private var focusedField: Field?
private enum Field { case handle, password, authFactorToken }
private var trimmedAuthFactorToken: String {
authFactorToken.trimmingCharacters(in: .whitespacesAndNewlines)
}
private var canSignIn: Bool {
!model.isSigningIn && !handle.isEmpty && !appPassword.isEmpty
&& (!model.requiresAuthFactorToken || !trimmedAuthFactorToken.isEmpty)
}
var body: some View {
NavigationStack {
ScrollView {
VStack(alignment: .leading, spacing: 26) {
VStack(alignment: .leading, spacing: 12) {
Image("BrandMark")
.resizable()
.scaledToFit()
.frame(width: 64, height: 64)
.accessibilityHidden(true)
Text("Sign in")
.appFont(.largeTitle, weight: .bold)
.accessibilityAddTraits(.isHeader)
Text("Use your Atmosphere account.")
.appFont(.body)
.foregroundStyle(.secondary)
.fixedSize(horizontal: false, vertical: true)
}
.padding(.top, 36)
VStack(spacing: 0) {
HStack(alignment: .center) {
TextField("Handle", text: $handle, prompt: Text("gee.sawra.social"))
.textContentType(.username)
.textInputAutocapitalization(.never)
.autocorrectionDisabled()
.keyboardType(.URL)
.submitLabel(.next)
.focused($focusedField, equals: .handle)
.onSubmit { focusedField = .password }
.accessibilityIdentifier("handleField")
.frame(maxWidth: .infinity, alignment: .leading)
}
.frame(height: 56)
Divider()
HStack(alignment: .center) {
SecureField("Password", text: $appPassword)
.textInputAutocapitalization(.never)
.autocorrectionDisabled()
.submitLabel(.go)
.focused($focusedField, equals: .password)
.onSubmit { Task { await signIn() } }
.accessibilityIdentifier("appPasswordField")
.frame(maxWidth: .infinity, alignment: .leading)
}
.frame(height: 56)
if model.requiresAuthFactorToken {
Divider()
HStack(alignment: .center) {
TextField("Email sign-in code", text: $authFactorToken)
.keyboardType(.asciiCapable)
.textContentType(.oneTimeCode)
.textInputAutocapitalization(.never)
.autocorrectionDisabled()
.submitLabel(.go)
.focused($focusedField, equals: .authFactorToken)
.onSubmit { Task { await signIn() } }
.accessibilityIdentifier("authFactorTokenField")
.frame(maxWidth: .infinity, alignment: .leading)
}
.frame(height: 56)
}
}
.padding(.horizontal, 16)
.background(.background, in: RoundedRectangle(cornerRadius: 14, style: .continuous))
.overlay(RoundedRectangle(cornerRadius: 14, style: .continuous).stroke(.quaternary))
.animation(reduceMotion ? nil : .easeInOut(duration: 0.25), value: model.requiresAuthFactorToken)
VStack(alignment: .leading, spacing: 14) {
Button {
Task { await signIn() }
} label: {
Text(model.requiresAuthFactorToken ? "Verify code" : "Continue")
.opacity(model.isSigningIn ? 0 : 1)
.overlay {
if model.isSigningIn {
ProgressView()
.tint(.white)
.accessibilityLabel("Signing in")
}
}
.frame(maxWidth: .infinity)
}
.buttonStyle(.borderedProminent)
.controlSize(.large)
.disabled(!canSignIn)
.accessibilityIdentifier("signInButton")
if model.requiresAuthFactorToken {
Text("Enter the code sent to your account email.")
.appFont(.footnote)
.foregroundStyle(.secondary)
.fixedSize(horizontal: false, vertical: true)
if let message = model.authFactorErrorMessage {
Text(message)
.appFont(.footnote)
.foregroundStyle(.red)
.accessibilityIdentifier("authFactorErrorMessage")
}
Button("Back") {
authFactorToken = ""
model.cancelAuthFactorSignIn()
}
.appFont(.footnote)
}
}
if ProcessInfo.processInfo.arguments.contains("-ui-testing") {
Button("Use demo account") {
Task { await model.signInWithDemo() }
}
.accessibilityIdentifier("demoSignInButton")
}
}
.padding(.horizontal, 24)
.padding(.bottom, 32)
.frame(maxWidth: 520)
.frame(maxWidth: .infinity)
}
.scrollDismissesKeyboard(.interactively)
.background(Color(uiColor: .systemGroupedBackground))
.toolbar(.hidden, for: .navigationBar)
}
}
private func signIn() async {
guard canSignIn else { return }
focusedField = nil
await model.signIn(
handle: handle,
appPassword: appPassword,
authFactorToken: model.requiresAuthFactorToken ? trimmedAuthFactorToken : nil
)
if model.requiresAuthFactorToken && trimmedAuthFactorToken.isEmpty {
focusedField = .authFactorToken
}
if model.account != nil {
appPassword = ""
authFactorToken = ""
}
}
}