From 5d513a7b8f46f35a98bf0cf0b42d8a137daab5b6 Mon Sep 17 00:00:00 2001 From: Tangem Date: Thu, 17 Mar 2022 23:13:05 +0300 Subject: [PATCH] Updated on 2026-08-14 --- .../com/tangem/tap/domain/TapWalletManager.kt | 60 +++++++------------ .../send/redux/middlewares/SendMiddleware.kt | 4 +- .../tap/features/wallet/redux/WalletAction.kt | 6 -- .../redux/middlewares/WalletMiddleware.kt | 27 +++------ .../wallet/redux/reducers/WalletReducer.kt | 6 +- 5 files changed, 30 insertions(+), 73 deletions(-) diff --git a/app/src/main/java/com/tangem/tap/domain/TapWalletManager.kt b/app/src/main/java/com/tangem/tap/domain/TapWalletManager.kt index 55fc7f2154..6b67d9d90f 100644 --- a/app/src/main/java/com/tangem/tap/domain/TapWalletManager.kt +++ b/app/src/main/java/com/tangem/tap/domain/TapWalletManager.kt @@ -3,7 +3,6 @@ package com.tangem.tap.domain import com.tangem.blockchain.blockchains.solana.RentProvider import com.tangem.blockchain.common.* import com.tangem.common.services.Result -import com.tangem.tap.common.extensions.dispatchDebugErrorNotification import com.tangem.tap.common.extensions.dispatchOnMain import com.tangem.tap.common.extensions.safeUpdate import com.tangem.tap.common.extensions.stripZeroPlainString @@ -42,20 +41,31 @@ class TapWalletManager { by lazy { WalletManagerFactory(blockchainSdkConfig) } suspend fun loadWalletData(walletManager: WalletManager) { - handleUpdateWalletResult(walletManager.safeUpdate(), walletManager) - } - - suspend fun updateWallet(walletManager: WalletManager) { val result = walletManager.safeUpdate() withContext(Dispatchers.Main) { when (result) { - is Result.Success -> - store.dispatch(WalletAction.UpdateWallet.Success(result.data)) - is Result.Failure -> - store.dispatch(WalletAction.UpdateWallet.Failure(result.error.localizedMessage)) + is Result.Success -> { + checkForRentWarning(walletManager) + store.dispatch(WalletAction.LoadWallet.Success(result.data)) + } + is Result.Failure -> { + when (result.error) { + is TapError.WalletManagerUpdate.NoAccountError -> { + store.dispatch(WalletAction.LoadWallet.NoAccount( + walletManager.wallet, + (result.error as TapError.WalletManagerUpdate.NoAccountError).customMessage + )) + } + else -> { + store.dispatch(WalletAction.LoadWallet.Failure( + walletManager.wallet, + result.error.localizedMessage + )) + } + } + } } } - } suspend fun loadFiatRate(fiatCurrency: FiatCurrencyName, wallet: Wallet) { @@ -224,36 +234,6 @@ class TapWalletManager { } } - private suspend fun handleUpdateWalletResult(result: Result, walletManager: WalletManager) { - withContext(Dispatchers.Main) { - when (result) { - is Result.Success -> { - checkForRentWarning(walletManager) - store.dispatch(WalletAction.LoadWallet.Success(result.data)) - } - is Result.Failure -> { - val error = (result.error as? TapError) ?: TapError.UnknownError - when (error) { - TapError.NoInternetConnection -> { - store.dispatch(WalletAction.LoadData.Failure(error)) - } - is TapError.WalletManagerUpdate.NoAccountError -> { - store.dispatch(WalletAction.LoadWallet - .NoAccount(walletManager.wallet, error.customMessage)) - } - is TapError.WalletManagerUpdate.InternalError -> { - store.dispatch(WalletAction.LoadWallet - .Failure(walletManager.wallet, error.customMessage)) - } - else -> { - store.dispatchDebugErrorNotification(error) - } - } - } - } - } - } - private fun checkForRentWarning(walletManager: WalletManager) { val rentProvider = walletManager as? RentProvider ?: return diff --git a/app/src/main/java/com/tangem/tap/features/send/redux/middlewares/SendMiddleware.kt b/app/src/main/java/com/tangem/tap/features/send/redux/middlewares/SendMiddleware.kt index f103fc3817..74aadc12bb 100644 --- a/app/src/main/java/com/tangem/tap/features/send/redux/middlewares/SendMiddleware.kt +++ b/app/src/main/java/com/tangem/tap/features/send/redux/middlewares/SendMiddleware.kt @@ -202,11 +202,11 @@ private fun sendTransaction( } scope.launch(Dispatchers.IO) { withContext(Dispatchers.Main) { - dispatch(WalletAction.UpdateWallet(walletManager.wallet.blockchain)) + dispatch(WalletAction.LoadWallet(walletManager.wallet.blockchain)) } delay(10000) withContext(Dispatchers.Main) { - dispatch(WalletAction.UpdateWallet(walletManager.wallet.blockchain)) + dispatch(WalletAction.LoadWallet(walletManager.wallet.blockchain)) } } } diff --git a/app/src/main/java/com/tangem/tap/features/wallet/redux/WalletAction.kt b/app/src/main/java/com/tangem/tap/features/wallet/redux/WalletAction.kt index 57a03d4313..92aded77cc 100644 --- a/app/src/main/java/com/tangem/tap/features/wallet/redux/WalletAction.kt +++ b/app/src/main/java/com/tangem/tap/features/wallet/redux/WalletAction.kt @@ -74,12 +74,6 @@ sealed class WalletAction : Action { class CheckRemainingSignatures(val remainingSignatures: Int?) : Warnings() } - data class UpdateWallet(val blockchain: Blockchain? = null) : WalletAction() { - object ScheduleUpdatingWallet : WalletAction() - data class Success(val wallet: Wallet) : WalletAction() - data class Failure(val errorMessage: String? = null) : WalletAction() - } - data class LoadFiatRate( val wallet: Wallet? = null, val currency: Currency? = null, ) : WalletAction() { diff --git a/app/src/main/java/com/tangem/tap/features/wallet/redux/middlewares/WalletMiddleware.kt b/app/src/main/java/com/tangem/tap/features/wallet/redux/middlewares/WalletMiddleware.kt index a9506668c6..7713008b2c 100644 --- a/app/src/main/java/com/tangem/tap/features/wallet/redux/middlewares/WalletMiddleware.kt +++ b/app/src/main/java/com/tangem/tap/features/wallet/redux/middlewares/WalletMiddleware.kt @@ -24,7 +24,10 @@ import com.tangem.tap.domain.extensions.toSendableAmounts import com.tangem.tap.features.demo.DemoHelper import com.tangem.tap.features.home.redux.HomeAction import com.tangem.tap.features.send.redux.PrepareSendScreen -import com.tangem.tap.features.wallet.redux.* +import com.tangem.tap.features.wallet.redux.Currency +import com.tangem.tap.features.wallet.redux.WalletAction +import com.tangem.tap.features.wallet.redux.WalletData +import com.tangem.tap.features.wallet.redux.WalletState import com.tangem.tap.network.NetworkStateChanged import com.tangem.tap.scope import com.tangem.tap.store @@ -63,9 +66,9 @@ class WalletMiddleware { is WalletAction.LoadWallet -> { scope.launch { if (action.blockchain == null) { - walletState.walletManagers.map { walletManager -> - async { globalState.tapWalletManager.loadWalletData(walletManager) } - }.awaitAll() + walletState.walletManagers.map { walletManager -> + async { globalState.tapWalletManager.loadWalletData(walletManager) } + }.awaitAll() } else { val walletManager = walletState.getWalletManager(action.blockchain) walletManager?.let { globalState.tapWalletManager.loadWalletData(it) } @@ -127,22 +130,6 @@ class WalletMiddleware { } } } - is WalletAction.UpdateWallet -> { - if (action.blockchain != null) { - scope.launch { - val walletManager = walletState.getWalletManager(action.blockchain) - walletManager?.let { globalState.tapWalletManager.updateWallet(it) } - } - } else { - scope.launch { - if (walletState.state == ProgressState.Done) { - walletState.walletManagers.map { walletManager -> - globalState.tapWalletManager.updateWallet(walletManager) - } - } - } - } - } is WalletAction.Scan -> { store.dispatch(HomeAction.ShouldScanCardOnResume(true)) store.dispatch(NavigationAction.PopBackTo(AppScreen.Home)) diff --git a/app/src/main/java/com/tangem/tap/features/wallet/redux/reducers/WalletReducer.kt b/app/src/main/java/com/tangem/tap/features/wallet/redux/reducers/WalletReducer.kt index 488f7e1ab9..7defb905f2 100644 --- a/app/src/main/java/com/tangem/tap/features/wallet/redux/reducers/WalletReducer.kt +++ b/app/src/main/java/com/tangem/tap/features/wallet/redux/reducers/WalletReducer.kt @@ -131,11 +131,7 @@ private fun internalReduce(action: Action, state: AppState): WalletState { newState = newState.copy(walletsData = newState.updateTradeCryptoState(exchangeManager, wallets)) } } - is WalletAction.LoadWallet.Success -> newState = - onWalletLoadedReducer.reduce(action.wallet, newState) - is WalletAction.UpdateWallet.Success -> { - newState = onWalletLoadedReducer.reduce(action.wallet, newState) - } + is WalletAction.LoadWallet.Success -> newState = onWalletLoadedReducer.reduce(action.wallet, newState) is WalletAction.LoadWallet.NoAccount -> { val walletData = newState.getWalletData(action.wallet.blockchain)?.copy( currencyData = BalanceWidgetData(