feat: add app lifecycle refresh coordinator
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
import 'package:flutter/widgets.dart';
|
||||
|
||||
class CacheRefreshCoordinator with WidgetsBindingObserver {
|
||||
final Duration minInterval;
|
||||
final void Function() onRefresh;
|
||||
final DateTime Function() now;
|
||||
|
||||
DateTime? _lastRefreshedAt;
|
||||
|
||||
CacheRefreshCoordinator({
|
||||
required this.minInterval,
|
||||
required this.onRefresh,
|
||||
DateTime Function()? now,
|
||||
}) : now = now ?? DateTime.now;
|
||||
|
||||
@override
|
||||
void didChangeAppLifecycleState(AppLifecycleState state) {
|
||||
if (state != AppLifecycleState.resumed) {
|
||||
return;
|
||||
}
|
||||
final current = now();
|
||||
final last = _lastRefreshedAt;
|
||||
if (last != null && current.difference(last) < minInterval) {
|
||||
return;
|
||||
}
|
||||
_lastRefreshedAt = current;
|
||||
onRefresh();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user