// A data tool's board: one tile per table, drawn as Home draws a tile.//// It is a ROUTE (`/tools/board/[id]`), and everything it opens is one too: a// table's list, a record's own screen or its table's page. Never a view it// pushes itself — a board pushed over Home cannot: SwiftUI honours the// `navigationDestination(item:)` for a type nearest the root and ignores a// pushed screen's own (build 228's Disk, `BoardOpensThroughRoutesTests`).importCottonKitimportCottonValuesimportSwiftUIstructToolBoardSurface: View {let tool: ToolDefinitionlet onOpenTable: (TableDefinition) -> Void @Environment(\.tableDefinitions) privatevar definitions @Environment(\.records) privatevar store @Environment(\.theme) privatevar theme/// Each table's chart, by table, for the tables the core draws one for —/// the tile's own chart face, else the table's chart view. @State privatevar charts: [String: ChartSeries] = [:]/// Each table's figures as one pencil line, by table. @State privatevar captions: [String: String] = [:]/// Whether the charts have been asked for: until then no table is drawn/// as a tile it may turn out not to be. @State privatevar read = false/// A tool of several tables is a BOARD OF TILES (architect, 14 Oct 2026):/// one tile per table, the faces Home draws — the table's photographed/// tile with its figures under it, or its chart where it has one — never/// a stack of each table's list.var body: someView {let tables = BoardLayout.tables(for: tool, in: definitions)PaperScreen {Text(tool.name).font(theme.type.title(theme.scale.screenTitleSize))if read {let tiles = tables.filter { charts[$0.id] == nil }if !tiles.isEmpty {BoardToolGrid( tiles: tiles.map(tile), columns: ToolBoardKnobs.columns, size: .large, captions: captions, identifierPrefix: ToolBoardSurface.identifierPrefix ) { tile iniflet definition = tiles.first(where: { $0.id == tile.key }) { onOpenTable(definition) } } }ForEach(tables.filter { charts[$0.id] != nil }) { definition inButton { onOpenTable(definition) } label: { VStack(alignment: .leading, spacing: BoardTileKnobs.faceTileContentGap) {// Named as a table's tile is: the chart alone reads as no table.Text(definition.plural) .font(theme.type.title(theme.scale.titleSize)) .foregroundStyle(theme.print(.ink))ChartDrawing(series: charts[definition.id], name: definition.plural) } .padding(TileMetrics.padding) .frame(maxWidth: .infinity, alignment: .leading) .background(PaperSurface(dye: .cream, edge: .cut, seed: definition.id)) .clipShape(RoundedRectangle(cornerRadius: BoardTileKnobs.faceTileRadius, style: .continuous)) } .buttonStyle(MotionButtonStyle()) .accessibilityIdentifier(ToolBoardSurface.identifierPrefix + definition.id) } } } .task(id: tables.map(\.id)) {guardlet store else { read = true; return }let now = Date()let today = ChartFace.day(now)let at = now.ISO8601Format()await withTaskGroup(of: (String, ChartSeries?, String?).self) { group infor definition in tables { group.addTask {let chart = try? await store.chart(table: definition.id, today: today, at: at)let asks = !definition.tile.summary.isEmptylet figures = asks ? ((try? await store.summarize(table: definition.id)) ?? []) : []return (definition.id, chart ?? nil, SummaryFigure.line(figures, in: definition)) } }var charted: [String: ChartSeries] = [:]var lines: [String: String] = [:]forawait (table, chart, line) in group {iflet chart { charted[table] = chart }iflet line { lines[table] = line } }guard !Task.isCancelled else { return } charts = charted captions = lines } read = true } }/// What a walk finds a table's tile by.staticlet identifierPrefix = "tool-table-"/// A table's tile: its own picture, else the tool's, on the tool's stock,/// under the table's name.privatefunc tile(_ definition: TableDefinition) -> GridTile {GridTile( key: definition.id, name: definition.plural, picture: definition.picture ?? tool.picture?.drawn, stock: tool.cardStock, opens: .route(BoardLayout.listRoute(for: definition)), wears: tool.id) }}/// The sizes ToolBoardSurface draws with: Swift's own, beside their reader.enumToolBoardKnobs {/// A tool's tables across: fewer than Home's tools, so each tile reads.staticlet columns = 3}/// `/tools/board/[id]`: the data tool the route names, from the shown/// database's catalogue, as its board; a table opens its list.structToolBoardRoute: View {let id: String @Environment(\.tableDefinitions) privatevar definitions @Environment(\.records) privatevar store @Environment(Navigator.self) privatevar navigator: Navigator? @State privatevar tool: ToolDefinition? @State privatevar read = falsevar body: someView {Group {iflet tool, let table = BoardLayout.directTable(for: tool, in: definitions) {// A tool over one table — one another tool owns, which is// why its route came here (`BoardLayout.route`) — is ONE list// of the view its ring chose, its own views (`ToolViewRoute`).ToolViewRoute(of: tool.id, table: table.id) } elseiflet tool {ToolBoardSurface(tool: tool) { navigator?.push(BoardLayout.listRoute(for: $0)) } } elseif read {PaperScreen {QuietState(title: ShellLines.line("record", "tool.missing"), message: ShellLines.line("record", "tool.missing_message")) } } else {PaperScreen { EmptyView() } // no loader: the catalogue is local and lands next frame } } .task(id: id) {guardlet store else { return } tool = (try? await store.toolDefinitions())?.first { $0.id == id } read = true } }}/// A tile's records: its face is the first page alone — one local read —/// and the count and link titles fill in after (`filled`).structToolTileRead: Sendable {var records: [TableRecord]/// Nil until counted; a first page with nothing behind it is its own count.var count: Int?var linkTitles: LinkTitles/// The page after the first, while the count still has pages to read.var next: Int?/// The tile's figures (`TileLayout.summary`), over the whole table; read/// with the count, so the face never waits on them.var summaries: [SummaryFigure] = []/// The face: page 0, and its count from the same read — the total the/// store answers with the rows (S6: the count is what the list shows)./// Never a second count read after it: two reads raced, and the heading/// flipped between "3 of 10046" and "3 of 35" from one visit to the next.staticfunc first(table: String, project: String?, query: String?, store: any Records) asyncthrows -> Self {let first = tryawait store.list(table: table, project: project, query: query, page: 0)let count = first.total ?? (first.next == nil ? first.records.count : nil)returnSelf(records: first.records, count: count, linkTitles: LinkTitles(), next: count == nil ? first.next : nil) }/// The face with its count and its link titles read. `definition` names/// the links; nil asks the store's definitions for it.func filled(table: String, project: String?, query: String?, definition: TableDefinition?, store: any Records) asyncthrows -> Self {var count = self.count ?? records.count// Only a store that answers no total is paged through for its count.var next = self.count == nil ? self.next : nilwhilelet page = next {tryTask.checkCancellation()let result = tryawait store.list(table: table, project: project, query: query, page: page) count += result.records.count next = result.next }let named: TableDefinition?iflet definition { named = definition } else { named = tryawait store.tableDefinitions().first { $0.id == table } }let links = named?.links(in: records) ?? []let titles = links.isEmpty ? LinkTitles() : ((try? await store.linkTitles(for: links)) ?? LinkTitles())let asksFigures = named?.tile.summary.isEmpty == falselet summaries = asksFigures ? ((try? await store.summarize(table: table)) ?? []) : []returnSelf(records: records, count: count, linkTitles: titles, next: nil, summaries: summaries) }/// The whole read at once: the face, then its count and titles.staticfunc load(table: String, project: String?, query: String?, store: any Records) asyncthrows -> Self {tryawait first(table: table, project: project, query: query, store: store) .filled(table: table, project: project, query: query, definition: nil, store: store) }}