fix: avoid background sync timer while retaining lifecycle reconciliation
This commit is contained in:
parent
58659e8f7f
commit
fcf90ec9c0
|
|
@ -7,26 +7,22 @@ import 'sync_provider.dart';
|
|||
|
||||
/// Central provider-agnostic synchronization coordinator.
|
||||
///
|
||||
/// Sync work is persisted in [SyncOutbox]. The manager also reconciles on
|
||||
/// startup, app resume, and periodically while the app is foregrounded so a
|
||||
/// ride recorded offline is automatically retried after connectivity returns.
|
||||
/// Sync work is persisted in [SyncOutbox]. The manager reconciles persisted
|
||||
/// work on startup and whenever the app returns to the foreground. Network
|
||||
/// failures remain queued instead of being discarded.
|
||||
class SyncManager extends ChangeNotifier with WidgetsBindingObserver {
|
||||
final SyncOutbox _outbox;
|
||||
final Map<String, SyncProvider> _providers = {};
|
||||
String _activeProviderId = 'fake';
|
||||
bool _isProcessing = false;
|
||||
DateTime? Function() _nowProvider;
|
||||
Timer? _foregroundRetryTimer;
|
||||
|
||||
SyncManager(this._outbox, {List<SyncProvider>? initialProviders, String? defaultProviderId, DateTime? Function()? nowProvider}) : _nowProvider = nowProvider ?? (() => DateTime.now()) {
|
||||
if (initialProviders != null) {
|
||||
for (final p in initialProviders) {
|
||||
registerProvider(p);
|
||||
}
|
||||
for (final p in initialProviders) registerProvider(p);
|
||||
}
|
||||
if (defaultProviderId != null) _activeProviderId = defaultProviderId;
|
||||
WidgetsBinding.instance.addObserver(this);
|
||||
_foregroundRetryTimer = Timer.periodic(const Duration(seconds: 30), (_) => unawaited(processQueue()));
|
||||
unawaited(processQueue());
|
||||
}
|
||||
|
||||
|
|
@ -102,9 +98,7 @@ class SyncManager extends ChangeNotifier with WidgetsBindingObserver {
|
|||
|
||||
@override
|
||||
void didChangeAppLifecycleState(AppLifecycleState state) {
|
||||
if (state == AppLifecycleState.resumed) {
|
||||
unawaited(processQueue());
|
||||
}
|
||||
if (state == AppLifecycleState.resumed) unawaited(processQueue());
|
||||
}
|
||||
|
||||
Future<void> processQueue() async {
|
||||
|
|
@ -170,7 +164,6 @@ class SyncManager extends ChangeNotifier with WidgetsBindingObserver {
|
|||
|
||||
@override
|
||||
void dispose() {
|
||||
_foregroundRetryTimer?.cancel();
|
||||
WidgetsBinding.instance.removeObserver(this);
|
||||
super.dispose();
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue