// 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`). import CottonKit import CottonValues import SwiftUI struct ToolBoardSurface: View { let tool: ToolDefinition let onOpenTable: (TableDefinition) -> Void @Environment(\.tableDefinitions) private var definitions @Environment(\.records) private var store @Environment(\.theme) private var 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 private var charts: [String: ChartSeries] = [:] /// Each table's figures as one pencil line, by table. @State private var 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 private var 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: some View { 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 in if let definition = tiles.first(where: { $0.id == tile.key }) { onOpenTable(definition) } } } ForEach(tables.filter { charts[$0.id] != nil }) { definition in Button { 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)) { guard let 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 in for definition in tables { group.addTask { let chart = try? await store.chart(table: definition.id, today: today, at: at) let asks = !definition.tile.summary.isEmpty let 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] = [:] for await (table, chart, line) in group { if let chart { charted[table] = chart } if let line { lines[table] = line } } guard !Task.isCancelled else { return } charts = charted captions = lines } read = true } } /// What a walk finds a table's tile by. static let identifierPrefix = "tool-table-" /// A table's tile: its own picture, else the tool's, on the tool's stock, /// under the table's name. private func 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. enum ToolBoardKnobs { /// A tool's tables across: fewer than Home's tools, so each tile reads. static let 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. struct ToolBoardRoute: View { let id: String @Environment(\.tableDefinitions) private var definitions @Environment(\.records) private var store @Environment(Navigator.self) private var navigator: Navigator? @State private var tool: ToolDefinition? @State private var read = false var body: some View { Group { if let 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) } else if let tool { ToolBoardSurface(tool: tool) { navigator?.push(BoardLayout.listRoute(for: $0)) } } else if 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) { guard let 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`). struct ToolTileRead: 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. static func first(table: String, project: String?, query: String?, store: any Records) async throws -> Self { let first = try await store.list(table: table, project: project, query: query, page: 0) let count = first.total ?? (first.next == nil ? first.records.count : nil) return Self(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) async throws -> 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 : nil while let page = next { try Task.checkCancellation() let result = try await store.list(table: table, project: project, query: query, page: page) count += result.records.count next = result.next } let named: TableDefinition? if let definition { named = definition } else { named = try await 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 == false let summaries = asksFigures ? ((try? await store.summarize(table: table)) ?? []) : [] return Self(records: records, count: count, linkTitles: titles, next: nil, summaries: summaries) } /// The whole read at once: the face, then its count and titles. static func load(table: String, project: String?, query: String?, store: any Records) async throws -> Self { try await first(table: table, project: project, query: query, store: store) .filled(table: table, project: project, query: query, definition: nil, store: store) } }