diff --git a/core/ui/src/main/java/com/tangem/core/ui/components/rows/BlockchainRow.kt b/core/ui/src/main/java/com/tangem/core/ui/components/rows/BlockchainRow.kt index 035be68d2e..70150328ce 100644 --- a/core/ui/src/main/java/com/tangem/core/ui/components/rows/BlockchainRow.kt +++ b/core/ui/src/main/java/com/tangem/core/ui/components/rows/BlockchainRow.kt @@ -137,6 +137,7 @@ private fun Preview_BlockchainRow(@PreviewParameter(BlockchainRowParameterProvid private class BlockchainRowParameterProvider : CollectionPreviewParameterProvider( collection = listOf( BlockchainRowUM( + id = "0", name = "BNB BEACON CHAIN", type = "BEP20", iconResId = R.drawable.img_bsc_22, @@ -144,6 +145,7 @@ private class BlockchainRowParameterProvider : CollectionPreviewParameterProvide isSelected = true, ), BlockchainRowUM( + id = "1", name = "1234567890111213141516171819", type = "BEP20", iconResId = R.drawable.ic_bsc_16, @@ -151,6 +153,7 @@ private class BlockchainRowParameterProvider : CollectionPreviewParameterProvide isSelected = false, ), BlockchainRowUM( + id = "2", name = "BNB BEACON CHAIN", type = "1234567890111213141516171819", iconResId = R.drawable.ic_bsc_16, diff --git a/core/ui/src/main/java/com/tangem/core/ui/components/rows/model/BlockchainRowUM.kt b/core/ui/src/main/java/com/tangem/core/ui/components/rows/model/BlockchainRowUM.kt index f8e756a9ae..700b4bc377 100644 --- a/core/ui/src/main/java/com/tangem/core/ui/components/rows/model/BlockchainRowUM.kt +++ b/core/ui/src/main/java/com/tangem/core/ui/components/rows/model/BlockchainRowUM.kt @@ -4,6 +4,7 @@ import androidx.compose.runtime.Immutable @Immutable data class BlockchainRowUM( + val id: String, val name: String, val type: String, val iconResId: Int, diff --git a/features/manage-tokens/impl/src/main/kotlin/com/tangem/features/managetokens/ui/ManageTokensScreen.kt b/features/manage-tokens/impl/src/main/kotlin/com/tangem/features/managetokens/ui/ManageTokensScreen.kt index c8771d1fa6..430896ed3a 100644 --- a/features/manage-tokens/impl/src/main/kotlin/com/tangem/features/managetokens/ui/ManageTokensScreen.kt +++ b/features/manage-tokens/impl/src/main/kotlin/com/tangem/features/managetokens/ui/ManageTokensScreen.kt @@ -348,6 +348,7 @@ private fun NetworksList( modifier = Modifier.padding(end = TangemTheme.dimens.spacing8), model = with(network) { BlockchainRowUM( + id = id, name = name, type = type, iconResId = iconResId, diff --git a/features/markets/impl/src/main/kotlin/com/tangem/features/markets/portfolio/impl/model/AddToPortfolioBSContentUMFactory.kt b/features/markets/impl/src/main/kotlin/com/tangem/features/markets/portfolio/impl/model/AddToPortfolioBSContentUMFactory.kt new file mode 100644 index 0000000000..9983cd1bb2 --- /dev/null +++ b/features/markets/impl/src/main/kotlin/com/tangem/features/markets/portfolio/impl/model/AddToPortfolioBSContentUMFactory.kt @@ -0,0 +1,113 @@ +package com.tangem.features.markets.portfolio.impl.model + +import com.tangem.common.ui.userwallet.converter.UserWalletItemUMConverter +import com.tangem.common.ui.userwallet.state.UserWalletItemUM +import com.tangem.core.ui.components.bottomsheets.TangemBottomSheetConfig +import com.tangem.domain.markets.TokenMarketInfo +import com.tangem.domain.markets.TokenMarketParams +import com.tangem.domain.wallets.models.UserWallet +import com.tangem.domain.wallets.models.UserWalletId +import com.tangem.features.markets.portfolio.impl.loader.PortfolioData +import com.tangem.features.markets.portfolio.impl.ui.state.AddToPortfolioBSContentUM +import com.tangem.features.markets.portfolio.impl.ui.state.WalletSelectorBSContentUM +import kotlinx.collections.immutable.toImmutableList + +/** + * Factory to create AddToPortfolio bottom sheet content [TangemBottomSheetConfig] + * + * @property token token params + * @property onAddToPortfolioVisibilityChange callback is invoked when add to portfolio visibility is changed + * @property onWalletSelectorVisibilityChange callback is invoked when wallet selector visibility is changed + * @property onNetworkSwitchClick callback is invoked when network switch is clicked + * @property onWalletSelect callback is invoked when wallet is selected + * @property onContinueClick callback is invoked when continue button is clicked + * +[REDACTED_AUTHOR] + */ +internal class AddToPortfolioBSContentUMFactory( + private val token: TokenMarketParams, + private val onAddToPortfolioVisibilityChange: (Boolean) -> Unit, + private val onWalletSelectorVisibilityChange: (Boolean) -> Unit, + private val onNetworkSwitchClick: (String, Boolean) -> Unit, + private val onWalletSelect: (UserWalletId) -> Unit, + private val onContinueClick: () -> Unit, +) { + + /** + * Create [TangemBottomSheetConfig] + * + * @param portfolioData portfolio data + * @param portfolioBSVisibilityModel portfolio bottom sheet visibility model + * @param selectedWallet selected wallet + * @param networksWithToggle networks with toggle + * @param isUserChangedNetworks flag indicates if user changed networks + */ + fun create( + portfolioData: PortfolioData, + portfolioBSVisibilityModel: PortfolioBSVisibilityModel, + selectedWallet: UserWallet, + networksWithToggle: Map, + isUserChangedNetworks: Boolean, + ): TangemBottomSheetConfig { + return TangemBottomSheetConfig( + isShow = portfolioBSVisibilityModel.addToPortfolioBSVisibility, + onDismissRequest = { onAddToPortfolioVisibilityChange(false) }, + content = AddToPortfolioBSContentUM( + selectedWallet = selectedWallet.toSelectedUserWalletItemUM(), + selectNetworkUM = SelectNetworkUMConverter( + networksWithToggle = networksWithToggle, + onNetworkSwitchClick = onNetworkSwitchClick, + ).convert(value = token), + isScanCardNotificationVisible = false, // TODO [REDACTED_JIRA] + continueButtonEnabled = isUserChangedNetworks, + onContinueButtonClick = onContinueClick, + walletSelectorConfig = crateWalletSelectorBSConfig( + isShow = portfolioBSVisibilityModel.walletSelectorBSVisibility, + portfolioData = portfolioData, + selectedWalletId = selectedWallet.walletId, + ), + ), + ) + } + + private fun UserWallet.toSelectedUserWalletItemUM(): UserWalletItemUM { + return UserWalletItemUMConverter( + onClick = { onWalletSelectorVisibilityChange(true) }, + endIcon = UserWalletItemUM.EndIcon.Arrow, + ).convert(value = this) + } + + private fun crateWalletSelectorBSConfig( + isShow: Boolean, + portfolioData: PortfolioData, + selectedWalletId: UserWalletId, + ): TangemBottomSheetConfig { + return TangemBottomSheetConfig( + isShow = isShow, + onDismissRequest = { onWalletSelectorVisibilityChange(false) }, + content = WalletSelectorBSContentUM( + userWallets = portfolioData.walletsWithCurrencyStatuses + .filterKeys(UserWallet::isMultiCurrency) + .map { it.key } + .map { userWallet -> + val balance = portfolioData.walletsWithBalance[userWallet.walletId] + + UserWalletItemUMConverter( + onClick = onWalletSelect, + appCurrency = portfolioData.appCurrency, + balance = balance?.getOrNull(), + isLoading = balance?.isLoading() == true, + isBalanceHidden = portfolioData.isBalanceHidden, + endIcon = if (userWallet.walletId == selectedWalletId) { + UserWalletItemUM.EndIcon.Checkmark + } else { + UserWalletItemUM.EndIcon.None + }, + ).convert(userWallet) + } + .toImmutableList(), + onBack = { onWalletSelectorVisibilityChange(false) }, + ), + ) + } +} \ No newline at end of file diff --git a/features/markets/impl/src/main/kotlin/com/tangem/features/markets/portfolio/impl/model/BlockchainRowUMConverter.kt b/features/markets/impl/src/main/kotlin/com/tangem/features/markets/portfolio/impl/model/BlockchainRowUMConverter.kt new file mode 100644 index 0000000000..b80d71c2d1 --- /dev/null +++ b/features/markets/impl/src/main/kotlin/com/tangem/features/markets/portfolio/impl/model/BlockchainRowUMConverter.kt @@ -0,0 +1,38 @@ +package com.tangem.features.markets.portfolio.impl.model + +import com.tangem.core.ui.components.rows.model.BlockchainRowUM +import com.tangem.core.ui.extensions.getActiveIconRes +import com.tangem.core.ui.extensions.getGreyedOutIconRes +import com.tangem.domain.markets.TokenMarketInfo +import com.tangem.lib.crypto.BlockchainUtils +import com.tangem.utils.converter.Converter + +/** + * Converter from [TokenMarketInfo.Network] to [BlockchainRowUM] + * +[REDACTED_AUTHOR] + */ +internal object BlockchainRowUMConverter : Converter, BlockchainRowUM> { + + override fun convert(value: Pair): BlockchainRowUM { + val (network, isSelected) = value + + val blockchainInfo = BlockchainUtils.getNetworkInfo(networkId = network.networkId) + ?: error("Can't find blockchain info for ${network.networkId}") + + val isMainNetwork = network.contractAddress == null + + return BlockchainRowUM( + id = network.networkId, + name = blockchainInfo.name, + type = if (isMainNetwork) "MAIN" else blockchainInfo.protocolName, + iconResId = if (isSelected) { + getActiveIconRes(blockchainInfo.blockchainId) + } else { + getGreyedOutIconRes(blockchainInfo.blockchainId) + }, + isMainNetwork = isMainNetwork, + isSelected = isSelected, + ) + } +} \ No newline at end of file diff --git a/features/markets/impl/src/main/kotlin/com/tangem/features/markets/portfolio/impl/model/PortfolioBSVisibilityModel.kt b/features/markets/impl/src/main/kotlin/com/tangem/features/markets/portfolio/impl/model/PortfolioBSVisibilityModel.kt new file mode 100644 index 0000000000..b2d43ea69a --- /dev/null +++ b/features/markets/impl/src/main/kotlin/com/tangem/features/markets/portfolio/impl/model/PortfolioBSVisibilityModel.kt @@ -0,0 +1,14 @@ +package com.tangem.features.markets.portfolio.impl.model + +/** + * Model for portfolio bottom sheet visibility + * + * @property addToPortfolioBSVisibility visibility of add to portfolio bottom sheet + * @property walletSelectorBSVisibility visibility of wallet selector bottom sheet + * +[REDACTED_AUTHOR] + */ +internal data class PortfolioBSVisibilityModel( + val addToPortfolioBSVisibility: Boolean = false, + val walletSelectorBSVisibility: Boolean = false, +) \ No newline at end of file diff --git a/features/markets/impl/src/main/kotlin/com/tangem/features/markets/portfolio/impl/model/SelectNetworkUMConverter.kt b/features/markets/impl/src/main/kotlin/com/tangem/features/markets/portfolio/impl/model/SelectNetworkUMConverter.kt new file mode 100644 index 0000000000..ea02d5c805 --- /dev/null +++ b/features/markets/impl/src/main/kotlin/com/tangem/features/markets/portfolio/impl/model/SelectNetworkUMConverter.kt @@ -0,0 +1,32 @@ +package com.tangem.features.markets.portfolio.impl.model + +import com.tangem.domain.markets.TokenMarketInfo +import com.tangem.domain.markets.TokenMarketParams +import com.tangem.features.markets.portfolio.impl.ui.state.SelectNetworkUM +import com.tangem.utils.converter.Converter +import kotlinx.collections.immutable.toImmutableList + +/** + * Converter from [TokenMarketParams] to [SelectNetworkUM] + * + * @property networksWithToggle map of networks with toggles + * @property onNetworkSwitchClick callback is called when network switch is clicked + * +[REDACTED_AUTHOR] + */ +internal class SelectNetworkUMConverter( + private val networksWithToggle: Map, + private val onNetworkSwitchClick: (String, Boolean) -> Unit, +) : Converter { + + override fun convert(value: TokenMarketParams): SelectNetworkUM { + return SelectNetworkUM( + tokenId = value.id, + iconUrl = value.imageUrl, + tokenName = value.name, + tokenCurrencySymbol = value.symbol, + networks = BlockchainRowUMConverter.convertList(networksWithToggle.toList()).toImmutableList(), + onNetworkSwitchClick = { um, isChecked -> onNetworkSwitchClick(um.id, isChecked) }, + ) + } +} \ No newline at end of file diff --git a/features/markets/impl/src/main/kotlin/com/tangem/features/markets/portfolio/impl/ui/AddToPortfolioBottomSheet.kt b/features/markets/impl/src/main/kotlin/com/tangem/features/markets/portfolio/impl/ui/AddToPortfolioBottomSheet.kt index 422bcdb678..315718d001 100644 --- a/features/markets/impl/src/main/kotlin/com/tangem/features/markets/portfolio/impl/ui/AddToPortfolioBottomSheet.kt +++ b/features/markets/impl/src/main/kotlin/com/tangem/features/markets/portfolio/impl/ui/AddToPortfolioBottomSheet.kt @@ -35,7 +35,6 @@ import com.tangem.core.ui.components.buttons.common.TangemButtonsDefaults import com.tangem.core.ui.components.currency.icon.CoinIcon import com.tangem.core.ui.components.rows.ArrowRow import com.tangem.core.ui.components.rows.BlockchainRow -import com.tangem.core.ui.components.rows.model.BlockchainRowUM import com.tangem.core.ui.extensions.resourceReference import com.tangem.core.ui.res.TangemTheme import com.tangem.core.ui.res.TangemThemePreview @@ -214,15 +213,7 @@ private fun NetworkSelection(state: SelectNetworkUM, modifier: Modifier = Modifi modifier = Modifier.padding( end = TangemTheme.dimens.spacing4, ), - model = with(network) { - BlockchainRowUM( - name = name, - type = type, - iconResId = iconResId, - isMainNetwork = isMainNetwork, - isSelected = isSelected, - ) - }, + model = network, action = { TangemSwitch( checked = network.isSelected, diff --git a/features/markets/impl/src/main/kotlin/com/tangem/features/markets/portfolio/impl/ui/preview/PreviewAddToPortfolioBSContentProvider.kt b/features/markets/impl/src/main/kotlin/com/tangem/features/markets/portfolio/impl/ui/preview/PreviewAddToPortfolioBSContentProvider.kt index d218f4f8db..725f9518be 100644 --- a/features/markets/impl/src/main/kotlin/com/tangem/features/markets/portfolio/impl/ui/preview/PreviewAddToPortfolioBSContentProvider.kt +++ b/features/markets/impl/src/main/kotlin/com/tangem/features/markets/portfolio/impl/ui/preview/PreviewAddToPortfolioBSContentProvider.kt @@ -15,6 +15,7 @@ import kotlinx.collections.immutable.persistentListOf internal class PreviewAddToPortfolioBSContentProvider : PreviewParameterProvider { private val blockchainRow = BlockchainRowUM( + id = "1", name = "Etherium 3", type = "TEST", iconResId = R.drawable.ic_eth_16, @@ -60,7 +61,6 @@ internal class PreviewAddToPortfolioBSContentProvider : PreviewParameterProvider onDismissRequest = {}, content = TangemBottomSheetConfigContent.Empty, ), - onWalletClick = {}, ), AddToPortfolioBSContentUM( selectedWallet = userWallet, @@ -88,7 +88,6 @@ internal class PreviewAddToPortfolioBSContentProvider : PreviewParameterProvider onDismissRequest = {}, content = TangemBottomSheetConfigContent.Empty, ), - onWalletClick = {}, ), ) } \ No newline at end of file diff --git a/features/markets/impl/src/main/kotlin/com/tangem/features/markets/portfolio/impl/ui/state/AddToPortfolioBSContentUM.kt b/features/markets/impl/src/main/kotlin/com/tangem/features/markets/portfolio/impl/ui/state/AddToPortfolioBSContentUM.kt index 7369b5cf69..ee30ab8615 100644 --- a/features/markets/impl/src/main/kotlin/com/tangem/features/markets/portfolio/impl/ui/state/AddToPortfolioBSContentUM.kt +++ b/features/markets/impl/src/main/kotlin/com/tangem/features/markets/portfolio/impl/ui/state/AddToPortfolioBSContentUM.kt @@ -10,6 +10,5 @@ internal data class AddToPortfolioBSContentUM( val isScanCardNotificationVisible: Boolean, val continueButtonEnabled: Boolean, val onContinueButtonClick: () -> Unit, - val onWalletClick: () -> Unit, val walletSelectorConfig: TangemBottomSheetConfig, ) : TangemBottomSheetConfigContent \ No newline at end of file diff --git a/libs/crypto/src/main/java/com/tangem/lib/crypto/BlockchainUtils.kt b/libs/crypto/src/main/java/com/tangem/lib/crypto/BlockchainUtils.kt index dd253860fd..830677086d 100644 --- a/libs/crypto/src/main/java/com/tangem/lib/crypto/BlockchainUtils.kt +++ b/libs/crypto/src/main/java/com/tangem/lib/crypto/BlockchainUtils.kt @@ -35,12 +35,6 @@ object BlockchainUtils { return blockchain == Blockchain.Bitcoin || blockchain == Blockchain.BitcoinTestnet } - /** If current [networkId] is Dogecoin */ - fun isDogecoin(networkId: String): Boolean { - val blockchain = Blockchain.fromId(networkId) - return blockchain == Blockchain.Dogecoin - } - /** If current [networkId] is Tezos */ fun isTezos(networkId: String): Boolean { val blockchain = Blockchain.fromId(networkId) @@ -61,4 +55,35 @@ object BlockchainUtils { fun isSupportedNetworkId(networkId: String): Boolean { return Blockchain.fromNetworkId(networkId)?.isSupportedInApp() ?: false } + + data class BlockchainInfo( + val blockchainId: String, + val name: String, + val protocolName: String, + ) + + fun getNetworkInfo(networkId: String): BlockchainInfo? { + val blockchain = Blockchain.fromNetworkId(networkId) ?: return null + + return BlockchainInfo( + blockchainId = blockchain.id, + name = getNetworkNameWithoutTestnet(blockchain), + protocolName = getNetworkStandardName(blockchain), + ) + } + + private fun getNetworkStandardName(blockchain: Blockchain): String { + return when (blockchain) { + Blockchain.Ethereum, Blockchain.EthereumTestnet -> "ERC20" + Blockchain.BSC, Blockchain.BSCTestnet -> "BEP20" + Blockchain.Binance, Blockchain.BinanceTestnet -> "BEP2" + Blockchain.Tron, Blockchain.TronTestnet -> "TRC20" + Blockchain.TON -> "TON" + else -> "" + } + } + + private fun getNetworkNameWithoutTestnet(blockchain: Blockchain): String { + return blockchain.getNetworkName().replace(oldValue = " Testnet", newValue = "") + } } \ No newline at end of file