fix: avoid background sync timer while retaining lifecycle reconciliation

This commit is contained in:
bponders12-cell 2026-09-01 23:13:13 -04:00
parent 58659e8f7f
commit fcf90ec9c0
1 changed files with 5 additions and 12 deletions

View File

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