// 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. import CottonKit import SwiftUI /// 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. @MainActor enum SyncSeenOpens { private static var askedAt: Date? /// Ask that Settings opened now stands on Sync, and tell an app whose Settings is a tab (You) to go there. static func ask() { askedAt = Date() NotificationCenter.default.post(name: notice, object: nil) } /// Posted by `ask`: an app whose fuller status is a tab goes to it. static let notice = Notification.Name("cotton.syncSeen.open") /// Whether Settings opening now should stand on Sync. static func take() -> Bool { guard let at = askedAt else { return false } if Date().timeIntervalSince(at) < 1.5 { return true } askedAt = nil return false } } public struct SyncSeenLine: View { let font: Font let color: Color let onTap: (() -> Void)? @Environment(\.cottonSync) private var sync /// The line drawn now: the last state the promises above let through. @State private var shown: SyncSeenRead? public init(font: Font = .footnote, color: Color = .secondary, onTap: (() -> Void)? = nil) { self.font = font self.color = color self.onTap = onTap } public var body: some View { // 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) if let shown { if let 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? await Task.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 in guard old != nil, new != nil, let spoken = shown?.spoken, !spoken.isEmpty else { return } AccessibilityNotification.Announcement(spoken).post() } .animation(.easeOut(duration: 0.2), value: shown?.state) } private func words(_ seen: SyncSeenRead) -> some View { 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. private func take(_ seen: SyncSeenRead?) async { guard let seen else { return } if let wait = Self.wait(before: seen, shown: shown, now: Date()) { try? await Task.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. static func wait(before seen: SyncSeenRead, shown: SyncSeenRead?, now: Date) -> Int? { guard seen.catchingUp, shown?.catchingUp != true, let since = seen.since else { return nil } let ran = Int(now.timeIntervalSince1970 * 1000 - since) let left = seen.showAfterMs - ran return left > 0 ? left : nil } }