From 40da684e1058dc0415aa2965d60e8bf8abc2c5e6 Mon Sep 17 00:00:00 2001 From: reasje Date: Fri, 27 Oct 2023 16:58:35 +0330 Subject: [PATCH] fix: Balance update bug in other chains --- .../domain/transactions_use_case.dart | 4 ++ .../presentation/wallet_page_presenter.dart | 39 +++++++++++++------ .../presentation/wallet_page_state.dart | 3 ++ 3 files changed, 35 insertions(+), 11 deletions(-) diff --git a/lib/common/components/recent_transactions/domain/transactions_use_case.dart b/lib/common/components/recent_transactions/domain/transactions_use_case.dart index 09e2c2df..8155a553 100644 --- a/lib/common/components/recent_transactions/domain/transactions_use_case.dart +++ b/lib/common/components/recent_transactions/domain/transactions_use_case.dart @@ -15,6 +15,8 @@ class TransactionsHistoryUseCase extends ReactiveUseCase { List getTransactionsHistory() => _repository.items; + late final ValueStream shouldUpdateBalances = reactive(false); + List updatingTxList = []; void updateItem( @@ -52,6 +54,7 @@ class TransactionsHistoryUseCase extends ReactiveUseCase { if (!updatingTxList.contains(item.hash)) { updatingTxList.add(item.hash); final stream = _web3Repository.tokenContract.spyTransaction(item.hash); + stream.onData((succeeded) { if (succeeded) { final updatedItem = item.copyWith(status: TransactionStatus.done); @@ -59,6 +62,7 @@ class TransactionsHistoryUseCase extends ReactiveUseCase { updatedItem, ); updatingTxList.remove(item.hash); + update(shouldUpdateBalances, true); stream.cancel(); } }); diff --git a/lib/features/wallet/presentation/wallet_page_presenter.dart b/lib/features/wallet/presentation/wallet_page_presenter.dart index d5916644..b082b8f7 100644 --- a/lib/features/wallet/presentation/wallet_page_presenter.dart +++ b/lib/features/wallet/presentation/wallet_page_presenter.dart @@ -30,8 +30,7 @@ class WalletPresenter extends CompletePresenter { super.initState(); getMXCTweets(); - _transactionHistoryUseCase.checkForPendingTransactions( - _chainConfigurationUseCase.getCurrentNetworkWithoutRefresh().chainId); + checkForPendingTx(); listen(_accountUserCase.account, (value) { if (value != null) { @@ -52,14 +51,15 @@ class WalletPresenter extends CompletePresenter { state.network = value; connectAndSubscribe(); getTransactions(); + resetBalanceUpdateStream(); } }); listen(_transactionHistoryUseCase.transactionsHistory, (value) { - if (state.network != null) { - if (!Config.isMxcChains(state.network!.chainId)) { - getCustomChainsTransactions(value); - } + if (state.network != null && + !Config.isMxcChains(state.network!.chainId)) { + getCustomChainsTransactions(value); + initBalanceUpdateStream(); } }); @@ -230,12 +230,9 @@ class WalletPresenter extends CompletePresenter { void getCustomChainsTransactions(List? txHistory) { txHistory = txHistory ?? _transactionHistoryUseCase.getTransactionsHistory(); + final chainTxHistory = txHistory; - if (state.network != null) { - final chainTxHistory = txHistory; - - notify(() => state.txList = chainTxHistory); - } + notify(() => state.txList = chainTxHistory); } void getMXCTransactions() async { @@ -460,4 +457,24 @@ class WalletPresenter extends CompletePresenter { notify(() => state.maxTweetViewHeight = height + 120); } } + + void checkForPendingTx() { + _transactionHistoryUseCase.checkForPendingTransactions( + _chainConfigurationUseCase.getCurrentNetworkWithoutRefresh().chainId); + } + + void initBalanceUpdateStream() { + state.balancesUpdateSubscription ??= + listen(_transactionHistoryUseCase.shouldUpdateBalances, (value) { + if (value) initializeBalancePanelAndTokens(); + }); + } + + void resetBalanceUpdateStream() { + if (Config.isMxcChains(state.network!.chainId) && + state.balancesUpdateSubscription != null) { + state.balancesUpdateSubscription!.cancel(); + state.balancesUpdateSubscription = null; + } + } } diff --git a/lib/features/wallet/presentation/wallet_page_state.dart b/lib/features/wallet/presentation/wallet_page_state.dart index 63fee92d..a0564495 100644 --- a/lib/features/wallet/presentation/wallet_page_state.dart +++ b/lib/features/wallet/presentation/wallet_page_state.dart @@ -32,6 +32,9 @@ class WalletState with EquatableMixin { StreamSubscription? subscription; + /// This stream is only used for chains other than MXC + StreamSubscription? balancesUpdateSubscription; + Network? network; double maxTweetViewHeight = 620;