Cotton code
301 lines · 14.5 KB · SwiftRaw
// The shell's standing cards drawn in Vault's look while Vault stands// (SHAPES "Decided" 6 and 10; Android's VaultCardLook): the sealed key, a// device approval, the recovery offer, a helper's ask, read-only and an// assistant's unlock request. Each stands above the screen and pushes it down// (the root's top inset), never floats over it. Words, acts and their order// stay the cards' own models': only the drawing is Vault's.#if os(iOS)import CottonKitimport CottonValuesimport SwiftUIimport UniformTypeIdentifiers/// One standing card: gutter-padded, the card corner, on the surface (or the/// attention wash when it asks a yes or no).struct VaultStandingCard<Content: View>: View {    var warm = false    @ViewBuilder let content: Content    @Environment(\.vault) private var look    @Environment(\.colorScheme) private var scheme    var body: some View {        VStack(alignment: .leading, spacing: look.gap("m")) { content }            .padding(look.gap("l"))            .frame(maxWidth: .infinity, alignment: .leading)            .background(RoundedRectangle(cornerRadius: look.corner("card"), style: .continuous)                .fill(look.colour(warm ? "attention_wash" : "surface", scheme)))            .padding(.horizontal, look.gap("gutter"))            .padding(.vertical, look.gap("s"))    }}/// A standing card's act: the raised fill, in `text`.struct VaultStandingAct: View {    let label: String    let action: () -> Void    @Environment(\.vault) private var look    @Environment(\.colorScheme) private var scheme    @Environment(\.isEnabled) private var enabled    @Environment(\.dynamicTypeSize) private var size    var body: some View {        Button(action: action) {            // Two lines before it cuts: "No other device?" read "No other devi…" at the largest text (walk, 23 Nov).            Text(label)                .font(look.font("button", at: size))                .lineLimit(2)                .multilineTextAlignment(.center)                .fixedSize(horizontal: false, vertical: true)                .foregroundStyle(look.colour(enabled ? "text" : "disabled", scheme))                .padding(.horizontal, look.gap("l"))                .padding(.vertical, look.gap("s"))                .frame(minHeight: look.gap("tap"))                .background(RoundedRectangle(cornerRadius: look.corner("button"), style: .continuous).fill(look.colour("raised", scheme)))                .contentShape(Rectangle())        }        .buttonStyle(VaultPressStyle())    }}/// A sentence naming Cotton, said in this app's own name.private func named(_ text: String?) -> String { ShellLines.said(text ?? "", as: ShellLines.speaker) }/// The sealed key, a device approval and the recovery cards, in Vault's look.struct VaultSealedKeyCards: View {    let keys: SealedKeys    let look: VaultLook    @State private var confirming = false    var body: some View {        VStack(spacing: 0) {            if let line = keys.line {                VaultStandingCard {                    Text(line).vault(look, "headline").fixedSize(horizontal: false, vertical: true)                        .accessibilityIdentifier("sealed-key-line")                    if keys.waiting { askPart }                    HStack(spacing: look.gap("s")) {                        if let act = keys.act {                            VaultStandingAct(label: act) { Task { await keys.unlock() } }                                .disabled(keys.busy)                                .accessibilityIdentifier("sealed-key-unlock")                        }                        if keys.waiting, let haveCode = keys.recovery.line("haveCode") {                            VaultStandingAct(label: haveCode) { keys.recovery.entering = true }                                .accessibilityIdentifier("recovery-have-code")                        }                        if keys.waiting, let helper = keys.recovery.askHelper {                            VaultStandingAct(label: helper) { Task { await keys.recovery.askHelp() } }                                .disabled(keys.recovery.busy || keys.recovery.asked)                                .accessibilityIdentifier("recovery-ask-helper")                        }                    }                }                .accessibilityIdentifier("sealed-key-banner")            }            if let asking = keys.asking {                VaultStandingCard(warm: true) {                    Text(asking.line).vault(look, "headline").fixedSize(horizontal: false, vertical: true)                        .accessibilityIdentifier("approval-line")                    if let how = asking.how {                        Text(how).vault(look, "row_detail", "secondary").fixedSize(horizontal: false, vertical: true)                            .accessibilityIdentifier("approval-how")                    }                    VaultStandingAct(label: asking.refuse) { confirming = true }                        .disabled(keys.busy)                        .accessibilityIdentifier("approval-refuse")                }                .accessibilityIdentifier("approval-banner")                .fullScreenCover(isPresented: $confirming) {                    VaultConfirmView(asking: VaultAsking(                        confirm: VaultConfirm(title: asking.confirm ?? asking.refuse, body: "", yes: asking.refuse, no: ""),                        tone: .danger) { yes in                            confirming = false                            if yes { Task { await keys.refuse() } }                        })                    .environment(\.vault, look)                    .presentationBackground(.clear)                }            }            VaultRecoveryCards(recovery: keys.recovery, look: look)        }        .environment(\.vault, look)        .fullScreenCover(isPresented: Bindable(keys.recovery).entering) {            VaultRecoveryCodeAsk(recovery: keys.recovery, look: look)                .presentationBackground(.clear)        }    }    /// On a phone waiting for the vault: Approve on another device, then that    /// ask's code, large, and the poll's own line.    @ViewBuilder private var askPart: some View {        let polled = keys.vaultPolled        if let card = keys.vaultAsk, polled?.flag("done") != true {            if let code = polled?.text("code") {                Text(named(polled?.text("line"))).vault(look, "body").fixedSize(horizontal: false, vertical: true)                Text(code).vault(look, "code").accessibilityIdentifier("sealed-ask-code")                Text(named(polled?.text("check"))).vault(look, "row_detail", "secondary").fixedSize(horizontal: false, vertical: true)            } else {                Text(named(polled?.text("line") ?? card.text("line"))).vault(look, "body").fixedSize(horizontal: false, vertical: true)                    .accessibilityIdentifier("sealed-ask-state")            }        } else {            if let line = polled?.text("line") {                Text(named(line)).vault(look, "body", polled?.text("state") == "approved" ? "text" : "danger")                    .fixedSize(horizontal: false, vertical: true)                    .accessibilityIdentifier("sealed-ask-state")            }            if keys.offersAsk, let ask = keys.mineAsk {                VaultStandingAct(label: ask) { keys.openPage() }                    .disabled(keys.busy)                    .accessibilityIdentifier("sealed-ask")            }        }    }}/// The recovery code (offered or shown again) and a helper's ask.struct VaultRecoveryCards: View {    @Bindable var recovery: RecoveryKeys    let look: VaultLook    var body: some View {        cards            .fileExporter(isPresented: $recovery.exporting, document: recovery.file, contentType: .plainText,                          defaultFilename: recovery.line("fileName") ?? "") { result in                recovery.savedToFiles((try? result.get()) != nil)            }    }    private var cards: some View {        VStack(spacing: 0) {            if let shown = recovery.shown {                VaultStandingCard {                    if shown.offered, let offer = recovery.line("offer") {                        Text(offer).vault(look, "headline").fixedSize(horizontal: false, vertical: true)                            .accessibilityIdentifier("recovery-offer")                    }                    Text(shown.code).vault(look, "secret_large")                        .textSelection(.enabled)                        .fixedSize(horizontal: false, vertical: true)                        .accessibilityIdentifier("recovery-code")                    HStack(spacing: look.gap("s")) {                        if let saveFiles = recovery.line("saveFiles"), recovery.file != nil {                            VaultStandingAct(label: saveFiles) { recovery.saveToFiles() }.accessibilityIdentifier("recovery-save-files")                        }                        if let copy = recovery.line("copy") {                            VaultStandingAct(label: copy) { recovery.copy() }.accessibilityIdentifier("recovery-copy")                        }                        if let close = recovery.line(shown.offered ? "notNow" : "done") {                            VaultStandingAct(label: close) { Task { await recovery.dismiss() } }.accessibilityIdentifier("recovery-dismiss")                        }                    }                }            }            if recovery.reminding, recovery.shown == nil, let reminder = recovery.line("reminder") {                VaultStandingCard {                    Text(reminder).vault(look, "headline").fixedSize(horizontal: false, vertical: true)                        .accessibilityIdentifier("recovery-reminder-line")                    HStack(spacing: look.gap("s")) {                        if let show = recovery.line("show") {                            VaultStandingAct(label: show) { Task { await recovery.showFromReminder() } }.accessibilityIdentifier("recovery-reminder-show")                        }                        if let later = recovery.line("notNow") {                            VaultStandingAct(label: later) { recovery.notNowReminder() }.accessibilityIdentifier("recovery-reminder-later")                        }                    }                }            }            ForEach(recovery.helpAsks) { ask in                VaultStandingCard(warm: true) {                    Text(ask.line).vault(look, "headline").fixedSize(horizontal: false, vertical: true)                        .accessibilityIdentifier("help-ask-line")                    HStack(spacing: look.gap("s")) {                        VaultStandingAct(label: ask.approve) { Task { await recovery.approve(ask) } }                            .disabled(recovery.busy)                            .accessibilityIdentifier("help-ask-approve")                        VaultStandingAct(label: ask.decline) { Task { await recovery.decline(ask) } }                            .disabled(recovery.busy)                            .accessibilityIdentifier("help-ask-decline")                    }                }            }        }        .environment(\.vault, look)    }}/// *I have a code*, as Vault's dialog: the code typed, its typo said as it is/// typed, Not now and Open.struct VaultRecoveryCodeAsk: View {    @Bindable var recovery: RecoveryKeys    let look: VaultLook    @Environment(\.colorScheme) private var scheme    var body: some View {        ZStack {            look.colour("scrim", scheme).ignoresSafeArea()            VStack(alignment: .leading, spacing: look.gap("m")) {                if let enter = recovery.line("enter") { Text(enter).vault(look, "headline").fixedSize(horizontal: false, vertical: true) }                VaultTextField(placeholder: recovery.line("hint") ?? "", text: $recovery.typed, mono: true)                    .textInputAutocapitalization(.characters)                    .autocorrectionDisabled()                    .accessibilityIdentifier("recovery-entry-field")                if let problem = recovery.problem {                    Text(problem).vault(look, "row_detail", "danger").fixedSize(horizontal: false, vertical: true)                        .accessibilityIdentifier("recovery-entry-problem")                }                HStack(spacing: look.gap("s")) {                    Spacer(minLength: 0)                    if let notNow = recovery.line("notNow") {                        VaultButton(label: notNow) { recovery.entering = false }                    }                    if let open = recovery.line("open") {                        VaultButton(label: open, tone: .accent) { Task { await recovery.open() } }                            .disabled(recovery.busy || recovery.problem != nil || recovery.typed.filter { $0.isLetter || $0.isNumber }.count < 24)                            .accessibilityIdentifier("recovery-entry-open")                    }                }                .padding(.top, look.gap("s"))            }            .padding(look.gap("xl"))            .background(RoundedRectangle(cornerRadius: look.corner("card"), style: .continuous).fill(look.colour("sheet", scheme)))            .padding(look.gap("gutter"))        }        .environment(\.vault, look)    }}/// This device reads only: the refusal's title and the two lines, in Vault's look.struct VaultReadOnlyCard: View {    let refusal: ServerRefusal    let look: VaultLook    var body: some View {        VaultStandingCard(warm: true) {            Text(refusal.title).vault(look, "headline").fixedSize(horizontal: false, vertical: true)                .accessibilityIdentifier("read-only-title")            Text(CottonStrings.readOnlyBody).vault(look, "row_detail", "danger").fixedSize(horizontal: false, vertical: true)                .accessibilityIdentifier("read-only-body")            Text(CottonStrings.readOnlyHeld).vault(look, "row_detail", "danger").fixedSize(horizontal: false, vertical: true)                .accessibilityIdentifier("read-only-held")        }        .environment(\.vault, look)        .accessibilityIdentifier("read-only-banner")    }}/// An assistant's unlock request standing above the screen in Vault's look:/// nothing while no request waits, or outside Vault. Words, Days, Story and Comms raise it in their own sheet/// instead (`UnlockAskSheet.swift`).struct VaultUnlockStanding: View {    let asks: UnlockAsks    @Environment(\.vaultLook) private var vaultLook    var body: some View {        if let look = vaultLook, !VaultLook.raisesAsks, let card = asks.card {            VaultUnlockCard(asks: asks, card: card, look: look)        }    }}#endif