Skip to content
Open
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
26 changes: 22 additions & 4 deletions lib/app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:provider/provider.dart';

import 'ai/briefing.dart' show BriefingPeriod;
import 'coach/coach_config.dart';
import 'data/db.dart';
import 'l10n/app_localizations.dart';
import 'notify/notification_service.dart';
import 'notify/tap_router.dart';
Expand Down Expand Up @@ -46,11 +47,22 @@ class OpenStrapApp extends StatefulWidget {
State<OpenStrapApp> createState() => _OpenStrapAppState();
}

/// Whether screen reads may use the reader connection at this lifecycle state.
/// Null (no report yet) counts as visible; the observer corrects it on the
/// first report.
@visibleForTesting
bool screenReadsForeground(AppLifecycleState? state) =>
state != AppLifecycleState.hidden &&
state != AppLifecycleState.paused &&
state != AppLifecycleState.detached;

class _OpenStrapAppState extends State<OpenStrapApp> with WidgetsBindingObserver {
@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
unawaited(LocalDb.setForeground(
screenReadsForeground(WidgetsBinding.instance.lifecycleState)));
// Hand the BYOK provider config to AppState (briefing generation + the
// key-aware AI notification schedule live there).
WidgetsBinding.instance.addPostFrameCallback((_) {
Expand Down Expand Up @@ -79,6 +91,7 @@ class _OpenStrapAppState extends State<OpenStrapApp> with WidgetsBindingObserver
void didChangeAppLifecycleState(AppLifecycleState state) {
final app = context.read<AppState>();
if (state == AppLifecycleState.resumed) {
unawaited(LocalDb.setForeground(true));
// The user may have flipped our notification switch either way in OS
// Settings while we were backgrounded. Drop the cached authorization
// decision so the next present/schedule re-reads reality — a denial used
Expand Down Expand Up @@ -114,10 +127,15 @@ class _OpenStrapAppState extends State<OpenStrapApp> with WidgetsBindingObserver
// a lock screen looking current.
unawaited(WidgetService.refresh(app.repo));
if (app.isPaired) app.openSession();
} else if (state == AppLifecycleState.paused) {
// Backgrounded: hand the band to the iOS restore path so it can wake-and-drain
// in the background (no-op on Android, where the foreground service holds it).
app.pauseForBackground();
} else if (state == AppLifecycleState.hidden ||
state == AppLifecycleState.paused ||
state == AppLifecycleState.detached) {
unawaited(LocalDb.setForeground(false));
if (state == AppLifecycleState.paused) {
// Backgrounded: hand the band to the iOS restore path so it can wake-and-drain
// in the background (no-op on Android, where the foreground service holds it).
app.pauseForBackground();
}
}
}

Expand Down
Loading
Loading