Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 22 additions & 15 deletions app/lib/providers/wallets_provider.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -11,36 +12,42 @@ class WalletsNotifier extends StateNotifier<List<Wallet>> {

bool _reload = true;
bool _loading = true;
final Mutex _mutex = Mutex();

Future<void> list() async {
_loading = true;
state = await listWallets();
_loading = false;
}

void removeWallet(String name) {
state = state.where((wallet) => wallet.name != name).toList();
Future<void> 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<Wallet> 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));
Expand Down
8 changes: 8 additions & 0 deletions app/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions app/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down