// A table's tile: its face at tile size — what the board shows for a tool. // The definition's tile // layout names the face; the size decides how many records it holds. The slip // list is a column of the table's own slips (`SlipListFace`), as many as // stand whole, the rest counted. import CottonValues import SwiftUI public struct BoardTile: View { @Environment(\.theme) private var theme private let definition: TableDefinition private let records: [TableRecord] private let size: TileSize private let now: Date private let onOpenRecord: ((TableRecord) -> Void)? /// The rounded box is the TILE's frame. A section on the database page /// is the same face with no frame: the page is its frame, the section's /// heading names it, and it is as tall as what it holds. private let framed: Bool public init(definition: TableDefinition, records: [TableRecord], size: TileSize, now: Date = Date(), framed: Bool = true, onOpenRecord: ((TableRecord) -> Void)? = nil) { self.framed = framed self.definition = definition self.records = records self.size = size self.now = now self.onOpenRecord = onOpenRecord } /// How many records a face shows at a size, by the theme's counts. public static func capacity(_ face: TileFace, _ size: TileSize, scale: ThemeScale) -> Int { switch (face, size) { // More slips than the tile holds: its column places those that stand // whole and counts the rest as "+N more" (`SlipTileColumn`). case (.slipList, _): return SlipTileLayout.capacity(size) // One row of covers on the small and medium canvases, two on the large. case (.coverGrid, .small), (.coverGrid, .medium): return coverColumns(size, scale: scale) case (.coverGrid, .large): return coverColumns(size, scale: scale) * BoardTileKnobs.faceBoardTileCoverRowsLarge case (.polaroidGrid, .small), (.capGrid, .small): return BoardTileKnobs.faceBoardTileGridCapacitySmall case (.polaroidGrid, .medium), (.capGrid, .medium): return BoardTileKnobs.faceBoardTileGridCapacityMedium case (.polaroidGrid, .large), (.capGrid, .large): return BoardTileKnobs.faceBoardTileGridCapacityLarge case (.sectionList, _): return SectionFace.shown case (.receipt, _): return 1 case (.cardGrid, _), (.month, _), (.sparkline, _), (.chart, _): return 0 } } /// Columns of a grid face at a size. static func columns(_ size: TileSize, scale: ThemeScale) -> Int { size == .small ? BoardTileKnobs.faceBoardTileGridColumnsSmall : BoardTileKnobs.faceBoardTileGridColumns } /// Paperback covers across at a size: as many as fit the canvas's height /// under the tile's name — five on the short medium canvas, four on the /// large one's two rows. static func coverColumns(_ size: TileSize, scale: ThemeScale) -> Int { switch size { case .small: return BoardTileKnobs.faceBoardTileCoverColumnsSmall case .medium: return BoardTileKnobs.faceBoardTileCoverColumnsMedium case .large: return BoardTileKnobs.faceBoardTileCoverColumnsLarge } } public var body: some View { let shown = Array(records.prefix(Self.capacity(definition.tile.face, size, scale: theme.scale))) // The slip list stands every record the core chose for it // (`shell.list {tile}`), so "+N more" counts what it left out. let drawn = face(definition.tile.face == .slipList ? records : shown) return Group { if framed { drawn .padding(TileMetrics.padding) .frame(width: size.canvas.width, height: size.canvas.height, alignment: .topLeading) .background(PaperSurface(dye: .cream, edge: .cut, seed: definition.id)) .clipShape(RoundedRectangle(cornerRadius: BoardTileKnobs.faceTileRadius, style: .continuous)) } else { drawn.frame(maxWidth: .infinity, alignment: .topLeading) } } .accessibilityElement(children: .contain) .accessibilityLabel(definition.plural) } @ViewBuilder private func face(_ shown: [TableRecord]) -> some View { switch definition.tile.face { case .slipList: SlipListFace(definition: definition, records: shown, now: now, named: framed, onOpenRecord: onOpenRecord) case .coverGrid: let count = Self.coverColumns(size, scale: theme.scale) let gap = BoardTileKnobs.faceTileGridGap let width = (size.canvas.width - TileMetrics.padding * 2 - CGFloat(count - 1) * gap) / CGFloat(count) VStack(alignment: .leading, spacing: BoardTileKnobs.faceTileContentGap) { if framed { name } LazyVGrid(columns: Array(repeating: GridItem(.fixed(width), spacing: gap, alignment: .top), count: count), alignment: .leading, spacing: gap) { ForEach(shown) { record in opening(record) { CoverFace(definition: definition, record: record, width: width) } } } } case .polaroidGrid, .capGrid: let count = Self.columns(size, scale: theme.scale) let gap = BoardTileKnobs.faceTileGridGap let width = (size.canvas.width - TileMetrics.padding * 2 - CGFloat(count - 1) * gap) / CGFloat(count) // Offered the page's whole width (unframed), the cells start under // the heading rather than centred in the width. LazyVGrid(columns: Array(repeating: GridItem(.fixed(width), spacing: gap), count: count), alignment: .leading, spacing: gap) { ForEach(shown) { record in opening(record) { if definition.tile.face == .capGrid { CapFace(definition: definition, record: record, side: width) } else { PolaroidFace(definition: definition, record: record, width: width) } } } } case .cardGrid: CardFace(definition: definition, now: now) case .month: MonthFace(definition: definition, records: records, now: now) case .sectionList: SectionFace(definition: definition, records: records, headed: framed) case .receipt: ForEach(shown) { record in opening(record) { ReceiptFace(definition: definition, record: record) } } case .sparkline: VStack(alignment: .leading, spacing: BoardTileKnobs.faceTileContentGap) { if framed { name } SparklineFace(definition: definition) } case .chart: VStack(alignment: .leading, spacing: BoardTileKnobs.faceTileContentGap) { if framed { name } ChartFace(definition: definition, now: now) } } } /// The faces drawn one record to an object, each of which opens ITS /// record when the tile is given `onOpenRecord` (S1 extended); the others /// (a month, a card, a section list) are the tool as a whole. static func opensRecords(_ face: TileFace) -> Bool { switch face { case .coverGrid, .slipList, .polaroidGrid, .capGrid, .receipt: return true case .cardGrid, .month, .sectionList, .sparkline, .chart: return false } } /// One record's object: a button opening that record when the tile opens /// records, else the face alone (a preview). @ViewBuilder private func opening(_ record: TableRecord, @ViewBuilder face: () -> some View) -> some View { if let onOpenRecord { Button { onOpenRecord(record) } label: { face().contentShape(Rectangle()) } .buttonStyle(MotionButtonStyle()) .accessibilityIdentifier("tile-\(definition.id)-\(record.id)") .modifier(UnreadUnderTheFan()) } else { face().modifier(UnreadUnderTheFan()) } } /// The table's name over a list or a grid of covers. private var name: some View { Text(definition.plural) .font(theme.type.title(size == .small ? BoardTileKnobs.faceBoardTileNameSmallSize : BoardTileKnobs.faceBoardTileNameSize)) .foregroundStyle(theme.print(.ink)) .lineLimit(1) } } /// The sizes, counts and timings BoardTile draws with: Swift's own, beside /// their reader — a theme sets only its tokens (`scale.tokens`). enum BoardTileKnobs { static let faceBoardTileCoverColumnsLarge = 4 static let faceBoardTileCoverColumnsMedium = 5 static let faceBoardTileCoverColumnsSmall = 2 static let faceBoardTileCoverRowsLarge = 2 static let faceBoardTileGridCapacityLarge = 8 static let faceBoardTileGridCapacityMedium = 4 static let faceBoardTileGridCapacitySmall = 4 static let faceBoardTileGridColumns = 4 static let faceBoardTileGridColumnsSmall = 2 static let faceBoardTileNameSize: CGFloat = 15 static let faceBoardTileNameSmallSize: CGFloat = 16 static let faceTileContentGap: CGFloat = 8 static let faceTileGridGap: CGFloat = 8 static let faceTileRadius: CGFloat = 22 }