diff --git a/SleepFocus/SleepFocusApp.swift b/SleepFocus/SleepFocusApp.swift index a45285b..09fb889 100644 --- a/SleepFocus/SleepFocusApp.swift +++ b/SleepFocus/SleepFocusApp.swift @@ -11,6 +11,9 @@ struct SleepFocusApp: App { @StateObject private var insightsStore = InsightsStore() private let healthAutoSyncService = HealthAutoSyncService.shared + // Observe scene phase to detect app foregrounding. + @Environment(\.scenePhase) private var scenePhase + var body: some Scene { WindowGroup { Group { @@ -52,6 +55,24 @@ struct SleepFocusApp: App { guard authManager.isAuthenticated else { return } await healthAutoSyncService.startIfNeeded(authManager: authManager) } + // When the app returns to foreground, force-refresh smart alarm guidance + // so the backend model result is used instead of the fallback. + .onChange(of: scenePhase) { _, newPhase in + guard newPhase == .active, authManager.isAuthenticated else { return } + Task { + guard !sleepPreferences.isRefreshingSmartAlarmGuidance else { return } + + await sleepPreferences.refreshSmartAlarmCycleGuidance( + using: authManager, + force: true + ) + + guard !sleepPreferences.isRefreshingSmartAlarmGuidance else { return } + await sleepPreferences.syncSmartAlarmRuntime( + notificationSettings: notificationSettings + ) + } + } .onOpenURL { url in navigationManager.handleDeepLink(url) }