Cotton code
242 lines · 9.7 KB · SwiftRaw
// What each screen outside every route PRINTS — the gate's state → wording// table, as a pure value.//// The four screens (`OutsideRouteScreens.swift`) are pieces of paper on the// plaster since the first-run redesign (2026-09-15): a refusal is a torn cream// slip, work on the person's own database and every other wait is a cut sheet// (`GateSlip.of`), and the last resort is a painted card. What goes ON the paper —// the red reason, the title, the typed sentence, the handwritten figure, the// buttons and how heavily each is drawn — is decided HERE, so `GateSlipTests`// pins every arm without a renderer and the views only lay it out.//// THE WORDS AND THE WAYS OUT ARE THE CORE'S (`shell.home`'s gate): this table// only puts them on paper — which paper, which identifier, how heavy.import CottonKitimport Foundation/// The paper a gate screen is printed on.enum GatePaper: Equatable, Sendable {    /// A torn cream slip — somebody said no.    case tornSlip    /// A cut cream sheet — work on the person's own database, and every wait    /// (`GateSlip.of`).    case sheet    /// A card painted in the cream dye with no photograph under it — the last    /// resort, which cannot assume the resource bundle is intact.    case painted}/// One thing a person can do from a gate screen.enum GateAction: String, Equatable, Sendable, CaseIterable {    case retry    case resetKey    case signOut    /// Take Cotton off this device — `BootModel.erase`, behind one confirmation.    ///    /// NOT a way out in the [`ServerRefusal.wayOut`] sense, and that is why it    /// is its own case rather than a member of that enum: a way out is the    /// screen's one route forward and there is at most one. This is offered    /// only where it is honest — a device the server removed, which is holding    /// a copy the person may want gone.    case erase    case copyDetails}/// How heavily an action is drawn.enum GateWeight: Equatable, Sendable {    /// The dark ink pill — the screen's way forward. At most one per screen.    case ink    /// A quiet typewriter line.    case line    /// A typewriter line in red ink — a way out that deletes something.    case red}/// One button on the paper.struct GateButton: Equatable, Sendable {    let action: GateAction    let label: String    let weight: GateWeight    /// The accessibility identifier the walks and the smoke already read.    let identifier: String}/// Everything a gate screen prints, top to bottom.struct GateSlip: Equatable, Sendable {    let paper: GatePaper    /// A few words in red ink above the title — what stands in the way.    let reason: String?    let title: String    let titleIdentifier: String?    let sentence: String    let sentenceIdentifier: String?    /// A figure written by hand under the sentence — the step, the megabytes.    let figure: String?    let figureIdentifier: String?    /// A typed caption in pencil — the fallback's raw message.    let detail: String?    /// Whether the rule under the title is drawn in red ink rather than pencil    /// — the failed migration's mark on the sheet. Never a line THROUGH words:    /// the app has no strike-through anywhere.    let redRule: Bool    let buttons: [GateButton]    /// A wait's progress: a bar where the core counts, an indicator that    /// moves only when the core answers where it does not. `nil` on every    /// slip that is not a wait — nothing else moves.    var progress: GateProgress?    /// A wait's line on how long its step usually takes.    var usually: String?    /// A wait's line past `BOOT_STALL_MS`: what is safe now.    var note: String?    /// The accessibility identifier of the quiet "Send a report" link drawn    /// last on a card that shows a problem (phone diagnostics part 3); `nil`    /// on a card that does not. Not a `GateAction`: it is no way forward, and    /// the frame files the report itself (`\.gateReport`).    var report: String?    /// The buttons' actions, in order — what a test reads.    var actions: [GateAction] { buttons.map(\.action) }    /// The slip for a gate the core answered.    static func of(_ gate: HomeAnswer.Gate, copied: Bool = false) -> GateSlip {        switch gate.stage {        case "refused":            return ServerRefusal(gate: gate).map(refusal) ?? fallback(message: gate.words.detail ?? "")        case "move_failed":            return GateSlip(                paper: .sheet,                reason: nil,                title: gate.words.title ?? "",                titleIdentifier: "migration-title",                sentence: gate.words.body ?? "",                sentenceIdentifier: nil,                figure: nil,                figureIdentifier: nil,                detail: gate.words.detail,                redRule: true,                buttons: buttons(gate.ways_out, prefix: "migration", copied: copied),                report: "migration-send-report"            )        case "failed":            return GateSlip(                paper: .painted,                reason: nil,                title: gate.words.title ?? "",                titleIdentifier: nil,                sentence: gate.words.body ?? "",                sentenceIdentifier: nil,                figure: nil,                figureIdentifier: nil,                detail: gate.words.detail,                redRule: false,                buttons: buttons(gate.ways_out, prefix: "error-fallback", copied: copied),                report: "error-fallback-send-report"            )        default:            return GateSlip(                paper: .sheet,                reason: nil,                title: gate.words.title ?? "",                titleIdentifier: "wait-title",                sentence: gate.words.body ?? "",                sentenceIdentifier: "wait-sentence",                figure: nil,                figureIdentifier: nil,                detail: nil,                redRule: false,                buttons: buttons(gate.ways_out, prefix: "stalled", copied: copied),                progress: progress(gate),                usually: gate.words.usually,                note: gate.words.note            )        }    }    /// A count where the core counted, else the beat and the clock.    private static func progress(_ gate: HomeAnswer.Gate) -> GateProgress {        if let counted = gate.progress, counted.done > 0, counted.total > 0 {            return .bar(done: counted.done, total: counted.total, label: gate.words.progress ?? "", identifier: "wait-steps")        }        return .activity(beat: gate.beat, label: gate.words.clock ?? "", identifier: "wait-elapsed")    }    /// The core's ways out, as buttons.    private static func buttons(_ ways: [HomeAnswer.WayOut], prefix: String, copied: Bool) -> [GateButton] {        ways.compactMap { way in            let action: GateAction            switch way.id {            case "retry": action = .retry            case "reset_key": action = .resetKey            case "sign_out": action = .signOut            case "erase": action = .erase            case "copy_details": action = .copyDetails            default: return nil            }            let weight: GateWeight = way.weight == "red" ? .red : way.weight == "line" ? .line : .ink            let label = action == .copyDetails && copied ? CottonStrings.copied : way.label            return GateButton(action: action, label: label, weight: weight, identifier: "\(prefix)-\(way.id.replacingOccurrences(of: "_", with: "-"))")        }    }    /// A refusal on its torn slip: the core's ways out where it answered    /// them, else (the Keychain's, before any core) Retry and the one way out.    static func refusal(_ refusal: ServerRefusal) -> GateSlip {        var drawn = buttons(refusal.waysOut, prefix: "server-refusal", copied: false)        if refusal.waysOut.isEmpty {            if refusal.retryable {                drawn.append(GateButton(action: .retry, label: CottonStrings.tryAgainAction, weight: .ink, identifier: "server-refusal-retry"))            }            if refusal.wayOut == .resetKey {                drawn.append(GateButton(action: .resetKey, label: CottonStrings.startOverOnThisPhone, weight: .red, identifier: "server-refusal-reset-key"))            }        }        return GateSlip(            paper: .tornSlip,            reason: refusal.reason,            title: refusal.title,            titleIdentifier: "server-refusal-title",            sentence: refusal.body,            sentenceIdentifier: "server-refusal-body",            figure: nil,            figureIdentifier: nil,            detail: nil,            redRule: false,            buttons: drawn,            report: "server-refusal-send-report"        )    }    // MARK: - The last resort    static func fallback(message: String) -> GateSlip {        GateSlip(            paper: .painted,            reason: nil,            title: CottonStrings.errorFallbackTitle,            titleIdentifier: nil,            sentence: CottonStrings.errorFallbackBody,            sentenceIdentifier: nil,            figure: nil,            figureIdentifier: nil,            detail: message,            redRule: false,            buttons: [                GateButton(                    action: .retry,                    label: CottonStrings.tryAgainAction,                    weight: .ink,                    identifier: "error-fallback-retry"                )            ],            report: "error-fallback-send-report"        )    }}/// What a wait's slip shows under its words: a bar where the core counted,/// else the beat and the clock.enum GateProgress: Equatable, Sendable {    case bar(done: Int, total: Int, label: String, identifier: String)    case activity(beat: Int, label: String, identifier: String)}