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 5cfa978a2a..d4d08e2e70 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 @@ -48,7 +48,8 @@ internal class TokenListAnalyticsSender @Inject constructor( if (screenLifecycleProvider.isBackgroundState.value) return if (displayedUiState == null || displayedUiState.pullToRefreshConfig.isRefreshing) return - if (totalFiatBalance is TotalFiatBalance.Loading) { + val isFlickering = totalFiatBalance is TotalFiatBalance.Loaded && totalFiatBalance.source == StatusSource.CACHE + if (totalFiatBalance is TotalFiatBalance.Loading || isFlickering) { startLoadingTraceIfNeeded(userWallet.walletId, flattenCurrencies) return } @@ -57,12 +58,10 @@ internal class TokenListAnalyticsSender @Inject constructor( stopLoadingTraceIfNeeded(userWallet.walletId, totalFiatBalance) } - val currenciesStatuses = flattenCurrencies - - sendBalanceLoadedEventIfNeeded(totalFiatBalance, currenciesStatuses) - sendToppedUpEventIfNeeded(userWallet, totalFiatBalance, currenciesStatuses) - sendUnreachableNetworksEventIfNeeded(currenciesStatuses) - sendTokenBalancesIfNeeded(currenciesStatuses) + sendBalanceLoadedEventIfNeeded(totalFiatBalance, flattenCurrencies) + sendToppedUpEventIfNeeded(userWallet, totalFiatBalance, flattenCurrencies) + sendUnreachableNetworksEventIfNeeded(flattenCurrencies) + sendTokenBalancesIfNeeded(flattenCurrencies) } private suspend fun startLoadingTraceIfNeeded( diff --git a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/loaders/implementors/MultiWalletContentLoaderV2.kt b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/loaders/implementors/MultiWalletContentLoaderV2.kt index fea4a02329..2c52604748 100644 --- a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/loaders/implementors/MultiWalletContentLoaderV2.kt +++ b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/loaders/implementors/MultiWalletContentLoaderV2.kt @@ -17,6 +17,7 @@ import dagger.assisted.AssistedInject internal class MultiWalletContentLoaderV2 @AssistedInject constructor( @Assisted private val userWallet: UserWallet, private val accountListSubscriberFactory: AccountListSubscriber.Factory, + private val tokenListAnalyticsSubscriberFactory: TokenListAnalyticsSubscriber.Factory, private val walletNFTListSubscriberV2Factory: WalletNFTListSubscriberV2.Factory, private val stateController: WalletStateController, private val clickIntents: WalletClickIntents, @@ -31,6 +32,7 @@ internal class MultiWalletContentLoaderV2 @AssistedInject constructor( override fun create(): List = listOfNotNull( accountListSubscriberFactory.create(userWallet = userWallet), + tokenListAnalyticsSubscriberFactory.create(userWallet = userWallet), walletNFTListSubscriberV2Factory.create(userWallet = userWallet), checkWalletWithFundsSubscriberFactory.create(userWallet = userWallet), MultiWalletWarningsSubscriber( diff --git a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/subscribers/TokenListAnalyticsSubscriber.kt b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/subscribers/TokenListAnalyticsSubscriber.kt new file mode 100644 index 0000000000..7fb410838e --- /dev/null +++ b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/subscribers/TokenListAnalyticsSubscriber.kt @@ -0,0 +1,49 @@ +package com.tangem.feature.wallet.presentation.wallet.subscribers + +import com.tangem.domain.account.models.AccountStatusList +import com.tangem.domain.account.status.supplier.SingleAccountStatusListSupplier +import com.tangem.domain.models.wallet.UserWallet +import com.tangem.feature.wallet.presentation.wallet.analytics.utils.TokenListAnalyticsSender +import com.tangem.feature.wallet.presentation.wallet.state.WalletStateController +import dagger.assisted.Assisted +import dagger.assisted.AssistedFactory +import dagger.assisted.AssistedInject +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.distinctUntilChanged +import kotlinx.coroutines.flow.onEach + +/** + * Subscriber that monitors account list changes and sends token list analytics + * when the total fiat balance changes. + * +[REDACTED_AUTHOR] + */ +internal class TokenListAnalyticsSubscriber @AssistedInject constructor( + @Assisted override val userWallet: UserWallet, + override val singleAccountStatusListSupplier: SingleAccountStatusListSupplier, + private val stateHolder: WalletStateController, + private val tokenListAnalyticsSender: TokenListAnalyticsSender, +) : BasicWalletSubscriber() { + + override fun create(coroutineScope: CoroutineScope): Flow<*> = getAccountStatusListFlow() + .distinctUntilChanged { old, new -> old.totalFiatBalance == new.totalFiatBalance } + .onEach(::sendTokenListAnalytics) + + private suspend fun sendTokenListAnalytics(accountStatusList: AccountStatusList) { + val displayedState = stateHolder.getWalletStateIfSelected(userWallet.walletId) + + val flattenCurrencies = accountStatusList.flattenCurrencies() + tokenListAnalyticsSender.send( + displayedUiState = displayedState, + userWallet = userWallet, + flattenCurrencies = flattenCurrencies, + totalFiatBalance = accountStatusList.totalFiatBalance, + ) + } + + @AssistedFactory + interface Factory { + fun create(userWallet: UserWallet): TokenListAnalyticsSubscriber + } +} \ No newline at end of file