diff --git a/app/src/main/java/com/tangem/tap/di/domain/ManageTokensDomainModule.kt b/app/src/main/java/com/tangem/tap/di/domain/ManageTokensDomainModule.kt index 89bd3b4ca9..c4e44fe950 100644 --- a/app/src/main/java/com/tangem/tap/di/domain/ManageTokensDomainModule.kt +++ b/app/src/main/java/com/tangem/tap/di/domain/ManageTokensDomainModule.kt @@ -1,6 +1,5 @@ package com.tangem.tap.di.domain -import com.tangem.domain.wallets.derivations.DerivationsRepository import com.tangem.domain.managetokens.* import com.tangem.domain.managetokens.repository.CustomTokensRepository import com.tangem.domain.managetokens.repository.ManageTokensRepository @@ -10,11 +9,14 @@ import com.tangem.domain.staking.StakingIdFactory import com.tangem.domain.staking.multi.MultiYieldBalanceFetcher import com.tangem.domain.tokens.repository.CurrenciesRepository import com.tangem.domain.walletmanager.WalletManagersFacade +import com.tangem.domain.wallets.derivations.DerivationsRepository import com.tangem.utils.coroutines.CoroutineDispatcherProvider import dagger.Module import dagger.Provides import dagger.hilt.InstallIn import dagger.hilt.components.SingletonComponent +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.SupervisorJob import javax.inject.Singleton @Module @@ -74,6 +76,7 @@ internal object ManageTokensDomainModule { multiQuoteStatusFetcher: MultiQuoteStatusFetcher, multiYieldBalanceFetcher: MultiYieldBalanceFetcher, stakingIdFactory: StakingIdFactory, + dispatchers: CoroutineDispatcherProvider, ): SaveManagedTokensUseCase { return SaveManagedTokensUseCase( customTokensRepository = customTokensRepository, @@ -84,6 +87,7 @@ internal object ManageTokensDomainModule { multiQuoteStatusFetcher = multiQuoteStatusFetcher, multiYieldBalanceFetcher = multiYieldBalanceFetcher, stakingIdFactory = stakingIdFactory, + parallelUpdatingScope = CoroutineScope(SupervisorJob() + dispatchers.default), ) } diff --git a/app/src/main/java/com/tangem/tap/di/domain/MarketsDomainModule.kt b/app/src/main/java/com/tangem/tap/di/domain/MarketsDomainModule.kt index 60c8926df6..9b33e83a66 100644 --- a/app/src/main/java/com/tangem/tap/di/domain/MarketsDomainModule.kt +++ b/app/src/main/java/com/tangem/tap/di/domain/MarketsDomainModule.kt @@ -15,10 +15,13 @@ import com.tangem.domain.tokens.repository.CurrenciesRepository import com.tangem.domain.wallets.derivations.DerivationsRepository import com.tangem.domain.wallets.legacy.UserWalletsListManager import com.tangem.features.hotwallet.HotWalletFeatureToggles +import com.tangem.utils.coroutines.CoroutineDispatcherProvider import dagger.Module import dagger.Provides import dagger.hilt.InstallIn import dagger.hilt.components.SingletonComponent +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.SupervisorJob import javax.inject.Singleton @Module @@ -67,6 +70,7 @@ object MarketsDomainModule { multiQuoteStatusFetcher: MultiQuoteStatusFetcher, multiYieldBalanceFetcher: MultiYieldBalanceFetcher, stakingIdFactory: StakingIdFactory, + dispatchers: CoroutineDispatcherProvider, ): SaveMarketTokensUseCase { return SaveMarketTokensUseCase( derivationsRepository = derivationsRepository, @@ -76,6 +80,7 @@ object MarketsDomainModule { multiQuoteStatusFetcher = multiQuoteStatusFetcher, multiYieldBalanceFetcher = multiYieldBalanceFetcher, stakingIdFactory = stakingIdFactory, + parallelUpdatingScope = CoroutineScope(SupervisorJob() + dispatchers.default), ) } diff --git a/domain/manage-tokens/src/main/kotlin/com/tangem/domain/managetokens/SaveManagedTokensUseCase.kt b/domain/manage-tokens/src/main/kotlin/com/tangem/domain/managetokens/SaveManagedTokensUseCase.kt index 6c1b21b89b..e6ac3a721d 100644 --- a/domain/manage-tokens/src/main/kotlin/com/tangem/domain/managetokens/SaveManagedTokensUseCase.kt +++ b/domain/manage-tokens/src/main/kotlin/com/tangem/domain/managetokens/SaveManagedTokensUseCase.kt @@ -14,6 +14,10 @@ import com.tangem.domain.staking.multi.MultiYieldBalanceFetcher import com.tangem.domain.tokens.repository.CurrenciesRepository import com.tangem.domain.walletmanager.WalletManagersFacade import com.tangem.domain.wallets.derivations.DerivationsRepository +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.NonCancellable +import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext @Suppress("LongParameterList") class SaveManagedTokensUseCase( @@ -25,6 +29,7 @@ class SaveManagedTokensUseCase( private val multiQuoteStatusFetcher: MultiQuoteStatusFetcher, private val multiYieldBalanceFetcher: MultiYieldBalanceFetcher, private val stakingIdFactory: StakingIdFactory, + private val parallelUpdatingScope: CoroutineScope, ) { suspend operator fun invoke( @@ -53,11 +58,23 @@ class SaveManagedTokensUseCase( currencies = addingCurrencies, ) - refreshUpdatedNetworks(userWalletId = userWalletId, addedCurrencies = savedCurrencies) - - refreshUpdatedYieldBalances(userWalletId = userWalletId, addedCurrencies = savedCurrencies) - - refreshUpdatedQuotes(addedCurrencies = savedCurrencies) + parallelUpdatingScope.launch { + withContext(NonCancellable) { + launch { + refreshUpdatedNetworks( + userWalletId = userWalletId, + addedCurrencies = savedCurrencies, + ) + } + launch { + refreshUpdatedYieldBalances( + userWalletId = userWalletId, + addedCurrencies = savedCurrencies, + ) + } + launch { refreshUpdatedQuotes(addedCurrencies = savedCurrencies) } + } + } } } diff --git a/domain/markets/src/main/java/com/tangem/domain/markets/SaveMarketTokensUseCase.kt b/domain/markets/src/main/java/com/tangem/domain/markets/SaveMarketTokensUseCase.kt index 3417a876fd..c5ef6b9cd7 100644 --- a/domain/markets/src/main/java/com/tangem/domain/markets/SaveMarketTokensUseCase.kt +++ b/domain/markets/src/main/java/com/tangem/domain/markets/SaveMarketTokensUseCase.kt @@ -1,7 +1,6 @@ package com.tangem.domain.markets import arrow.core.Either -import com.tangem.domain.wallets.derivations.DerivationsRepository import com.tangem.domain.markets.repositories.MarketsTokenRepository import com.tangem.domain.models.currency.CryptoCurrency import com.tangem.domain.models.network.Network @@ -11,6 +10,11 @@ import com.tangem.domain.quotes.multi.MultiQuoteStatusFetcher import com.tangem.domain.staking.StakingIdFactory import com.tangem.domain.staking.multi.MultiYieldBalanceFetcher import com.tangem.domain.tokens.repository.CurrenciesRepository +import com.tangem.domain.wallets.derivations.DerivationsRepository +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.NonCancellable +import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext /** * Use case for saving tokens from Markets @@ -30,6 +34,7 @@ class SaveMarketTokensUseCase( private val multiQuoteStatusFetcher: MultiQuoteStatusFetcher, private val multiYieldBalanceFetcher: MultiYieldBalanceFetcher, private val stakingIdFactory: StakingIdFactory, + private val parallelUpdatingScope: CoroutineScope, ) { suspend operator fun invoke( @@ -69,11 +74,13 @@ class SaveMarketTokensUseCase( currencies = addedCurrencies, ) - refreshUpdatedNetworks(userWalletId, savedCurrencies) - - refreshUpdatedYieldBalances(userWalletId, savedCurrencies) - - refreshUpdatedQuotes(savedCurrencies) + parallelUpdatingScope.launch { + withContext(NonCancellable) { + launch { refreshUpdatedNetworks(userWalletId, savedCurrencies) } + launch { refreshUpdatedYieldBalances(userWalletId, savedCurrencies) } + launch { refreshUpdatedQuotes(savedCurrencies) } + } + } } }