405 lines · 21.0 KB · SwiftRaw
// The unlock card and the sign-in approval, drawn in Vault's look when they// stand inside Cotton Vault. The models are the shell's own (`UnlockAsks`,// `SignInAsks`): the same reads, the same Allow, Not now, Approve and Refuse;// only the drawing is Vault's. `UnlockAsksLayer` and `SignInAskBanner` hand// over to these when the look is set.#if os(iOS)import CottonKitimport CottonValuesimport SwiftUI/// The unlock request standing above the screen: the card's places (title, asker,/// reason, the tables with their switches, the lengths, how long it waits,/// who else waits), its status, and its acts.struct VaultUnlockCard: View { let asks: UnlockAsks let card: DeckCard let look: VaultLook @Environment(\.colorScheme) private var scheme private func said(_ key: String) -> String? { if case .text(let text)? = card.fields[key], !text.isEmpty { return text } return nil } private var toggles: [(table: String, label: String, on: Bool)] { guard case .list(let rows)? = card.fields["toggles"] else { return [] } return rows.compactMap { row in guard case .object(let toggle) = row, let table = toggle["table"]?.text else { return nil } var on = true if case .bool(let value)? = toggle["on"] { on = value } return (table, toggle["label"]?.text ?? table, on) } } var body: some View { VaultStandingCard(warm: true) { if let title = said("title") { Text(title).vault(look, "headline").fixedSize(horizontal: false, vertical: true).accessibilityAddTraits(.isHeader) } if let asker = said("asker") { Text(asker).vault(look, "row_detail", "secondary") } if let reason = said("reason") { Text(reason).vault(look, "body").fixedSize(horizontal: false, vertical: true) } if !toggles.isEmpty { VStack(spacing: 0) { ForEach(toggles, id: \.table) { toggle in VaultSwitch(label: toggle.label, on: toggle.on) { asks.flip(toggle.table) } .accessibilityIdentifier("unlock-toggle-\(toggle.table)") } } } if !asks.durations.isEmpty, !asks.acts.isEmpty { ScrollView(.horizontal, showsIndicators: false) { HStack(spacing: look.gap("s")) { ForEach(asks.durations, id: \.minutes) { duration in VaultChipView(chip: VaultChip(id: "\(duration.minutes)", label: duration.label, count: 0, on: duration.minutes == asks.chosen)) { asks.choose(duration) } .accessibilityIdentifier("unlock-duration-\(duration.minutes)") } } } } ForEach(["expires", "queue"], id: \.self) { key in if let line = said(key) { Text(line).vault(look, "label", "secondary") } } if let status = asks.status { Text(status).vault(look, "row_detail", "attention").fixedSize(horizontal: false, vertical: true) .accessibilityIdentifier("unlock-status") } if !asks.acts.isEmpty { HStack(spacing: look.gap("s")) { ForEach(asks.acts, id: \.id) { act in VaultStandingAct(label: act.label) { Task { await asks.act(act) } } .disabled(asks.busy) .accessibilityIdentifier("unlock-act-\(act.id)") } } } } .environment(\.vault, look) .accessibilityIdentifier("unlock-card") }}/// A new device asking to sign in, in the app's own look (GETTING-IN.md): who asks, Approve (the lock, then the/// camera) and Not me; the digits once Type the code instead was chosen; or, on a device that cannot hand over the/// key, the one line saying why, with no button.struct VaultSignInBanner: View { @Bindable var asks: SignInAsks let look: VaultLook @Environment(\.colorScheme) private var scheme @Environment(\.dynamicTypeSize) private var size var body: some View { Group { if let cannot = asks.cannot { VaultStandingCard(warm: true) { Text(cannot.title).vault(look, "headline").fixedSize(horizontal: false, vertical: true) Text(cannot.line).vault(look, "row_detail", "secondary").fixedSize(horizontal: false, vertical: true) .accessibilityIdentifier("signin-cannot-line") } .accessibilityIdentifier("signin-cannot") } else if let ask = asks.ask { card(ask) } } .environment(\.vault, look) } @ViewBuilder private func card(_ ask: SignInAsk) -> some View { VaultStandingCard(warm: true) { Text(ask.title).vault(look, "headline").fixedSize(horizontal: false, vertical: true) .accessibilityIdentifier("signin-ask-line") // At the largest text the band above the app holds only so much: Approve and Not me come right under the // title, the sentence after them (they stood cut off under Words' header on the walk, 23 Nov 2026). if size.isAccessibilitySize { acts(ask) } if !ask.sub.isEmpty { Text(ask.sub).vault(look, "row_detail", "secondary").fixedSize(horizontal: false, vertical: true) } if let wrong = asks.wrong, !asks.scanning, !asks.typing { Text(wrong).vault(look, "row_detail", "danger").fixedSize(horizontal: false, vertical: true) .accessibilityIdentifier("signin-ask-wrong") } if !size.isAccessibilitySize { acts(ask) } } .accessibilityIdentifier("signin-ask-banner") // Typing the code takes a screen of its own: in the band above the app the field, its sentence and Approve // stood cut off under the number pad, and at the largest text (iPhone walk, 23 Nov 2026). .fullScreenCover(isPresented: Binding(get: { asks.typing }, set: { if !$0 { asks.stopTyping() } })) { VaultTypeCode(asks: asks, ask: ask, look: look) } .fullScreenCover(isPresented: $asks.confirming) { VaultConfirmView(asking: VaultAsking( confirm: VaultConfirm(title: ask.notMeConfirm, body: "", yes: ask.notMe, no: ""), tone: .danger) { yes in asks.confirming = false if yes { Task { await asks.refuse() } } }) .environment(\.vault, look) .presentationBackground(.clear) } }}extension VaultSignInBanner { @ViewBuilder fileprivate func acts(_ ask: SignInAsk) -> some View { ViewThatFits(in: .horizontal) { HStack(spacing: look.gap("s")) { cardActs(ask) } VStack(alignment: .leading, spacing: look.gap("s")) { cardActs(ask) } } } @ViewBuilder fileprivate func cardActs(_ ask: SignInAsk) -> some View { VaultStandingAct(label: ask.approve) { Task { await asks.approve() } } .disabled(asks.busy) .accessibilityIdentifier("signin-ask-approve") VaultStandingAct(label: ask.notMe) { asks.confirming = true } .disabled(asks.busy) .accessibilityIdentifier("signin-ask-refuse") }}/// The code typed on a screen of its own: the card's title, the heading (or the wait for the digits), the field/// with the number pad up, the note or the wrong-code sentence, Approve; the close goes back to the card.private struct VaultTypeCode: View { @Bindable var asks: SignInAsks let ask: SignInAsk let look: VaultLook @Environment(\.colorScheme) private var scheme @FocusState private var focused: Bool var body: some View { ScrollViewReader { proxy in ScrollView { VStack(alignment: .leading, spacing: look.gap("l")) { Text(ask.title).vault(look, "headline").fixedSize(horizontal: false, vertical: true) Text(ask.comparing ? ask.typeHeading : ask.typeWaiting).vault(look, "row_detail", "secondary") .fixedSize(horizontal: false, vertical: true) if ask.comparing { VaultTextField(placeholder: "", text: $asks.typed, mono: true) .keyboardType(.numberPad) .textContentType(.oneTimeCode) .autocorrectionDisabled() .focused($focused) .accessibilityLabel(Text(ask.typeHeading)) .accessibilityIdentifier("signin-ask-code") } if let note = asks.note, asks.wrong == nil { Text(note).vault(look, "label", "secondary").fixedSize(horizontal: false, vertical: true) .accessibilityIdentifier("signin-ask-note") } if let wrong = asks.wrong { Text(wrong).vault(look, "row_detail", "danger").fixedSize(horizontal: false, vertical: true) .accessibilityIdentifier("signin-ask-wrong") } if ask.comparing { VaultButton(label: ask.approve, tone: .accent, fill: .main, wide: true) { Task { await asks.approveTyped() } } .disabled(!asks.ready) .accessibilityIdentifier("signin-ask-approve-typed") } Color.clear.frame(height: 1).id("type-end") } .padding(look.gap("gutter")) } .scrollDismissesKeyboard(.never) // A wrong code's sentence and Approve stay in view above the number pad (they stood under it at the largest // text on the walk, 23 Nov 2026). .onChange(of: asks.wrong) { _, _ in withAnimation { proxy.scrollTo("type-end", anchor: .bottom) } } .onChange(of: focused) { _, now in if now { withAnimation { proxy.scrollTo("type-end", anchor: .bottom) } } } } // The close stands in the safe area's own bar, never under the clock and battery. .safeAreaInset(edge: .top, spacing: 0) { HStack { Spacer() VaultButton(label: ShellLines.line("chrome", "sheet.close"), fill: .plain) { asks.stopTyping() } .accessibilityIdentifier("signin-ask-type-close") } .padding(.horizontal, look.gap("gutter") - look.gap("s")) .background(look.colour("background", scheme)) } .accessibilityAddTraits(.isModal) .background(look.colour("background", scheme).ignoresSafeArea()) .environment(\.vault, look) .onAppear { focused = true } .onChange(of: ask.comparing) { _, now in if now { focused = true } } }}/// Sign in (phone number, then a code; or approve from a signed-in device,/// or a passkey), standing on Vault's ground with no slip around it: the same model and panes as every/// app's sheet (`CustomerSheets`), only the drawing is Vault's.struct VaultSignInSheet<Model: PhoneSignInModel>: View { @Bindable var model: Model let look: VaultLook @Environment(\.colorScheme) private var scheme /// The field the keyboard opens on: the number when the sheet comes, the code once it is sent (the iPhone walk, /// 24 Nov 2026: the sheet came with no keyboard, and a tap on the field was owed before anything could be typed). private enum Field: Hashable { case number, code, name } @FocusState private var field: Field? private static func named(_ text: String?) -> String { ShellLines.said(text ?? "", as: ShellLines.speaker) } private var typed: Bool { !model.phone.trimmingCharacters(in: .whitespaces).isEmpty } private var paneField: Field? { switch model.pane { case .number: .number case .code: .code case .name: .name default: nil } } var body: some View { VStack(spacing: 0) { HStack { Spacer() VaultButton(label: VaultWords.line("edit.cancel"), fill: .plain) { model.asking = false } .accessibilityIdentifier("signin-close") } .padding(.horizontal, look.gap("gutter") - look.gap("s")) ScrollView { VStack(alignment: .leading, spacing: look.gap("l")) { switch model.pane { case .number: labelled(HomeCustomerWords.numberHeading) { VaultTextField(placeholder: HomeCustomerWords.numberHint, text: $model.phone) .keyboardType(.phonePad) .textContentType(.telephoneNumber) .focused($field, equals: .number) .accessibilityIdentifier("home-customer-phone") } // The number, then a code by text; beside it, approval from a device already signed in (NEXT-SIGNIN // 3, owner, 28 Nov 2026). A passkey stays where one is offered. if model.offersOtherWays { VaultButton(label: HomeCustomerWords.send, tone: .accent, fill: .main, wide: true) { Task { await model.askCode() } } .disabled(!typed || model.signing) .accessibilityIdentifier("home-customer-send") VaultButton(label: SignInWords.ask, fill: .quiet, wide: true) { Task { await model.askAnother() } } .disabled(!typed || model.signing) .accessibilityIdentifier("signin-ask") if model.offersPasskey { VaultButton(label: SignInWords.passkey, fill: .quiet, wide: true) { Task { await model.passkey() } } .disabled(model.signing) .accessibilityIdentifier("signin-passkey") } } else { VaultButton(label: HomeCustomerWords.send, tone: .accent, fill: .main, wide: true) { Task { await model.askCode() } } .disabled(!typed || model.signing) .accessibilityIdentifier("home-customer-send") } case .asking: if let card = model.askCard { asking(card, model.askPolled) } case .deleting(let setToGo): // Set to be deleted: the core's words, Keep my account (the way out, solid) and Leave it (outlined). Text(setToGo.words.heading).vault(look, "title").fixedSize(horizontal: false, vertical: true) .accessibilityAddTraits(.isHeader) Text(setToGo.words.line).vault(look, "body").fixedSize(horizontal: false, vertical: true) VaultButton(label: setToGo.words.keep, tone: .accent, fill: .main, wide: true) { Task { await model.keepAccount(setToGo) } } .disabled(model.signing) .accessibilityIdentifier("signin-keep-account") VaultButton(label: setToGo.words.leave, tone: .neutral, fill: .quiet, wide: true) { Task { await model.leaveAccount(setToGo) } } .disabled(model.signing) .accessibilityIdentifier("signin-leave-account") case .code(let challenge): if let sentTo = challenge.sentTo, !sentTo.isEmpty { Text(verbatim: sentTo).vault(look, "body") .fixedSize(horizontal: false, vertical: true) .accessibilityIdentifier("home-customer-sent-to") } labelled(HomeCustomerWords.codeHeading) { VaultTextField(placeholder: HomeCustomerWords.codeHint, text: $model.code, mono: true) .keyboardType(.numberPad) .textContentType(.oneTimeCode) .focused($field, equals: .code) .accessibilityIdentifier("home-customer-code") } VaultButton(label: HomeCustomerWords.confirm, tone: .accent, fill: .main, wide: true) { Task { await model.confirm(challenge) } } .disabled(!HomeCustomerKnobs.codeReady(model.code) || model.signing) .accessibilityIdentifier("home-customer-confirm") VaultButton(label: HomeCustomerWords.sendAgain, tone: .neutral, fill: .quiet, wide: true) { Task { await model.askCode() } } .disabled(model.signing) .accessibilityIdentifier("home-customer-send-again") case .name(let challenge): // Only for a number with no account, once its code is proved (NEXT-SIGNIN). labelled(HomeCustomerWords.nameHeading) { VaultTextField(placeholder: HomeCustomerWords.nameHint(model.business), text: $model.name) .textContentType(.name) .focused($field, equals: .name) .accessibilityIdentifier("home-customer-name") } VaultButton(label: HomeCustomerWords.confirm, tone: .accent, fill: .main, wide: true) { Task { await model.confirm(challenge) } } .disabled(model.name.trimmingCharacters(in: .whitespaces).isEmpty || model.signing) .accessibilityIdentifier("home-customer-name-confirm") } if let refusal = model.signInRefusal { Text(refusal).vault(look, "row_detail", "danger").fixedSize(horizontal: false, vertical: true) .accessibilityIdentifier("home-customer-refusal") } } .padding(.horizontal, look.gap("gutter")) .onChange(of: model.phone) { model.clearRefusal() } .onChange(of: model.code) { model.clearRefusal() } .onAppear { field = paneField } .onChange(of: paneField) { _, now in field = now } .padding(.vertical, look.gap("xl")) } } .background(look.colour("background", scheme).ignoresSafeArea()) .environment(\.vault, look) } private func labelled<Field: View>(_ label: String, @ViewBuilder _ field: () -> Field) -> some View { VStack(alignment: .leading, spacing: look.gap("xs")) { Text(label).vault(look, "label", "secondary") field() } } /// Approve from a signed-in device: the ask's heading, the compare code /// once the other device shows one, how long it waits, and its ways out. @ViewBuilder private func asking(_ card: JSONValue, _ polled: JSONValue?) -> some View { let done = polled?.flag("done") == true let approved = polled?.text("state") == "approved" Text(Self.named(card.text("heading"))).vault(look, "title").fixedSize(horizontal: false, vertical: true) if polled?.text("code") == nil, !done, let rows = AskingCard.square(card) { SquareCodeView(rows: rows) .frame(width: HomeCustomerKnobs.squareSide, height: HomeCustomerKnobs.squareSide) .accessibilityIdentifier("signin-ask-square") } if let code = polled?.text("code") { Text(Self.named(polled?.text("line"))).vault(look, "body").fixedSize(horizontal: false, vertical: true) Text(code).vault(look, "code").accessibilityIdentifier("signin-ask-code") Text(Self.named(polled?.text("check"))).vault(look, "row_detail", "secondary").fixedSize(horizontal: false, vertical: true) } else { Text(Self.named(polled?.text("line") ?? card.text("line"))) .vault(look, "body", done && !approved ? "danger" : "text") .fixedSize(horizontal: false, vertical: true) .accessibilityIdentifier("signin-ask-state") } if let left = polled.map({ $0.text("left") }) ?? card.text("left") { Text(left).vault(look, "label", "tertiary") } if done && !approved { VaultButton(label: polled?.text("again") ?? "", tone: .accent, fill: .main, wide: true) { Task { await model.askAnother() } } .accessibilityIdentifier("signin-ask-again") VaultButton(label: polled?.text("sendCode") ?? card.text("sendCode") ?? "", fill: .quiet, wide: true) { model.cancelAsk() Task { await model.askCode() } } } else if !approved { VaultButton(label: card.text("sendCode") ?? "", fill: .quiet, wide: true) { model.cancelAsk() Task { await model.askCode() } } VaultButton(label: card.text("cancel") ?? "", fill: .plain) { model.cancelAsk() } .accessibilityIdentifier("signin-ask-cancel") } }}#endif