This commit is contained in:
davmar
2026-09-10 11:40:28 +02:00
parent 68623b38ff
commit aec89ac41d
5 changed files with 127 additions and 7 deletions
+29
View File
@@ -36,6 +36,7 @@ class SyncController extends ChangeNotifier {
_wasLoggedIn = auth.isLoggedIn;
_meta.onPersisted = _schedulePush;
_auth.addListener(_onAuthChanged);
_bank.onAfterSuccessfulLoad = _onBankDataLoaded;
if (_auth.isLoggedIn) {
unawaited(syncNow());
}
@@ -188,6 +189,33 @@ class SyncController extends ChangeNotifier {
}
}
/// Bank pull-to-refresh / reconnect finished — reconcile with PocketBase.
///
/// Always runs a full sync when logged in so (1) new API txs are pushed and
/// (2) any cloud-only history the bank window lacks is pulled back.
void _onBankDataLoaded() {
if (!_auth.isLoggedIn) return;
unawaited(syncNow());
}
/// If the server has more bank txs than this device, sync now.
Future<void> syncIfBehindServer() async {
if (!_auth.isLoggedIn || _syncing) return;
try {
final remoteCount =
await PbSyncService(_auth.client).countTransactions();
if (remoteCount > _bank.transactions.length) {
debugPrint(
'SyncController: behind server '
'(local=${_bank.transactions.length} remote=$remoteCount) — syncing',
);
await syncNow();
}
} catch (error, stackTrace) {
debugPrint('SyncController.syncIfBehindServer failed: $error\n$stackTrace');
}
}
void _schedulePush() {
if (!_auth.isLoggedIn) return;
_pushDebounce?.cancel();
@@ -329,6 +357,7 @@ class SyncController extends ChangeNotifier {
_progressHold?.cancel();
_uiNotifyThrottle?.cancel();
_auth.removeListener(_onAuthChanged);
_bank.onAfterSuccessfulLoad = null;
_meta.onPersisted = null;
super.dispose();
}