Cotton code
55 lines · 2.3 KB · SwiftRaw
// What a list or a page says while its first read is still out (292 row 5,// 290 walk: Storage stood blank for 20 s and Import's rows never came, with no// word either way). A local read lands within a frame or two and draws// nothing here; one still out past `StillReadingKnobs.after` says what this// device is doing with the database now — its own `copy` row in copy.json's// words, the line Me draws ("Catching up", "3 changes waiting to send · This// device · synced 2 min ago"). Every word is read.sync_state's, through the// definition: no sentence is composed here, and a wait names the event the// core names, never the time of this read.import CottonKitimport SwiftUIstruct StillReading: View {    @Environment(\.cottonCore) private var core    @Environment(\.theme) private var theme    @Environment(DatabaseScene.self) private var scene: DatabaseScene?    @State private var line: (state: String?, when: String?)?    var body: some View {        VStack(alignment: .leading, spacing: 2) {            if let state = line?.state {                Text(state)                    .font(theme.type.body(StillReadingKnobs.size))                    .foregroundStyle(theme.colors.inkSecondary.color)            }            if let when = line?.when {                Text(when)                    .font(theme.type.body(StillReadingKnobs.size))                    .foregroundStyle(theme.colors.inkSecondary.color)            }        }        .frame(maxWidth: .infinity, alignment: .leading)        .accessibilityElement(children: .combine)        .accessibilityIdentifier("still-reading")        .task(id: scene?.shownId) {            try? await Task.sleep(for: .seconds(StillReadingKnobs.after))            while !Task.isCancelled {                if let core, let own = await HomeSync.thisDevice(core, database: scene?.shownId), !Task.isCancelled {                    line = own                }                try? await Task.sleep(for: .seconds(StillReadingKnobs.every))            }        }    }}enum StillReadingKnobs {    /// Seconds a read may be out before the line is drawn: a local read    /// answers long before.    static let after: Double = 2    /// Seconds between reads of the device's state while the line stands.    static let every: Double = 5    static let size: CGFloat = 15}