Updated on 2026-08-14

This commit is contained in:
Tangem 2025-10-30 13:45:40 +04:00
parent b9262b9a16
commit baad8b9880
4 changed files with 59 additions and 10 deletions

View file

@ -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<UserWalletId, Boolean>()
suspend fun check(tokenList: TokenList) {
val hasNonZeroWallets = tokenList.flattenCurrencies().hasNonZeroWallets()
if (hasNonZeroWallets) setWalletWithFundsFoundUseCase()
}
suspend fun check(userWalletId: UserWalletId, currencies: List<CryptoCurrencyStatus>) {
val hasNonZeroWallets = currencies.hasNonZeroWallets()
val prevStatus = statusByWalletId.get(userWalletId)
if (hasNonZeroWallets && prevStatus != true) {
statusByWalletId[userWalletId] = true
setWalletWithFundsFoundUseCase()
}
}
private fun List<CryptoCurrencyStatus>.hasNonZeroWallets(): Boolean {
return any {
val amount = it.value.amount ?: return@any false

View file

@ -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<WalletSubscriber> = 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

View file

@ -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)
}
}

View file

@ -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()
}
}