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 = "" } } }