// The faces that hold several records: a section (the project page's heading, // its count and three slips) and a month (the calendar grid, a tile only). import CottonValues import SwiftUI /// A heading, a count and the first three slips, with the chevron that says /// the heading opens the whole list — the project overview's section. public struct SectionFace: View { public static let shown = 3 @Environment(\.theme) private var theme private let definition: TableDefinition private let records: [TableRecord] private let total: Int private let onOpen: (() -> Void)? private let headed: Bool private let onToggle: ((TableRecord) -> Void)? private let onSlip: ((TableRecord) -> Void)? /// - Parameter total: how many records the table holds under the current /// filter; `records` may be only the first page of them. /// - Parameter headed: false under a heading somebody else drew (a /// section of the database page), so the name is said once. /// - Parameter onToggle: what a tap on a slip's check does; nil draws the /// check without a press. /// - Parameter onSlip: what a tap on the rest of a slip does. public init( definition: TableDefinition, records: [TableRecord], total: Int? = nil, headed: Bool = true, onOpen: (() -> Void)? = nil, onToggle: ((TableRecord) -> Void)? = nil, onSlip: ((TableRecord) -> Void)? = nil ) { self.definition = definition self.records = records self.total = total ?? records.count self.headed = headed self.onOpen = onOpen self.onToggle = onToggle self.onSlip = onSlip } /// "3 of 12", or the count alone when every record is shown. public static func count(shown: Int, total: Int) -> String { shown < total ? "\(shown) of \(total)" : "\(total)" } public var body: some View { let first = Array(records.prefix(Self.shown)) VStack(alignment: .leading, spacing: GroupFacesKnobs.faceSectionSlipGap) { if headed { Button { onOpen?() } label: { ProjectSectionHeading(title: definition.plural, count: Self.count(shown: first.count, total: total)) } .buttonStyle(.plain) .disabled(onOpen == nil) } ForEach(first) { record in let slip = SlipFace(definition: definition, record: record, onToggle: onToggle.map { toggle in { toggle(record) } }) if let onSlip { // A tap, never a button around the slip: the check inside // it is a button of its own. slip.contentShape(Rectangle()) .onTapGesture { onSlip(record) } .accessibilityAddTraits(.isButton) } else { slip } } } } } /// The month grid with a dot under each day per record dated on it, up to /// three. A tile face: the calendar grid is not a screen in the app. public struct MonthFace: View { private let definition: TableDefinition private let records: [TableRecord] private let now: Date @Environment(\.calendar) private var calendar public init(definition: TableDefinition, records: [TableRecord], now: Date = Date()) { self.definition = definition self.records = records self.now = now } /// Records per day key ("yyyy-MM-dd") by the table's date column. public static func dotCounts(_ definition: TableDefinition, _ records: [TableRecord], calendar: Calendar) -> [String: Int] { var counts: [String: Int] = [:] for record in records { guard let iso = record[definition.date].text, let key = FaceText.dayKey(iso, calendar: calendar) else { continue } counts[key, default: 0] += 1 } return counts } public var body: some View { let today = CalendarDates.todayKey(now: now, calendar: calendar) MonthGrid( monthKey: today, selectedKey: today, dotCounts: Self.dotCounts(definition, records, calendar: calendar), now: now, onSelectDay: { _ in } ) } } /// The sizes, counts and timings GroupFaces draws with: Swift's own, beside /// their reader — a theme sets only its tokens (`scale.tokens`). enum GroupFacesKnobs { static let faceSectionSlipGap: CGFloat = 12 }