import RastrilloApple import SwiftUI /// Signing in, on the rare device with no Oficina ID yet (the kit goes /// straight in otherwise): the kit's Oficina ID flow drawn in Base's look /// (mockups mac-signin, phone-signin). The sky; the wordmark; "Sign in to /// Base" and which account this is; then a glass card with the email field, /// Email me a code, or, and Sign in with a passkey. The code step says /// where the code went, and sends a new one. Errors are the kit's own. /// /// The kit's model drives every step. The few that copy review hasn't /// drawn for Base (choosing among several IDs or teams, Continue as, no /// teams, Oficina ID unreachable) are the kit's own onboarding. P3: a /// person Base doesn't know yet (the kit's notMember) is told to sign in /// to Base on the web once. public struct BaseSignIn: View { @ObservedObject var model: OficinaIDModel let openURL: (URL) -> Void @State private var typed = "" @FocusState private var focused: Bool @Environment(\.colorScheme) private var scheme public init(model: OficinaIDModel, openURL: @escaping (URL) -> Void = { _ in }) { self.model = model self.openURL = openURL } public var body: some View { switch model.step { case .welcome, .email, .code, .passkey, .notMember, .loading: door default: OficinaOnboardingView(model: model) } } private var door: some View { let p = Palette(scheme) return ZStack { Sky() ScrollView { VStack(spacing: 0) { HStack(spacing: 10) { BaseMark(size: 20) Text(BaseText.text("native.wordmark")).font(BaseFont.heading(24, .semibold, relativeTo: .title2)) } .accessibilityHidden(true) .padding(.top, 54) if model.step == .loading { ProgressView().controlSize(.large).padding(.top, 60) } else { Text(heading).font(BaseFont.heading(30, .medium, relativeTo: .largeTitle)) .multilineTextAlignment(.center) .padding(.top, 22).padding(.bottom, 6) .accessibilityAddTraits(.isHeader) Text(lede).font(BaseFont.body(15)).foregroundStyle(p[.haze]) .multilineTextAlignment(.center).frame(maxWidth: 360) .padding(.bottom, 22) card(p) if case .code = model.step { Text(BaseText.text("native.signin.code.passkey_next")) .font(BaseFont.body(12.5, relativeTo: .footnote)).foregroundStyle(p[.haze]) .multilineTextAlignment(.center).frame(maxWidth: 380).padding(.top, 18) } } } .padding(.horizontal, 22).padding(.bottom, 22) .frame(maxWidth: .infinity) } } .foregroundStyle(p[.frost]) .onChange(of: model.error) { _, value in if let value { AccessibilityNotification.Announcement(value).post() } } } private var heading: String { switch model.step { case .code: BaseText.text("native.signin.code.title") default: BaseText.text("native.signin.title") } } private var lede: String { switch model.step { case .code(let email): BaseText.format("native.signin.code.lede", ["email": email]) case .notMember: BaseText.text("native.signin.unknown") default: BaseText.text("native.signin.lede") } } @ViewBuilder private func card(_ p: Palette) -> some View { VStack(alignment: .leading, spacing: 10) { switch model.step { case .code: TextField(text: $model.code, prompt: Text(verbatim: "000000")) { Text(BaseText.text("native.signin.code.title")) } .textContentType(.oneTimeCode) #if os(iOS) .keyboardType(.numberPad) #endif .font(BaseFont.heading(24, .semibold).monospacedDigit()) .multilineTextAlignment(.center) .modifier(FieldStyle()) .focused($focused) .onAppear { focused = true } .onChange(of: model.code) { _, value in if value.filter(\.isNumber).count == 6, !model.busy { Task { await model.submitCode() } } } .onSubmit { Task { await model.submitCode() } } ResendButton(availableAt: model.resendAvailableAt, busy: model.busy) { Task { await model.resend() } } case .notMember(let team): Button { if let base = team.origin(for: "base") { openURL(base.appendingPathComponent("signin")) } } label: { Text(BaseText.text("native.signin.unknown.button")) } .buttonStyle(BaseButtonStyle(.primary, expands: true)) case .passkey: passkeyButton(primary: true) default: Text(BaseText.text("native.signin.email.label")).font(BaseFont.body(13, .semibold)) TextField(text: $typed, prompt: Text(verbatim: "")) { Text(BaseText.text("native.signin.email.label")) } .textContentType(.emailAddress) #if os(iOS) .keyboardType(.emailAddress).textInputAutocapitalization(.never) #endif .autocorrectionDisabled() .font(BaseFont.body(16)) .modifier(FieldStyle()) .focused($focused) .onSubmit(sendCode) Button(action: sendCode) { Text(BaseText.text("native.signin.email.send")) } .buttonStyle(BaseButtonStyle(.primary, expands: true)) .disabled(typed.trimmingCharacters(in: .whitespaces).isEmpty) HStack(spacing: 10) { Rectangle().fill(p[.rule]).frame(height: 1) Text(BaseText.text("native.signin.or")).font(BaseFont.body(13)).foregroundStyle(p[.haze]) Rectangle().fill(p[.rule]).frame(height: 1) } passkeyButton(primary: false) } if model.busy { ProgressView().controlSize(.small).frame(maxWidth: .infinity) } // The kit's own words for what went wrong (the coordinator's // ruling: they stay the kit's). if let error = model.error { OficinaErrorLine(error) } } .disabled(model.busy) .padding(18) .frame(maxWidth: 380) .glass(cornerRadius: 20) } private func passkeyButton(primary: Bool) -> some View { Button { Task { await model.usePasskey() } } label: { HStack(spacing: 8) { Image(systemName: "person.badge.key").accessibilityHidden(true) Text(BaseText.text("native.signin.passkey")) } } .buttonStyle(BaseButtonStyle(primary ? .primary : .ghost, expands: true)) } private func sendCode() { let email = typed.trimmingCharacters(in: .whitespacesAndNewlines) guard !email.isEmpty else { return } if model.step == .welcome { model.continueWithEmail() } model.email = email Task { await model.submitEmail() } } } /// The web's field (`.input`): 44 pt, 10 pt corners, its fill and edge. struct FieldStyle: ViewModifier { @Environment(\.colorScheme) private var scheme func body(content: Content) -> some View { let p = Palette(scheme) content .textFieldStyle(.plain) .padding(.horizontal, 14).frame(minHeight: 44) .background(p[.field], in: RoundedRectangle(cornerRadius: 10, style: .continuous)) .overlay { RoundedRectangle(cornerRadius: 10, style: .continuous).strokeBorder(p[.fieldEdge], lineWidth: 1) } } } /// Send a new code: disabled until the kit's `resendAvailableAt`, then /// enabled on time by itself, with nothing else happening on screen (a /// cancellable wait for the deadline, as the kit's own control redraws /// on its timeline). struct ResendButton: View { let availableAt: Date? let busy: Bool let action: () -> Void /// Hears whether it is enabled, each time that changes (tests). var probe: ((Bool) -> Void)? @State private var now = Date() var waiting: Bool { availableAt.map { $0 > now } ?? false } var body: some View { Button(action: action) { Text(BaseText.text("native.signin.code.resend")) } .buttonStyle(BaseButtonStyle(.ghost, expands: true)) .disabled(busy || waiting) .onChange(of: busy || waiting, initial: true) { _, off in probe?(!off) } .task(id: availableAt) { now = Date() while let deadline = availableAt, deadline > now { do { try await Task.sleep(for: .seconds(max(0.05, deadline.timeIntervalSinceNow))) } catch { return } now = Date() } } } }