From 9b853355fc22e974fa75ac1e6d297528dc8e1bbf Mon Sep 17 00:00:00 2001 From: Tangem Date: Fri, 19 Jan 2024 16:20:25 +0300 Subject: [PATCH] Updated on 2026-08-14 --- .../com/tangem/domain/common/LogConfig.kt | 10 +-- .../utils/TokenListAnalyticsSender.kt | 24 +++---- .../utils/WalletWarningsAnalyticsSender.kt | 56 ++++++++++------- .../loaders/WalletScreenContentLoader.kt | 2 +- .../implementors/MultiWalletContentLoader.kt | 4 +- .../wallet/state2/WalletStateController.kt | 8 +++ ...scriber.kt => BasicTokenListSubscriber.kt} | 38 ++++++------ .../MultiWalletTokenListSubscriber.kt | 30 +++++++++ .../MultiWalletWarningsSubscriber.kt | 4 +- .../SingleWalletNotificationsSubscriber.kt | 4 +- .../SingleWalletWithTokenListSubscriber.kt | 62 +++++-------------- 11 files changed, 127 insertions(+), 115 deletions(-) rename features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/subscribers/{TokenListSubscriber.kt => BasicTokenListSubscriber.kt} (68%) create mode 100644 features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/subscribers/MultiWalletTokenListSubscriber.kt diff --git a/domain/legacy/src/main/java/com/tangem/domain/common/LogConfig.kt b/domain/legacy/src/main/java/com/tangem/domain/common/LogConfig.kt index 2e115068d1..1f0b7b7b51 100644 --- a/domain/legacy/src/main/java/com/tangem/domain/common/LogConfig.kt +++ b/domain/legacy/src/main/java/com/tangem/domain/common/LogConfig.kt @@ -4,7 +4,7 @@ import com.tangem.domain.features.BuildConfig object LogConfig { const val imageLoader: Boolean = false - val storeAction: Boolean = BuildConfig.DEBUG + val storeAction: Boolean = BuildConfig.LOG_ENABLED val network: NetworkLogConfig = NetworkLogConfig val analyticsHandlers: AnalyticsHandlersLogConfig = AnalyticsHandlersLogConfig } @@ -13,13 +13,13 @@ object NetworkLogConfig { const val mercuryoService: Boolean = false const val moonPayService: Boolean = false const val utorgService: Boolean = false - val tangemTechService: Boolean = BuildConfig.DEBUG - val paymentologyApiService: Boolean = BuildConfig.DEBUG - val blockchainSdkNetwork: Boolean = BuildConfig.DEBUG + val tangemTechService: Boolean = BuildConfig.LOG_ENABLED + val paymentologyApiService: Boolean = BuildConfig.LOG_ENABLED + val blockchainSdkNetwork: Boolean = BuildConfig.LOG_ENABLED } object AnalyticsHandlersLogConfig { const val firebase: Boolean = false const val appsFlyer: Boolean = false - const val amplitude: Boolean = false + val amplitude: Boolean = BuildConfig.LOG_ENABLED } \ No newline at end of file diff --git a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/analytics/utils/TokenListAnalyticsSender.kt b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/analytics/utils/TokenListAnalyticsSender.kt index 1ea371fc65..b540cf8dc7 100644 --- a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/analytics/utils/TokenListAnalyticsSender.kt +++ b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/analytics/utils/TokenListAnalyticsSender.kt @@ -1,18 +1,17 @@ package com.tangem.feature.wallet.presentation.wallet.analytics.utils -import arrow.core.Either import arrow.core.getOrElse import com.tangem.core.analytics.api.AnalyticsEventHandler import com.tangem.core.analytics.models.AnalyticsParam import com.tangem.domain.analytics.CheckIsWalletToppedUpUseCase import com.tangem.domain.analytics.model.WalletBalanceState -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 import com.tangem.domain.wallets.models.UserWallet import com.tangem.feature.wallet.presentation.wallet.analytics.WalletScreenAnalyticsEvent.Basic import com.tangem.feature.wallet.presentation.wallet.analytics.WalletScreenAnalyticsEvent.MainScreen +import com.tangem.feature.wallet.presentation.wallet.state2.model.WalletState import dagger.hilt.android.scopes.ViewModelScoped import java.math.BigDecimal import javax.inject.Inject @@ -23,9 +22,8 @@ internal class TokenListAnalyticsSender @Inject constructor( private val checkIsWalletToppedUpUseCase: CheckIsWalletToppedUpUseCase, ) { - suspend fun send(userWallet: UserWallet, maybeTokenList: Either) { - val tokenList = maybeTokenList.getOrElse { return } - + suspend fun send(displayedUiState: WalletState?, userWallet: UserWallet, tokenList: TokenList) { + if (displayedUiState == null || displayedUiState.pullToRefreshConfig.isRefreshing) return if (tokenList.totalFiatBalance is TokenList.FiatBalance.Loading) return val currenciesStatuses = getCurrenciesStatuses(tokenList) @@ -33,7 +31,6 @@ internal class TokenListAnalyticsSender @Inject constructor( sendBalanceLoadedEventIfNeeded(tokenList.totalFiatBalance, currenciesStatuses) sendToppedUpEventIfNeeded(userWallet, tokenList.totalFiatBalance, currenciesStatuses) sendUnreachableNetworksEventIfNeeded(currenciesStatuses) - sendMissedAddressesEventIfNeeded(currenciesStatuses) } private fun getCurrenciesStatuses(tokenList: TokenList): List = when (tokenList) { @@ -119,20 +116,13 @@ internal class TokenListAnalyticsSender @Inject constructor( } private fun sendUnreachableNetworksEventIfNeeded(currenciesStatuses: List) { - val hasUnreachableCurrencies = currenciesStatuses.any { it.value is CryptoCurrencyStatus.Unreachable } + val hasUnreachableCurrencies = currenciesStatuses.any { + it.value is CryptoCurrencyStatus.Unreachable || + it.value is CryptoCurrencyStatus.UnreachableWithoutAddresses + } if (hasUnreachableCurrencies) { analyticsEventHandler.send(MainScreen.NetworksUnreachable) } } - - private fun sendMissedAddressesEventIfNeeded(currenciesStatuses: List) { - val hasCurrenciesWithMissedDerivation = currenciesStatuses.any { - it.value is CryptoCurrencyStatus.MissedDerivation - } - - if (hasCurrenciesWithMissedDerivation) { - analyticsEventHandler.send(MainScreen.MissingAddresses) - } - } } \ No newline at end of file diff --git a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/analytics/utils/WalletWarningsAnalyticsSender.kt b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/analytics/utils/WalletWarningsAnalyticsSender.kt index bb4427955e..d0b54d53e8 100644 --- a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/analytics/utils/WalletWarningsAnalyticsSender.kt +++ b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/analytics/utils/WalletWarningsAnalyticsSender.kt @@ -1,10 +1,11 @@ package com.tangem.feature.wallet.presentation.wallet.analytics.utils import com.tangem.core.analytics.api.AnalyticsEventHandler +import com.tangem.core.analytics.models.AnalyticsEvent import com.tangem.feature.wallet.presentation.wallet.analytics.WalletScreenAnalyticsEvent.MainScreen import com.tangem.feature.wallet.presentation.wallet.state.components.WalletNotification +import com.tangem.feature.wallet.presentation.wallet.state2.model.WalletState import dagger.hilt.android.scopes.ViewModelScoped -import kotlinx.collections.immutable.ImmutableList import javax.inject.Inject @ViewModelScoped @@ -12,29 +13,40 @@ internal class WalletWarningsAnalyticsSender @Inject constructor( private val analyticsEventHandler: AnalyticsEventHandler, ) { - fun send(warnings: ImmutableList) { - val analyticsEvents = warnings.mapNotNull { warning -> - when (warning) { - is WalletNotification.Critical.DevCard -> MainScreen.DevelopmentCard - is WalletNotification.Informational.DemoCard -> MainScreen.DemoCard - is WalletNotification.RateApp -> MainScreen.HowDoYouLikeTangem - is WalletNotification.Warning.NumberOfSignedHashesIncorrect -> MainScreen.CardSignedTransactions - is WalletNotification.Warning.TestNetCard -> MainScreen.TestnetCard - is WalletNotification.UnlockWallets -> MainScreen.WalletUnlock - is WalletNotification.Critical.FailedCardValidation -> MainScreen.ProductSampleCard - is WalletNotification.Warning.MissingBackup -> MainScreen.BackupYourWallet - is WalletNotification.Informational.MissingAddresses, - is WalletNotification.Informational.NoAccount, - is WalletNotification.Warning.LowSignatures, - is WalletNotification.Warning.NetworksUnreachable, - is WalletNotification.Warning.SomeNetworksUnreachable, - is WalletNotification.SwapPromo, - -> null - } - } + fun send(displayedUiState: WalletState?, newWarnings: List) { + if (newWarnings.isEmpty()) return + if (displayedUiState == null || displayedUiState.pullToRefreshConfig.isRefreshing) return - analyticsEvents.forEach { event -> + val eventsFromNewWarnings = getEvents(newWarnings) + val eventsFromDisplayedWarnings = getEvents(displayedUiState.warnings) + val eventsToSend = eventsFromNewWarnings.filter { it !in eventsFromDisplayedWarnings } + + eventsToSend.forEach { event -> analyticsEventHandler.send(event) } } + + private fun getEvents(warnings: List): Set { + return warnings.mapNotNullTo(mutableSetOf(), ::getEvent) + } + + private fun getEvent(warning: WalletNotification): AnalyticsEvent? { + return when (warning) { + is WalletNotification.Critical.DevCard -> MainScreen.DevelopmentCard + is WalletNotification.Critical.FailedCardValidation -> MainScreen.ProductSampleCard + is WalletNotification.Warning.MissingBackup -> MainScreen.BackupYourWallet + is WalletNotification.Warning.NumberOfSignedHashesIncorrect -> MainScreen.CardSignedTransactions + is WalletNotification.Warning.TestNetCard -> MainScreen.TestnetCard + is WalletNotification.Informational.DemoCard -> MainScreen.DemoCard + is WalletNotification.Informational.MissingAddresses -> MainScreen.MissingAddresses + is WalletNotification.RateApp -> MainScreen.HowDoYouLikeTangem + is WalletNotification.UnlockWallets -> MainScreen.WalletUnlock + is WalletNotification.Informational.NoAccount, + is WalletNotification.Warning.LowSignatures, + is WalletNotification.Warning.SomeNetworksUnreachable, + is WalletNotification.Warning.NetworksUnreachable, + is WalletNotification.SwapPromo, + -> null + } + } } \ No newline at end of file diff --git a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/loaders/WalletScreenContentLoader.kt b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/loaders/WalletScreenContentLoader.kt index 27ad8b60f5..4742aea08d 100644 --- a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/loaders/WalletScreenContentLoader.kt +++ b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/loaders/WalletScreenContentLoader.kt @@ -47,7 +47,7 @@ internal class WalletScreenContentLoader @Inject constructor( } else { if (isRefresh) { storage.remove(id) - loadInternal(userWallet, clickIntents, coroutineScope, true) + loadInternal(userWallet, clickIntents, coroutineScope, isRefresh = true) } else { Timber.d("$id content loading has already started") } 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 585073bc03..4fba2e23e0 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 @@ -9,8 +9,8 @@ import com.tangem.feature.wallet.presentation.wallet.analytics.utils.WalletWarni import com.tangem.feature.wallet.presentation.wallet.domain.GetMultiWalletWarningsFactory import com.tangem.feature.wallet.presentation.wallet.domain.WalletWithFundsChecker import com.tangem.feature.wallet.presentation.wallet.state2.WalletStateController +import com.tangem.feature.wallet.presentation.wallet.subscribers.MultiWalletTokenListSubscriber import com.tangem.feature.wallet.presentation.wallet.subscribers.MultiWalletWarningsSubscriber -import com.tangem.feature.wallet.presentation.wallet.subscribers.TokenListSubscriber import com.tangem.feature.wallet.presentation.wallet.subscribers.WalletConnectNetworksSubscriber import com.tangem.feature.wallet.presentation.wallet.subscribers.WalletSubscriber import com.tangem.feature.wallet.presentation.wallet.viewmodels.intents.WalletClickIntentsV2 @@ -31,7 +31,7 @@ internal class MultiWalletContentLoader( override fun create(): List { return listOf( - TokenListSubscriber( + MultiWalletTokenListSubscriber( userWallet = userWallet, stateHolder = stateHolder, clickIntents = clickIntents, diff --git a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/state2/WalletStateController.kt b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/state2/WalletStateController.kt index 8002fef454..d2e9c06cb8 100644 --- a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/state2/WalletStateController.kt +++ b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/state2/WalletStateController.kt @@ -42,6 +42,14 @@ internal class WalletStateController @Inject constructor() { mutableUiState.update { getInitialState() } } + fun getWalletIfSelected(walletId: UserWalletId): WalletState? { + val selectedWalletId = getSelectedWalletId() + + return value.wallets.firstOrNull { + it.walletCardState.id == walletId && it.walletCardState.id == selectedWalletId + } + } + fun getSelectedWallet(): WalletState { return with(value) { wallets[selectedWalletIndex] } } diff --git a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/subscribers/TokenListSubscriber.kt b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/subscribers/BasicTokenListSubscriber.kt similarity index 68% rename from features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/subscribers/TokenListSubscriber.kt rename to features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/subscribers/BasicTokenListSubscriber.kt index 38ec1fa7df..379657ed45 100644 --- a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/subscribers/TokenListSubscriber.kt +++ b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/subscribers/BasicTokenListSubscriber.kt @@ -4,7 +4,6 @@ import arrow.core.Either import arrow.core.getOrElse import com.tangem.domain.appcurrency.GetSelectedAppCurrencyUseCase import com.tangem.domain.appcurrency.model.AppCurrency -import com.tangem.domain.tokens.GetTokenListUseCase import com.tangem.domain.tokens.error.TokenListError import com.tangem.domain.tokens.model.TokenList import com.tangem.domain.wallets.models.UserWallet @@ -15,37 +14,40 @@ import com.tangem.feature.wallet.presentation.wallet.state2.transformers.SetToke import com.tangem.feature.wallet.presentation.wallet.state2.transformers.SetTokenListTransformer import com.tangem.feature.wallet.presentation.wallet.viewmodels.intents.WalletClickIntentsV2 import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.flow.* +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.combine +import kotlinx.coroutines.flow.distinctUntilChanged +import kotlinx.coroutines.flow.onEach -typealias MaybeTokenListFlow = Flow> +internal typealias MaybeTokenListFlow = Flow> @Suppress("LongParameterList") -internal class TokenListSubscriber( +internal abstract class BasicTokenListSubscriber( private val userWallet: UserWallet, private val stateHolder: WalletStateController, private val clickIntents: WalletClickIntentsV2, private val tokenListAnalyticsSender: TokenListAnalyticsSender, private val walletWithFundsChecker: WalletWithFundsChecker, - private val getTokenListUseCase: GetTokenListUseCase, private val getSelectedAppCurrencyUseCase: GetSelectedAppCurrencyUseCase, ) : WalletSubscriber() { - override fun create(coroutineScope: CoroutineScope): Flow, AppCurrency>> { + protected abstract fun tokenListFlow(): MaybeTokenListFlow + + override fun create(coroutineScope: CoroutineScope): Flow<*> { return combine( - flow = getTokenListUseCase(userWalletId = userWallet.walletId) - .conflate() + flow = tokenListFlow() + .onEach { + val displayedState = stateHolder.getWalletIfSelected(userWallet.walletId) + + tokenListAnalyticsSender.send(displayedState, userWallet, it.getOrElse { return@onEach }) + } .distinctUntilChanged(), - flow2 = getSelectedAppCurrencyUseCase() - .conflate() - .distinctUntilChanged() - .map { maybeAppCurrency -> maybeAppCurrency.getOrElse { AppCurrency.Default } }, - transform = { tokenList, appCurrency -> tokenList to appCurrency }, - ) - .onEach { (maybeTokenList, appCurrency) -> - updateContent(maybeTokenList, appCurrency) - tokenListAnalyticsSender.send(userWallet, maybeTokenList) + flow2 = getSelectedAppCurrencyUseCase().distinctUntilChanged(), + transform = { maybeTokenList, maybeAppCurrency -> + updateContent(maybeTokenList, maybeAppCurrency.getOrElse { AppCurrency.Default }) walletWithFundsChecker.check(maybeTokenList) - } + }, + ) } private fun updateContent(maybeTokenList: Either, appCurrency: AppCurrency) { 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 new file mode 100644 index 0000000000..46de409a7d --- /dev/null +++ b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/subscribers/MultiWalletTokenListSubscriber.kt @@ -0,0 +1,30 @@ +package com.tangem.feature.wallet.presentation.wallet.subscribers + +import com.tangem.domain.appcurrency.GetSelectedAppCurrencyUseCase +import com.tangem.domain.tokens.GetTokenListUseCase +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 +import com.tangem.feature.wallet.presentation.wallet.state2.WalletStateController +import com.tangem.feature.wallet.presentation.wallet.viewmodels.intents.WalletClickIntentsV2 + +@Suppress("LongParameterList") +internal class MultiWalletTokenListSubscriber( + private val userWallet: UserWallet, + private val getTokenListUseCase: GetTokenListUseCase, + stateHolder: WalletStateController, + clickIntents: WalletClickIntentsV2, + tokenListAnalyticsSender: TokenListAnalyticsSender, + walletWithFundsChecker: WalletWithFundsChecker, + getSelectedAppCurrencyUseCase: GetSelectedAppCurrencyUseCase, +) : BasicTokenListSubscriber( + userWallet = userWallet, + stateHolder = stateHolder, + clickIntents = clickIntents, + tokenListAnalyticsSender = tokenListAnalyticsSender, + walletWithFundsChecker = walletWithFundsChecker, + getSelectedAppCurrencyUseCase = getSelectedAppCurrencyUseCase, +) { + + override fun tokenListFlow(): MaybeTokenListFlow = getTokenListUseCase(userWallet.walletId) +} \ No newline at end of file diff --git a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/subscribers/MultiWalletWarningsSubscriber.kt b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/subscribers/MultiWalletWarningsSubscriber.kt index c27e175318..09a4cc324d 100644 --- a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/subscribers/MultiWalletWarningsSubscriber.kt +++ b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/subscribers/MultiWalletWarningsSubscriber.kt @@ -27,8 +27,10 @@ internal class MultiWalletWarningsSubscriber( .conflate() .distinctUntilChanged() .onEach { warnings -> + val displayedState = stateHolder.getWalletIfSelected(userWalletId) + stateHolder.update(SetWarningsTransformer(userWalletId, warnings)) - walletWarningsAnalyticsSender.send(warnings) + walletWarningsAnalyticsSender.send(displayedState, warnings) } } } \ No newline at end of file diff --git a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/subscribers/SingleWalletNotificationsSubscriber.kt b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/subscribers/SingleWalletNotificationsSubscriber.kt index be7ff474b7..e4b6e2f5e9 100644 --- a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/subscribers/SingleWalletNotificationsSubscriber.kt +++ b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/subscribers/SingleWalletNotificationsSubscriber.kt @@ -30,8 +30,10 @@ internal class SingleWalletNotificationsSubscriber( .conflate() .distinctUntilChanged() .onEach { warnings -> + val displayedState = stateHolder.getWalletIfSelected(userWalletId) + stateHolder.update(SetWarningsTransformer(userWalletId, warnings)) - walletWarningsAnalyticsSender.send(warnings) + walletWarningsAnalyticsSender.send(displayedState, warnings) } } } \ No newline at end of file diff --git a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/subscribers/SingleWalletWithTokenListSubscriber.kt b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/subscribers/SingleWalletWithTokenListSubscriber.kt index 8496a45e8c..3fe184f524 100644 --- a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/subscribers/SingleWalletWithTokenListSubscriber.kt +++ b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/subscribers/SingleWalletWithTokenListSubscriber.kt @@ -1,64 +1,30 @@ package com.tangem.feature.wallet.presentation.wallet.subscribers -import arrow.core.Either -import arrow.core.getOrElse import com.tangem.domain.appcurrency.GetSelectedAppCurrencyUseCase -import com.tangem.domain.appcurrency.model.AppCurrency import com.tangem.domain.tokens.GetCardTokensListUseCase -import com.tangem.domain.tokens.error.TokenListError -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 import com.tangem.feature.wallet.presentation.wallet.state2.WalletStateController -import com.tangem.feature.wallet.presentation.wallet.state2.transformers.SetTokenListErrorTransformer -import com.tangem.feature.wallet.presentation.wallet.state2.transformers.SetTokenListTransformer import com.tangem.feature.wallet.presentation.wallet.viewmodels.intents.WalletClickIntentsV2 -import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.flow.* @Suppress("LongParameterList") internal class SingleWalletWithTokenListSubscriber( private val userWallet: UserWallet, - private val stateHolder: WalletStateController, - private val clickIntents: WalletClickIntentsV2, - private val tokenListAnalyticsSender: TokenListAnalyticsSender, - private val walletWithFundsChecker: WalletWithFundsChecker, private val getCardTokensListUseCase: GetCardTokensListUseCase, - private val getSelectedAppCurrencyUseCase: GetSelectedAppCurrencyUseCase, -) : WalletSubscriber() { + stateHolder: WalletStateController, + clickIntents: WalletClickIntentsV2, + tokenListAnalyticsSender: TokenListAnalyticsSender, + walletWithFundsChecker: WalletWithFundsChecker, + getSelectedAppCurrencyUseCase: GetSelectedAppCurrencyUseCase, +) : BasicTokenListSubscriber( + userWallet = userWallet, + stateHolder = stateHolder, + clickIntents = clickIntents, + tokenListAnalyticsSender = tokenListAnalyticsSender, + walletWithFundsChecker = walletWithFundsChecker, + getSelectedAppCurrencyUseCase = getSelectedAppCurrencyUseCase, +) { - override fun create(coroutineScope: CoroutineScope): Flow, AppCurrency>> { - return combine( - flow = getCardTokensListUseCase(userWalletId = userWallet.walletId) - .conflate() - .distinctUntilChanged(), - flow2 = getSelectedAppCurrencyUseCase() - .conflate() - .distinctUntilChanged() - .map { maybeAppCurrency -> maybeAppCurrency.getOrElse { AppCurrency.Default } }, - transform = { tokenList, appCurrency -> tokenList to appCurrency }, - ) - .onEach { (maybeTokenList, appCurrency) -> - updateContent(maybeTokenList, appCurrency) - tokenListAnalyticsSender.send(userWallet, maybeTokenList) - walletWithFundsChecker.check(maybeTokenList) - } - } - - private fun updateContent(maybeTokenList: Either, appCurrency: AppCurrency) { - stateHolder.update( - maybeTokenList.fold( - ifLeft = { SetTokenListErrorTransformer(userWalletId = userWallet.walletId, error = it) }, - ifRight = { - SetTokenListTransformer( - tokenList = it, - userWallet = userWallet, - appCurrency = appCurrency, - clickIntents = clickIntents, - ) - }, - ), - ) - } + override fun tokenListFlow(): MaybeTokenListFlow = getCardTokensListUseCase(userWallet.walletId) } \ No newline at end of file