// Home's page as a deck, a READER of the core's answer (`shell.today`; the // shell boundary, 7 Nov 2026): the cards in the order the core dealt them — // this device's own card, today's rows across every database held, the // birthdays and money due, the unread conversations, then a card for each of // Home's bands with something to say — each worn in the face the core named // (`DeckFace.view`), its tap the core's. A row with no record of its own to // wear (an occurrence of a repeating event) wears the `today` face // (`TodayFace`). Each card opens its record in its own database // (`FeedRowOpening`); this device's card opens in place. The words, the order // and the faces are the core's; Home decides none of them. import CottonKit import CottonValues import SwiftUI /// One of Home's bands with something to say now — its notices, what is /// asked of the person, what was done for them — drawn on a card of its own. struct HomePartCard { let id: String let face: AnyView } struct HomeToday: View { @Environment(\.theme) private var theme @Environment(Navigator.self) private var navigator: Navigator? @Environment(\.screenRoute) private var screenRoute @Environment(DatabasesModel.self) private var databases: DatabasesModel? @Environment(DatabaseScene.self) private var scene: DatabaseScene? @Environment(ToastCenter.self) private var toasts: ToastCenter? @Environment(\.cottonSync) private var sync @Environment(\.records) private var store @Environment(\.tableDefinitions) private var definitions /// Home's page, as the core answered it. let home: HomeModel /// Home's whole page as the deck (the phone, cards on). Off, a section of /// Home's page: the day's heading over its rows. var deckPage = false /// The record Home's `device` part names, read from the board's box. var devicePart = HomeDevicePart() /// The faces of Home's bands, by part (`notices`, `asked`, `done`). var parts: [HomePartCard] = [] /// The board's `today` part, for the section's rows and quiet line. var today: BoardEntry? = nil /// This device's card opened in place on Home. @State private var deviceOpened = false private var cards: [TodayAnswer.Card] { home.answer?.cards ?? [] } var body: some View { Group { if deckPage { deck } else { section } } .accessibilityElement(children: .contain) .accessibilityIdentifier("home-today") } /// Today as a section of Home's page: the day's heading over its rows. private var section: some View { VStack(alignment: .leading, spacing: 10) { Text(DayHeads.today) .font(theme.type.title(HomeTodayKnobs.headingSize)) .foregroundStyle(theme.colors.ink.color) .accessibilityAddTraits(.isHeader) let rows = (today?.cards ?? []).compactMap { id in cards.first { $0.id == id } } if !rows.isEmpty { ForEach(rows, id: \.id) { card in Button { open(card) } label: { ObjectSlip( seed: card.id, dye: card.dye.flatMap(PaperDye.init(rawValue:)) ?? .cream, title: card.deck.slots["title"]?.text ?? "", meta: card.deck.slots["line"]?.text, struck: false) } .buttonStyle(.plain) .accessibilityIdentifier(Self.identifier(card)) .modifier(UnreadUnderTheFan()) } } else { Text(today?.quiet ?? " ") .font(theme.type.body(HomeTodayKnobs.quietSize)) .foregroundStyle(theme.colors.inkSecondary.color) } } } /// What reads this device's card again: each sync revision, each pull /// landing, each change of phase and each write this device makes. private struct DeviceReads: Hashable { var device: String? var revision: Int var pulled: String? var phase: String var local: Int } private static let labels = DeckAnswer.Labels(by_slot: [:], by_column: [:]) private var deck: some View { CardDeck( items: cards, emptyLine: home.answer?.empty ?? ShellLines.line("board", "today.empty"), open: open, words: { card in [card.deck.slots["title"]?.text, card.deck.slots["body"]?.text].compactMap { $0 }.joined(separator: " ") }, plus: (ShellLines.line("capture", "composer.make_something"), { navigator?.open(.capture, start: .typing) }), // A hold on the sync card opens it in place, as a tap does. holds: { card in if card.kind == "device" { deviceOpened = true } }, opened: deviceOpened ? cards.first { $0.kind == "device" }?.id : nil ) { card in face(card) } .preference(key: FanPageReadingPreference.self, value: FanPageReading(filled: devicePart.id == nil || home.answer != nil)) // This device's card moves with the sync (rows done of total, bytes, // synced at): read alone, on each pass and every few seconds. .task(id: DeviceReads( device: devicePart.id, revision: sync?.revision ?? 0, pulled: sync?.lastPullAt, phase: sync?.loop?.state ?? "", local: sync?.localWrites ?? 0)) { guard devicePart.id != nil else { return } while !Task.isCancelled { await home.readDevice() try? await Task.sleep(for: .seconds(HomeSyncKnobs.every)) } } } @ViewBuilder private func face(_ card: TodayAnswer.Card) -> some View { let drawn = card.deck.drawn(labels: Self.labels) let record = card.deck.record.record switch card.kind { case "part": // Home's own rows and buttons: the deck's zones never move over them. if let part = parts.first(where: { "part/" + $0.id == card.id }) { part.face .deckKeepsTaps() .padding(TodayFaceKnobs.inset) .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading) .clipped() .onCardPaper(.framed) .modifier(UnreadUnderTheFan()) } default: if DeckFace.draws(drawn) { DeckFace.view(for: drawn, record: record) { tick(card) } .modifier(HomeOpensInPlace(opened: deviceOpened && card.kind == "device", record: record, card: drawn, definition: card.table.flatMap { definitions[$0] }) { deviceOpened = false }) .environment(\.faceChoiceTable, card.table) .accessibilityElement(children: .contain) .accessibilityIdentifier(Self.identifier(card)) .modifier(UnreadUnderTheFan()) } else if card.deck.face == "today" { let kind = card.deck.slots["kind"]?.text TodayFace(today: TodayItem(title: card.deck.slots["title"]?.text ?? "", done: false, line: card.deck.slots["line"]?.text), kind: kind) { side in ToolObject(name: kind ?? "", picture: ToolObjects.picture(for: card.deck.slots["picture"]?.text ?? "todos"), printing: .contentOnly, side: side) } .accessibilityElement(children: .contain) .accessibilityIdentifier(Self.identifier(card)) .modifier(UnreadUnderTheFan()) } else { Color.clear.onCardPaper(.framed) } } } /// The name a card answers to, kept whichever face draws it. static func identifier(_ card: TodayAnswer.Card) -> String { switch card.kind { case "day": "home-today-\(card.id.dropFirst("day/".count))" case "thread": "home-today-thread-\(card.deck.record.id)" case "part": "home-part-\(card.id.dropFirst("part/".count))" default: "home-later-\(card.deck.record.id)" } } /// A tick on a card's own check: what the core says it writes, then Home /// reads again. private func tick(_ card: TodayAnswer.Card) { guard let store, let table = card.table, let patch = card.deck.toggle, !patch.isEmpty else { return } Task { _ = try? await store.update(table: table, id: card.deck.record.id, patch: patch) await home.load() } } private func open(_ card: TodayAnswer.Card) { guard let navigator else { return } switch card.kind { case "device": deviceOpened = true case "day": guard let opens = card.opens else { return } // A row of the shown database opens on its tool's deck, the fan // turned to it; one of another database switches to it first. if opens.database == nil, navigator.fanBrings?(opens.table, opens.page, false) == true { return } // The record's own screen, as its definition routes it; a // repeating event's occurrence opens at the start the core names. let own = BoardRecord.route(table: opens.table, id: opens.id, definition: definitions[opens.table]) let route = opens.at.flatMap { Route.parse(Route.join(own.path, [("at", $0)])) } ?? own navigator.rowOpened[screenRoute ?? route] = PathRecord(table: opens.table, record: card.deck.record.record) FeedRowOpening(model: databases, scene: scene, toasts: toasts).open(route, in: card.database?.row, on: navigator) case "thread", "later": guard let table = card.table else { return } navigator.open(table: table, id: card.deck.record.id, definition: definitions[table]) default: return } } } /// A Home card opened in place, where its table's definition is held: only /// the sync card ever opens. private struct HomeOpensInPlace: ViewModifier { let opened: Bool let record: TableRecord let card: DeckCard let definition: TableDefinition? let close: () -> Void func body(content: Content) -> some View { if let definition { content.opensInPlace(opened, definition: definition, record: record, worn: card.face, close: close) } else { content } } } enum HomeTodayKnobs { static let headingSize: CGFloat = 22 static let quietSize: CGFloat = 16 }