// Home's one sync line, the board's `sync` part (lane syncface, 21 Oct 2026): // where this device's copy of the shown database stands, in ONE line the core // chose from typed states (`rules::sync_status`) and said in the shell's // words (`kits/shell/lines/sync.json`) — "Up to date with Studio Mac · 3 min // ago", "Receiving 40 changes from Studio Mac", "Studio Mac not reached since // 2 h ago". Swift composes no word of it: it picks the ink by the state — // quiet grey at rest, print ink while working, red only when the Mac was not // reached. No spinner, no badge. A tap opens this device's own record page. // // Read from the core's one answer for Home (`shell.home`'s `sync`: the line // and its typed state), off the phone's own copy, so it answers offline; again // on every pass the window's sync model sees, and on a slow tick for the ages. // With nothing read it keeps its room. import CottonKit import CottonValues import SwiftUI /// The core's resolved line: its typed state and its words. struct SyncLineRead: Decodable, Equatable, Sendable { let state: String let words: String /// The quiet states: nothing is moving. var atRest: Bool { state == "up_to_date" || state == "reader_only" || state == "alone" } var notReached: Bool { state == "not_reached" } } struct HomeSync: View { @Environment(\.cottonCore) private var core @Environment(\.cottonSync) private var sync @Environment(\.theme) private var theme @Environment(Navigator.self) private var navigator: Navigator? @Environment(DatabaseScene.self) private var scene: DatabaseScene? @State private var line: SyncLineRead? var body: some View { if let line = line ?? HomeSyncLines.last[scene?.shownId ?? ""], !line.words.isEmpty { Button { navigator?.open(.device) } label: { Text(line.words) .font(theme.type.body(HomeSyncKnobs.size)) .foregroundStyle(ink(line)) .frame(maxWidth: .infinity, alignment: .leading) .contentShape(Rectangle()) } .buttonStyle(.plain) .accessibilityIdentifier("home-sync") .accessibilityValue(line.state) .background { reader } } else { // Its room before its first read: Today stands where it will // stay, never stepping down as the line arrives (294 row 3, // walker293). Text(verbatim: " ").font(theme.type.body(HomeSyncKnobs.size)) .hidden() .accessibilityHidden(true) .background { reader } } } private func ink(_ line: SyncLineRead) -> Color { if line.notReached { return theme.print(.red) } return line.atRest ? theme.colors.inkSecondary.color : theme.print(.ink) } /// What moves the line: the shown database, and each pass the window's /// sync model sees (a pull landing, an outbox draining, a media move). private struct Moves: Equatable { let shown: String? let pulled: String? let pending: Int } private var reader: some View { Color.clear .task(id: Moves(shown: scene?.shownId, pulled: sync?.lastPullAt, pending: sync?.pendingOps ?? 0)) { while !Task.isCancelled { await read() try? await Task.sleep(for: .seconds(HomeSyncKnobs.every)) } } } private func read() async { guard let core, let own = await Self.line(core), !Task.isCancelled else { return } line = own HomeSyncLines.last[scene?.shownId ?? ""] = own } /// This device's line for the database `core` is aimed at; nil when it /// could not be read or the answer carries none. static func line(_ core: any CottonCalling) async -> SyncLineRead? { guard let sync = try? await core.call(CottonMethod.shellHome, as: HomeAnswer.self).sync, let state = sync.line_state else { return nil } return SyncLineRead(state: state, words: sync.line) } /// Read `database`'s line now, through `core` — aimed at it — and keep it /// (`HomeSyncLines`): a switch reads it before it turns (`HomeModel.prepare`), /// so the page it rebuilds draws the line in its first frame, never its /// empty room (295 A, walker294). static func prepare(_ core: any CottonCalling, database: String) async { guard let own = await line(core) else { return } HomeSyncLines.last[database] = own } /// This device's copy of `database` in copy.json's words — what it is /// doing and when it last synced — or nil-nil when it holds no row; nil /// when the rows could not be read. A read still out (`StillReading`) /// says it. static func thisDevice(_ core: any CottonCalling, database: String?) async -> (state: String?, when: String?)? { guard let rows = await CopyRows.read(core, database: database) else { return nil } guard let own = rows.first(where: { $0["role"].text == HomeSync.thisDevice }) else { return (nil, nil) } return (own["@slip_subtitle"].text, own["@slip_caption"].text) } static let table = "copy" static let thisDevice = "this_device" } /// A database's `copy` rows as this device's own file holds them, read /// SINGLE-FLIGHT (architect's ruling 21:00 IST 16 Oct 2026): while a read for /// a database is out, every other asker — the next tick, Me's line on the /// same page, a remount after a switch — waits for its /// answer and never starts a second. A Swift task cancelled mid-read does not /// stop the core's call, so a read restarted beside it stacked one more call /// on the read pool every 20 s on a starved file (realfan, 16 Oct 2026). @MainActor enum CopyRows { private static var flights: [String: Task<[TableRecord]?, Never>] = [:] /// Nil when the rows could not be read. static func read(_ core: any CottonCalling, database: String?) async -> [TableRecord]? { let key = database ?? "" if let flight = flights[key] { return await flight.value } let flight = Task { () -> [TableRecord]? in var request: [String: RecordValue] = ["table": .text(HomeSync.table)] if let database { request["where"] = .object(["database": .text(database)]) } let rows = try? await core.call(CottonMethod.recordsList, request, as: Page.self) // The flight clears itself, before any asker resumes, so the // next ask after it lands starts one fresh read. flights[key] = nil return rows?.records.map(\.record) } flights[key] = flight return await flight.value } private struct Page: Decodable { let records: [CoreRecords.WireRecord] } } /// The record Home's board names on its `device` part, in a box Home's page /// reads (`HomeToday`) and the board fills each time it is read /// (`BoardSurface`). A box, as Me's print is (`HomeRead`), and never a value /// handed down: Home is drawn inside the fan's live page, which draws again /// only when its tile or database moves (`FanLivePage`), so the board read /// that lands after a database turned — the fresh one an Erase starts — must /// reach the deck by observation. @MainActor @Observable final class HomeDevicePart { var id: String? init(id: String? = nil) { self.id = id } /// The card the answer's `device` part names; nil when it names none /// (no such part, or no identity yet). static func named(in parts: [BoardEntry]) -> String? { parts.first { $0.kind == "device" }?.card } } enum HomeSyncKnobs { static let size: CGFloat = 16 /// Seconds between reads while Me is shown. static let every: Double = 20 } /// Each database's last sync line, over a switch: the switch rebuilds Me's /// page and its line arrived last, pushing Today down under a page already /// drawn (293 row 2, walker292). A database's line is drawn as it last read /// — or as the switch read it before turning (`HomeSync.prepare`) — until its /// read answers again. @MainActor enum HomeSyncLines { static var last: [String: SyncLineRead] = [:] }