oficina / base Public

Clone
git clone https://amadan.net/oficina/base

Plain git — no account needed to clone.

Download

Download this file

1import RastrilloApple
2import SwiftUI
3
4/// Signing in, on the rare device with no Oficina ID yet (the kit goes
5/// straight in otherwise): the kit's Oficina ID flow drawn in Base's look
6/// (mockups mac-signin, phone-signin). The sky; the wordmark; "Sign in to
7/// Base" and which account this is; then a glass card with the email field,
8/// Email me a code, or, and Sign in with a passkey. The code step says
9/// where the code went, and sends a new one. Errors are the kit's own.
10///
11/// The kit's model drives every step. The few that copy review hasn't
12/// drawn for Base (choosing among several IDs or teams, Continue as, no
13/// teams, Oficina ID unreachable) are the kit's own onboarding. P3: a
14/// person Base doesn't know yet (the kit's notMember) is told to sign in
15/// to Base on the web once.
16public struct BaseSignIn: View {
17 @ObservedObject var model: OficinaIDModel
18 let openURL: (URL) -> Void
19 @State private var typed = ""
20 @FocusState private var focused: Bool
21 @Environment(\.colorScheme) private var scheme
22
23 public init(model: OficinaIDModel, openURL: @escaping (URL) -> Void = { _ in }) {
24 self.model = model
25 self.openURL = openURL
26 }
27
28 public var body: some View {
29 switch model.step {
30 case .welcome, .email, .code, .passkey, .notMember, .loading:
31 door
32 default:
33 OficinaOnboardingView(model: model)
34 }
35 }
36
37 private var door: some View {
38 let p = Palette(scheme)
39 return ZStack {
40 Sky()
41 ScrollView {
42 VStack(spacing: 0) {
43 HStack(spacing: 10) {
44 BaseMark(size: 20)
45 Text(BaseText.text("native.wordmark")).font(BaseFont.heading(24, .semibold, relativeTo: .title2))
46 }
47 .accessibilityHidden(true)
48 .padding(.top, 54)
49 if model.step == .loading {
50 ProgressView().controlSize(.large).padding(.top, 60)
51 } else {
52 Text(heading).font(BaseFont.heading(30, .medium, relativeTo: .largeTitle))
53 .multilineTextAlignment(.center)
54 .padding(.top, 22).padding(.bottom, 6)
55 .accessibilityAddTraits(.isHeader)
56 Text(lede).font(BaseFont.body(15)).foregroundStyle(p[.haze])
57 .multilineTextAlignment(.center).frame(maxWidth: 360)
58 .padding(.bottom, 22)
59 card(p)
60 if case .code = model.step {
61 Text(BaseText.text("native.signin.code.passkey_next"))
62 .font(BaseFont.body(12.5, relativeTo: .footnote)).foregroundStyle(p[.haze])
63 .multilineTextAlignment(.center).frame(maxWidth: 380).padding(.top, 18)
64 }
65 }
66 }
67 .padding(.horizontal, 22).padding(.bottom, 22)
68 .frame(maxWidth: .infinity)
69 }
70 }
71 .foregroundStyle(p[.frost])
72 .onChange(of: model.error) { _, value in if let value { AccessibilityNotification.Announcement(value).post() } }
73 }
74
75 private var heading: String {
76 switch model.step {
77 case .code: BaseText.text("native.signin.code.title")
78 default: BaseText.text("native.signin.title")
79 }
80 }
81
82 private var lede: String {
83 switch model.step {
84 case .code(let email): BaseText.format("native.signin.code.lede", ["email": email])
85 case .notMember: BaseText.text("native.signin.unknown")
86 default: BaseText.text("native.signin.lede")
87 }
88 }
89
90 @ViewBuilder private func card(_ p: Palette) -> some View {
91 VStack(alignment: .leading, spacing: 10) {
92 switch model.step {
93 case .code:
94 TextField(text: $model.code, prompt: Text(verbatim: "000000")) { Text(BaseText.text("native.signin.code.title")) }
95 .textContentType(.oneTimeCode)
96 #if os(iOS)
97 .keyboardType(.numberPad)
98 #endif
99 .font(BaseFont.heading(24, .semibold).monospacedDigit())
100 .multilineTextAlignment(.center)
101 .modifier(FieldStyle())
102 .focused($focused)
103 .onAppear { focused = true }
104 .onChange(of: model.code) { _, value in
105 if value.filter(\.isNumber).count == 6, !model.busy { Task { await model.submitCode() } }
106 }
107 .onSubmit { Task { await model.submitCode() } }
108 ResendButton(availableAt: model.resendAvailableAt, busy: model.busy) { Task { await model.resend() } }
109 case .notMember(let team):
110 Button { if let base = team.origin(for: "base") { openURL(base.appendingPathComponent("signin")) } } label: {
111 Text(BaseText.text("native.signin.unknown.button"))
112 }
113 .buttonStyle(BaseButtonStyle(.primary, expands: true))
114 case .passkey:
115 passkeyButton(primary: true)
116 default:
117 Text(BaseText.text("native.signin.email.label")).font(BaseFont.body(13, .semibold))
118 TextField(text: $typed, prompt: Text(verbatim: "")) { Text(BaseText.text("native.signin.email.label")) }
119 .textContentType(.emailAddress)
120 #if os(iOS)
121 .keyboardType(.emailAddress).textInputAutocapitalization(.never)
122 #endif
123 .autocorrectionDisabled()
124 .font(BaseFont.body(16))
125 .modifier(FieldStyle())
126 .focused($focused)
127 .onSubmit(sendCode)
128 Button(action: sendCode) { Text(BaseText.text("native.signin.email.send")) }
129 .buttonStyle(BaseButtonStyle(.primary, expands: true))
130 .disabled(typed.trimmingCharacters(in: .whitespaces).isEmpty)
131 HStack(spacing: 10) {
132 Rectangle().fill(p[.rule]).frame(height: 1)
133 Text(BaseText.text("native.signin.or")).font(BaseFont.body(13)).foregroundStyle(p[.haze])
134 Rectangle().fill(p[.rule]).frame(height: 1)
135 }
136 passkeyButton(primary: false)
137 }
138 if model.busy { ProgressView().controlSize(.small).frame(maxWidth: .infinity) }
139 // The kit's own words for what went wrong (the coordinator's
140 // ruling: they stay the kit's).
141 if let error = model.error { OficinaErrorLine(error) }
142 }
143 .disabled(model.busy)
144 .padding(18)
145 .frame(maxWidth: 380)
146 .glass(cornerRadius: 20)
147 }
148
149 private func passkeyButton(primary: Bool) -> some View {
150 Button { Task { await model.usePasskey() } } label: {
151 HStack(spacing: 8) {
152 Image(systemName: "person.badge.key").accessibilityHidden(true)
153 Text(BaseText.text("native.signin.passkey"))
154 }
155 }
156 .buttonStyle(BaseButtonStyle(primary ? .primary : .ghost, expands: true))
157 }
158
159 private func sendCode() {
160 let email = typed.trimmingCharacters(in: .whitespacesAndNewlines)
161 guard !email.isEmpty else { return }
162 if model.step == .welcome { model.continueWithEmail() }
163 model.email = email
164 Task { await model.submitEmail() }
165 }
166}
167
168/// The web's field (`.input`): 44 pt, 10 pt corners, its fill and edge.
169struct FieldStyle: ViewModifier {
170 @Environment(\.colorScheme) private var scheme
171 func body(content: Content) -> some View {
172 let p = Palette(scheme)
173 content
174 .textFieldStyle(.plain)
175 .padding(.horizontal, 14).frame(minHeight: 44)
176 .background(p[.field], in: RoundedRectangle(cornerRadius: 10, style: .continuous))
177 .overlay { RoundedRectangle(cornerRadius: 10, style: .continuous).strokeBorder(p[.fieldEdge], lineWidth: 1) }
178 }
179}
180
181/// Send a new code: disabled until the kit's `resendAvailableAt`, then
182/// enabled on time by itself, with nothing else happening on screen (a
183/// cancellable wait for the deadline, as the kit's own control redraws
184/// on its timeline).
185struct ResendButton: View {
186 let availableAt: Date?
187 let busy: Bool
188 let action: () -> Void
189 /// Hears whether it is enabled, each time that changes (tests).
190 var probe: ((Bool) -> Void)?
191 @State private var now = Date()
192
193 var waiting: Bool { availableAt.map { $0 > now } ?? false }
194
195 var body: some View {
196 Button(action: action) { Text(BaseText.text("native.signin.code.resend")) }
197 .buttonStyle(BaseButtonStyle(.ghost, expands: true))
198 .disabled(busy || waiting)
199 .onChange(of: busy || waiting, initial: true) { _, off in probe?(!off) }
200 .task(id: availableAt) {
201 now = Date()
202 while let deadline = availableAt, deadline > now {
203 do { try await Task.sleep(for: .seconds(max(0.05, deadline.timeIntervalSinceNow))) } catch { return }
204 now = Date()
205 }
206 }
207 }
208}
209