feat: automatic foreground sync — 5 min timer + resume refresh
Some checks failed
CI / Build · macos-14 · Debug (push) Has been cancelled
CI / Build · macos-15 · Debug (push) Has been cancelled
CI / Build · macos-14 · Release (push) Has been cancelled
CI / Build · macos-15 · Release (push) Has been cancelled

StoryStore gains startForegroundSync() / stopForegroundSync():
- Polls loadStories(refresh:true) every 5 minutes while app is active
- WebSocket handles real-time storyCreated events between polls
- Timer cancels when app goes to background to save battery

JarvisApp wires scenePhase:
- .active  → start 5-min timer; immediate refresh if last sync > 2 min ago
- .background → cancel timer
- On launch → timer starts after setup completes

Net result: feed replenishes automatically; user never hits an empty list.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Robin Kutesa
2026-06-21 02:57:54 +03:00
parent 7a97118d12
commit a1d7979048
2 changed files with 50 additions and 2 deletions

View File

@@ -82,6 +82,7 @@ struct JarvisApp: App {
@StateObject private var connectivity = ConnectivitySettings.shared
@StateObject private var connManager = ConnectivityManager.shared
@StateObject private var notifications = NotificationManager.shared
@Environment(\.scenePhase) private var scenePhase
@State private var splashDone = false
// Single ModelContainer instance shared with BackgroundRefreshManager.
@@ -120,6 +121,22 @@ struct JarvisApp: App {
connManager.start()
await connManager.resolveAndActivate()
await notifications.bootstrap()
store.startForegroundSync()
}
.onChange(of: scenePhase) { _, phase in
switch phase {
case .active:
store.startForegroundSync()
// Refresh immediately if we've been away more than 2 minutes.
if let last = store.lastSyncedAt,
Date().timeIntervalSince(last) > 120 {
Task { await store.loadStories(refresh: true) }
}
case .background:
store.stopForegroundSync()
default:
break
}
}
}
.modelContainer(container)