From a816b14c6946776a5d4b96082d89444a9cea6706 Mon Sep 17 00:00:00 2001 From: Tangem Date: Tue, 27 Aug 2024 14:58:46 +0400 Subject: [PATCH] Updated on 2026-08-14 --- common/ui/build.gradle.kts | 7 +- .../converter/UserWalletItemUMConverter.kt | 102 ++++++++++++++++++ .../domain/tokens/model/TotalFiatBalance.kt | 0 .../details/utils/UserWalletMappers.kt | 102 ------------------ .../details/utils/UserWalletsFetcher.kt | 33 ++++-- 5 files changed, 130 insertions(+), 114 deletions(-) create mode 100644 common/ui/src/main/java/com/tangem/common/ui/userwallet/converter/UserWalletItemUMConverter.kt rename domain/tokens/{src/main/kotlin => models/src/main/java}/com/tangem/domain/tokens/model/TotalFiatBalance.kt (100%) delete mode 100644 features/details/impl/src/main/kotlin/com/tangem/features/details/utils/UserWalletMappers.kt diff --git a/common/ui/build.gradle.kts b/common/ui/build.gradle.kts index d7b55fb311..9380922c04 100644 --- a/common/ui/build.gradle.kts +++ b/common/ui/build.gradle.kts @@ -28,11 +28,12 @@ dependencies { implementation(projects.core.utils) /** Project - Domain */ - implementation(projects.domain.tokens.models) - implementation(projects.domain.wallets.models) - implementation(projects.domain.staking.models) implementation(projects.domain.appCurrency.models) + implementation(projects.domain.legacy) + implementation(projects.domain.staking.models) + implementation(projects.domain.tokens.models) implementation(projects.domain.transaction.models) + implementation(projects.domain.wallets.models) implementation(deps.tangem.blockchain) { exclude(module = "joda-time") diff --git a/common/ui/src/main/java/com/tangem/common/ui/userwallet/converter/UserWalletItemUMConverter.kt b/common/ui/src/main/java/com/tangem/common/ui/userwallet/converter/UserWalletItemUMConverter.kt new file mode 100644 index 0000000000..704315fc82 --- /dev/null +++ b/common/ui/src/main/java/com/tangem/common/ui/userwallet/converter/UserWalletItemUMConverter.kt @@ -0,0 +1,102 @@ +package com.tangem.common.ui.userwallet.converter + +import com.tangem.common.ui.R +import com.tangem.common.ui.userwallet.state.UserWalletItemUM +import com.tangem.core.ui.extensions.* +import com.tangem.core.ui.utils.BigDecimalFormatter +import com.tangem.domain.appcurrency.model.AppCurrency +import com.tangem.domain.common.util.getCardsCount +import com.tangem.domain.tokens.model.TotalFiatBalance +import com.tangem.domain.wallets.models.UserWallet +import com.tangem.domain.wallets.models.UserWalletId +import com.tangem.utils.StringsSigns.DOT +import com.tangem.utils.StringsSigns.STARS +import com.tangem.utils.converter.Converter + +/** + * Converter from [UserWallet] to [UserWalletItemUM] + * + * @property onClick lambda be invoked when item is clicked + * @property appCurrency selected app currency + * @property balance wallet balance + * @property isLoading wallet loading state + * @property isBalanceHidden wallet balance is hidden + * +[REDACTED_AUTHOR] + */ +class UserWalletItemUMConverter( + private val onClick: (UserWalletId) -> Unit, + private val appCurrency: AppCurrency? = null, + private val balance: TotalFiatBalance? = null, + private val isLoading: Boolean = true, + private val isBalanceHidden: Boolean = false, +) : Converter { + + override fun convert(value: UserWallet): UserWalletItemUM { + return with(value) { + UserWalletItemUM( + id = walletId, + name = stringReference(name), + information = getInfo( + appCurrency = appCurrency, + balance = balance, + isBalanceHidden = isBalanceHidden, + isLoading = isLoading, + ), + imageUrl = artworkUrl, + isEnabled = !isLocked, + onClick = { onClick(value.walletId) }, + ) + } + } + + private fun UserWallet.getInfo( + appCurrency: AppCurrency?, + balance: TotalFiatBalance?, + isBalanceHidden: Boolean, + isLoading: Boolean, + ): TextReference { + val dividerRef = stringReference(value = " $DOT ") + + val cardCount = getCardsCount() ?: 1 + val cardCountRef = TextReference.PluralRes( + id = R.plurals.card_label_card_count, + count = cardCount, + formatArgs = wrappedList(cardCount), + ) + + return when { + isBalanceHidden -> combinedReference(cardCountRef, dividerRef, stringReference(STARS)) + isLocked -> combinedReference(cardCountRef, dividerRef, resourceReference(R.string.common_locked)) + isLoading -> cardCountRef + else -> getBalanceInfo(balance, appCurrency, cardCountRef, dividerRef) + } + } + + private fun getBalanceInfo( + balance: TotalFiatBalance?, + appCurrency: AppCurrency?, + cardCountRef: TextReference, + dividerRef: TextReference, + ): TextReference { + val amount = when (balance) { + is TotalFiatBalance.Loaded -> balance.amount + is TotalFiatBalance.Failed, + is TotalFiatBalance.Loading, + null, + -> null + } + + return if (amount != null && appCurrency != null) { + val formattedAmount = BigDecimalFormatter.formatFiatAmount( + fiatAmount = amount, + fiatCurrencyCode = appCurrency.code, + fiatCurrencySymbol = appCurrency.symbol, + ) + val amountRef = stringReference(formattedAmount) + combinedReference(cardCountRef, dividerRef, amountRef) + } else { + combinedReference(cardCountRef, dividerRef, stringReference(BigDecimalFormatter.EMPTY_BALANCE_SIGN)) + } + } +} \ No newline at end of file diff --git a/domain/tokens/src/main/kotlin/com/tangem/domain/tokens/model/TotalFiatBalance.kt b/domain/tokens/models/src/main/java/com/tangem/domain/tokens/model/TotalFiatBalance.kt similarity index 100% rename from domain/tokens/src/main/kotlin/com/tangem/domain/tokens/model/TotalFiatBalance.kt rename to domain/tokens/models/src/main/java/com/tangem/domain/tokens/model/TotalFiatBalance.kt diff --git a/features/details/impl/src/main/kotlin/com/tangem/features/details/utils/UserWalletMappers.kt b/features/details/impl/src/main/kotlin/com/tangem/features/details/utils/UserWalletMappers.kt deleted file mode 100644 index f51b383fbd..0000000000 --- a/features/details/impl/src/main/kotlin/com/tangem/features/details/utils/UserWalletMappers.kt +++ /dev/null @@ -1,102 +0,0 @@ -package com.tangem.features.details.utils - -import com.tangem.common.ui.userwallet.state.UserWalletItemUM -import com.tangem.core.ui.extensions.* -import com.tangem.core.ui.utils.BigDecimalFormatter -import com.tangem.domain.appcurrency.model.AppCurrency -import com.tangem.domain.common.util.getCardsCount -import com.tangem.domain.tokens.model.TotalFiatBalance -import com.tangem.domain.wallets.models.UserWallet -import com.tangem.domain.wallets.models.UserWalletId -import com.tangem.features.details.impl.R -import com.tangem.utils.StringsSigns.STARS -import kotlinx.collections.immutable.ImmutableList -import kotlinx.collections.immutable.toImmutableList - -internal fun List.toUiModels( - onClick: (UserWalletId) -> Unit, - appCurrency: AppCurrency? = null, - balances: Map = emptyMap(), - isLoading: Boolean = true, - isBalancesHidden: Boolean = false, -): ImmutableList = this.map { model -> - val balance = balances[model.walletId] - - model.toUiModel( - balance = balance, - appCurrency = appCurrency, - isLoading = isLoading, - isBalanceHidden = isBalancesHidden, - onClick = { onClick(model.walletId) }, - ) -}.toImmutableList() - -private fun UserWallet.toUiModel( - balance: TotalFiatBalance?, - appCurrency: AppCurrency?, - isLoading: Boolean, - isBalanceHidden: Boolean, - onClick: () -> Unit, -): UserWalletItemUM = UserWalletItemUM( - id = walletId, - name = stringReference(name), - information = getInfo( - appCurrency = appCurrency, - balance = balance, - isBalanceHidden = isBalanceHidden, - isLoading = isLoading, - ), - imageUrl = artworkUrl, - isEnabled = !isLocked, - onClick = onClick, -) - -private fun UserWallet.getInfo( - appCurrency: AppCurrency?, - balance: TotalFiatBalance?, - isBalanceHidden: Boolean, - isLoading: Boolean, -): TextReference { - val dividerRef = stringReference(value = " • ") - - val cardCount = getCardsCount() ?: 1 - val cardCountRef = TextReference.PluralRes( - id = R.plurals.card_label_card_count, - count = cardCount, - formatArgs = wrappedList(cardCount), - ) - - return when { - isBalanceHidden -> combinedReference(cardCountRef, dividerRef, stringReference(STARS)) - isLocked -> combinedReference(cardCountRef, dividerRef, resourceReference(R.string.common_locked)) - isLoading -> cardCountRef - else -> getBalanceInfo(balance, appCurrency, cardCountRef, dividerRef) - } -} - -private fun getBalanceInfo( - balance: TotalFiatBalance?, - appCurrency: AppCurrency?, - cardCountRef: TextReference, - dividerRef: TextReference, -): TextReference { - val amount = when (balance) { - is TotalFiatBalance.Loaded -> balance.amount - is TotalFiatBalance.Failed, - is TotalFiatBalance.Loading, - null, - -> null - } - - return if (amount != null && appCurrency != null) { - val formattedAmount = BigDecimalFormatter.formatFiatAmount( - fiatAmount = amount, - fiatCurrencyCode = appCurrency.code, - fiatCurrencySymbol = appCurrency.symbol, - ) - val amountRef = stringReference(formattedAmount) - combinedReference(cardCountRef, dividerRef, amountRef) - } else { - combinedReference(cardCountRef, dividerRef, stringReference(BigDecimalFormatter.EMPTY_BALANCE_SIGN)) - } -} \ No newline at end of file diff --git a/features/details/impl/src/main/kotlin/com/tangem/features/details/utils/UserWalletsFetcher.kt b/features/details/impl/src/main/kotlin/com/tangem/features/details/utils/UserWalletsFetcher.kt index cd5013c005..c9115930ec 100644 --- a/features/details/impl/src/main/kotlin/com/tangem/features/details/utils/UserWalletsFetcher.kt +++ b/features/details/impl/src/main/kotlin/com/tangem/features/details/utils/UserWalletsFetcher.kt @@ -2,6 +2,7 @@ package com.tangem.features.details.utils import arrow.core.Either import com.tangem.common.routing.AppRoute +import com.tangem.common.ui.userwallet.converter.UserWalletItemUMConverter import com.tangem.common.ui.userwallet.state.UserWalletItemUM import com.tangem.core.decompose.di.ComponentScoped import com.tangem.core.decompose.navigation.Router @@ -25,6 +26,7 @@ import com.tangem.domain.wallets.models.UserWalletId import com.tangem.domain.wallets.usecase.GetWalletsUseCase import com.tangem.features.details.impl.R import kotlinx.collections.immutable.ImmutableList +import kotlinx.collections.immutable.toImmutableList import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.flow.* import javax.inject.Inject @@ -41,7 +43,10 @@ internal class UserWalletsFetcher @Inject constructor( @OptIn(ExperimentalCoroutinesApi::class) val userWallets: Flow> = getWalletsUseCase().transformLatest { wallets -> - emit(wallets.toUiModels(onClick = ::navigateToWalletSettings)) + val uiModels = UserWalletItemUMConverter(onClick = ::navigateToWalletSettings).convertList(wallets) + .toImmutableList() + + emit(uiModels) combine( getSelectedAppCurrencyUseCase().distinctUntilChanged(), @@ -75,20 +80,30 @@ internal class UserWalletsFetcher @Inject constructor( ): Lce> = lce { val balances = withError( transform = { Error.UnableToGetBalances }, - block = { maybeBalances.bindOrNull().orEmpty() }, + block = { + maybeBalances.bindOrNull().orEmpty() + .filterKeys { userWalletId -> wallets.any { it.walletId == userWalletId } } + .mapKeys { entry -> wallets.first { it.walletId == entry.key } } + }, ) + val appCurrency = withError( transform = { Error.UnableToGetAppCurrency }, block = { maybeAppCurrency.toLce().bind() }, ) - wallets.toUiModels( - appCurrency = appCurrency, - balances = balances, - onClick = ::navigateToWalletSettings, - isBalancesHidden = balanceHidingSettings.isBalanceHidden, - isLoading = maybeBalances.isLoading(), - ) + balances + .map { (userWallet, balance) -> + UserWalletItemUMConverter( + onClick = ::navigateToWalletSettings, + appCurrency = appCurrency, + balance = balance, + isBalanceHidden = balanceHidingSettings.isBalanceHidden, + isLoading = maybeBalances.isLoading(), + ) + .convert(userWallet) + } + .toImmutableList() } private fun navigateToWalletSettings(userWalletId: UserWalletId) {