// 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) import CottonKit import SwiftUI import UIKit /// The four places at the foot. enum DataTab: String, CaseIterable, Identifiable { case tables, lately, search, you var id: String { rawValue } } /// A screen stood on over a tab, newest last. enum DataScreen: 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, look var id: String { switch self { 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. enum DataSheetKind: 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 { switch self { 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 @Observable final class DataModel { let core: any CottonCalling let calls: DataCalls var tab: DataTab = .tables private(set) var stack: [DataScreen] = [] var sheet: DataSheetKind? private(set) var toast: String? @ObservationIgnored private var toastTask: Task? /// 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 = [.tables] /// The first screen was shown on this phone (DECISIONS "Opening": first open only). private(set) var welcomed: Bool private static let welcomedKey = "data-app.welcomed" init(core: any CottonCalling) { self.core = core self.calls = DataCalls(core: core) welcomed = UserDefaults.standard.bool(forKey: Self.welcomedKey) } func welcome() { UserDefaults.standard.set(true, forKey: Self.welcomedKey) welcomed = true } func open(_ 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?) { guard let words, !words.isEmpty else { return } toastTask?.cancel() withAnimation(.easeOut(duration: 0.2)) { toast = words } toastTask = Task { @MainActor in try? await Task.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 static func 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 @Observable final class ReadInTurn { /// 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 private var again = false /// After a read, how long before the next one a move asked for (a sync pass moves every quarter second). static let pause: Duration = .seconds(2) func ask(_ work: @escaping @MainActor () async -> Void) { if asking { again = true; return } asking = true since = Date() Task { @MainActor in await work() while again { again = false try? await Task.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. enum DataRead: Sendable { case reading case read(A) case refused(String) var answer: A? { if case .read(let a) = self { a } else { nil } } var refusal: String? { if case .refused(let r) = self { r } else { nil } } @MainActor static func of(_ work: @MainActor () async throws -> A) async -> DataRead { do { return .read(try await work()) } catch { return .refused(DataModel.words(error)) } } } #endif