Pause polling when screens are off-route or app is backgrounded

PolledValue gains pause()/resume() methods. ScannersStatusCard and
NotificationsList now implement RouteAware (didPushNext/didPopNext)
and WidgetsBindingObserver to stop API calls and tick timers when
the widget is not visible, resuming with an immediate fetch on return.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
rzuasti
2026-05-31 17:34:19 -04:00
co-authored by Claude Sonnet 4.6
parent 838b292456
commit f75d1ce313
3 changed files with 94 additions and 6 deletions
+14
View File
@@ -13,12 +13,14 @@ class PolledValue<T> extends ChangeNotifier {
required Duration pollInterval,
required Duration staleErrorAfter,
}) : _fetch = fetch,
_pollInterval = pollInterval,
_staleErrorAfter = staleErrorAfter {
_load();
_pollTimer = Timer.periodic(pollInterval, (_) => _load());
}
final Future<T> Function({CancelToken? cancelToken}) _fetch;
final Duration _pollInterval;
final Duration _staleErrorAfter;
Timer? _pollTimer;
CancelToken? _cancelToken;
@@ -66,6 +68,18 @@ class PolledValue<T> extends ChangeNotifier {
}
}
void pause() {
_pollTimer?.cancel();
_pollTimer = null;
_cancelToken?.cancel();
}
void resume() {
if (_disposed) return;
_load();
_pollTimer = Timer.periodic(_pollInterval, (_) => _load());
}
@override
void dispose() {
_disposed = true;