Cotton code
111 lines · 5.0 KB · SwiftRaw
// The board parts that read their own source (design note 26203e21):////   • `list` — one table's records as one section, drawn by the same//     `BoardSection` a tool's section is, under the part's heading;//   • `facts` — the record of the table `source` for the database this window//     shows (the `database` source's row: its name, colour, what is kept),//     drawn as that record's page face, read only. A tap opens its whole page,//     where a column is edited — the board never edits in place;//   • `device` — this device's own record, the one the core served the board//     naming, drawn by the device tool's card over its live sync columns//     (`shell.today`'s device card). On the phone's deck it is Home's first card//     (`HomeToday`); a page of parts draws it here. A tap opens This device.//// Each is a part on its own: none draws another part.import CottonKitimport CottonValuesimport SwiftUI/// The `list` part: the rows the core answered, as one section.struct BoardListPart: View {    let entry: BoardEntry    let onOpen: (GridTile) -> Void    let onOpenRecord: (TableRecord, BoardSectionItem) -> Void    let onToggle: (TableRecord, TableDefinition) -> Void    @Environment(\.tableDefinitions) private var definitions    var body: some View {        if let table = entry.table, let definition = definitions[table] {            let records = entry.records            let section = BoardSectionItem(                tile: GridTile(key: "list-\(table)", name: entry.heading ?? "", picture: nil, opens: .route(BoardLayout.route(forTable: definition))),                definition: definition,                read: ToolTileRead(records: records, count: entry.count ?? records.count, linkTitles: LinkTitles(), next: nil),                whole: true)            BoardSection(                section: section, onOpen: { onOpen(section.tile) },                onToggle: { onToggle($0, section.definition) },                onOpenRecord: { onOpenRecord($0, section) })        }    }}/// The `facts` part: the shown database's record of the table `source`, as/// its page face, each column in the words the core answered.struct BoardFactsPart: View {    let entry: BoardEntry    /// Opens the record on its deck.    let onOpen: () -> Void    @Environment(\.tableDefinitions) private var definitions    var body: some View {        if let source = entry.source, let id = entry.record, let definition = definitions[source] {            RecordCardFace(definition: definition, record: TableRecord(id: id, values: Dictionary(uniqueKeysWithValues: entry.facts.map { ($0.key, RecordValue.text($0.value)) })))                .contentShape(Rectangle())                .onTapGesture(perform: onOpen)                .accessibilityIdentifier("board-facts-\(source)")        }    }}/// The `device` part on a page of parts: this device's own record (`id`, the/// one the core served the board naming) as the device tool's card, as Home's/// page answers it (`shell.today`) and read again alone on each sync revision/// and every few seconds, so its sync rows are the phone's as they move. A/// tap opens This device.struct BoardDevicePart: View {    let id: String    @Environment(\.cottonCore) private var core    @Environment(\.cottonSync) private var sync    @Environment(\.tableDefinitions) private var definitions    @Environment(Navigator.self) private var navigator: Navigator?    /// This device's card, as the core dealt it (`shell.today {only: device}`).    @State private var card: TodayAnswer.Card?    private struct Asked: Hashable {        var id: String        var revision: Int        var pulled: String?    }    private struct DeviceAsk: Encodable, Sendable { let only = "device" }    private struct DeviceRead: Decodable, Sendable { let cards: [TodayAnswer.Card] }    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(spacing: 0) {            if let card, let definition = definitions[card.table ?? "device"] {                RecordCardFace(definition: definition, record: card.deck.record.record)                    .contentShape(Rectangle())                    .onTapGesture { navigator?.open(.device) }                    .accessibilityIdentifier("board-device")            }        }        // It moves with every sync pass: read again on each, and every few        // seconds. A read that fails keeps the card it has.        .task(id: Asked(id: id, revision: sync?.revision ?? 0, pulled: sync?.lastPullAt)) {            while !Task.isCancelled {                if let core, let read = try? await core.call(CottonMethod.shellToday, DeviceAsk(), as: DeviceRead.self),                   let next = read.cards.first, next != card {                    card = next                }                try? await Task.sleep(for: .seconds(HomeSyncKnobs.every))            }        }    }}