From bb7a53008601f4f2c87ee98e4bb21d432e76693c Mon Sep 17 00:00:00 2001 From: Tangem Date: Mon, 4 Jul 2022 01:35:26 +0300 Subject: [PATCH] Updated on 2026-08-14 --- .../com/tangem/tap/common/DialogManager.kt | 2 +- .../java/com/tangem/tap/domain/TapErrors.kt | 19 ++-- .../features/tokens/redux/TokensMiddleware.kt | 10 +- .../tap/features/wallet/models/Currency.kt | 2 - .../tap/features/wallet/redux/WalletAction.kt | 18 ++-- .../middlewares/MultiWalletMiddleware.kt | 97 +++++++++---------- .../wallet/redux/models/WalletDialog.kt | 12 +-- .../redux/reducers/MultiWalletReducer.kt | 19 +--- .../wallet/ui/WalletDetailsFragment.kt | 2 +- .../wallet/ui/wallet/MultiWalletView.kt | 4 - 10 files changed, 82 insertions(+), 103 deletions(-) diff --git a/app/src/main/java/com/tangem/tap/common/DialogManager.kt b/app/src/main/java/com/tangem/tap/common/DialogManager.kt index 342e29a83e..dd24af74c5 100644 --- a/app/src/main/java/com/tangem/tap/common/DialogManager.kt +++ b/app/src/main/java/com/tangem/tap/common/DialogManager.kt @@ -129,7 +129,7 @@ class DialogManager : StoreSubscriber { messageRes = state.dialog.messageRes, context = context, primaryButtonRes = state.dialog.primaryButtonRes, - primaryButtonAction = state.dialog.action + primaryButtonAction = state.dialog.onOk ) else -> null } diff --git a/app/src/main/java/com/tangem/tap/domain/TapErrors.kt b/app/src/main/java/com/tangem/tap/domain/TapErrors.kt index 0c38cd5a46..ff19d10171 100644 --- a/app/src/main/java/com/tangem/tap/domain/TapErrors.kt +++ b/app/src/main/java/com/tangem/tap/domain/TapErrors.kt @@ -39,21 +39,26 @@ sealed class TapError( object DustChange : TapError(R.string.send_error_dust_change) data class CreateAccountUnderfunded(override val args: List) : TapError(R.string.send_error_no_target_account) + data class UnsupportedState( + val stateError: String, + val customMessage: String = "Unsupported state:" + ) : TapError(R.string.common_custom_string, listOf("$customMessage $stateError")) + sealed class XmlError { object AssetAccountNotCreated : TapError(R.string.send_error_no_account_xlm) } sealed class WalletManager { - object CreationError: CustomError("Can't create wallet manager") - class NoAccountError(amountToCreateAccount: String): CustomError(amountToCreateAccount) - class InternalError(message: String): CustomError(message) - object BlockchainIsUnreachable: TapError(R.string.wallet_balance_blockchain_unreachable) - object BlockchainIsUnreachableTryLater: TapError(R.string.wallet_balance_blockchain_unreachable_try_later) + object CreationError : CustomError("Can't create wallet manager") + class NoAccountError(amountToCreateAccount: String) : CustomError(amountToCreateAccount) + class InternalError(message: String) : CustomError(message) + object BlockchainIsUnreachable : TapError(R.string.wallet_balance_blockchain_unreachable) + object BlockchainIsUnreachableTryLater : TapError(R.string.wallet_balance_blockchain_unreachable_try_later) } data class ValidateTransactionErrors( - override val errorList: List, - override val builder: (List) -> String + override val errorList: List, + override val builder: (List) -> String ) : TapError(-1), MultiMessageError } diff --git a/app/src/main/java/com/tangem/tap/features/tokens/redux/TokensMiddleware.kt b/app/src/main/java/com/tangem/tap/features/tokens/redux/TokensMiddleware.kt index dd40f7074e..08c296ed82 100644 --- a/app/src/main/java/com/tangem/tap/features/tokens/redux/TokensMiddleware.kt +++ b/app/src/main/java/com/tangem/tap/features/tokens/redux/TokensMiddleware.kt @@ -308,12 +308,10 @@ class TokensMiddleware { private fun removeCurrenciesIfNeeded(currencies: List) { if (currencies.isNotEmpty()) { currencies.forEach { currency -> - store.state.walletState.getWalletData(currency)?.let { - store.dispatch(WalletAction.MultiWallet.RemoveWallet( - walletData = it, - fromWalletDetails = false - )) - } + store.dispatch(WalletAction.MultiWallet.RemoveWallet( + currency = currency, + fromScreen = AppScreen.AddTokens + )) } } } diff --git a/app/src/main/java/com/tangem/tap/features/wallet/models/Currency.kt b/app/src/main/java/com/tangem/tap/features/wallet/models/Currency.kt index 76945c588f..75f752b278 100644 --- a/app/src/main/java/com/tangem/tap/features/wallet/models/Currency.kt +++ b/app/src/main/java/com/tangem/tap/features/wallet/models/Currency.kt @@ -1,8 +1,6 @@ package com.tangem.tap.features.wallet.models -import com.tangem.blockchain.common.Blockchain import com.tangem.blockchain.common.DerivationStyle -import com.tangem.blockchain.common.Token import com.tangem.domain.common.extensions.toCoinId import com.tangem.domain.features.addCustomToken.CustomCurrency import com.tangem.tap.common.redux.global.CryptoCurrencyName 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 edc6be9c6e..59556b5893 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 @@ -1,23 +1,20 @@ package com.tangem.tap.features.wallet.redux import android.content.Context -import com.tangem.blockchain.common.Amount -import com.tangem.blockchain.common.Blockchain -import com.tangem.blockchain.common.Token -import com.tangem.blockchain.common.Wallet -import com.tangem.blockchain.common.WalletManager +import com.tangem.blockchain.common.* import com.tangem.blockchain.common.address.AddressType import com.tangem.common.card.Card import com.tangem.tap.common.entities.FiatCurrency import com.tangem.tap.common.redux.ErrorAction import com.tangem.tap.common.redux.NotificationAction +import com.tangem.tap.common.redux.navigation.AppScreen import com.tangem.tap.domain.TapError import com.tangem.tap.domain.configurable.warningMessage.WarningMessage import com.tangem.tap.domain.tokens.models.BlockchainNetwork import com.tangem.tap.features.wallet.models.Currency import com.tangem.wallet.R -import java.math.BigDecimal import org.rekotlin.Action +import java.math.BigDecimal sealed class WalletAction : Action { @@ -75,10 +72,13 @@ sealed class WalletAction : Action { ) : MultiWallet() data class SelectWallet(val walletData: WalletData?) : MultiWallet() - data class RemoveWallet(val walletData: WalletData, val fromWalletDetails: Boolean = true) : - MultiWallet() - data class TryToRemoveWallet(val walletData: WalletData) : MultiWallet() + data class TryToRemoveWallet(val currency: Currency) : MultiWallet() + data class RemoveWallet( + val currency: Currency, + val fromScreen: AppScreen + ) : MultiWallet() + data class SetPrimaryBlockchain(val blockchain: Blockchain) : MultiWallet() data class SetPrimaryToken(val token: Token) : MultiWallet() diff --git a/app/src/main/java/com/tangem/tap/features/wallet/redux/middlewares/MultiWalletMiddleware.kt b/app/src/main/java/com/tangem/tap/features/wallet/redux/middlewares/MultiWalletMiddleware.kt index e3af462d5d..632fbd34ac 100644 --- a/app/src/main/java/com/tangem/tap/features/wallet/redux/middlewares/MultiWalletMiddleware.kt +++ b/app/src/main/java/com/tangem/tap/features/wallet/redux/middlewares/MultiWalletMiddleware.kt @@ -5,12 +5,14 @@ import com.tangem.blockchain.common.Token import com.tangem.blockchain.common.WalletManager import com.tangem.common.extensions.guard import com.tangem.tap.common.extensions.dispatchDialogShow +import com.tangem.tap.common.extensions.dispatchErrorNotification import com.tangem.tap.common.extensions.safeUpdate import com.tangem.tap.common.redux.global.GlobalAction import com.tangem.tap.common.redux.global.GlobalState import com.tangem.tap.common.redux.navigation.AppScreen import com.tangem.tap.common.redux.navigation.NavigationAction import com.tangem.tap.currenciesRepository +import com.tangem.tap.domain.TapError import com.tangem.tap.domain.extensions.makeWalletManagerForApp import com.tangem.tap.domain.tokens.models.BlockchainNetwork import com.tangem.tap.features.demo.DemoHelper @@ -21,17 +23,16 @@ import com.tangem.tap.features.wallet.redux.WalletState import com.tangem.tap.features.wallet.redux.models.WalletDialog import com.tangem.tap.scope import com.tangem.tap.store -import java.math.BigDecimal import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch import kotlinx.coroutines.withContext +import java.math.BigDecimal class MultiWalletMiddleware { fun handle( action: WalletAction.MultiWallet, walletState: WalletState?, globalState: GlobalState?, ) { val globalState = globalState ?: return -// val tapWalletManager = globalState.tapWalletManager when (action) { is WalletAction.MultiWallet.AddBlockchains -> { @@ -81,64 +82,62 @@ class MultiWalletMiddleware { } } is WalletAction.MultiWallet.TryToRemoveWallet -> { - val walletState = store.state.walletState - val currency = action.walletData.currency - val walletCanBeRemoved = store.state.walletState.canBeRemoved( - store.state.walletState.getSelectedWalletData() - ) - if (walletCanBeRemoved) { - store.dispatch(WalletAction.MultiWallet.RemoveWallet(action.walletData)) + val currency = action.currency + val walletManager = walletState?.getWalletManager(currency).guard { + store.dispatchErrorNotification(TapError.UnsupportedState("walletManager is NULL")) + store.dispatch(NavigationAction.PopBackTo(AppScreen.Home)) return } - val walletManager = walletState.getWalletManager(currency).guard { - store.dispatch(WalletAction.MultiWallet.RemoveWallet(action.walletData)) - return + if (currency.isBlockchain() && walletManager.cardTokens.isNotEmpty()) { + store.dispatchDialogShow(WalletDialog.TokensAreLinkedDialog( + currencyTitle = currency.currencyName, + currencySymbol = currency.currencySymbol + )) + } else { + store.dispatchDialogShow(WalletDialog.RemoveWalletDialog( + currencyTitle = currency.currencyName, + onOk = { + store.dispatch(WalletAction.MultiWallet.RemoveWallet( + currency = currency, + fromScreen = AppScreen.WalletDetails + )) + store.dispatch(NavigationAction.PopBackTo()) + } + )) } - - val dialog = when { - currency is Currency.Blockchain && - walletManager.cardTokens.isNotEmpty() -> - WalletDialog.TokensAreLinkedDialog( - currency.currencyName, currency.currencySymbol - ) - - else -> - WalletDialog.RemoveWalletDialog(currency.currencyName, action.walletData) - } - store.dispatchDialogShow(dialog) } is WalletAction.MultiWallet.RemoveWallet -> { - val cardId = globalState.scanResponse?.card?.cardId - when (val currency = action.walletData.currency) { + val currency = action.currency + val cardId = globalState.scanResponse?.card?.cardId.guard { + store.dispatchErrorNotification(TapError.UnsupportedState("cardId is NULL")) + store.dispatch(NavigationAction.PopBackTo(AppScreen.Home)) + return + } + + when (currency) { is Currency.Blockchain -> { - cardId?.let { - currenciesRepository.removeBlockchain( - cardId = it, - blockchainNetwork = BlockchainNetwork( - currency.blockchain, currency.derivationPath, - emptyList() - ) + currenciesRepository.removeBlockchain( + cardId = cardId, + blockchainNetwork = BlockchainNetwork( + blockchain = currency.blockchain, + derivationPath = currency.derivationPath, + tokens = emptyList() + ) + ) + } + is Currency.Token -> { + walletState?.getWalletManager(currency)?.let { + it.removeToken(currency.token) + currenciesRepository.removeToken( + cardId = cardId, + token = currency.token, + blockchainNetwork = BlockchainNetwork.fromWalletManager(it) ) } } - is Currency.Token -> { - val walletManager = walletState?.getWalletManager(currency) - if (walletManager != null) { - walletManager.removeToken(currency.token) - cardId?.let { - currenciesRepository.removeToken( - cardId = it, - token = currency.token, - blockchainNetwork = BlockchainNetwork.fromWalletManager( - walletManager - ) - ) - } - } - } } - if (action.fromWalletDetails) { + if (action.fromScreen == AppScreen.AddTokens) { store.dispatch(WalletAction.MultiWallet.SelectWallet(null)) store.dispatch(NavigationAction.PopBackTo()) } diff --git a/app/src/main/java/com/tangem/tap/features/wallet/redux/models/WalletDialog.kt b/app/src/main/java/com/tangem/tap/features/wallet/redux/models/WalletDialog.kt index 5e2458c15d..a5a54c93f6 100644 --- a/app/src/main/java/com/tangem/tap/features/wallet/redux/models/WalletDialog.kt +++ b/app/src/main/java/com/tangem/tap/features/wallet/redux/models/WalletDialog.kt @@ -3,9 +3,6 @@ package com.tangem.tap.features.wallet.redux.models import com.tangem.blockchain.common.Amount import com.tangem.tap.common.entities.FiatCurrency import com.tangem.tap.common.redux.StateDialog -import com.tangem.tap.features.wallet.redux.WalletAction -import com.tangem.tap.features.wallet.redux.WalletData -import com.tangem.tap.store import com.tangem.wallet.R sealed interface WalletDialog : StateDialog { @@ -19,21 +16,18 @@ sealed interface WalletDialog : StateDialog { data class RemoveWalletDialog( val currencyTitle: String, - private val walletData: WalletData - ): WalletDialog { + val onOk: () -> Unit + ) : WalletDialog { val messageRes: Int = R.string.token_details_hide_alert_message val titleRes: Int = R.string.token_details_hide_alert_title val primaryButtonRes: Int = R.string.token_details_hide_alert_hide - val action = { store.dispatch(WalletAction.MultiWallet.RemoveWallet(walletData)) } } data class TokensAreLinkedDialog( val currencyTitle: String, val currencySymbol: String - ): WalletDialog { + ) : WalletDialog { val messageRes: Int = R.string.token_details_unable_hide_alert_message val titleRes: Int = R.string.token_details_unable_hide_alert_title } - - } \ No newline at end of file diff --git a/app/src/main/java/com/tangem/tap/features/wallet/redux/reducers/MultiWalletReducer.kt b/app/src/main/java/com/tangem/tap/features/wallet/redux/reducers/MultiWalletReducer.kt index 03cee11142..e8f9551910 100644 --- a/app/src/main/java/com/tangem/tap/features/wallet/redux/reducers/MultiWalletReducer.kt +++ b/app/src/main/java/com/tangem/tap/features/wallet/redux/reducers/MultiWalletReducer.kt @@ -7,17 +7,9 @@ import com.tangem.tap.common.extensions.toFiatString import com.tangem.tap.common.extensions.toFormattedCurrencyString import com.tangem.tap.domain.getFirstToken import com.tangem.tap.domain.tokens.models.BlockchainNetwork -import com.tangem.tap.features.wallet.models.Currency -import com.tangem.tap.features.wallet.models.WalletRent -import com.tangem.tap.features.wallet.models.filterByToken -import com.tangem.tap.features.wallet.models.getPendingTransactions -import com.tangem.tap.features.wallet.models.removeUnknownTransactions -import com.tangem.tap.features.wallet.redux.WalletAction -import com.tangem.tap.features.wallet.redux.WalletData -import com.tangem.tap.features.wallet.redux.WalletMainButton -import com.tangem.tap.features.wallet.redux.WalletState +import com.tangem.tap.features.wallet.models.* +import com.tangem.tap.features.wallet.redux.* import com.tangem.tap.features.wallet.redux.WalletState.Companion.UNKNOWN_AMOUNT_SIGN -import com.tangem.tap.features.wallet.redux.WalletStore import com.tangem.tap.features.wallet.ui.BalanceStatus import com.tangem.tap.features.wallet.ui.BalanceWidgetData import com.tangem.tap.features.wallet.ui.TokenData @@ -152,19 +144,16 @@ class MultiWalletReducer { is WalletAction.MultiWallet.SelectWallet -> state.copy(selectedCurrency = action.walletData?.currency) - + is WalletAction.MultiWallet.TryToRemoveWallet -> state is WalletAction.MultiWallet.RemoveWallet -> { - state.removeWallet(action.walletData) + state.removeWallet(state.getWalletData(action.currency)) } is WalletAction.MultiWallet.SetPrimaryBlockchain -> state.copy(primaryBlockchain = action.blockchain) is WalletAction.MultiWallet.SetPrimaryToken -> state.copy(primaryToken = action.token) -// is WalletAction.MultiWallet.FindTokensInUse -> state -// is WalletAction.MultiWallet.FindBlockchainsInUse -> state is WalletAction.MultiWallet.SaveCurrencies -> state - is WalletAction.MultiWallet.TryToRemoveWallet -> state is WalletAction.MultiWallet.ShowWalletBackupWarning -> state.copy( showBackupWarning = action.show ) diff --git a/app/src/main/java/com/tangem/tap/features/wallet/ui/WalletDetailsFragment.kt b/app/src/main/java/com/tangem/tap/features/wallet/ui/WalletDetailsFragment.kt index 26f1163798..0dfa0d5774 100644 --- a/app/src/main/java/com/tangem/tap/features/wallet/ui/WalletDetailsFragment.kt +++ b/app/src/main/java/com/tangem/tap/features/wallet/ui/WalletDetailsFragment.kt @@ -317,7 +317,7 @@ class WalletDetailsFragment : Fragment(R.layout.fragment_wallet_details), return when (item.itemId) { R.id.menu_remove -> { store.state.walletState.getSelectedWalletData()?.let { walletData -> - store.dispatch(WalletAction.MultiWallet.TryToRemoveWallet(walletData)) + store.dispatch(WalletAction.MultiWallet.TryToRemoveWallet(walletData.currency)) true } false diff --git a/app/src/main/java/com/tangem/tap/features/wallet/ui/wallet/MultiWalletView.kt b/app/src/main/java/com/tangem/tap/features/wallet/ui/wallet/MultiWalletView.kt index 994a491ac2..95560bd43d 100644 --- a/app/src/main/java/com/tangem/tap/features/wallet/ui/wallet/MultiWalletView.kt +++ b/app/src/main/java/com/tangem/tap/features/wallet/ui/wallet/MultiWalletView.kt @@ -112,10 +112,6 @@ class MultiWalletView : WalletView { derivationStyle = card.derivationStyle ) ) - store.dispatch( - TokensAction.SetNonRemovableCurrencies( - state.walletsData.filterNot { state.canBeRemoved(it) }) - ) store.dispatch(NavigationAction.NavigateTo(AppScreen.AddTokens)) } handleErrorStates(state = state, binding = binding, fragment = fragment)