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
+35 -7
View File
@@ -36,6 +36,12 @@ class BankController extends ChangeNotifier {
final IngtrakApi _api;
final StorageService? _storage;
/// Fired after a successful live balance + transactions fetch.
///
/// Used by [SyncController] to reconcile with PocketBase so a bank refresh
/// cannot leave the device behind the cloud (or wipe cloud-only history).
VoidCallback? onAfterSuccessfulLoad;
ConnectStatus _status = ConnectStatus.disconnected;
BankBalance? _balance;
List<BankTransaction> _remoteTransactions = const [];
@@ -193,6 +199,7 @@ class BankController extends ChangeNotifier {
_error = null;
notifyListeners();
var loadedLive = false;
try {
try {
_status = await _api.fetchStatus();
@@ -209,7 +216,7 @@ class BankController extends ChangeNotifier {
// have a local snapshot. Never wipe local txs because status said
// "unlinked" — only a 409 from balance/transactions clears them.
if (_status.linked || hasData) {
await _loadLinkedData();
loadedLive = await _loadLinkedData();
await _persistCache();
}
} on IngtrakApiException catch (error, stackTrace) {
@@ -230,16 +237,24 @@ class BankController extends ChangeNotifier {
_isLoading = false;
notifyListeners();
}
if (loadedLive) {
onAfterSuccessfulLoad?.call();
}
}
Future<void> _loadLinkedData() async {
/// Returns true when balance + transactions were fetched successfully.
Future<bool> _loadLinkedData() async {
try {
final results = await Future.wait([
_api.fetchBalance(),
_api.fetchTransactions(),
]);
_balance = results[0] as BankBalance;
_remoteTransactions = results[1] as List<BankTransaction>;
final apiTxs = results[1] as List<BankTransaction>;
// Bank API is ~89 days. Prefer API rows for overlaps, but keep any
// cloud/CSV history that the API window does not include.
_remoteTransactions = mergeTransactions(apiTxs, _remoteTransactions);
if (!_status.linked) {
_status = ConnectStatus(
status: 'linked',
@@ -257,6 +272,7 @@ class BankController extends ChangeNotifier {
_invalidateMerged();
await _persistImported();
_error = null;
return true;
} on IngtrakApiException catch (error) {
if (error.isNotLinked) {
// Confirmed unlinked by data endpoints — clear remote only.
@@ -272,6 +288,7 @@ class BankController extends ChangeNotifier {
// Keep previous balance/txs if refresh of data fails.
_error = 'Using saved data · ${error.message}';
}
return false;
}
}
@@ -349,11 +366,19 @@ class BankController extends ChangeNotifier {
state: state ?? _pendingState,
);
_pendingState = null;
var loadedLive = false;
if (_status.linked) {
await _loadLinkedData();
loadedLive = await _loadLinkedData();
await _persistCache();
}
return _status.linked;
final linked = _status.linked;
// Notify before cloud reconcile so UI can leave the connecting state.
_isLoading = false;
notifyListeners();
if (loadedLive) {
onAfterSuccessfulLoad?.call();
}
return linked;
} on IngtrakApiException catch (error, stackTrace) {
_error = error.message;
debugPrint('BankController.completeWithCode failed: $error\n$stackTrace');
@@ -363,13 +388,16 @@ class BankController extends ChangeNotifier {
debugPrint('BankController.completeWithCode failed: $error\n$stackTrace');
return false;
} finally {
_isLoading = false;
notifyListeners();
if (_isLoading) {
_isLoading = false;
notifyListeners();
}
}
}
@override
void dispose() {
onAfterSuccessfulLoad = null;
_api.close();
super.dispose();
}
+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();
}