From 06e2f991692ea7a26f91889f1afdd70286701a26 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 23 Nov 2026 00:07:29 +0000 Subject: [PATCH] Sync calendario: automatica al primo piano e su EKEventStoreChanged, una alla volta (#21) Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_01AVhvvMPkWeFNJApkqB8jN3 --- CalenTask/App/AppShellView.swift | 11 +++++++++ CalenTask/Services/CalendarSyncService.swift | 25 ++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/CalenTask/App/AppShellView.swift b/CalenTask/App/AppShellView.swift index 8d3dce5..1808cae 100644 --- a/CalenTask/App/AppShellView.swift +++ b/CalenTask/App/AppShellView.swift @@ -167,8 +167,19 @@ struct AppShellView: View { // notifiche (anche delle attività arrivate da iCloud). if phase == .active { NotificationService.shared.resyncAll(in: modelContext) + // #21 — sync calendario al ritorno in primo piano. + Task { await CalendarSyncService.shared.autoSyncIfNeeded(in: modelContext) } } } + // #21 — modifiche al calendario di sistema con l'app aperta, a raffica + // finita (un account Google ne manda molte). + .onReceive( + CalendarSyncService.storeChangedPublisher + .debounce(for: .seconds(3), scheduler: RunLoop.main) + ) { _ in + guard scenePhase == .active else { return } + Task { await CalendarSyncService.shared.autoSyncIfNeeded(in: modelContext, fromStoreChange: true) } + } // #29 — modifiche arrivate da iCloud mentre l'app è aperta: notifiche, // digest e widget si riallineano subito (prima solo all'avvio e al // ritorno in primo piano). A raffica finita: una sync ne manda tante. diff --git a/CalenTask/Services/CalendarSyncService.swift b/CalenTask/Services/CalendarSyncService.swift index 78d08b2..100370a 100644 --- a/CalenTask/Services/CalendarSyncService.swift +++ b/CalenTask/Services/CalendarSyncService.swift @@ -33,6 +33,12 @@ final class CalendarSyncService { set { UserDefaults.standard.set(newValue, forKey: Self.syncEnabledKey) } } + /// #21 — Notifica di modifica dello store di sistema (altro dispositivo, + /// app Calendario, account Google/Exchange). + nonisolated static var storeChangedPublisher: NotificationCenter.Publisher { + NotificationCenter.default.publisher(for: .EKEventStoreChanged) + } + private init() { isAuthorized = EKEventStore.authorizationStatus(for: .event) == .fullAccess } @@ -64,7 +70,26 @@ final class CalendarSyncService { return (start, end) } + /// #21 — una sola sync alla volta, a prescindere da chi la chiede + /// (apertura del Calendario, primo piano, `EKEventStoreChanged`). + private var isSyncing = false + /// Le scritture della sync stessa fanno scattare `EKEventStoreChanged`: + /// le notifiche arrivate subito dopo la fine vanno ignorate (niente cicli). + private var lastFinishedAt: Date? + + /// #21 — Sync automatica: solo se attiva e già autorizzata (non chiede + /// permessi di sua iniziativa) e non come eco di una sync appena finita. + func autoSyncIfNeeded(in context: ModelContext, fromStoreChange: Bool = false) async { + guard isSyncEnabled, isAuthorized else { return } + if fromStoreChange, let last = lastFinishedAt, Date.now.timeIntervalSince(last) < 5 { return } + guard let identity = try? SeedService.identity(in: context) else { return } + await syncNow(in: context, workspaceID: identity.workspace.id, createdBy: identity.me.id) + } + func syncNow(in context: ModelContext, workspaceID: UUID, createdBy: UUID) async { + guard !isSyncing else { return } + isSyncing = true + defer { isSyncing = false; lastFinishedAt = .now } guard await requestAccessIfNeeded() else { return } do { let events = await Self.fetchEvents(in: store, window: syncWindow)