From 3189203419632b2f712a608708343f432a9cc952 Mon Sep 17 00:00:00 2001 From: julian Date: Thu, 25 May 2023 10:05:20 -0600 Subject: [PATCH] fix: Firo total displayed on favourite card --- .../sub_widgets/favorite_card.dart | 111 +++++++++++------- .../sub_widgets/favorite_wallets.dart | 2 - .../desktop_favorite_wallets.dart | 1 - 3 files changed, 66 insertions(+), 48 deletions(-) diff --git a/lib/pages/wallets_view/sub_widgets/favorite_card.dart b/lib/pages/wallets_view/sub_widgets/favorite_card.dart index 87ffbccd1..8daf05a5d 100644 --- a/lib/pages/wallets_view/sub_widgets/favorite_card.dart +++ b/lib/pages/wallets_view/sub_widgets/favorite_card.dart @@ -6,7 +6,7 @@ import 'package:flutter_svg/flutter_svg.dart'; import 'package:stackwallet/pages/wallet_view/wallet_view.dart'; import 'package:stackwallet/pages_desktop_specific/my_stack_view/wallet_view/desktop_wallet_view.dart'; import 'package:stackwallet/providers/providers.dart'; -import 'package:stackwallet/services/coins/manager.dart'; +import 'package:stackwallet/services/coins/firo/firo_wallet.dart'; import 'package:stackwallet/themes/coin_icon_provider.dart'; import 'package:stackwallet/themes/stack_colors.dart'; import 'package:stackwallet/utilities/amount/amount.dart'; @@ -24,13 +24,11 @@ class FavoriteCard extends ConsumerStatefulWidget { required this.walletId, required this.width, required this.height, - required this.managerProvider, }) : super(key: key); final String walletId; final double width; final double height; - final ChangeNotifierProvider managerProvider; @override ConsumerState createState() => _FavoriteCardState(); @@ -38,15 +36,10 @@ class FavoriteCard extends ConsumerStatefulWidget { class _FavoriteCardState extends ConsumerState { late final String walletId; - late final ChangeNotifierProvider managerProvider; - - Amount _cachedBalance = Amount.zero; - Amount _cachedFiatValue = Amount.zero; @override void initState() { walletId = widget.walletId; - managerProvider = widget.managerProvider; super.initState(); } @@ -55,9 +48,13 @@ class _FavoriteCardState extends ConsumerState { @override Widget build(BuildContext context) { - final coin = ref.watch(managerProvider.select((value) => value.coin)); + final coin = ref.watch( + walletsChangeNotifierProvider + .select((value) => value.getManager(walletId).coin), + ); final externalCalls = ref.watch( - prefsChangeNotifierProvider.select((value) => value.externalCalls)); + prefsChangeNotifierProvider.select((value) => value.externalCalls), + ); return ConditionalParent( condition: Util.isDesktop, @@ -109,7 +106,10 @@ class _FavoriteCardState extends ConsumerState { child: GestureDetector( onTap: () async { if (coin == Coin.monero || coin == Coin.wownero) { - await ref.read(managerProvider).initializeExisting(); + await ref + .read(walletsChangeNotifierProvider) + .getManager(walletId) + .initializeExisting(); } if (mounted) { if (Util.isDesktop) { @@ -122,7 +122,9 @@ class _FavoriteCardState extends ConsumerState { WalletView.routeName, arguments: Tuple2( walletId, - managerProvider, + ref + .read(walletsChangeNotifierProvider) + .getManagerProvider(walletId), ), ); } @@ -205,8 +207,12 @@ class _FavoriteCardState extends ConsumerState { children: [ Expanded( child: Text( - ref.watch(managerProvider - .select((value) => value.walletName)), + ref.watch( + walletsChangeNotifierProvider.select( + (value) => + value.getManager(walletId).walletName, + ), + ), style: STextStyles.itemSubtitle12(context).copyWith( color: Theme.of(context) .extension()! @@ -225,41 +231,54 @@ class _FavoriteCardState extends ConsumerState { ], ), ), - FutureBuilder( - future: Future(() => ref.watch(managerProvider - .select((value) => value.balance.total))), - builder: (builderContext, AsyncSnapshot snapshot) { - if (snapshot.connectionState == ConnectionState.done && - snapshot.hasData) { - if (snapshot.data != null) { - _cachedBalance = snapshot.data!; - if (externalCalls && _cachedBalance > Amount.zero) { - _cachedFiatValue = (_cachedBalance.decimal * - ref - .watch( - priceAnd24hChangeNotifierProvider - .select( - (value) => value.getPrice(coin), - ), - ) - .item1) - .toAmount(fractionDigits: 2); - } - } + Builder( + builder: (context) { + final balance = ref.watch( + walletsChangeNotifierProvider.select( + (value) => value.getManager(walletId).balance, + ), + ); + + Amount total = balance.total; + if (coin == Coin.firo || coin == Coin.firoTestNet) { + final balancePrivate = ref.watch( + walletsChangeNotifierProvider.select( + (value) => (value.getManager(walletId).wallet + as FiroWallet) + .balancePrivate, + ), + ); + + total += balancePrivate.total; } + + Amount fiatTotal = Amount.zero; + + if (externalCalls && total > Amount.zero) { + fiatTotal = (total.decimal * + ref + .watch( + priceAnd24hChangeNotifierProvider.select( + (value) => value.getPrice(coin), + ), + ) + .item1) + .toAmount(fractionDigits: 2); + } + return Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ FittedBox( fit: BoxFit.scaleDown, child: Text( - "${_cachedBalance.localizedStringAsFixed( + "${total.localizedStringAsFixed( locale: ref.watch( - localeServiceChangeNotifierProvider - .select((value) => value.locale), + localeServiceChangeNotifierProvider.select( + (value) => value.locale, + ), ), - decimalPlaces: ref.watch(managerProvider - .select((value) => value.coin.decimals)), + decimalPlaces: coin.decimals, )} ${coin.ticker}", style: STextStyles.titleBold12(context).copyWith( fontSize: 16, @@ -275,15 +294,17 @@ class _FavoriteCardState extends ConsumerState { ), if (externalCalls) Text( - "${_cachedFiatValue.localizedStringAsFixed( + "${fiatTotal.localizedStringAsFixed( locale: ref.watch( - localeServiceChangeNotifierProvider - .select((value) => value.locale), + localeServiceChangeNotifierProvider.select( + (value) => value.locale, + ), ), decimalPlaces: 2, )} ${ref.watch( - prefsChangeNotifierProvider - .select((value) => value.currency), + prefsChangeNotifierProvider.select( + (value) => value.currency, + ), )}", style: STextStyles.itemSubtitle12(context).copyWith( diff --git a/lib/pages/wallets_view/sub_widgets/favorite_wallets.dart b/lib/pages/wallets_view/sub_widgets/favorite_wallets.dart index afbea4562..665c147e1 100644 --- a/lib/pages/wallets_view/sub_widgets/favorite_wallets.dart +++ b/lib/pages/wallets_view/sub_widgets/favorite_wallets.dart @@ -211,7 +211,6 @@ class _FavoriteWalletsState extends ConsumerState { child: FavoriteCard( key: Key("favCard_$walletId"), walletId: walletId!, - managerProvider: managerProvider!, width: cardWidth, height: cardHeight, ), @@ -219,7 +218,6 @@ class _FavoriteWalletsState extends ConsumerState { : FavoriteCard( key: Key("favCard_$walletId"), walletId: walletId!, - managerProvider: managerProvider!, width: cardWidth, height: cardHeight, ) diff --git a/lib/pages_desktop_specific/my_stack_view/desktop_favorite_wallets.dart b/lib/pages_desktop_specific/my_stack_view/desktop_favorite_wallets.dart index b9ce38eb4..03d0df2ca 100644 --- a/lib/pages_desktop_specific/my_stack_view/desktop_favorite_wallets.dart +++ b/lib/pages_desktop_specific/my_stack_view/desktop_favorite_wallets.dart @@ -74,7 +74,6 @@ class DesktopFavoriteWallets extends ConsumerWidget { key: Key(walletName), width: cardWidth, height: cardHeight, - managerProvider: managerProvider, ); }) ],