diff --git a/app/lib/providers/wallets_provider.dart b/app/lib/providers/wallets_provider.dart index 8507496a4..9af2f9bda 100644 --- a/app/lib/providers/wallets_provider.dart +++ b/app/lib/providers/wallets_provider.dart @@ -1,3 +1,4 @@ +import 'package:mutex/mutex.dart'; import 'package:threebotlogin/helpers/globals.dart'; import 'package:threebotlogin/models/wallet.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; @@ -11,36 +12,42 @@ class WalletsNotifier extends StateNotifier> { bool _reload = true; bool _loading = true; + final Mutex _mutex = Mutex(); + Future list() async { _loading = true; state = await listWallets(); _loading = false; } - void removeWallet(String name) { - state = state.where((wallet) => wallet.name != name).toList(); + Future removeWallet(String name) async { + await _mutex.protect(() async { + state = state.where((wallet) => wallet.name != name).toList(); + }); } void reloadBalances() async { if (!_reload) return await TFChainService.disconnect(); if (!_loading) { final chainUrl = Globals().chainUrl; + await _mutex.protect(() async { final List currentState = state.where((w) => true).toList(); - for (final wallet in currentState) { - final balance = - await TFChainService.getBalance(chainUrl, wallet.tfchainAddress); - final tfchainBalance = - balance.toString() == '0.0' ? '0' : balance.toString(); - final stellarBalance = - await StellarService.getBalance(wallet.stellarSecret); + for (final wallet in currentState) { + final balance = + await TFChainService.getBalance(chainUrl, wallet.tfchainAddress); + final tfchainBalance = + balance.toString() == '0.0' ? '0' : balance.toString(); + final stellarBalance = + await StellarService.getBalance(wallet.stellarSecret); - if (tfchainBalance != wallet.tfchainBalance || - stellarBalance != wallet.stellarBalance) { - wallet.stellarBalance = stellarBalance; - wallet.tfchainBalance = tfchainBalance; + if (tfchainBalance != wallet.tfchainBalance || + stellarBalance != wallet.stellarBalance) { + wallet.stellarBalance = stellarBalance; + wallet.tfchainBalance = tfchainBalance; + } } - } - state = currentState; + state = currentState; + }); } final refreshBalance = Globals().refreshBalance; await Future.delayed(Duration(seconds: refreshBalance)); diff --git a/app/pubspec.lock b/app/pubspec.lock index 3a489912a..a10d3f835 100644 --- a/app/pubspec.lock +++ b/app/pubspec.lock @@ -898,6 +898,14 @@ packages: url: "https://pub.dev" source: hosted version: "2.2.1+beta.0" + mutex: + dependency: "direct main" + description: + name: mutex + sha256: "8827da25de792088eb33e572115a5eb0d61d61a3c01acbc8bcbe76ed78f1a1f2" + url: "https://pub.dev" + source: hosted + version: "3.1.0" nested: dependency: transitive description: diff --git a/app/pubspec.yaml b/app/pubspec.yaml index 2965c9e36..ad238ee80 100644 --- a/app/pubspec.yaml +++ b/app/pubspec.yaml @@ -77,6 +77,7 @@ dependencies: stellar_flutter_sdk: ^1.8.9 flutter_launcher_icons: ^0.14.1 logger: ^2.4.0 + mutex: ^3.1.0 dev_dependencies: flutter_test: sdk: flutter