This commit is contained in:
2026-02-27 13:32:25 +01:00
parent bb53110b9a
commit c71470a627
3 changed files with 10 additions and 9 deletions

View File

@@ -102,7 +102,8 @@ class DepositViewModel extends Notifier<DepositViewState> {
if (!ref.mounted) return;
ref.read(childDataProvider(childId).notifier).load();
ref.read(homeViewModelProvider.notifier).refreshChildWallet(childId);
ref.read(parentWalletBalanceProvider.notifier).refresh();
await ref.read(parentWalletBalanceProvider.notifier).refresh();
if (!ref.mounted) return;
state = state.copyWith(isSubmitting: false, success: true);
} catch (e) {
if (!ref.mounted) return;

View File

@@ -94,7 +94,7 @@ class ExtractViewModel extends Notifier<ExtractViewState> {
if (!ref.mounted) return;
ref.read(childDataProvider(childId).notifier).load();
ref.read(homeViewModelProvider.notifier).refreshChildWallet(childId);
ref.read(parentWalletBalanceProvider.notifier).refresh();
ref.read(parentWalletBalanceProvider.notifier).applyOptimisticPayin(amount);
state = state.copyWith(isSubmitting: false, success: true);
} catch (e) {
if (!ref.mounted) return;

View File

@@ -35,16 +35,16 @@ class ParentWalletBalanceNotifier extends Notifier<WalletBalanceEntity?> {
void applyOptimisticPayin(double amount) {
final balance = state;
if (balance == null) return;
final optimisticTotal = balance.totalBalance + amount;
final previousTotal = balance.totalBalance;
state = balance.copyWith(
availableBalance: balance.availableBalance + amount,
totalBalance: optimisticTotal,
totalBalance: previousTotal + amount,
);
_refreshTimer?.cancel();
_refreshWithRetry(optimisticTotal);
_refreshWithRetry(previousTotal);
}
void _refreshWithRetry(double expectedTotal, [int attempt = 0]) {
void _refreshWithRetry(double previousTotal, [int attempt = 0]) {
const delays = [5, 10, 20];
if (attempt >= delays.length) return;
_refreshTimer = Timer(Duration(seconds: delays[attempt]), () async {
@@ -53,13 +53,13 @@ class ParentWalletBalanceNotifier extends Notifier<WalletBalanceEntity?> {
try {
final repo = ref.read(treezorRepositoryProvider);
final fresh = await repo.getWalletBalance(walletId: walletId);
if (fresh.totalBalance >= expectedTotal) {
if (fresh.totalBalance != previousTotal) {
state = fresh;
} else {
_refreshWithRetry(expectedTotal, attempt + 1);
_refreshWithRetry(previousTotal, attempt + 1);
}
} catch (_) {
_refreshWithRetry(expectedTotal, attempt + 1);
_refreshWithRetry(previousTotal, attempt + 1);
}
});
}