Refresh button for balance

This commit is contained in:
Arseni
2025-12-30 18:36:29 +03:00
parent 202582626a
commit b157522fdb
8 changed files with 148 additions and 17 deletions

View File

@@ -29,6 +29,8 @@ class WalletsProvider with ChangeNotifier {
bool _isRefreshingBalances = false;
bool get isRefreshingBalances => _isRefreshingBalances;
final Set<String> _refreshingWallets = <String>{};
bool isWalletRefreshing(String walletId) => _refreshingWallets.contains(walletId);
void update(OrganizationsProvider organizations) {
_organizations = organizations;
@@ -81,6 +83,31 @@ class WalletsProvider with ChangeNotifier {
}
}
Future<void> refreshBalance(String walletId) async {
if (_refreshingWallets.contains(walletId)) return;
final wallet = wallets.firstWhereOrNull((w) => w.id == walletId);
if (wallet == null) return;
_refreshingWallets.add(walletId);
notifyListeners();
try {
final balance = await _service.getBalance(_organizations.current.id, walletId);
final updatedWallet = wallet.copyWith(balance: balance);
final next = List<Wallet>.from(wallets);
final idx = next.indexWhere((w) => w.id == walletId);
if (idx >= 0) {
next[idx] = updatedWallet;
_setResource(_resource.copyWith(data: next));
}
} catch (e) {
_setResource(_resource.copyWith(error: toException(e)));
} finally {
_refreshingWallets.remove(walletId);
notifyListeners();
}
}
void toggleVisibility(String walletId) {
final index = wallets.indexWhere((w) => w.id == walletId);
if (index < 0) return;