Cotton code
177 lines · 8.5 KB · SwiftRaw
// SETTINGS' FIRST TWO CARDS (owner, 1 Nov 2026): Backup and Cotton server,// shell screens over the personal object's session (`PersonalObject`), never// definitions — they hold the way to a credential. The settings board lists// them only where the manifest names a personal object (`needs:// personal_object`), so Rigby's never shows them and nothing here asks which// flavour this is.//// - Backup, before sign-in: what backing up does, one act (Back up: the//   number-and-code sheet), and the line for a person who already has a//   Cotton. Signed in with rows made on this phone: how many have gone up//   (`personal.adopt`), polled once a second while the card is up, then//   Backed up — or the object's line and Try again. Connected: this phone's device card//   (the sync status Home's first card used to carry), Stop backing up, and//   Remove from this phone last, red, asked first.// - Cotton server, before sign-in: what the server adds, and that it needs//   backup. Connected: the server's address, and that MCP keys are made from//   the assistants' side for now (making them on the phone is a later job;//   nothing untrue is said).//// A tool whose rows only the server brings says so on its empty deck before// backup (`ToolServerLine`), with Back up opening this card.import CottonKitimport SwiftUI/// `/backup` — Settings › Backup.struct BackupRoute: View {    @Environment(PersonalObject.self) private var personal: PersonalObject?    var body: some View {        if let personal {            BackupScreen(personal: personal)        } else {            // An app whose manifest names no personal object lists no Backup            // card; a link here draws nothing.            PaperScreen { EmptyView() }        }    }}struct BackupScreen: View {    let personal: PersonalObject    var body: some View {        PaperScreen(title: ShellLines.line("board", "object.backup.title"), clearsFan: true) {            OpenedCardContent {                if personal.stand == .connected { ThisDeviceCard() }            } acts: {                VStack(alignment: .leading, spacing: BackupCardKnobs.gap) {                    switch personal.stand {                    case .local:                        Text(ShellLines.line("board", "object.backup.body")).paperBody(BackupCardKnobs.body)                        TypedRow(symbol: "phone", label: ShellLines.line("board", "object.backup.act")) {                            Task { await personal.backUp() }                        }                        .accessibilityIdentifier("backup-act")                        Text(ShellLines.line("board", "object.backup.again")).paperBody(BackupCardKnobs.small, ink: .pencil)                    case .connecting:                        Text(ShellLines.line("board", "object.backup.connecting")).paperBody(BackupCardKnobs.body, ink: .pencil)                    case .backingUp:                        Text(personal.backupLine).paperBody(BackupCardKnobs.body)                            .accessibilityIdentifier("backup-progress")                    case .backupFailed:                        Text(personal.backupLine).paperBody(BackupCardKnobs.body)                            .accessibilityIdentifier("backup-failed")                        TypedRow(symbol: "arrow.clockwise", label: ShellLines.line("board", "object.backup.try_again")) {                            Task { await personal.backUp() }                        }                        .accessibilityIdentifier("backup-try-again")                        TypedRow(label: ShellLines.line("board", "object.backup.stop"), chevron: false) {                            Task { await personal.stopBackingUp() }                        }                        .accessibilityIdentifier("backup-stop")                    case .connected:                        if personal.backedUp {                            Text(ShellLines.line("board", "object.backup.backed_up")).paperBody(BackupCardKnobs.body)                                .accessibilityIdentifier("backup-done")                        }                        TypedRow(label: ShellLines.line("board", "object.backup.stop"), chevron: false) {                            Task { await personal.stopBackingUp() }                        }                        .accessibilityIdentifier("backup-stop")                    }                    if personal.stand == .local || personal.stand == .connected {                        TypedRow(label: ShellLines.line("board", "object.backup.remove"), chevron: false, destructive: true) {                            personal.asksRemoval = .remove                        }                        .accessibilityIdentifier("backup-remove")                    }                    if personal.isLocal, !personal.asking, let refusal = personal.refusal ?? personal.home?.notices.first(where: { $0.id == "signed_out" })?.words {                        CustomerRefusalLine(refusal: refusal)                    }                }            }        }        // While the rows go up, the card asks where they stand once a second.        .task { await personal.refresh() }        .task(id: personal.isBackingUp) {            while personal.isBackingUp, !Task.isCancelled {                await personal.followAdopt()                try? await Task.sleep(for: .seconds(1))            }        }        // The sign-in sheet and the ask before Remove are the root's        // (`PersonalSheets`), raised from here and from Home's card alike.        .accessibilityIdentifier("backup-page")    }}/// `/server` — Settings › Cotton server.struct ServerRoute: View {    @Environment(PersonalObject.self) private var personal: PersonalObject?    var body: some View {        PaperScreen(title: ShellLines.line("board", "object.server.title"), clearsFan: true) {            VStack(alignment: .leading, spacing: BackupCardKnobs.gap) {                if let personal, personal.stand == .connected {                    HStack {                        Text(ShellLines.line("board", "object.server.address")).paperBody(BackupCardKnobs.body, ink: .pencil)                        Spacer()                        Text(personal.server).paperBody(BackupCardKnobs.body)                            .textSelection(.enabled)                    }                    Text(ShellLines.line("board", "object.server.keys")).paperBody(BackupCardKnobs.small, ink: .pencil)                } else {                    Text(ShellLines.line("board", "object.server.body")).paperBody(BackupCardKnobs.body)                    Text(ShellLines.line("board", "object.server.needs")).paperBody(BackupCardKnobs.small, ink: .pencil)                        .accessibilityIdentifier("server-needs")                }            }        }        .accessibilityIdentifier("server-page")    }}/// This phone's own device card — the device tool's card with its sync rows,/// as Home's `device` part drew it — for the record `read.devices` names this/// device.struct ThisDeviceCard: View {    @Environment(\.cottonCore) private var core    @State private var id: String?    var body: some View {        // A stack, not a Group: a Group with nothing in it is no view, so its        // task would never run and the card never be read (walk, 1 Nov 2026).        VStack(alignment: .leading, spacing: 0) {            if let id { BoardDevicePart(id: id) }        }        .task {            guard id == nil, let core else { return }            id = (try? await core.callJSON(CottonMethod.readDevices))?.untrimmed("thisDevice")        }    }}/// The sizes the two cards draw with: Swift's own, beside their reader.enum BackupCardKnobs {    static let gap: CGFloat = 18    static let body: CGFloat = 16    static let small: CGFloat = 14}/// What a tool's empty deck says before backup: the line of the tool that/// owns `table` and whose rows only the server brings (`server.needs`) —/// the table's own built-in tool (Calls) or a package tool whose tables or/// views read it — from the tool's definition alone. Nil where no such tool/// owns the table.enum ToolServerLine {    static func of(_ table: TableDefinition, tools: [ToolDefinition]) -> String? {        if let server = table.tool?.server, server.needs { return server.line }        let owning = tools.first { tool in            guard let server = tool.server, server.needs else { return false }            return tool.tables.contains(table.id) || tool.views.contains { $0.read["table"] == .string(table.id) }        }        return owning?.server?.line    }}