Cotton code
323 lines · 15.3 KB · SwiftRaw
// HEALTH, PEOPLE, YOU, GETTING IN ON A NEW PHONE AND LOOK (secrets.html). Health: the summary, then each group that// has something (found in a leak, used in more than one place, easy to guess, old) with its secrets listed, and the// one sentence on what the leak check sends. People: his people from Comms, once the core's sharing answers// (`secrets.people`); until then its words for none. You: the app's own rows as the core answers them (`vault.settings`),// Look and Getting in on a new phone, then the rest. Look: the shared Look screen in the Plum look, its preview two// list rows and one revealed secret.#if os(iOS)import CottonKitimport CottonWritingimport SwiftUI/// The groups of `vault.security` each place draws: Health's findings in the drawing's order, and the ways in.enum SecretsGroups {    static let health = ["breached", "reused", "weak", "old"]    static let recovery = ["recovery", "devices", "passkeys"]    static let assistants = ["waiting", "windows", "reads"]    static func pick(_ ids: [String], from groups: [VaultGroup]) -> [VaultGroup] {        ids.compactMap { id in groups.first { $0.id == id } }    }}/// HEALTH (`vault.security`): the summary, then found in a leak (with its check), used in more than one place, easy/// to guess and old, each with its secrets; a group with nothing in it is not drawn, but the leak check always is.struct SecretsHealthScreen: View {    let model: VaultModel    @State private var security: VaultSecurity?    var body: some View {        VStack(spacing: 0) {            SecretsBar(back: SecretsWords.line("tab.secrets"), onBack: { model.pop() }).padding(.horizontal, Plum.side)            ScrollView {                VStack(alignment: .leading, spacing: 0) {                    if let security {                        let health = security.health                        SecretsTitle(text: health.title)                        Text(health.summary).font(SecretsType.text(16)).foregroundStyle(Plum.quiet)                            .fixedSize(horizontal: false, vertical: true).padding(.top, 4)                        if !health.open, let unlock = health.unlock {                            SecretsButton(label: unlock.label, weight: .go, id: "secrets-health-unlock") { Task { await model.run(unlock) } }                                .padding(.top, 16)                        }                        ForEach(SecretsGroups.pick(SecretsGroups.health, from: security.groups).filter { !$0.rows.isEmpty || $0.id == "breached" }) { group in                            SecretsGroupView(model: model, group: group)                        }                    }                }                .padding(.horizontal, Plum.side)                .padding(.bottom, 40)            }        }        .background(Plum.ground)        .task(id: model.generation) { security = await model.read("vault.security", .object(["platform": "ios"]), as: VaultSecurity.self) }        .accessibilityIdentifier("secrets-health")    }}/// YOU: the app's own rows (`vault.settings`) with Look and Getting in on a new phone among them.@MainActorextension SettingsLook {    /// The shared settings parts in Plum: its quiet headings, plain rows, accent acts, and the accent as danger.    static var plum: SettingsLook {        var look = SettingsLook(            ink: Plum.ink, quiet: Plum.quiet, rule: Plum.rule, ground: Plum.ground,            body: SecretsType.text(16), small: SecretsType.small,            heading: { AnyView(SecretsHeading(text: $0)) },            act: { words, id, enabled, tap in                AnyView(Button(action: tap) {                    Text(verbatim: words).font(SecretsType.text(16, .semibold)).foregroundStyle(Plum.accent)                        .frame(minHeight: 44, alignment: .leading).contentShape(Rectangle())                }                .buttonStyle(.plain).disabled(!enabled).opacity(enabled ? 1 : 0.45).accessibilityIdentifier(id))            },            choice: { words, on, id, enabled, tap in                AnyView(SecretsChip(label: words, on: on, id: id, action: tap).disabled(!enabled))            }        )        look.danger = Plum.accent        look.title = SecretsType.title        return look    }}struct SecretsYouScreen: View {    let model: VaultModel    @State private var settings: VaultSettings?    @State private var look = LookModel.standing("keys")    @State private var security: VaultSecurity?    var body: some View {        ScrollView {            VStack(alignment: .leading, spacing: 0) {                SecretsTitle(text: SecretsWords.line("you.title"))                SecretsRule().padding(.top, 8)                link(SecretsWords.line("you.recovery"), detail: nil, id: "secrets-you-recovery") { model.push(.recovery) }                link(SecretsWords.line("you.look"), detail: look.answer?.line, id: "secrets-you-look") { model.push(.look) }                if let settings {                    // Appearance is Look's now (paper for light and for dark), so its old group is not drawn; Look is the                    // link above (with its line), so the core's Look group is not drawn twice.                    ForEach(settings.groups.filter { $0.id != "appearance" && $0.id != "look" }) { group in                        SecretsGroupView(model: model, group: group)                    }                }                // What assistants asked for and were allowed: Secrets shows them nothing (DECISIONS question 10), and                // these say so.                if let security {                    ForEach(SecretsGroups.pick(SecretsGroups.assistants, from: security.groups)) { group in                        SecretsGroupView(model: model, group: group)                    }                }                // Delete account, last while signed in (DELETE_ACCOUNT.md; every Cotton app, as the App Store asks).                DeleteAccountPart(core: model.core, look: .plum, onDeleted: { model.tab = .secrets })                    .padding(.top, 18)            }            .padding(.horizontal, Plum.side)            .padding(.bottom, 40)        }        .background(Plum.ground)        .task(id: model.generation) {            if let read = await model.read("vault.settings", .object(["platform": "ios"]), as: VaultSettings.self) {                settings = read                model.take(read)            }            security = await model.read("vault.security", .object(["platform": "ios"]), as: VaultSecurity.self)        }        .accessibilityIdentifier("secrets-you")    }    private func link(_ title: String, detail: String?, id: String, _ action: @escaping () -> Void) -> some View {        Button(action: action) {            SecretsRow(title: title) {                HStack(spacing: 6) {                    if let detail { SecretsEnd(text: detail) }                    SecretsMarkView(mark: .next, size: 18, ink: Plum.quiet)                }            }        }        .buttonStyle(.plain)        .accessibilityIdentifier(id)    }}/// One group the core answers (You's and the recovery page's): its heading, what it means, its rows (a chosen one/// ticked; a row with an act runs it), and its acts quiet under it.struct SecretsGroupView: View {    let model: VaultModel    let group: VaultGroup    var body: some View {        VStack(alignment: .leading, spacing: 0) {            SecretsHeading(text: group.title)            if let detail = group.detail { SecretsNote(text: detail).padding(.top, -6).padding(.bottom, 4) }            if group.rows.isEmpty, let empty = group.empty {                Text(empty).font(SecretsType.text(15)).foregroundStyle(Plum.quiet).padding(.vertical, 8)            }            ForEach(Array(group.rows.enumerated()), id: \.element.id) { index, row in                let chosen = row.tone == .accent                let last = index == group.rows.count - 1                Group {                    if let open = row.open {                        Button { Task { await model.run(open) } } label: { rowView(row, chosen: chosen, last: last) }                            .buttonStyle(.plain)                    } else {                        rowView(row, chosen: chosen, last: last)                    }                }                .accessibilityAddTraits(chosen ? .isSelected : [])                .accessibilityIdentifier("secrets-\(group.id)-\(row.id)")            }            ForEach(group.acts) { act in                SecretsButton(label: act.label, weight: act.tone == .accent ? .go : .quiet, id: "secrets-\(group.id)-\(act.id)") {                    Task { await model.run(act) }                }                .padding(.top, 10)            }        }    }    private func rowView(_ row: VaultListRow, chosen: Bool, last: Bool) -> some View {        SecretsRow(title: row.title, line: row.detail, last: last) {            HStack(spacing: 12) {                ForEach(row.acts) { act in                    SecretsAct(label: act.label, id: "secrets-\(group.id)-\(row.id)-\(act.id)") { Task { await model.run(act) } }                }                if chosen { SecretsMarkView(mark: .check, size: 20, ink: Plum.ink) }            }        }    }}/// GETTING IN ON A NEW PHONE: the ways in as the core answers them (`vault.security`'s recovery, this phone and his/// other devices, passkeys, and what assistants were allowed), each group with its own acts.struct SecretsRecoveryScreen: View {    let model: VaultModel    @State private var security: VaultSecurity?    var body: some View {        VStack(spacing: 0) {            SecretsBar(back: SecretsWords.line("tab.you"), onBack: { model.pop() }).padding(.horizontal, Plum.side)            ScrollView {                VStack(alignment: .leading, spacing: 0) {                    SecretsTitle(text: SecretsWords.line("you.recovery"))                    if let security {                        ForEach(SecretsGroups.pick(SecretsGroups.recovery, from: security.groups)) { group in                            SecretsGroupView(model: model, group: group)                        }                    }                    SecretsNote(text: SecretsWords.line("you.recovery_note")).padding(.top, 16)                }                .padding(.horizontal, Plum.side)                .padding(.bottom, 40)            }        }        .background(Plum.ground)        .task(id: model.generation) { security = await model.read("vault.security", .object(["platform": "ios"]), as: VaultSecurity.self) }        .accessibilityIdentifier("secrets-recovery")    }}/// LOOK: the shared Look screen (`Chrome/LookScreen.swift`) in the Plum look, fed the core's `look {app: keys}`.struct SecretsLookPage: View {    let model: VaultModel    @State private var look = LookModel.standing("keys")    @State private var asking = false    var body: some View {        VStack(spacing: 0) {            SecretsBar(back: SecretsWords.line("tab.you"), onBack: { model.pop() }).padding(.horizontal, Plum.side)            ScrollView {                VStack(alignment: .leading, spacing: 0) {                    if let answer = look.answer {                        SecretsTitle(text: answer.word("title"))                        LookScreen(choices: LookChoices(answer), words: LookWords(answer), skin: Self.skin, gutter: Plum.side,                                   choose: { pick in Task { await look.choose(pick, core: model.core); tellRefusal() } },                                   reset: { asking = true }) { dark in                            SecretsLookPreview(answer: answer, dark: dark)                        } more: { EmptyView() }                        .padding(.top, 8)                    }                }                .padding(.horizontal, Plum.side)                .padding(.bottom, 40)            }        }        .background(Plum.ground)        .task { await look.read(model.core) }        .overlay {            if asking, let answer = look.answer {                SecretsAskCard(title: answer.word("reset_title"), body_: answer.word("reset_ask"), yes: answer.word("reset_yes"), no: answer.word("reset_no")) { yes in                    asking = false                    if yes { Task { await look.reset(core: model.core); tellRefusal() } }                }            }        }        .animation(.easeOut(duration: 0.22), value: asking)        .accessibilityIdentifier("secrets-look")    }    private func tellRefusal() {        if let refused = look.refused { model.toasts.show(refused, tone: .error); look.refused = nil }    }    @MainActor static var skin: LookSkin {        LookSkin(ground: Plum.ground, ink: Plum.ink, quiet: Plum.quiet, accent: Plum.ink, onAccent: Plum.ground, sheet: Plum.ground, rule: Plum.rule,                 font: { size, weight in                     SecretsType.text(size, weight == .bold ? .bold : weight == .semibold ? .semibold : weight == .medium ? .medium : .regular)                 },                 face: { id, size in SecretsType.face(id, size: size) })    }}/// The preview: two list rows and one revealed secret, in the paper of one appearance and the chosen ink; the value/// always in Red Hat Mono.struct SecretsLookPreview: View {    let answer: LookAnswer    let dark: Bool    private func colour(_ token: String, _ fallback: Color) -> Color { answer.colour(token, dark: dark) ?? fallback }    var body: some View {        let ink = colour("ink", Plum.ink), quiet = colour("quiet", Plum.quiet), rule = colour("rule", Plum.rule)        VStack(alignment: .leading, spacing: 0) {            ForEach(Array((answer.preview?.secrets ?? []).enumerated()), id: \.offset) { _, row in                HStack {                    VStack(alignment: .leading, spacing: 2) {                        Text(row.title).font(SecretsType.text(17, .semibold)).foregroundStyle(ink)                        Text(row.line).font(row.mono ? SecretsType.mono(14) : SecretsType.text(14)).foregroundStyle(quiet)                    }                    Spacer()                    HStack(spacing: -6) {                        ForEach(row.holds, id: \.self) { initial in                            Text(initial).font(SecretsType.text(10, .bold)).foregroundStyle(ink)                                .frame(width: 24, height: 24).overlay(Circle().strokeBorder(ink, lineWidth: 1.1))                        }                    }                }                .padding(.vertical, 9)                .overlay(alignment: .bottom) { Rectangle().fill(rule).frame(height: 1) }            }            if let shown = answer.preview?.shown {                VStack(alignment: .leading, spacing: 3) {                    Text(shown.label).font(SecretsType.text(13)).foregroundStyle(quiet)                    HStack {                        Text(shown.value).font(SecretsType.mono(17)).foregroundStyle(ink)                        Spacer()                        Text(shown.copy).font(SecretsType.text(15, .bold)).foregroundStyle(colour("accent", Plum.accent))                    }                }                .padding(.top, 12)                .padding(.bottom, 2)            }        }        .padding(.horizontal, 14)        .padding(.top, 4)        .padding(.bottom, 10)        .frame(maxWidth: .infinity, alignment: .leading)        .background(RoundedRectangle(cornerRadius: 18).fill(colour("ground", Plum.ground)))        .overlay(RoundedRectangle(cornerRadius: 18).strokeBorder(rule, lineWidth: 1))        .accessibilityElement(children: .combine)    }}#endif