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 6561164dd0..df33f4e1f8 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,21 +1,37 @@ package com.tangem.feature.wallet.presentation.wallet.domain import com.tangem.common.extensions.isZero +import com.tangem.core.decompose.di.ModelScoped import com.tangem.domain.models.currency.CryptoCurrencyStatus import com.tangem.domain.models.tokenlist.TokenList +import com.tangem.domain.models.wallet.UserWalletId import com.tangem.domain.settings.SetWalletWithFundsFoundUseCase +import java.util.concurrent.ConcurrentHashMap import javax.inject.Inject +@ModelScoped internal class WalletWithFundsChecker @Inject constructor( private val setWalletWithFundsFoundUseCase: SetWalletWithFundsFoundUseCase, ) { + private val statusByWalletId = ConcurrentHashMap() + suspend fun check(tokenList: TokenList) { val hasNonZeroWallets = tokenList.flattenCurrencies().hasNonZeroWallets() if (hasNonZeroWallets) setWalletWithFundsFoundUseCase() } + suspend fun check(userWalletId: UserWalletId, currencies: List) { + val hasNonZeroWallets = currencies.hasNonZeroWallets() + val prevStatus = statusByWalletId.get(userWalletId) + + if (hasNonZeroWallets && prevStatus != true) { + statusByWalletId[userWalletId] = true + setWalletWithFundsFoundUseCase() + } + } + private fun List.hasNonZeroWallets(): Boolean { return any { val amount = it.value.amount ?: return@any false diff --git a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/loaders/implementors/SingleWalletContentLoaderV2.kt b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/loaders/implementors/SingleWalletContentLoaderV2.kt index ad0f01c797..c75db95d7f 100644 --- a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/loaders/implementors/SingleWalletContentLoaderV2.kt +++ b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/loaders/implementors/SingleWalletContentLoaderV2.kt @@ -6,7 +6,6 @@ import com.tangem.domain.appcurrency.GetSelectedAppCurrencyUseCase import com.tangem.domain.models.wallet.UserWallet import com.tangem.domain.onramp.GetOnrampTransactionsUseCase import com.tangem.domain.onramp.OnrampRemoveTransactionUseCase -import com.tangem.domain.settings.SetWalletWithFundsFoundUseCase import com.tangem.domain.txhistory.usecase.GetTxHistoryItemsCountUseCase import com.tangem.domain.txhistory.usecase.GetTxHistoryItemsUseCase import com.tangem.domain.wallets.usecase.ShouldSaveUserWalletsUseCase @@ -14,8 +13,10 @@ import com.tangem.feature.wallet.child.wallet.model.intents.WalletClickIntents import com.tangem.feature.wallet.presentation.account.AccountDependencies import com.tangem.feature.wallet.presentation.wallet.analytics.utils.WalletWarningsAnalyticsSender import com.tangem.feature.wallet.presentation.wallet.domain.GetSingleWalletWarningsFactory +import com.tangem.feature.wallet.presentation.wallet.domain.WalletWithFundsChecker import com.tangem.feature.wallet.presentation.wallet.state.WalletStateController import com.tangem.feature.wallet.presentation.wallet.subscribers.* +import com.tangem.utils.coroutines.CoroutineDispatcherProvider import dagger.assisted.Assisted import dagger.assisted.AssistedFactory import dagger.assisted.AssistedInject @@ -28,7 +29,6 @@ internal class SingleWalletContentLoaderV2 @AssistedInject constructor( private val stateHolder: WalletStateController, private val getCryptoCurrencyActionsUseCaseV2: GetCryptoCurrencyActionsUseCaseV2, private val getSingleWalletWarningsFactory: GetSingleWalletWarningsFactory, - private val setWalletWithFundsFoundUseCase: SetWalletWithFundsFoundUseCase, private val txHistoryItemsCountUseCase: GetTxHistoryItemsCountUseCase, private val txHistoryItemsUseCase: GetTxHistoryItemsUseCase, private val getSelectedAppCurrencyUseCase: GetSelectedAppCurrencyUseCase, @@ -38,6 +38,8 @@ internal class SingleWalletContentLoaderV2 @AssistedInject constructor( private val analyticsEventHandler: AnalyticsEventHandler, private val walletWarningsAnalyticsSender: WalletWarningsAnalyticsSender, private val accountDependencies: AccountDependencies, + private val walletWithFundsChecker: WalletWithFundsChecker, + private val dispatchers: CoroutineDispatcherProvider, ) : WalletContentLoader(id = userWallet.walletId) { override fun create(): List = listOf( @@ -47,7 +49,6 @@ internal class SingleWalletContentLoaderV2 @AssistedInject constructor( getSelectedAppCurrencyUseCase = getSelectedAppCurrencyUseCase, stateController = stateHolder, analyticsEventHandler = analyticsEventHandler, - setWalletWithFundsFoundUseCase = setWalletWithFundsFoundUseCase, ), SingleWalletButtonsSubscriberV2( userWallet = userWallet, @@ -87,6 +88,12 @@ internal class SingleWalletContentLoaderV2 @AssistedInject constructor( stateController = stateHolder, clickIntents = clickIntents, ), + CheckWalletWithFundsSubscriber( + userWallet = userWallet, + singleAccountStatusListSupplier = accountDependencies.singleAccountStatusListSupplier, + walletWithFundsChecker = walletWithFundsChecker, + dispatchers = dispatchers, + ), ) @AssistedFactory diff --git a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/subscribers/CheckWalletWithFundsSubscriber.kt b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/subscribers/CheckWalletWithFundsSubscriber.kt new file mode 100644 index 0000000000..05f27338de --- /dev/null +++ b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/subscribers/CheckWalletWithFundsSubscriber.kt @@ -0,0 +1,33 @@ +package com.tangem.feature.wallet.presentation.wallet.subscribers + +import com.tangem.domain.account.status.supplier.SingleAccountStatusListSupplier +import com.tangem.domain.models.wallet.UserWallet +import com.tangem.feature.wallet.presentation.wallet.domain.WalletWithFundsChecker +import com.tangem.utils.coroutines.CoroutineDispatcherProvider +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.flow.* + +/** + * Subscriber that checks if the wallet has funds and notifies the [WalletWithFundsChecker]. + * + * @property userWallet The user wallet. + * @property singleAccountStatusListSupplier Supplier for account status list. + * @property walletWithFundsChecker The checker to notify when funds are detected. + * +[REDACTED_AUTHOR] + */ +internal class CheckWalletWithFundsSubscriber( + override val userWallet: UserWallet, + override val singleAccountStatusListSupplier: SingleAccountStatusListSupplier, + private val walletWithFundsChecker: WalletWithFundsChecker, + private val dispatchers: CoroutineDispatcherProvider, +) : BasicWalletSubscriber() { + + override fun create(coroutineScope: CoroutineScope): Flow<*> { + return getAccountStatusListFlow() + .mapNotNull { it.flattenCurrencies() } + .distinctUntilChanged() + .onEach { walletWithFundsChecker.check(userWalletId = userWallet.walletId, currencies = it) } + .flowOn(dispatchers.default) + } +} \ No newline at end of file diff --git a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/subscribers/PrimaryCurrencySubscriberV2.kt b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/subscribers/PrimaryCurrencySubscriberV2.kt index e6b137a056..3aa5c39bb4 100644 --- a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/subscribers/PrimaryCurrencySubscriberV2.kt +++ b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/subscribers/PrimaryCurrencySubscriberV2.kt @@ -8,7 +8,6 @@ import com.tangem.domain.appcurrency.GetSelectedAppCurrencyUseCase import com.tangem.domain.appcurrency.model.AppCurrency import com.tangem.domain.models.currency.CryptoCurrencyStatus import com.tangem.domain.models.wallet.UserWallet -import com.tangem.domain.settings.SetWalletWithFundsFoundUseCase import com.tangem.feature.wallet.presentation.wallet.analytics.WalletScreenAnalyticsEvent import com.tangem.feature.wallet.presentation.wallet.state.WalletStateController import com.tangem.feature.wallet.presentation.wallet.state.transformers.SetPrimaryCurrencyTransformer @@ -24,7 +23,6 @@ internal class PrimaryCurrencySubscriberV2( private val getSelectedAppCurrencyUseCase: GetSelectedAppCurrencyUseCase, private val stateController: WalletStateController, private val analyticsEventHandler: AnalyticsEventHandler, - private val setWalletWithFundsFoundUseCase: SetWalletWithFundsFoundUseCase, ) : BasicSingleWalletSubscriber() { override fun create(coroutineScope: CoroutineScope): Flow<*> { @@ -36,7 +34,6 @@ internal class PrimaryCurrencySubscriberV2( .onEach { (status, appCurrency) -> updateContent(status, appCurrency) sendAnalyticsEvent(status) - checkWalletWithFunds(status) } } @@ -84,8 +81,4 @@ internal class PrimaryCurrencySubscriberV2( else -> AnalyticsParam.CardBalanceState.Full } } - - private suspend fun checkWalletWithFunds(status: CryptoCurrencyStatus) { - if (status.value.amount?.isZero() == false) setWalletWithFundsFoundUseCase() - } } \ No newline at end of file