// COTTON DATA'S STATE (assets/design/data/DECISIONS.md "The structure"; Android's `DataState.kt`): the tab at the// foot, the screens stood on over it, the sheet up from the foot and the toast. Every word is the core's (`data.*`// answers and the `data` line set); nothing is decided here, and nothing here writes to the database.#if os(iOS)importCottonKitimportSwiftUIimport UIKit/// The four places at the foot.enumDataTab: String, CaseIterable, Identifiable {case tables, lately, search, youvar id: String { rawValue }}/// A screen stood on over a tab, newest last.enumDataScreen: Hashable, Identifiable {/// A table's rows (`data.rows`).case rows(String)/// One row (`data.row`), with the trail of rows followed to reach it.case row(table: String, id: String, trail: [DataStep])case about(String)case columns(String)/// How a table links; a neighbour tapped stands in the middle.case links(String)case room, leftovers, assistants, keep, lookvar id: String {switchself {case .rows(let t): "rows-\(t)"case .row(let t, let id, let trail): "row-\(t)-\(id)-\(trail.count)"case .about(let t): "about-\(t)"case .columns(let t): "columns-\(t)"case .links(let t): "links-\(t)"case .room: "room"case .leftovers: "leftovers"case .assistants: "assistants"case .keep: "keep"case .look: "look" } }}/// What stands up from the foot.enumDataSheetKind: Hashable, Identifiable {/// A link opened (`data.link`).case link(table: String, id: String, column: String)/// A picture or file opened (`data.media`, its bytes by `media.ensure`).case media(table: String, id: String, column: String)var id: String {switchself {case .link(let t, let i, let c): "link-\(t)-\(i)-\(c)"case .media(let t, let i, let c): "media-\(t)-\(i)-\(c)" } }}@MainActor @ObservablefinalclassDataModel {let core: any CottonCallinglet calls: DataCallsvar tab: DataTab = .tablesprivate(set) var stack: [DataScreen] = []var sheet: DataSheetKind?private(set) var toast: String? @ObservationIgnored privatevar toastTask: Task<Void, Never>?/// Bumped when the database changes under the app (signing in, a sync): each screen asks its answer again.private(set) var revision = 0/// Tables has its first answer: the launch's mark may lift, showing the list whole, never an empty title.var tablesShown = false/// How many times a tab was tapped: a tab's root goes back to its top on each.private(set) var placed = 0/// The tabs opened at least once: a tab's screen is made the first time it is chosen, then kept.private(set) var visited: Set<DataTab> = [.tables]/// The first screen was shown on this phone (DECISIONS "Opening": first open only).private(set) var welcomed: Boolprivatestaticlet welcomedKey = "data-app.welcomed"init(core: any CottonCalling) {self.core = coreself.calls = DataCalls(core: core) welcomed = UserDefaults.standard.bool(forKey: Self.welcomedKey) }func welcome() {UserDefaults.standard.set(true, forKey: Self.welcomedKey) welcomed = true }funcopen(_ screen: DataScreen) { sheet = nil stack.append(screen) UIAccessibility.post(notification: .screenChanged, argument: nil) }/// Back one screen; `steps` back along a row's trail.func back(_ steps: Int = 1) {guard !stack.isEmpty else { return } stack.removeLast(min(steps, stack.count)) UIAccessibility.post(notification: .screenChanged, argument: nil) }/// How It Links' neighbour tapped: it moves to the middle, in place.func recentre(_ table: String) {guard !stack.isEmpty else { return } stack[stack.count - 1] = .links(table) }func place(_ next: DataTab) { tab = next visited.insert(next) stack.removeAll() sheet = nil placed += 1 UIAccessibility.post(notification: .screenChanged, argument: nil) }func changed() { revision += 1 }func tell(_ words: String?) {guardlet words, !words.isEmpty else { return } toastTask?.cancel() withAnimation(.easeOut(duration: 0.2)) { toast = words } toastTask = Task { @MainActor intry? awaitTask.sleep(for: .seconds(3))guard !Task.isCancelled else { return } withAnimation(.easeOut(duration: 0.3)) { toast = nil } } }/// A refusal in the core's words; anything else, the app's own line for a read that did not load. nonisolated staticfunc words(_ error: Error) -> String { (error as? DataRefusal)?.words ?? DataWords.line("refused.failed") }/// Where Back goes from the screen on top: the tab's name, or the screen below's name.var backWord: String { DataWords.line("tab.\(tab.rawValue)") }}/// One screen's reads of the core, ONE AT A TIME (DATA iPhone 3, the owner's build 2 during his first sync: every/// inner screen stood blank). A screen reads again when the database moves under it (`model.revision`), and while a/// sync pass runs that moves every quarter second. A core call cannot be called off, so each move started one more/// read beside the last, each of them several round trips to a server busy with the pass: they piled up faster than/// they came back, and no screen ever had its first answer. Now a move while a read is out asks once more after it/// lands (and a moment later), never beside it. The read runs on its own task, so a screen's task called off by the/// next move leaves it running to its answer.@MainActor @ObservablefinalclassReadInTurn {/// A read is out.private(set) var asking = false/// When the read that is out began: the screen says it is slow after a while.private(set) var since: Date? @ObservationIgnored privatevar again = false/// After a read, how long before the next one a move asked for (a sync pass moves every quarter second).staticlet pause: Duration = .seconds(2)func ask(_ work: @escaping @MainActor () async -> Void) {if asking { again = true; return } asking = true since = Date()Task { @MainActor inawait work()while again { again = falsetry? awaitTask.sleep(for: Self.pause) since = Date()await work() } asking = false since = nil } }}/// One core read on a screen: the answer, or the refusal in the core's own words.enumDataRead<A: Sendable>: Sendable {case readingcase read(A)case refused(String)var answer: A? { ifcase .read(let a) = self { a } else { nil } }var refusal: String? { ifcase .refused(let r) = self { r } else { nil } } @MainActor staticfunc of(_ work: @MainActor () asyncthrows -> A) async -> DataRead<A> {do { return .read(tryawait work()) } catch { return .refused(DataModel.words(error)) } }}#endif