mirror of
https://github.com/rzuasti/oott.git
synced 2026-07-08 19:21:54 +02:00
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:
co-authored by
Claude Sonnet 4.6
parent
838b292456
commit
f75d1ce313
@@ -37,7 +37,8 @@ class NotificationsList extends StatefulWidget {
|
|||||||
State<NotificationsList> createState() => _NotificationsListState();
|
State<NotificationsList> createState() => _NotificationsListState();
|
||||||
}
|
}
|
||||||
|
|
||||||
class _NotificationsListState extends State<NotificationsList> with RouteAware {
|
class _NotificationsListState extends State<NotificationsList>
|
||||||
|
with RouteAware, WidgetsBindingObserver {
|
||||||
_NotificationFilter _filter = _NotificationFilter.newOnly;
|
_NotificationFilter _filter = _NotificationFilter.newOnly;
|
||||||
Timer? _notificationTimer;
|
Timer? _notificationTimer;
|
||||||
|
|
||||||
@@ -51,11 +52,9 @@ class _NotificationsListState extends State<NotificationsList> with RouteAware {
|
|||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
super.initState();
|
super.initState();
|
||||||
|
WidgetsBinding.instance.addObserver(this);
|
||||||
_fetchPage(0);
|
_fetchPage(0);
|
||||||
_notificationTimer = Timer.periodic(
|
_startTimer();
|
||||||
const Duration(minutes: 1),
|
|
||||||
(_) => _fetchPage(_currentPage),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
@@ -67,13 +66,40 @@ class _NotificationsListState extends State<NotificationsList> with RouteAware {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void didPushNext() {
|
||||||
|
_notificationTimer?.cancel();
|
||||||
|
_notificationTimer = null;
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void didPopNext() {
|
void didPopNext() {
|
||||||
_fetchPage(_currentPage);
|
_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
|
@override
|
||||||
void dispose() {
|
void dispose() {
|
||||||
|
WidgetsBinding.instance.removeObserver(this);
|
||||||
routeObserver.unsubscribe(this);
|
routeObserver.unsubscribe(this);
|
||||||
_notificationTimer?.cancel();
|
_notificationTimer?.cancel();
|
||||||
_fetchToken?.cancel();
|
_fetchToken?.cancel();
|
||||||
|
|||||||
@@ -13,12 +13,14 @@ class PolledValue<T> extends ChangeNotifier {
|
|||||||
required Duration pollInterval,
|
required Duration pollInterval,
|
||||||
required Duration staleErrorAfter,
|
required Duration staleErrorAfter,
|
||||||
}) : _fetch = fetch,
|
}) : _fetch = fetch,
|
||||||
|
_pollInterval = pollInterval,
|
||||||
_staleErrorAfter = staleErrorAfter {
|
_staleErrorAfter = staleErrorAfter {
|
||||||
_load();
|
_load();
|
||||||
_pollTimer = Timer.periodic(pollInterval, (_) => _load());
|
_pollTimer = Timer.periodic(pollInterval, (_) => _load());
|
||||||
}
|
}
|
||||||
|
|
||||||
final Future<T> Function({CancelToken? cancelToken}) _fetch;
|
final Future<T> Function({CancelToken? cancelToken}) _fetch;
|
||||||
|
final Duration _pollInterval;
|
||||||
final Duration _staleErrorAfter;
|
final Duration _staleErrorAfter;
|
||||||
Timer? _pollTimer;
|
Timer? _pollTimer;
|
||||||
CancelToken? _cancelToken;
|
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
|
@override
|
||||||
void dispose() {
|
void dispose() {
|
||||||
_disposed = true;
|
_disposed = true;
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import 'package:go_router/go_router.dart';
|
|||||||
|
|
||||||
import '../model/arp_scanner_status.dart';
|
import '../model/arp_scanner_status.dart';
|
||||||
import '../model/mdns_scanner_status.dart';
|
import '../model/mdns_scanner_status.dart';
|
||||||
|
import '../navigation.dart';
|
||||||
import '../utils/duration_formatter.dart';
|
import '../utils/duration_formatter.dart';
|
||||||
import '../utils/oott_api.dart';
|
import '../utils/oott_api.dart';
|
||||||
import '../utils/polled_value.dart';
|
import '../utils/polled_value.dart';
|
||||||
@@ -17,7 +18,8 @@ class ScannersStatusCard extends StatefulWidget {
|
|||||||
State<ScannersStatusCard> createState() => _ScannersStatusCardState();
|
State<ScannersStatusCard> createState() => _ScannersStatusCardState();
|
||||||
}
|
}
|
||||||
|
|
||||||
class _ScannersStatusCardState extends State<ScannersStatusCard> {
|
class _ScannersStatusCardState extends State<ScannersStatusCard>
|
||||||
|
with RouteAware, WidgetsBindingObserver {
|
||||||
late final PolledValue<ArpScannerStatus> _arp;
|
late final PolledValue<ArpScannerStatus> _arp;
|
||||||
late final PolledValue<MdnsScannerStatus> _mdns;
|
late final PolledValue<MdnsScannerStatus> _mdns;
|
||||||
Timer? _tickTimer;
|
Timer? _tickTimer;
|
||||||
@@ -25,6 +27,7 @@ class _ScannersStatusCardState extends State<ScannersStatusCard> {
|
|||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
super.initState();
|
super.initState();
|
||||||
|
WidgetsBinding.instance.addObserver(this);
|
||||||
_arp = PolledValue<ArpScannerStatus>(
|
_arp = PolledValue<ArpScannerStatus>(
|
||||||
fetch: ({cancelToken}) =>
|
fetch: ({cancelToken}) =>
|
||||||
BackendAPI.instance.getArpScannerStatus(cancelToken: cancelToken),
|
BackendAPI.instance.getArpScannerStatus(cancelToken: cancelToken),
|
||||||
@@ -42,8 +45,53 @@ class _ScannersStatusCardState extends State<ScannersStatusCard> {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void didChangeDependencies() {
|
||||||
|
super.didChangeDependencies();
|
||||||
|
final route = ModalRoute.of(context);
|
||||||
|
if (route is ModalRoute<void>) {
|
||||||
|
routeObserver.subscribe(this, route);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void didPushNext() {
|
||||||
|
_pausePolling();
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void didPopNext() {
|
||||||
|
_resumePolling();
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void didChangeAppLifecycleState(AppLifecycleState state) {
|
||||||
|
if (state == AppLifecycleState.paused) {
|
||||||
|
_pausePolling();
|
||||||
|
} else if (state == AppLifecycleState.resumed) {
|
||||||
|
_resumePolling();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void _pausePolling() {
|
||||||
|
_arp.pause();
|
||||||
|
_mdns.pause();
|
||||||
|
_tickTimer?.cancel();
|
||||||
|
_tickTimer = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
void _resumePolling() {
|
||||||
|
_arp.resume();
|
||||||
|
_mdns.resume();
|
||||||
|
_tickTimer ??= Timer.periodic(const Duration(seconds: 1), (_) {
|
||||||
|
if (mounted) setState(() {});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void dispose() {
|
void dispose() {
|
||||||
|
WidgetsBinding.instance.removeObserver(this);
|
||||||
|
routeObserver.unsubscribe(this);
|
||||||
_tickTimer?.cancel();
|
_tickTimer?.cancel();
|
||||||
_arp.dispose();
|
_arp.dispose();
|
||||||
_mdns.dispose();
|
_mdns.dispose();
|
||||||
|
|||||||
Reference in New Issue
Block a user