diff --git a/features/onboarding-v2/impl/src/main/kotlin/com/tangem/features/onboarding/v2/note/impl/child/topup/model/OnboardingNoteTopUpModel.kt b/features/onboarding-v2/impl/src/main/kotlin/com/tangem/features/onboarding/v2/note/impl/child/topup/model/OnboardingNoteTopUpModel.kt index 29d8601ba0..cbaed64847 100644 --- a/features/onboarding-v2/impl/src/main/kotlin/com/tangem/features/onboarding/v2/note/impl/child/topup/model/OnboardingNoteTopUpModel.kt +++ b/features/onboarding-v2/impl/src/main/kotlin/com/tangem/features/onboarding/v2/note/impl/child/topup/model/OnboardingNoteTopUpModel.kt @@ -19,9 +19,11 @@ import com.tangem.domain.models.scan.ScanResponse import com.tangem.domain.onramp.GetLegacyTopUpUrlUseCase import com.tangem.domain.tokens.FetchCurrencyStatusUseCase import com.tangem.domain.tokens.GetSingleCryptoCurrencyStatusUseCase +import com.tangem.domain.tokens.TokensFeatureToggles import com.tangem.domain.tokens.model.CryptoCurrencyStatus import com.tangem.domain.tokens.model.ScenarioUnavailabilityReason import com.tangem.domain.tokens.model.analytics.TokenReceiveAnalyticsEvent +import com.tangem.domain.tokens.wallet.WalletBalanceFetcher import com.tangem.domain.wallets.builder.ColdUserWalletBuilder import com.tangem.domain.wallets.models.UserWallet import com.tangem.domain.wallets.usecase.SaveWalletUseCase @@ -32,6 +34,7 @@ import com.tangem.utils.coroutines.CoroutineDispatcherProvider import com.tangem.utils.extensions.isPositive import kotlinx.coroutines.flow.* import kotlinx.coroutines.launch +import timber.log.Timber import javax.inject.Inject @Suppress("LongParameterList") @@ -49,6 +52,8 @@ internal class OnboardingNoteTopUpModel @Inject constructor( private val rampStateManager: RampStateManager, private val cardRepository: CardRepository, private val saveWalletUseCase: SaveWalletUseCase, + private val walletBalanceFetcher: WalletBalanceFetcher, + private val tokensFeatureToggles: TokensFeatureToggles, ) : Model() { private val params = paramsContainer.require() @@ -83,10 +88,12 @@ internal class OnboardingNoteTopUpModel @Inject constructor( showBalanceLoadingProgress(true) createUserWalletIfNull() val userWalletId = requireNotNull(userWallet?.walletId) - fetchCurrencyStatusUseCase( - userWalletId = userWalletId, - refresh = true, - ) + if (tokensFeatureToggles.isWalletBalanceFetcherEnabled) { + walletBalanceFetcher(params = WalletBalanceFetcher.Params(userWalletId = userWalletId)) + .onLeft(Timber::e) + } else { + fetchCurrencyStatusUseCase(userWalletId = userWalletId, refresh = true) + } showBalanceLoadingProgress(false) } } diff --git a/features/onboarding-v2/impl/src/main/kotlin/com/tangem/features/onboarding/v2/twin/impl/model/OnboardingTwinModel.kt b/features/onboarding-v2/impl/src/main/kotlin/com/tangem/features/onboarding/v2/twin/impl/model/OnboardingTwinModel.kt index f4b803c505..ca88682ad0 100644 --- a/features/onboarding-v2/impl/src/main/kotlin/com/tangem/features/onboarding/v2/twin/impl/model/OnboardingTwinModel.kt +++ b/features/onboarding-v2/impl/src/main/kotlin/com/tangem/features/onboarding/v2/twin/impl/model/OnboardingTwinModel.kt @@ -36,8 +36,10 @@ import com.tangem.domain.onboarding.SaveTwinsOnboardingShownUseCase import com.tangem.domain.onramp.GetLegacyTopUpUrlUseCase import com.tangem.domain.tokens.FetchCurrencyStatusUseCase import com.tangem.domain.tokens.GetSingleCryptoCurrencyStatusUseCase +import com.tangem.domain.tokens.TokensFeatureToggles import com.tangem.domain.tokens.model.CryptoCurrencyStatus import com.tangem.domain.tokens.model.analytics.TokenReceiveAnalyticsEvent +import com.tangem.domain.tokens.wallet.WalletBalanceFetcher import com.tangem.domain.wallets.builder.ColdUserWalletBuilder import com.tangem.domain.wallets.builder.UserWalletIdBuilder import com.tangem.domain.wallets.legacy.UserWalletsListManager @@ -85,6 +87,8 @@ internal class OnboardingTwinModel @Inject constructor( private val sendFeedbackEmailUseCase: SendFeedbackEmailUseCase, private val clipboardManager: ClipboardManager, private val shareManager: ShareManager, + private val tokensFeatureToggles: TokensFeatureToggles, + private val walletBalanceFetcher: WalletBalanceFetcher, ) : Model() { private val params = paramsContainer.require() @@ -329,13 +333,15 @@ internal class OnboardingTwinModel @Inject constructor( cardRepository.finishCardActivation(params.scanResponse.card.cardId) - fetchCurrencyStatusUseCase.invoke( - userWalletId = userWallet.walletId, - refresh = true, - ).onLeft { - Timber.e("Unable to fetch currency status: $it") - setLoading(false) + if (tokensFeatureToggles.isWalletBalanceFetcherEnabled) { + walletBalanceFetcher(params = WalletBalanceFetcher.Params(userWalletId = userWallet.walletId)) + } else { + fetchCurrencyStatusUseCase.invoke(userWalletId = userWallet.walletId, refresh = true) } + .onLeft { + Timber.e("Unable to fetch currency status: $it") + setLoading(false) + } val cryptoCurrencyStatus = getSingleCryptoCurrencyStatusUseCase.invokeSingleWallet(userWallet.walletId) .firstOrNull()?.getOrNull() @@ -431,10 +437,12 @@ internal class OnboardingTwinModel @Inject constructor( it.copy(isLoading = true) } modelScope.launch { - fetchCurrencyStatusUseCase( - userWalletId = userWallet.walletId, - refresh = true, - ) + if (tokensFeatureToggles.isWalletBalanceFetcherEnabled) { + walletBalanceFetcher(params = WalletBalanceFetcher.Params(userWalletId = userWallet.walletId)) + .onLeft(Timber::e) + } else { + fetchCurrencyStatusUseCase(userWalletId = userWallet.walletId, refresh = true) + } } } diff --git a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/child/wallet/model/WalletModel.kt b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/child/wallet/model/WalletModel.kt index fe3e58ff9e..287c272322 100644 --- a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/child/wallet/model/WalletModel.kt +++ b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/child/wallet/model/WalletModel.kt @@ -20,6 +20,8 @@ import com.tangem.domain.nft.ObserveAndClearNFTCacheIfNeedUseCase import com.tangem.domain.settings.* import com.tangem.domain.tokens.FetchCurrencyStatusUseCase import com.tangem.domain.tokens.RefreshMultiCurrencyWalletQuotesUseCase +import com.tangem.domain.tokens.TokensFeatureToggles +import com.tangem.domain.tokens.wallet.WalletBalanceFetcher import com.tangem.domain.wallets.models.UserWallet import com.tangem.domain.wallets.models.UserWalletId import com.tangem.domain.wallets.models.isMultiCurrency @@ -83,6 +85,8 @@ internal class WalletModel @Inject constructor( private val analyticsEventsHandler: AnalyticsEventHandler, private val deepLinksRegistry: DeepLinksRegistry, private val fetchCurrencyStatusUseCase: FetchCurrencyStatusUseCase, + private val walletBalanceFetcher: WalletBalanceFetcher, + private val tokensFeatureToggles: TokensFeatureToggles, private val appRouter: AppRouter, private val routingFeatureToggle: RoutingFeatureToggle, private val observeAndClearNFTCacheIfNeedUseCase: ObserveAndClearNFTCacheIfNeedUseCase, @@ -518,10 +522,15 @@ internal class WalletModel @Inject constructor( } private fun fetchIfSingleWallet(userWallet: UserWallet) { - if (userWallet is UserWallet.Cold && userWallet.scanResponse.cardTypesResolver.isSingleWallet()) { - modelScope.launch { - fetchCurrencyStatusUseCase(userWalletId = userWallet.walletId) - .onLeft { Timber.e(it.toString()) } + modelScope.launch { + if (tokensFeatureToggles.isWalletBalanceFetcherEnabled) { + walletBalanceFetcher(params = WalletBalanceFetcher.Params(userWalletId = userWallet.walletId)) + .onLeft(Timber::e) + } else { + if (userWallet is UserWallet.Cold && userWallet.scanResponse.cardTypesResolver.isSingleWallet()) { + fetchCurrencyStatusUseCase(userWalletId = userWallet.walletId) + .onLeft { Timber.e(it.toString()) } + } } } } diff --git a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/child/wallet/model/intents/WalletClickIntents.kt b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/child/wallet/model/intents/WalletClickIntents.kt index 1537149e1a..759a4ac87b 100644 --- a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/child/wallet/model/intents/WalletClickIntents.kt +++ b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/child/wallet/model/intents/WalletClickIntents.kt @@ -11,6 +11,9 @@ import com.tangem.domain.tokens.FetchCardTokenListUseCase import com.tangem.domain.tokens.FetchCurrencyStatusUseCase import com.tangem.domain.tokens.FetchTokenListUseCase import com.tangem.domain.tokens.FetchTokenListUseCase.RefreshMode +import com.tangem.domain.tokens.TokensFeatureToggles +import com.tangem.domain.tokens.error.TokenListError +import com.tangem.domain.tokens.wallet.WalletBalanceFetcher import com.tangem.domain.wallets.models.UserWallet import com.tangem.domain.wallets.usecase.GetSelectedWalletSyncUseCase import com.tangem.domain.wallets.usecase.SelectWalletUseCase @@ -23,11 +26,11 @@ import com.tangem.feature.wallet.presentation.wallet.state.model.WalletState import com.tangem.feature.wallet.presentation.wallet.state.transformers.SetRefreshStateTransformer import com.tangem.feature.wallet.presentation.wallet.state.transformers.SetTokenListErrorTransformer import com.tangem.features.onramp.OnrampFeatureToggles -import com.tangem.utils.coroutines.CoroutineDispatcherProvider import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.async import kotlinx.coroutines.awaitAll import kotlinx.coroutines.launch +import timber.log.Timber import javax.inject.Inject @Suppress("LongParameterList") @@ -44,12 +47,13 @@ internal class WalletClickIntents @Inject constructor( private val getSelectedWalletSyncUseCase: GetSelectedWalletSyncUseCase, private val selectWalletUseCase: SelectWalletUseCase, private val fetchTokenListUseCase: FetchTokenListUseCase, + private val walletBalanceFetcher: WalletBalanceFetcher, + private val tokensFeatureToggles: TokensFeatureToggles, private val fetchCardTokenListUseCase: FetchCardTokenListUseCase, private val fetchCurrencyStatusUseCase: FetchCurrencyStatusUseCase, private val getSelectedAppCurrencyUseCase: GetSelectedAppCurrencyUseCase, private val neverToShowWalletsScrollPreview: NeverToShowWalletsScrollPreview, private val rampStateManager: RampStateManager, - private val dispatchers: CoroutineDispatcherProvider, private val onrampFeatureToggles: OnrampFeatureToggles, private val fetchHotCryptoUseCase: FetchHotCryptoUseCase, private val onrampStatusFactory: OnrampStatusFactory, @@ -127,13 +131,21 @@ internal class WalletClickIntents @Inject constructor( ) modelScope.launch { - val maybeFetchResult = if ( - userWallet is UserWallet.Cold && - userWallet.scanResponse.cardTypesResolver.isSingleWalletWithToken() - ) { - fetchCardTokenListUseCase(userWalletId = userWallet.walletId, refresh = true) + val maybeFetchResult = if (tokensFeatureToggles.isWalletBalanceFetcherEnabled) { + walletBalanceFetcher.invoke(params = WalletBalanceFetcher.Params(userWalletId = userWallet.walletId)) + .mapLeft { + Timber.e(it) + TokenListError.DataError(it) + } } else { - fetchTokenListUseCase(userWalletId = userWallet.walletId, mode = RefreshMode.FULL) + val isSingleWalletWithToken = userWallet is UserWallet.Cold && + userWallet.cardTypesResolver.isSingleWalletWithToken() + + if (isSingleWalletWithToken) { + fetchCardTokenListUseCase(userWalletId = userWallet.walletId, refresh = true) + } else { + fetchTokenListUseCase(userWalletId = userWallet.walletId, mode = RefreshMode.FULL) + } } buildList { @@ -172,8 +184,13 @@ internal class WalletClickIntents @Inject constructor( SetRefreshStateTransformer(userWalletId = userWallet.walletId, isRefreshing = showRefreshState), ) - modelScope.launch(dispatchers.main) { - fetchCurrencyStatusUseCase(userWallet.walletId, refresh = true) + modelScope.launch { + if (tokensFeatureToggles.isWalletBalanceFetcherEnabled) { + walletBalanceFetcher(params = WalletBalanceFetcher.Params(userWalletId = userWallet.walletId)) + .onLeft(Timber::e) + } else { + fetchCurrencyStatusUseCase(userWallet.walletId, refresh = true) + } onrampStatusFactory.updateOnrmapTransactionStatuses(userWallet) walletScreenContentLoader.load( userWallet = userWallet,