fix sync
This commit is contained in:
@@ -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();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user