From 8e1c45dfcbb598633e77f0835d6bc52dd13cab43 Mon Sep 17 00:00:00 2001 From: Tangem Date: Wed, 25 Oct 2023 10:07:27 +0800 Subject: [PATCH] Updated on 2026-08-14 --- .../middlewares/TradeCryptoMiddleware.kt | 31 +++++++++---------- .../domain/tokens/legacy/TradeCryptoAction.kt | 3 +- .../viewmodels/TokenDetailsViewModel.kt | 18 ++++++----- .../wallet/viewmodels/WalletViewModel.kt | 16 +++------- 4 files changed, 33 insertions(+), 35 deletions(-) diff --git a/app/src/main/java/com/tangem/tap/features/wallet/redux/middlewares/TradeCryptoMiddleware.kt b/app/src/main/java/com/tangem/tap/features/wallet/redux/middlewares/TradeCryptoMiddleware.kt index beeb8d029c..70c30acd7f 100644 --- a/app/src/main/java/com/tangem/tap/features/wallet/redux/middlewares/TradeCryptoMiddleware.kt +++ b/app/src/main/java/com/tangem/tap/features/wallet/redux/middlewares/TradeCryptoMiddleware.kt @@ -332,8 +332,7 @@ class TradeCryptoMiddleware { } private fun handleNewSendToken(action: TradeCryptoAction.New.SendToken) { - val cryptoStatus = action.tokenStatus - val currency = cryptoStatus.currency + val currency = action.tokenCurrency val blockchain = Blockchain.fromId(currency.network.id.value) scope.launch { @@ -352,21 +351,21 @@ class TradeCryptoMiddleware { return@launch } - val sendableAmounts = walletManager.wallet.amounts.values.filter { it.type is AmountType.Token } - when (currency) { - is CryptoCurrency.Coin -> error("Action.tokenStatus.currency is Coin") - is CryptoCurrency.Token -> { - store.dispatchOnMain( - action = PrepareSendScreen( - walletManager = walletManager, - coinAmount = walletManager.wallet.amounts[AmountType.Coin], - coinRate = action.coinFiatRate, - tokenAmount = sendableAmounts.first(), - tokenRate = cryptoStatus.value.fiatRate, - ), - ) - } + val sendableAmount = walletManager.wallet.amounts.values.firstOrNull { + val amountType = it.type + amountType is AmountType.Token && amountType.token.contractAddress == currency.contractAddress } + + store.dispatchOnMain( + action = PrepareSendScreen( + walletManager = walletManager, + coinAmount = walletManager.wallet.amounts[AmountType.Coin], + coinRate = action.coinFiatRate, + tokenAmount = sendableAmount, + tokenRate = action.tokenFiatRate, + ), + ) + val bundle = bundleOf(SendRouter.CRYPTO_CURRENCY_KEY to currency) store.dispatchOnMain(NavigationAction.NavigateTo(screen = AppScreen.Send, bundle = bundle)) } diff --git a/domain/tokens/src/main/kotlin/com/tangem/domain/tokens/legacy/TradeCryptoAction.kt b/domain/tokens/src/main/kotlin/com/tangem/domain/tokens/legacy/TradeCryptoAction.kt index 36ea2aa3c9..b2ce607a1f 100644 --- a/domain/tokens/src/main/kotlin/com/tangem/domain/tokens/legacy/TradeCryptoAction.kt +++ b/domain/tokens/src/main/kotlin/com/tangem/domain/tokens/legacy/TradeCryptoAction.kt @@ -39,7 +39,8 @@ sealed class TradeCryptoAction : Action { data class SendToken( val userWallet: UserWallet, - val tokenStatus: CryptoCurrencyStatus, + val tokenCurrency: CryptoCurrency.Token, + val tokenFiatRate: BigDecimal?, val coinFiatRate: BigDecimal?, ) : New() diff --git a/features/tokendetails/impl/src/main/kotlin/com/tangem/feature/tokendetails/presentation/tokendetails/viewmodels/TokenDetailsViewModel.kt b/features/tokendetails/impl/src/main/kotlin/com/tangem/feature/tokendetails/presentation/tokendetails/viewmodels/TokenDetailsViewModel.kt index 5aedee9158..40989288b2 100644 --- a/features/tokendetails/impl/src/main/kotlin/com/tangem/feature/tokendetails/presentation/tokendetails/viewmodels/TokenDetailsViewModel.kt +++ b/features/tokendetails/impl/src/main/kotlin/com/tangem/feature/tokendetails/presentation/tokendetails/viewmodels/TokenDetailsViewModel.kt @@ -46,6 +46,7 @@ import kotlinx.coroutines.awaitAll import kotlinx.coroutines.flow.* import kotlinx.coroutines.launch import timber.log.Timber +import java.math.BigDecimal import javax.inject.Inject import kotlin.properties.Delegates @@ -264,7 +265,7 @@ internal class TokenDetailsViewModel @Inject constructor( val cryptoCurrencyStatus = cryptoCurrencyStatus ?: return viewModelScope.launch(dispatchers.io) { - when (cryptoCurrencyStatus.currency) { + when (val currency = cryptoCurrencyStatus.currency) { is CryptoCurrency.Coin -> { reduxStateHolder.dispatch( action = TradeCryptoAction.New.SendCoin( @@ -273,26 +274,29 @@ internal class TokenDetailsViewModel @Inject constructor( ), ) } - is CryptoCurrency.Token -> sendToken(status = cryptoCurrencyStatus) + is CryptoCurrency.Token -> { + sendToken(tokenCurrency = currency, tokenFiatRate = cryptoCurrencyStatus.value.fiatRate) + } } } } - private fun sendToken(status: CryptoCurrencyStatus) { + private fun sendToken(tokenCurrency: CryptoCurrency.Token, tokenFiatRate: BigDecimal?) { viewModelScope.launch(dispatchers.io) { val wallet = getUserWalletUseCase(userWalletId).getOrElse { return@launch } val maybeCoinStatus = getNetworkCoinStatusUseCase( userWalletId = userWalletId, - networkId = status.currency.network.id, - derivationPath = status.currency.network.derivationPath, + networkId = tokenCurrency.network.id, + derivationPath = tokenCurrency.network.derivationPath, isSingleWalletWithTokens = wallet.scanResponse.cardTypesResolver.isSingleWalletWithToken(), ).firstOrNull() maybeCoinStatus?.onRight { coinStatus -> reduxStateHolder.dispatch( action = TradeCryptoAction.New.SendToken( - userWallet = getUserWalletUseCase(userWalletId).getOrElse { return@launch }, - tokenStatus = status, + userWallet = wallet, + tokenCurrency = tokenCurrency, + tokenFiatRate = tokenFiatRate, coinFiatRate = coinStatus.value.fiatRate, ), ) diff --git a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/viewmodels/WalletViewModel.kt b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/viewmodels/WalletViewModel.kt index b826160bc5..8c38af790a 100644 --- a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/viewmodels/WalletViewModel.kt +++ b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/viewmodels/WalletViewModel.kt @@ -638,36 +638,30 @@ internal class WalletViewModel @Inject constructor( } override fun onMultiCurrencySendClick(cryptoCurrencyStatus: CryptoCurrencyStatus) { - if (cryptoCurrencyStatus.currency is CryptoCurrency.Coin) { - onSingleCurrencySendClick(cryptoCurrencyStatus = cryptoCurrencyStatus) - return - } - val state = uiState as? WalletState.ContentState ?: return analyticsEventsHandler.send( event = TokenScreenAnalyticsEvent.ButtonSend(cryptoCurrencyStatus.currency.symbol), ) + val currency = cryptoCurrencyStatus.currency as? CryptoCurrency.Token ?: return viewModelScope.launch(dispatchers.io) { val userWallet = getWallet(index = state.walletsListConfig.selectedWalletIndex) - val isSingleWalletWithTokens = !userWallet.isMultiCurrency && - userWallet.scanResponse.walletData?.token != null - getNetworkCoinStatusUseCase( userWalletId = userWallet.walletId, networkId = cryptoCurrencyStatus.currency.network.id, derivationPath = cryptoCurrencyStatus.currency.network.derivationPath, - isSingleWalletWithTokens = isSingleWalletWithTokens, + isSingleWalletWithTokens = userWallet.scanResponse.cardTypesResolver.isSingleWalletWithToken(), ) .take(count = 1) .collectLatest { it.onRight { coinStatus -> reduxStateHolder.dispatch( action = TradeCryptoAction.New.SendToken( - userWallet = getWallet(index = state.walletsListConfig.selectedWalletIndex), - tokenStatus = cryptoCurrencyStatus, + userWallet = userWallet, + tokenCurrency = currency, + tokenFiatRate = cryptoCurrencyStatus.value.fiatRate, coinFiatRate = coinStatus.value.fiatRate, ), )