diff --git a/lib/pages/token_view/sub_widgets/my_token_select_item.dart b/lib/pages/token_view/sub_widgets/my_token_select_item.dart index ef4303df1..508ab7e53 100644 --- a/lib/pages/token_view/sub_widgets/my_token_select_item.dart +++ b/lib/pages/token_view/sub_widgets/my_token_select_item.dart @@ -6,36 +6,57 @@ import 'package:stackwallet/pages/token_view/token_view.dart'; import 'package:stackwallet/providers/global/secure_store_provider.dart'; import 'package:stackwallet/providers/providers.dart'; import 'package:stackwallet/services/coins/ethereum/ethereum_wallet.dart'; +import 'package:stackwallet/services/ethereum/cached_eth_token_balance.dart'; import 'package:stackwallet/services/ethereum/ethereum_token_service.dart'; import 'package:stackwallet/services/transaction_notification_tracker.dart'; import 'package:stackwallet/utilities/assets.dart'; import 'package:stackwallet/utilities/constants.dart'; import 'package:stackwallet/utilities/enums/coin_enum.dart'; -import 'package:stackwallet/utilities/format.dart'; import 'package:stackwallet/utilities/show_loading.dart'; import 'package:stackwallet/utilities/text_styles.dart'; import 'package:stackwallet/widgets/rounded_white_container.dart'; -class MyTokenSelectItem extends ConsumerWidget { - const MyTokenSelectItem( - {Key? key, required this.walletId, required this.token, required}) - : super(key: key); +class MyTokenSelectItem extends ConsumerStatefulWidget { + const MyTokenSelectItem({ + Key? key, + required this.walletId, + required this.token, + }) : super(key: key); final String walletId; final EthContractInfo token; @override - Widget build(BuildContext context, WidgetRef ref) { - final balanceInDecimal = Format.satoshisToEthTokenAmount( - 1111111111111, //token.balance, - token.decimals, - ); + ConsumerState createState() => _MyTokenSelectItemState(); +} +class _MyTokenSelectItemState extends ConsumerState { + late final CachedEthTokenBalance cachedBalance; + + @override + void initState() { + cachedBalance = CachedEthTokenBalance(widget.walletId, widget.token); + + WidgetsBinding.instance.addPostFrameCallback((_) async { + final address = await ref + .read(walletsChangeNotifierProvider) + .getManager(widget.walletId) + .currentReceivingAddress; + await cachedBalance.fetchAndUpdateCachedBalance(address); + if (mounted) { + setState(() {}); + } + }); + + super.initState(); + } + + @override + Widget build(BuildContext context) { return RoundedWhiteContainer( padding: const EdgeInsets.all(0), child: MaterialButton( - // splashColor: Theme.of(context).extension()!.highlight, - key: Key("walletListItemButtonKey_${token.symbol}"), + key: Key("walletListItemButtonKey_${widget.token.symbol}"), padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 13), materialTapTargetSize: MaterialTapTargetSize.shrinkWrap, shape: RoundedRectangleBorder( @@ -45,29 +66,30 @@ class MyTokenSelectItem extends ConsumerWidget { onPressed: () async { ref.read(tokenServiceStateProvider.state).state = EthereumTokenService( - token: token, + token: widget.token, secureStore: ref.read(secureStoreProvider), ethWallet: ref .read(walletsChangeNotifierProvider) - .getManager(walletId) + .getManager(widget.walletId) .wallet as EthereumWallet, tracker: TransactionNotificationTracker( - walletId: walletId, + walletId: widget.walletId, ), ); await showLoading( - whileFuture: ref.read(tokenServiceProvider)!.initializeExisting(), + whileFuture: ref.read(tokenServiceProvider)!.initialize(), context: context, - message: "Loading ${token.name}", + message: "Loading ${widget.token.name}", ); - await Navigator.of(context).pushNamed( - TokenView.routeName, - arguments: walletId, - ); + if (mounted) { + await Navigator.of(context).pushNamed( + TokenView.routeName, + arguments: widget.walletId, + ); + } }, - child: Row( children: [ SvgPicture.asset( @@ -87,12 +109,13 @@ class MyTokenSelectItem extends ConsumerWidget { Row( children: [ Text( - token.name, + widget.token.name, style: STextStyles.titleBold12(context), ), const Spacer(), Text( - "$balanceInDecimal ${token.symbol}", + "${cachedBalance.getCachedBalance().getTotal()} " + "${widget.token.symbol}", style: STextStyles.itemSubtitle(context), ), ], @@ -103,11 +126,23 @@ class MyTokenSelectItem extends ConsumerWidget { Row( children: [ Text( - token.symbol, + widget.token.symbol, style: STextStyles.itemSubtitle(context), ), const Spacer(), - const Text("0 USD"), + Text("${ref.watch( + priceAnd24hChangeNotifierProvider.select( + (value) => value + .getTokenPrice(widget.token.contractAddress) + .item1 + .toStringAsFixed(2), + ), + )} " + "${ref.watch( + prefsChangeNotifierProvider.select( + (value) => value.currency, + ), + )}"), ], ), ], diff --git a/lib/services/ethereum/cached_eth_token_balance.dart b/lib/services/ethereum/cached_eth_token_balance.dart new file mode 100644 index 000000000..1867d57b5 --- /dev/null +++ b/lib/services/ethereum/cached_eth_token_balance.dart @@ -0,0 +1,39 @@ +import 'package:stackwallet/models/ethereum/eth_token.dart'; +import 'package:stackwallet/models/token_balance.dart'; +import 'package:stackwallet/services/ethereum/ethereum_api.dart'; +import 'package:stackwallet/services/mixins/eth_token_cache.dart'; +import 'package:stackwallet/utilities/logger.dart'; + +class CachedEthTokenBalance with EthTokenCache { + final String walletId; + final EthContractInfo token; + + CachedEthTokenBalance(this.walletId, this.token) { + initCache(walletId, token); + } + + Future fetchAndUpdateCachedBalance(String address) async { + final response = await EthereumAPI.getWalletTokenBalance( + address: address, + contractAddress: token.contractAddress, + ); + + if (response.value != null) { + await updateCachedBalance( + TokenBalance( + contractAddress: token.contractAddress, + decimalPlaces: token.decimals, + total: response.value!, + spendable: response.value!, + blockedTotal: 0, + pendingSpendable: 0, + ), + ); + } else { + Logging.instance.log( + "CachedEthTokenBalance.fetchAndUpdateCachedBalance failed: ${response.exception}", + level: LogLevel.Warning, + ); + } + } +} diff --git a/lib/services/mixins/eth_token_cache.dart b/lib/services/mixins/eth_token_cache.dart index 614688dee..b182da682 100644 --- a/lib/services/mixins/eth_token_cache.dart +++ b/lib/services/mixins/eth_token_cache.dart @@ -35,8 +35,6 @@ mixin EthTokenCache { } return TokenBalance.fromJson( jsonString, - _token.contractAddress, - _token.decimals, ); }