diff --git a/lib/providers/my_wallets.dart b/lib/providers/my_wallets.dart index c5d2c3b..33a93b3 100644 --- a/lib/providers/my_wallets.dart +++ b/lib/providers/my_wallets.dart @@ -35,18 +35,84 @@ class MyWalletsProvider with ChangeNotifier { } } - List readAllWallets([int? chest]) { + Future> readAllWallets([int? chest]) async { + final sub = Provider.of(homeContext, listen: false); chest = chest ?? configBox.get('currentChest') ?? 0; listWallets.clear(); - walletBox.toMap().forEach((key, value) { - if (value.chest == chest) { - listWallets.add(value); + final wallets = walletBox.toMap().values.toList(); + for (var walletFromBox in wallets) { + if (walletFromBox.chest == chest) { + if (walletFromBox.identityStatus == IdtyStatus.unknown) { + walletFromBox.identityStatus = + await sub.idtyStatus(walletFromBox.address); + walletBox.put(walletFromBox.address, walletFromBox); + } + listWallets.add(walletFromBox); } - }); + } return listWallets; } + // List readAllWallets([int? chest]) { + // final sub = Provider.of(homeContext, listen: false); + // bool hasFetchStatus = false; + // List futures = []; + // chest = chest ?? configBox.get('currentChest') ?? 0; + // listWallets.clear(); + // walletBox.toMap().forEach((key, walletFromBox) { + // if (walletFromBox.chest == chest) { + // if (walletFromBox.identityStatus == IdtyStatus.unknown) { + // hasFetchStatus = true; + // futures.add(sub.idtyStatus(walletFromBox.address).then((valueStatus) { + // walletFromBox.identityStatus = valueStatus; + // listWallets.add(walletFromBox); + // // walletBox.put(key, walletFromBox); + // })); + // } else { + // listWallets.add(walletFromBox); + // } + // } + // }); + // // if (hasFetchStatus) { + // // sleep(const Duration(milliseconds: 300)); + // // log.d('yoooooooooo'); + // // readAllWallets(chest); + // // } + + // if (hasFetchStatus) { + // Future.wait(futures).then((_) { + // return listWallets; + // // while (listWallets.any((element) => + // // element.chest == chest && + // // element.identityStatus == IdtyStatus.unknown)) { + + // // List tata = listWallets.toList(); + // // log.d(listWallets); + // // while (tata.length < walletBox.length) { + // // tata = listWallets.toList(); + // // sleep(const Duration(milliseconds: 500)); + // // Map status = {}; + // // for (var walletInList in tata) { + // // status.putIfAbsent( + // // walletInList.name, () => walletInList.identityStatus); + // // } + // // log.d(status); + // // log.d('yoooooo'); + + // // status.clear(); + // // } + // }); + // } + + // // if (hasFetchStatus) { + // // walletBox.putAll( + // // Map.fromEntries(listWallets.map((e) => MapEntry(e.address, e)))); + // // } + + // return listWallets; + // } + WalletData? getWalletDataById(List id) { if (id.isEmpty) return WalletData(address: '', isOwned: true); int? chest = id[0]; @@ -118,7 +184,7 @@ class MyWalletsProvider with ChangeNotifier { isNewDerivationLoading = true; notifyListeners(); - final List idList = getNextWalletNumberAndDerivation(); + final List idList = await getNextWalletNumberAndDerivation(); int newWalletNbr = idList[0]; int newDerivationNbr = number ?? idList[1]; @@ -155,7 +221,7 @@ class MyWalletsProvider with ChangeNotifier { int newWalletNbr; int? chest = getCurrentChest(); - List walletConfig = readAllWallets(chest); + List walletConfig = await readAllWallets(chest); walletConfig.sort((p1, p2) { return Comparable.compare(p1.number!, p2.number!); }); @@ -187,14 +253,14 @@ class MyWalletsProvider with ChangeNotifier { notifyListeners(); } - List getNextWalletNumberAndDerivation( - {int? chestNumber, bool isOneshoot = false}) { + Future> getNextWalletNumberAndDerivation( + {int? chestNumber, bool isOneshoot = false}) async { int newDerivationNbr = 0; int newWalletNbr = 0; chestNumber ??= getCurrentChest(); - List walletConfig = readAllWallets(chestNumber); + List walletConfig = await readAllWallets(chestNumber); walletConfig.sort((p1, p2) { return Comparable.compare(p1.number!, p2.number!); }); diff --git a/lib/screens/myWallets/custom_derivations.dart b/lib/screens/myWallets/custom_derivations.dart index 82089b4..f94bdd8 100644 --- a/lib/screens/myWallets/custom_derivations.dart +++ b/lib/screens/myWallets/custom_derivations.dart @@ -35,9 +35,7 @@ class _CustomDerivationState extends State { for (var i = 0; i < 51; i += 1) i.toString() ]; - final listWallets = myWalletProvider.readAllWallets(); - - for (WalletData wallet in listWallets) { + for (WalletData wallet in myWalletProvider.listWallets) { derivationList.remove(wallet.derivation.toString()); if (wallet.derivation == -1) { derivationList.remove('root'); diff --git a/lib/screens/myWallets/wallets_home.dart b/lib/screens/myWallets/wallets_home.dart index fdbfb78..ada4324 100644 --- a/lib/screens/myWallets/wallets_home.dart +++ b/lib/screens/myWallets/wallets_home.dart @@ -41,8 +41,6 @@ class _WalletsHomeState extends State { final currentChestNumber = myWalletProvider.getCurrentChest(); final ChestData currentChest = chestBox.get(currentChestNumber)!; - myWalletProvider.listWallets = - myWalletProvider.readAllWallets(currentChestNumber); return WillPopScope( onWillPop: () { @@ -83,12 +81,29 @@ class _WalletsHomeState extends State { actualRoute: 'safeHome', ) : dragInfo(context), - body: SafeArea( - child: Stack(children: [ - myWalletsTiles(context, currentChestNumber), - const OfflineInfo(), - ]), - ), + body: FutureBuilder( + future: myWalletProvider.readAllWallets(currentChestNumber), + builder: (context, snapshot) { + if (snapshot.connectionState != ConnectionState.done || + snapshot.hasError) { + return const Center( + child: SizedBox( + height: 50, + width: 50, + child: CircularProgressIndicator( + color: orangeC, + strokeWidth: 3, + ), + ), + ); + } + return SafeArea( + child: Stack(children: [ + myWalletsTiles(context, currentChestNumber), + const OfflineInfo(), + ]), + ); + }), ), ); } diff --git a/lib/screens/onBoarding/10.dart b/lib/screens/onBoarding/10.dart index 92eff67..6fd3387 100644 --- a/lib/screens/onBoarding/10.dart +++ b/lib/screens/onBoarding/10.dart @@ -253,7 +253,7 @@ class OnboardingStepTen extends StatelessWidget { isOwned: true); await walletBox.put(myWallet.address, myWallet); } - myWalletProvider.readAllWallets(currentChest); + await myWalletProvider.readAllWallets(currentChest); myWalletProvider.reload(); generateWalletProvider.generatedMnemonic = ''; diff --git a/lib/widgets/payment_popup.dart b/lib/widgets/payment_popup.dart index 5974fd4..e106b10 100644 --- a/lib/widgets/payment_popup.dart +++ b/lib/widgets/payment_popup.dart @@ -66,10 +66,8 @@ void paymentPopup(BuildContext context, String toAddress, String username) { } } - myWalletProvider.readAllWallets(); - myWalletProvider.listWallets - .sort((a, b) => a.derivation!.compareTo(b.derivation!)); - log.d(myWalletProvider.listWallets); + myWalletProvider.readAllWallets().then((value) => myWalletProvider.listWallets + .sort((a, b) => a.derivation!.compareTo(b.derivation!))); showModalBottomSheet( shape: const RoundedRectangleBorder(