From 2a175de4e14ba79daf72896fcf4bfb40dbe7ff0e Mon Sep 17 00:00:00 2001 From: Tangem Date: Tue, 13 Aug 2024 12:12:43 +0300 Subject: [PATCH] Updated on 2026-08-14 --- .../tokens/GetCurrencyWarningsUseCase.kt | 89 +++++++++++-------- 1 file changed, 51 insertions(+), 38 deletions(-) diff --git a/domain/tokens/src/main/kotlin/com/tangem/domain/tokens/GetCurrencyWarningsUseCase.kt b/domain/tokens/src/main/kotlin/com/tangem/domain/tokens/GetCurrencyWarningsUseCase.kt index c4e96be175..03c0debc06 100644 --- a/domain/tokens/src/main/kotlin/com/tangem/domain/tokens/GetCurrencyWarningsUseCase.kt +++ b/domain/tokens/src/main/kotlin/com/tangem/domain/tokens/GetCurrencyWarningsUseCase.kt @@ -14,6 +14,7 @@ import com.tangem.feature.swap.domain.api.SwapRepository import com.tangem.feature.swap.domain.models.domain.LeastTokenInfo import com.tangem.lib.crypto.BlockchainUtils import com.tangem.utils.coroutines.CoroutineDispatcherProvider +import com.tangem.utils.coroutines.runCatching import com.tangem.utils.isNullOrZero import kotlinx.coroutines.flow.* import java.math.BigDecimal @@ -85,49 +86,61 @@ class GetCurrencyWarningsUseCase( currencyStatus: CryptoCurrencyStatus, ): Flow { val currency = currencyStatus.currency - val cryptoCurrencyStatuses = operations.getCurrenciesStatusesSync().getOrNull() ?: emptyList() + val cryptoStatuses = operations.getCurrenciesStatusesSync() val promoBanner = promoRepository.getOkxPromoBanner() - val pairs = swapRepository.getPairsOnly( - LeastTokenInfo( - contractAddress = (currency as? CryptoCurrency.Token)?.contractAddress ?: "0", - network = currency.network.backendId, - ), - cryptoCurrencyStatuses.map { it.currency }, - ).pairs return combine( - showSwapPromoTokenUseCase().conflate(), - flowOf(marketCryptoCurrencyRepository.isExchangeable(userWalletId, currency)).conflate(), + showSwapPromoTokenUseCase() + .conflate() + .distinctUntilChanged(), + flowOf(marketCryptoCurrencyRepository.isExchangeable(userWalletId, currency)) + .conflate() + .distinctUntilChanged(), ) { shouldShowSwapPromo, isExchangeable -> promoBanner ?: return@combine null - val showPromoBannerActive = promoBanner.isActive && shouldShowSwapPromo - if (showPromoBannerActive && isExchangeable && currencyStatus.value !is CryptoCurrencyStatus.Unreachable) { - val filteredCurrencies = cryptoCurrencyStatuses.filterNot { - it.currency.id == currency.id - } - val currencyPairs = pairs.filter { - it.from.network == currency.network.backendId || - it.to.network == currency.network.backendId - } - val showPromoForPair = currencyPairs.any { pair -> - val availablePair = if (currencyStatus.value.amount.isNullOrZero()) { - filteredCurrencies.filterNot { it.value.amount.isNullOrZero() } - } else { - filteredCurrencies - } - availablePair - .any { - it.currency.network.backendId == pair.to.network || - it.currency.network.backendId == pair.from.network + val showPromoBanner = promoBanner.isActive && shouldShowSwapPromo + if (showPromoBanner && isExchangeable && currencyStatus.value !is CryptoCurrencyStatus.Unreachable) { + cryptoStatuses.fold( + ifLeft = { null }, + ifRight = { cryptoCurrencyStatuses -> + val pairs = runCatching(dispatchers.io) { + swapRepository.getPairsOnly( + LeastTokenInfo( + contractAddress = (currency as? CryptoCurrency.Token)?.contractAddress ?: "0", + network = currency.network.backendId, + ), + cryptoCurrencyStatuses.map { it.currency }, + ) + }.getOrNull()?.pairs ?: emptyList() + + val filteredCurrencies = cryptoCurrencyStatuses.filterNot { + it.currency.id == currency.id } - } - if (showPromoForPair) { - CryptoCurrencyWarning.SwapPromo( - startDateTime = promoBanner.bannerState.timeline.start, - endDateTime = promoBanner.bannerState.timeline.end, - ) - } else { - null - } + val currencyPairs = pairs.filter { + it.from.network == currency.network.backendId || + it.to.network == currency.network.backendId + } + val showPromo = currencyPairs.any { pair -> + val availablePair = if (currencyStatus.value.amount.isNullOrZero()) { + filteredCurrencies.filterNot { it.value.amount.isNullOrZero() } + } else { + filteredCurrencies + } + availablePair + .any { + it.currency.network.backendId == pair.to.network || + it.currency.network.backendId == pair.from.network + } + } + if (showPromo) { + CryptoCurrencyWarning.SwapPromo( + startDateTime = promoBanner.bannerState.timeline.start, + endDateTime = promoBanner.bannerState.timeline.end, + ) + } else { + null + } + }, + ) } else { null }