Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions CalenTask/App/AppShellView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
25 changes: 25 additions & 0 deletions CalenTask/Services/CalendarSyncService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
Expand Down
Loading