Updated on 2026-08-14

This commit is contained in:
Tangem 2022-07-04 21:14:26 +03:00
parent bb7a530086
commit 80c67e4d99
10 changed files with 104 additions and 63 deletions

View file

@ -0,0 +1,14 @@
package com.tangem.tap.common.compose.extensions
import androidx.compose.runtime.MutableState
/**
[REDACTED_AUTHOR]
*/
fun <T> MutableState<List<T>>.addAndNotify(value: T) {
this.value = this.value.toMutableList().apply { add(value) }
}
fun <T> MutableState<List<T>>.removeAndNotify(value: T) {
this.value = this.value.toMutableList().apply { remove(value) }
}

View file

@ -28,8 +28,6 @@ sealed class TokensAction : Action {
val wallets: List<WalletData>, val derivationStyle: DerivationStyle?
) : TokensAction()
data class SetNonRemovableCurrencies(val wallets: List<WalletData>) : TokensAction()
data class SaveChanges(
val addedTokens: List<TokenWithBlockchain>,
val addedBlockchains: List<Blockchain>

View file

@ -43,13 +43,6 @@ private fun internalReduce(action: Action, state: AppState): TokensState {
derivationStyle = action.derivationStyle
)
}
is TokensAction.SetNonRemovableCurrencies -> {
tokensState.copy(
nonRemovableBlockchains = action.wallets.toNonCustomBlockchains(tokensState.derivationStyle),
nonRemovableTokens = action.wallets.toTokensContractAddresses(),
)
}
is TokensAction.AllowToAddTokens -> {
tokensState.copy(allowToAdd = action.allow)
}

View file

@ -14,8 +14,6 @@ data class TokensState(
val addedWallets: List<WalletData> = emptyList(),
val addedTokens: List<TokenWithBlockchain> = emptyList(),
val addedBlockchains: List<Blockchain> = emptyList(),
val nonRemovableTokens: List<ContractAddress> = emptyList(),
val nonRemovableBlockchains: List<Blockchain> = emptyList(),
val currencies: List<Currency> = emptyList(),
val searchInput: String? = null,
val allowToAdd: Boolean = true,

View file

@ -21,6 +21,8 @@ import com.tangem.blockchain.common.Blockchain
import com.tangem.domain.common.TapWorkarounds.useOldStyleDerivation
import com.tangem.domain.common.extensions.fromNetworkId
import com.tangem.tap.common.compose.Keyboard
import com.tangem.tap.common.compose.extensions.addAndNotify
import com.tangem.tap.common.compose.extensions.removeAndNotify
import com.tangem.tap.common.compose.keyboardAsState
import com.tangem.tap.common.extensions.dispatchDialogShow
import com.tangem.tap.common.extensions.pixelsToDp
@ -30,6 +32,7 @@ import com.tangem.tap.features.tokens.redux.ContractAddress
import com.tangem.tap.features.tokens.redux.LoadCoinsState
import com.tangem.tap.features.tokens.redux.TokenWithBlockchain
import com.tangem.tap.features.tokens.redux.TokensState
import com.tangem.tap.features.wallet.redux.models.WalletDialog
import com.tangem.tap.store
import com.tangem.wallet.R
@ -40,30 +43,30 @@ fun CurrenciesScreen(
onNetworkItemClicked: (ContractAddress) -> Unit,
onLoadMore: () -> Unit
) {
val context = LocalContext.current
val tokensAddedOnMainScreen = remember { tokensState.value.addedTokens }
val blockchainsAddedOnMainScreen = remember { tokensState.value.addedBlockchains }
val addedTokensState = remember { mutableStateOf(tokensState.value.addedTokens) }
val addedBlockchainsState = remember { mutableStateOf(tokensState.value.addedBlockchains) }
val isKeyboardOpen by keyboardAsState()
val onAddCurrencyToggleClick = { currency: Currency, token: TokenWithBlockchain? ->
if (token != null) {
val mutableList = addedTokensState.value.toMutableList()
if (mutableList.contains(token)) {
mutableList.remove(token)
} else {
mutableList.add(token)
}
addedTokensState.value = mutableList
} else {
val blockchain = Blockchain.fromNetworkId(currency.id)
val mutableList = addedBlockchainsState.value.toMutableList()
if (mutableList.contains(blockchain)) {
mutableList.remove(blockchain)
} else {
blockchain?.let { mutableList.add(blockchain) }
}
addedBlockchainsState.value = mutableList
val blockchain = Blockchain.fromNetworkId(currency.id)
if (blockchain != null && token == null) {
toggleBlockchain(
blockchain,
blockchainsAddedOnMainScreen,
addedBlockchainsState,
addedTokensState,
)
} else if (token != null) {
toggleToken(
token,
tokensAddedOnMainScreen,
addedTokensState,
tokensState,
)
}
}
@ -100,23 +103,11 @@ fun CurrenciesScreen(
ListOfCurrencies(
header = { if (showHeader) CurrenciesWarning() },
currencies = tokensState.value.currencies,
nonRemovableTokens = tokensState.value.nonRemovableTokens,
nonRemovableBlockchains = tokensState.value.nonRemovableBlockchains,
addedTokens = addedTokensState.value,
addedBlockchains = addedBlockchainsState.value,
allowToAdd = tokensState.value.allowToAdd,
onAddCurrencyToggled = { currency, token ->
onAddCurrencyToggleClick(currency, token)
token?.let {
if (!tokensState.value.canHandleToken(it)) {
val dialog = AppDialog.SimpleOkDialog(
header = context.getString(R.string.common_warning),
message = context.getString(R.string.alert_manage_tokens_unsupported_message)
) { onAddCurrencyToggleClick(currency, it) }
store.dispatchDialogShow(dialog)
}
}
},
onNetworkItemClicked = onNetworkItemClicked,
onLoadMore = onLoadMore
@ -127,6 +118,68 @@ fun CurrenciesScreen(
}
}
private fun toggleBlockchain(
blockchain: Blockchain,
blockchainsAddedOnMainScreen: List<Blockchain>,
addedBlockchainsState: MutableState<List<Blockchain>>,
addedTokensState: MutableState<List<TokenWithBlockchain>>
) {
val isTryingToRemove = addedBlockchainsState.value.contains(blockchain)
val isAddedOnMainScreen = blockchainsAddedOnMainScreen.contains(blockchain)
val isTokenWithSameBlockchainFound = addedTokensState.value.any { it.blockchain == blockchain }
if (isTryingToRemove) {
if (isTokenWithSameBlockchainFound) {
store.dispatchDialogShow(WalletDialog.TokensAreLinkedDialog(
currencyTitle = blockchain.name,
currencySymbol = blockchain.currency
))
} else {
if (isAddedOnMainScreen) {
store.dispatchDialogShow(WalletDialog.RemoveWalletDialog(
currencyTitle = blockchain.name,
onOk = { addedBlockchainsState.removeAndNotify(blockchain) }
))
} else {
addedBlockchainsState.removeAndNotify(blockchain)
}
}
} else {
addedBlockchainsState.addAndNotify(blockchain)
}
}
private fun toggleToken(
token: TokenWithBlockchain,
tokensAddedOnMainScreen: List<TokenWithBlockchain>,
addedTokensState: MutableState<List<TokenWithBlockchain>>,
tokensState: MutableState<TokensState>,
) {
val isTryingToRemove = addedTokensState.value.contains(token)
val isAddedOnMainScreen = tokensAddedOnMainScreen.contains(token)
val isUnsupportedToken = !tokensState.value.canHandleToken(token)
if (isTryingToRemove) {
if (isAddedOnMainScreen) {
store.dispatchDialogShow(WalletDialog.RemoveWalletDialog(
currencyTitle = token.token.name,
onOk = { addedTokensState.removeAndNotify(token) }
))
} else {
addedTokensState.removeAndNotify(token)
}
} else {
if (isUnsupportedToken) {
store.dispatchDialogShow(AppDialog.SimpleOkDialogRes(
headerId = R.string.common_warning,
messageId = R.string.alert_manage_tokens_unsupported_message,
))
} else {
addedTokensState.addAndNotify(token)
}
}
}
@Composable
fun SaveChangesButton(keyboardState: Keyboard, onSaveChanges: () -> Unit) {

View file

@ -9,8 +9,6 @@ import com.tangem.tap.features.tokens.redux.TokenWithBlockchain
@Composable
fun CurrencyItem(
currency: Currency,
nonRemovableTokens: List<ContractAddress>,
nonRemovableBlockchains: List<Blockchain>,
addedTokens: List<TokenWithBlockchain>,
addedBlockchains: List<Blockchain>,
allowToAdd: Boolean,
@ -22,8 +20,6 @@ fun CurrencyItem(
if (expanded) {
ExpandedCurrencyItem(
currency = currency,
nonRemovableTokens = nonRemovableTokens,
nonRemovableBlockchains = nonRemovableBlockchains,
addedTokens = addedTokens,
addedBlockchains = addedBlockchains,
allowToAdd = allowToAdd,

View file

@ -28,8 +28,6 @@ import com.tangem.wallet.R
@Composable
fun ExpandedCurrencyItem(
currency: Currency,
nonRemovableTokens: List<ContractAddress>,
nonRemovableBlockchains: List<Blockchain>,
addedTokens: List<TokenWithBlockchain>,
addedBlockchains: List<Blockchain>,
allowToAdd: Boolean,
@ -138,17 +136,12 @@ fun ExpandedCurrencyItem(
} else {
addedBlockchains.contains(blockchain)
}
val canBeRemoved = if (contract.address != null) {
!nonRemovableTokens.contains(contract.address)
} else {
!nonRemovableBlockchains.contains(blockchain)
}
NetworkItem(
currency = currency,
contract = contract,
blockchain = blockchain, allowToAdd = allowToAdd,
blockchain = blockchain,
allowToAdd = allowToAdd,
added = added,
canBeRemoved = canBeRemoved,
onAddCurrencyToggled = onAddCurrencyToggled,
onNetworkItemClicked = onNetworkItemClicked,
)

View file

@ -22,8 +22,6 @@ import com.tangem.tap.features.tokens.redux.TokenWithBlockchain
fun ListOfCurrencies(
header: @Composable () -> Unit,
currencies: List<Currency>,
nonRemovableTokens: List<ContractAddress>,
nonRemovableBlockchains: List<Blockchain>,
addedTokens: List<TokenWithBlockchain>,
addedBlockchains: List<Blockchain>,
allowToAdd: Boolean,
@ -58,8 +56,6 @@ fun ListOfCurrencies(
itemsIndexed(currencies) { index, currency ->
CurrencyItem(
currency = currency,
nonRemovableTokens = nonRemovableTokens,
nonRemovableBlockchains = nonRemovableBlockchains,
addedTokens = addedTokens,
addedBlockchains = addedBlockchains,
allowToAdd = allowToAdd,

View file

@ -35,9 +35,11 @@ import com.tangem.tap.features.tokens.redux.TokenWithBlockchain
@OptIn(ExperimentalFoundationApi::class)
@Composable
fun NetworkItem(
currency: Currency, contract: Contract,
blockchain: Blockchain, allowToAdd: Boolean,
added: Boolean, canBeRemoved: Boolean,
currency: Currency,
contract: Contract,
blockchain: Blockchain,
allowToAdd: Boolean,
added: Boolean,
onAddCurrencyToggled: (Currency, TokenWithBlockchain?) -> Unit,
onNetworkItemClicked: (ContractAddress) -> Unit
) {
@ -126,7 +128,6 @@ fun NetworkItem(
Switch(
checked = added,
enabled = canBeRemoved,
onCheckedChange = { onAddCurrencyToggled(currencyToSave, tokenWithBlockchain) },
modifier = Modifier.padding(start = 16.dp, end = 16.dp),
colors = SwitchDefaults.colors(

View file

@ -139,7 +139,6 @@ class MultiWalletMiddleware {
}
if (action.fromScreen == AppScreen.AddTokens) {
store.dispatch(WalletAction.MultiWallet.SelectWallet(null))
store.dispatch(NavigationAction.PopBackTo())
}
}
is WalletAction.MultiWallet.ShowWalletBackupWarning -> Unit