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
+31 -5
View File
@@ -37,7 +37,8 @@ class NotificationsList extends StatefulWidget {
State<NotificationsList> createState() => _NotificationsListState();
}
class _NotificationsListState extends State<NotificationsList> with RouteAware {
class _NotificationsListState extends State<NotificationsList>
with RouteAware, WidgetsBindingObserver {
_NotificationFilter _filter = _NotificationFilter.newOnly;
Timer? _notificationTimer;
@@ -51,11 +52,9 @@ class _NotificationsListState extends State<NotificationsList> with RouteAware {
@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
_fetchPage(0);
_notificationTimer = Timer.periodic(
const Duration(minutes: 1),
(_) => _fetchPage(_currentPage),
);
_startTimer();
}
@override
@@ -67,13 +66,40 @@ class _NotificationsListState extends State<NotificationsList> with RouteAware {
}
}
@override
void didPushNext() {
_notificationTimer?.cancel();
_notificationTimer = null;
}
@override
void didPopNext() {
_fetchPage(_currentPage);
_startTimer();
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
if (state == AppLifecycleState.paused) {
_notificationTimer?.cancel();
_notificationTimer = null;
} else if (state == AppLifecycleState.resumed) {
_fetchPage(_currentPage);
_startTimer();
}
}
void _startTimer() {
_notificationTimer?.cancel();
_notificationTimer = Timer.periodic(
const Duration(minutes: 1),
(_) => _fetchPage(_currentPage),
);
}
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
routeObserver.unsubscribe(this);
_notificationTimer?.cancel();
_fetchToken?.cancel();