// SYNC SEEN: the one quiet line each app's home draws about its sync (owner, 25 Nov 2026: "when it's syncing, if we// show some indicator that it is syncing, that we'll just wait... right now that's the missing part").//// The state and its words are the core's (`sync.status` `seen`, decided by `shell_account::seen_of`, the same line// Android reads off `shell.home`): signed out, syncing (how far, when known), changes waiting to sync, can't reach// Cotton (it keeps trying), synced at a time, not syncing. This view only places it, and keeps three promises the core// cannot keep alone://// • NO FLICKER. A catching-up line shows only once its pass has run `showAfterMs` (a second): a routine pass that// ends inside that leaves the line as it was. A line already catching up takes each new number at once.// • ALWAYS FRESH. While catching up the status is read again every two seconds for its percent; otherwise each// minute, so "5 min ago" stays true. Every loop event and every edit made here reads it again too (`SyncModel`).// • SAID ONCE. VoiceOver hears the state when it changes, in words without a ticking number; never on every tick.//// A tap opens the app's fuller status (Settings' or You's Sync page) when the app gives one.importCottonKitimportSwiftUI/// The sync page a tap on the seen line asks for: Settings opened in the next moment starts on its Sync page. A moment,/// not one opening, because an app's Settings may make its place more than once as it opens.@MainActorenumSyncSeenOpens {privatestaticvar askedAt: Date?/// Ask that Settings opened now stands on Sync, and tell an app whose Settings is a tab (You) to go there.staticfunc ask() { askedAt = Date()NotificationCenter.default.post(name: notice, object: nil) }/// Posted by `ask`: an app whose fuller status is a tab goes to it.staticlet notice = Notification.Name("cotton.syncSeen.open")/// Whether Settings opening now should stand on Sync.staticfunc take() -> Bool {guardlet at = askedAt else { returnfalse }ifDate().timeIntervalSince(at) < 1.5 { returntrue } askedAt = nilreturnfalse }}publicstructSyncSeenLine: View {let font: Fontlet color: Colorlet onTap: (() -> Void)? @Environment(\.cottonSync) privatevar sync/// The line drawn now: the last state the promises above let through. @State privatevar shown: SyncSeenRead?publicinit(font: Font = .footnote, color: Color = .secondary, onTap: (() -> Void)? = nil) {self.font = fontself.color = colorself.onTap = onTap }publicvar body: someView {// A zero-size anchor stands while nothing is shown: an empty view runs no `.task`, and the line would never// take its first state. ZStack(alignment: .leading) {Color.clear.frame(width: 0, height: 0).accessibilityHidden(true)iflet shown {iflet onTap {Button(action: onTap) { words(shown) } .buttonStyle(.plain) } else { words(shown) } } }// Each state the core says, let through by the no-flicker promise. .task(id: sync?.seen) { await take(sync?.seen) }// Fresh: the percent while catching up, the age otherwise. A home first drawn before any status read (a phone// with no sync loop to raise one) reads it at once. .task(id: shown?.catchingUp) {if sync?.seen == nil { await sync?.refresh() }while !Task.isCancelled {try? awaitTask.sleep(for: .seconds(shown?.catchingUp == true ? 2 : 60))guard !Task.isCancelled else { return }await sync?.refresh() } }// Said once when the state changes, never on a tick of its number. .onChange(of: shown?.state) { old, new inguard old != nil, new != nil, let spoken = shown?.spoken, !spoken.isEmpty else { return }AccessibilityNotification.Announcement(spoken).post() } .animation(.easeOut(duration: 0.2), value: shown?.state) }privatefunc words(_ seen: SyncSeenRead) -> someView {Text(seen.line) .font(font) .foregroundStyle(color) .multilineTextAlignment(.leading) .fixedSize(horizontal: false, vertical: true) .frame(minHeight: onTap == nil ? nil : 44, alignment: .leading) .contentShape(Rectangle()) .accessibilityElement(children: .ignore) .accessibilityLabel(Text(seen.line)) .accessibilityIdentifier("sync-seen-\(seen.state)") }/// Let `seen` through: at once, unless it is a catching-up line whose pass has not yet run its wait and the line/// is not already catching up; then after the wait, if the pass is still running.privatefunc take(_ seen: SyncSeenRead?) async {guardlet seen else { return }iflet wait = Self.wait(before: seen, shown: shown, now: Date()) {try? awaitTask.sleep(for: .milliseconds(wait))guard !Task.isCancelled else { return } } shown = seen }/// The milliseconds to hold `seen` back before drawing it, or nil to draw it now.staticfunc wait(before seen: SyncSeenRead, shown: SyncSeenRead?, now: Date) -> Int? {guard seen.catchingUp, shown?.catchingUp != true, let since = seen.since else { returnnil }let ran = Int(now.timeIntervalSince1970 * 1000 - since)let left = seen.showAfterMs - ranreturn left > 0 ? left : nil }}