From 8da3529141ca77c2735201fd05141d59b4cf3f88 Mon Sep 17 00:00:00 2001 From: Tangem Date: Tue, 13 Feb 2024 18:56:17 +0300 Subject: [PATCH] Updated on 2026-08-14 --- .../wallet/domain/WalletWithFundsChecker.kt | 6 +-- .../implementors/MultiWalletContentLoader.kt | 3 ++ .../MultiWalletContentLoaderFactory.kt | 3 ++ .../subscribers/BasicTokenListSubscriber.kt | 44 ++++++++++++------- .../MultiWalletTokenListSubscriber.kt | 28 ++++++++++++ 5 files changed, 64 insertions(+), 20 deletions(-) diff --git a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/domain/WalletWithFundsChecker.kt b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/domain/WalletWithFundsChecker.kt index 3426a96ae1..bec1af9947 100644 --- a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/domain/WalletWithFundsChecker.kt +++ b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/domain/WalletWithFundsChecker.kt @@ -1,9 +1,7 @@ package com.tangem.feature.wallet.presentation.wallet.domain -import arrow.core.Either import com.tangem.common.extensions.isZero import com.tangem.domain.settings.SetWalletWithFundsFoundUseCase -import com.tangem.domain.tokens.error.TokenListError import com.tangem.domain.tokens.model.CryptoCurrencyStatus import com.tangem.domain.tokens.model.NetworkGroup import com.tangem.domain.tokens.model.TokenList @@ -13,9 +11,7 @@ internal class WalletWithFundsChecker @Inject constructor( private val setWalletWithFundsFoundUseCase: SetWalletWithFundsFoundUseCase, ) { - suspend fun check(maybeTokenList: Either) { - val tokenList = (maybeTokenList as? Either.Right)?.value ?: return - + suspend fun check(tokenList: TokenList) { val hasNonZeroWallets = when (tokenList) { is TokenList.GroupedByNetwork -> { tokenList.groups diff --git a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/loaders/implementors/MultiWalletContentLoader.kt b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/loaders/implementors/MultiWalletContentLoader.kt index 91b6a97137..b12083df95 100644 --- a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/loaders/implementors/MultiWalletContentLoader.kt +++ b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/loaders/implementors/MultiWalletContentLoader.kt @@ -2,6 +2,7 @@ package com.tangem.feature.wallet.presentation.wallet.loaders.implementors import com.tangem.domain.appcurrency.GetSelectedAppCurrencyUseCase import com.tangem.domain.redux.ReduxStateHolder +import com.tangem.domain.tokens.ApplyTokenListSortingUseCase import com.tangem.domain.tokens.GetTokenListUseCase import com.tangem.domain.wallets.models.UserWallet import com.tangem.feature.wallet.presentation.wallet.analytics.utils.TokenListAnalyticsSender @@ -25,6 +26,7 @@ internal class MultiWalletContentLoader( private val walletWithFundsChecker: WalletWithFundsChecker, private val getTokenListUseCase: GetTokenListUseCase, private val getSelectedAppCurrencyUseCase: GetSelectedAppCurrencyUseCase, + private val applyTokenListSortingUseCase: ApplyTokenListSortingUseCase, private val getMultiWalletWarningsFactory: GetMultiWalletWarningsFactory, private val reduxStateHolder: ReduxStateHolder, ) : WalletContentLoader(id = userWallet.walletId) { @@ -39,6 +41,7 @@ internal class MultiWalletContentLoader( walletWithFundsChecker = walletWithFundsChecker, getTokenListUseCase = getTokenListUseCase, getSelectedAppCurrencyUseCase = getSelectedAppCurrencyUseCase, + applyTokenListSortingUseCase = applyTokenListSortingUseCase, ), MultiWalletWarningsSubscriber( userWalletId = userWallet.walletId, diff --git a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/loaders/implementors/MultiWalletContentLoaderFactory.kt b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/loaders/implementors/MultiWalletContentLoaderFactory.kt index a434e73c58..794574309a 100644 --- a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/loaders/implementors/MultiWalletContentLoaderFactory.kt +++ b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/loaders/implementors/MultiWalletContentLoaderFactory.kt @@ -2,6 +2,7 @@ package com.tangem.feature.wallet.presentation.wallet.loaders.implementors import com.tangem.domain.appcurrency.GetSelectedAppCurrencyUseCase import com.tangem.domain.redux.ReduxStateHolder +import com.tangem.domain.tokens.ApplyTokenListSortingUseCase import com.tangem.domain.tokens.GetTokenListUseCase import com.tangem.domain.wallets.models.UserWallet import com.tangem.feature.wallet.presentation.wallet.analytics.utils.TokenListAnalyticsSender @@ -22,6 +23,7 @@ internal class MultiWalletContentLoaderFactory @Inject constructor( private val getMultiWalletWarningsFactory: GetMultiWalletWarningsFactory, private val getTokenListUseCase: GetTokenListUseCase, private val getSelectedAppCurrencyUseCase: GetSelectedAppCurrencyUseCase, + private val applyTokenListSortingUseCase: ApplyTokenListSortingUseCase, private val reduxStateHolder: ReduxStateHolder, private val walletWarningsAnalyticsSender: WalletWarningsAnalyticsSender, ) { @@ -38,6 +40,7 @@ internal class MultiWalletContentLoaderFactory @Inject constructor( getMultiWalletWarningsFactory = getMultiWalletWarningsFactory, reduxStateHolder = reduxStateHolder, walletWarningsAnalyticsSender = walletWarningsAnalyticsSender, + applyTokenListSortingUseCase = applyTokenListSortingUseCase, ) } } \ No newline at end of file diff --git a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/subscribers/BasicTokenListSubscriber.kt b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/subscribers/BasicTokenListSubscriber.kt index a1f88b718d..d9cc56d227 100644 --- a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/subscribers/BasicTokenListSubscriber.kt +++ b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/subscribers/BasicTokenListSubscriber.kt @@ -18,6 +18,7 @@ import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.combine import kotlinx.coroutines.flow.distinctUntilChanged import kotlinx.coroutines.flow.onEach +import timber.log.Timber internal typealias MaybeTokenListFlow = Flow> @@ -33,35 +34,48 @@ internal abstract class BasicTokenListSubscriber( protected abstract fun tokenListFlow(): MaybeTokenListFlow + protected open suspend fun onTokenListReceived(tokenList: TokenList) { /* no-op */ + } + override fun create(coroutineScope: CoroutineScope): Flow<*> { return combine( flow = tokenListFlow() - .onEach { + .onEach { maybeTokenList -> val displayedState = stateHolder.getWalletStateIfSelected(userWallet.walletId) - tokenListAnalyticsSender.send(displayedState, userWallet, it.getOrElse { return@onEach }) + tokenListAnalyticsSender.send( + displayedUiState = displayedState, + userWallet = userWallet, + tokenList = maybeTokenList.getOrElse { return@onEach }, + ) } .distinctUntilChanged(), flow2 = getSelectedAppCurrencyUseCase().distinctUntilChanged(), transform = { maybeTokenList, maybeAppCurrency -> - updateContent(maybeTokenList, maybeAppCurrency.getOrElse { AppCurrency.Default }) - walletWithFundsChecker.check(maybeTokenList) + val tokenList = maybeTokenList.getOrElse { e -> + Timber.e("Failed to load token list: $e") + SetTokenListErrorTransformer(userWallet.walletId, e) + return@combine + } + val appCurrency = maybeAppCurrency.getOrElse { e -> + Timber.e("Failed to load app currency: $e") + AppCurrency.Default + } + + updateContent(tokenList, appCurrency) + walletWithFundsChecker.check(tokenList) + onTokenListReceived(tokenList) }, ) } - private fun updateContent(maybeTokenList: Either, appCurrency: AppCurrency) { + private fun updateContent(tokenList: TokenList, appCurrency: AppCurrency) { stateHolder.update( - maybeTokenList.fold( - ifLeft = { SetTokenListErrorTransformer(userWalletId = userWallet.walletId, error = it) }, - ifRight = { - SetTokenListTransformer( - tokenList = it, - userWallet = userWallet, - appCurrency = appCurrency, - clickIntents = clickIntents, - ) - }, + SetTokenListTransformer( + tokenList = tokenList, + userWallet = userWallet, + appCurrency = appCurrency, + clickIntents = clickIntents, ), ) } diff --git a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/subscribers/MultiWalletTokenListSubscriber.kt b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/subscribers/MultiWalletTokenListSubscriber.kt index cb5fdbc166..8eb0af9ab2 100644 --- a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/subscribers/MultiWalletTokenListSubscriber.kt +++ b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/subscribers/MultiWalletTokenListSubscriber.kt @@ -1,7 +1,9 @@ package com.tangem.feature.wallet.presentation.wallet.subscribers import com.tangem.domain.appcurrency.GetSelectedAppCurrencyUseCase +import com.tangem.domain.tokens.ApplyTokenListSortingUseCase import com.tangem.domain.tokens.GetTokenListUseCase +import com.tangem.domain.tokens.model.TokenList import com.tangem.domain.wallets.models.UserWallet import com.tangem.feature.wallet.presentation.wallet.analytics.utils.TokenListAnalyticsSender import com.tangem.feature.wallet.presentation.wallet.domain.WalletWithFundsChecker @@ -12,6 +14,7 @@ import com.tangem.feature.wallet.presentation.wallet.viewmodels.intents.WalletCl internal class MultiWalletTokenListSubscriber( private val userWallet: UserWallet, private val getTokenListUseCase: GetTokenListUseCase, + private val applyTokenListSortingUseCase: ApplyTokenListSortingUseCase, stateHolder: WalletStateController, clickIntents: WalletClickIntents, tokenListAnalyticsSender: TokenListAnalyticsSender, @@ -27,4 +30,29 @@ internal class MultiWalletTokenListSubscriber( ) { override fun tokenListFlow(): MaybeTokenListFlow = getTokenListUseCase(userWallet.walletId) + + override suspend fun onTokenListReceived(tokenList: TokenList) { + updateSortingIfNeeded(tokenList) + } + + private suspend fun updateSortingIfNeeded(tokenList: TokenList) { + if (tokenList.totalFiatBalance is TokenList.FiatBalance.Loading || + tokenList.sortedBy == TokenList.SortType.NONE + ) { + return + } + + applyTokenListSortingUseCase( + userWalletId = userWallet.walletId, + sortedTokensIds = when (tokenList) { + is TokenList.GroupedByNetwork -> tokenList.groups.flatMap { group -> + group.currencies.map { it.currency.id } + } + is TokenList.Ungrouped -> tokenList.currencies.map { it.currency.id } + is TokenList.Empty -> return + }, + isGroupedByNetwork = tokenList is TokenList.GroupedByNetwork, + isSortedByBalance = true, + ) + } } \ No newline at end of file