Cotton code
246 lines · 12.1 KB · SwiftRaw
// THE NEW DEVICE'S PAGE WHILE ANOTHER DEVICE OF THE PERSON'S MUST SAY YES// (GETTING-IN.md, 22 Nov 2026; Android's `GettingInPage.kt` is the reference).//// After sign-in, a device that cannot yet open the person's things gets this// page over the app by itself: the core asks (`gettingIn.screen`, read by// `SealedKeys` every `pollSeconds`), and the page draws its answer: the// heading, the line, the square code from `qr.rows` (squares on white with a// quiet border of four; never the text, never a link), the six digits once the// other device opened the request, how long is left, and each end with Ask// again. Always on it: Sign out, No other device? (the recovery code and the// trusted person) and Do this later (the page closes; the slim line brings it// back). It never says Approved: the core's `approved` line says the other// device said yes, and the page closes once the key is here. Every word is the// core's.import CottonKitimport CottonPaperimport SwiftUIstruct GettingInPage: View {    @Bindable var keys: SealedKeys    @Environment(\.vaultLook) private var vaultLook    /// The page's words as the core says them: "any Cotton app" is any of them, never this app's own name.    private static func named(_ text: String?) -> String { text ?? "" }    var body: some View {        #if os(iOS)        if let look = vaultLook, let page = keys.page {            // Words, Days, Story and Comms draw it in their own look, as their cards.            LookedGettingInPage(keys: keys, page: page, look: look)        } else {            plain        }        #else        plain        #endif    }    @ViewBuilder private func plainActs(_ page: JSONValue) -> some View {        if let later = page.text("later") {            Button(later) { keys.later = true }                .accessibilityIdentifier("getting-in-later")        }        if let signOut = page.text("signOut") {            Button(signOut) { Task { await keys.signOut() } }                .accessibilityIdentifier("getting-in-sign-out")        }    }    @ViewBuilder private var plain: some View {        if let page = keys.page {            ScrollView {                VStack(alignment: .leading, spacing: 16) {                    Text(Self.named(page.text("heading")))                        .font(.title2.weight(.semibold))                        .fixedSize(horizontal: false, vertical: true)                        .accessibilityIdentifier("getting-in-heading")                    Text(Self.named(page.text("line")))                        .font(.body)                        .fixedSize(horizontal: false, vertical: true)                        .accessibilityIdentifier("getting-in-line")                    if let rows = page["qr"]?["rows"]?.array?.compactMap(\.string), !rows.isEmpty {                        SquareCodeView(rows: rows)                            .frame(width: 240, height: 240)                            .frame(maxWidth: .infinity)                            .accessibilityIdentifier("getting-in-square")                    }                    if let codeLine = page.text("codeLine") {                        Text(Self.named(codeLine)).font(.callout).foregroundStyle(.secondary).fixedSize(horizontal: false, vertical: true)                    }                    if let code = page.text("code") {                        Text(code)                            .font(.system(size: 36, weight: .semibold, design: .monospaced))                            .frame(maxWidth: .infinity)                            .accessibilityIdentifier("getting-in-code")                    }                    if let left = page.text("left") {                        Text(left).font(.footnote).foregroundStyle(.secondary)                    }                    if let never = page.text("never"), page.text("state") == "waiting" {                        Text(Self.named(never)).font(.footnote).foregroundStyle(.secondary).fixedSize(horizontal: false, vertical: true)                    }                    if let again = page.text("again") {                        Button(again) { keys.again() }                            .buttonStyle(.borderedProminent)                            .frame(maxWidth: .infinity)                            .accessibilityIdentifier("getting-in-again")                    }                    if keys.noOther, page["lost"] != nil {                        if let heading = page["lost"]?.text("heading") { Text(heading).font(.headline).fixedSize(horizontal: false, vertical: true) }                        if let line = page["lost"]?.text("line") { Text(line).font(.callout).foregroundStyle(.secondary).fixedSize(horizontal: false, vertical: true) }                        NoOtherDevice(recovery: keys.recovery, useCode: page["lost"]?.text("code"))                    } else if let noDevice = page.text("noDevice") {                        Button(noDevice) { keys.noOther = true }                            .accessibilityIdentifier("getting-in-no-device")                    }                    ViewThatFits(in: .horizontal) { HStack { plainActs(page) }; VStack(alignment: .leading, spacing: 10) { plainActs(page) } }                    .padding(.top, 8)                }                .padding(20)            }            .background(.background)            .accessibilityAddTraits(.isModal)            .accessibilityIdentifier("getting-in-page")        }    }}#if os(iOS)/// The page in an app's own look: its ground, its type roles, its acts.private struct LookedGettingInPage: View {    @Bindable var keys: SealedKeys    let page: JSONValue    let look: VaultLook    @Environment(\.colorScheme) private var scheme    /// The page's words as the core says them: "any Cotton app" is any of them, never this app's own name.    private static func named(_ text: String?) -> String { text ?? "" }    @ViewBuilder private var lookedActs: some View {        if let later = page.text("later") {            VaultStandingAct(label: later) { keys.later = true }                .accessibilityIdentifier("getting-in-later")        }        if let signOut = page.text("signOut") {            VaultStandingAct(label: signOut) { Task { await keys.signOut() } }                .accessibilityIdentifier("getting-in-sign-out")        }    }    var body: some View {        ScrollView {            VStack(alignment: .leading, spacing: look.gap("l")) {                Text(Self.named(page.text("heading"))).vault(look, "headline")                    .fixedSize(horizontal: false, vertical: true)                    .accessibilityIdentifier("getting-in-heading")                Text(Self.named(page.text("line"))).vault(look, "row_detail", "secondary")                    .fixedSize(horizontal: false, vertical: true)                    .accessibilityIdentifier("getting-in-line")                if let rows = page["qr"]?["rows"]?.array?.compactMap(\.string), !rows.isEmpty {                    SquareCodeView(rows: rows)                        .frame(width: 240, height: 240)                        .clipShape(RoundedRectangle(cornerRadius: look.corner("card"), style: .continuous))                        .frame(maxWidth: .infinity)                        .accessibilityIdentifier("getting-in-square")                }                if let codeLine = page.text("codeLine") {                    Text(Self.named(codeLine)).vault(look, "row_detail", "secondary").fixedSize(horizontal: false, vertical: true)                }                if let code = page.text("code") {                    Text(code)                        .font(.system(size: 36, weight: .semibold, design: .monospaced))                        .foregroundStyle(look.colour("text", scheme))                        .frame(maxWidth: .infinity)                        .accessibilityIdentifier("getting-in-code")                }                if let left = page.text("left") { Text(left).vault(look, "label", "secondary") }                if let never = page.text("never"), page.text("state") == "waiting" {                    Text(Self.named(never)).vault(look, "label", "secondary").fixedSize(horizontal: false, vertical: true)                }                if let again = page.text("again") {                    VaultStandingAct(label: again) { keys.again() }                        .accessibilityIdentifier("getting-in-again")                }                if keys.noOther, page["lost"] != nil {                    VStack(alignment: .leading, spacing: look.gap("s")) {                        if let heading = page["lost"]?.text("heading") {                            Text(heading).vault(look, "headline").fixedSize(horizontal: false, vertical: true)                        }                        if let line = page["lost"]?.text("line") {                            Text(line).vault(look, "row_detail", "secondary").fixedSize(horizontal: false, vertical: true)                        }                        if let useCode = page["lost"]?.text("code") ?? keys.recovery.line("haveCode") {                            VaultStandingAct(label: useCode) { keys.recovery.entering = true }                                .accessibilityIdentifier("recovery-have-code")                        }                        if let helper = keys.recovery.askHelper {                            VaultStandingAct(label: helper) { Task { await keys.recovery.askHelp() } }                                .disabled(keys.recovery.busy || keys.recovery.asked)                                .accessibilityIdentifier("recovery-ask-helper")                        }                    }                } else if let noDevice = page.text("noDevice") {                    VaultStandingAct(label: noDevice) { keys.noOther = true }                        .accessibilityIdentifier("getting-in-no-device")                }                // Side by side where they fit, one above the other at the largest text (cut to "Sig…" on the walk).                ViewThatFits(in: .horizontal) {                    HStack(spacing: look.gap("s")) { lookedActs }                    VStack(alignment: .leading, spacing: look.gap("s")) { lookedActs }                }            }            .padding(look.gap("gutter"))        }        .background(look.colour("background", scheme).ignoresSafeArea())        .environment(\.vault, look)        .accessibilityAddTraits(.isModal)        .accessibilityIdentifier("getting-in-page")    }}#endif/// No other device?: the recovery code and, where one is chosen, the trusted person.private struct NoOtherDevice: View {    let recovery: RecoveryKeys    let useCode: String?    var body: some View {        // TRUSTED RECOVERY: the core's "back" screen (C, D, G) when it answers; the code and the one trusted person otherwise.        RecoveryBackView(core: recovery.calling, openCode: { recovery.entering = true }) { older }    }    private var older: some View {        VStack(alignment: .leading, spacing: 10) {            if let haveCode = useCode ?? recovery.line("haveCode") {                Button(haveCode) { recovery.entering = true }                    .accessibilityIdentifier("recovery-have-code")            }            if let helper = recovery.askHelper {                Button(helper) { Task { await recovery.askHelp() } }                    .disabled(recovery.busy || recovery.asked)                    .accessibilityIdentifier("recovery-ask-helper")            }        }    }}/// The core's square code: one string per row, `1` dark, drawn on white with a quiet border of four modules.struct SquareCodeView: View {    let rows: [String]    var body: some View {        Canvas { context, size in            let modules = CGFloat(rows.count + 8)            let unit = min(size.width, size.height) / modules            context.fill(Path(CGRect(origin: .zero, size: size)), with: .color(.white))            for (y, row) in rows.enumerated() {                for (x, bit) in row.enumerated() where bit == "1" {                    context.fill(Path(CGRect(x: (CGFloat(x) + 4) * unit, y: (CGFloat(y) + 4) * unit, width: unit, height: unit)), with: .color(.black))                }            }        }        .accessibilityHidden(true)    }}