Updated on 2026-08-14
This commit is contained in:
parent
d7cec0764e
commit
2a175de4e1
1 changed files with 51 additions and 38 deletions
|
|
@ -14,6 +14,7 @@ import com.tangem.feature.swap.domain.api.SwapRepository
|
||||||
import com.tangem.feature.swap.domain.models.domain.LeastTokenInfo
|
import com.tangem.feature.swap.domain.models.domain.LeastTokenInfo
|
||||||
import com.tangem.lib.crypto.BlockchainUtils
|
import com.tangem.lib.crypto.BlockchainUtils
|
||||||
import com.tangem.utils.coroutines.CoroutineDispatcherProvider
|
import com.tangem.utils.coroutines.CoroutineDispatcherProvider
|
||||||
|
import com.tangem.utils.coroutines.runCatching
|
||||||
import com.tangem.utils.isNullOrZero
|
import com.tangem.utils.isNullOrZero
|
||||||
import kotlinx.coroutines.flow.*
|
import kotlinx.coroutines.flow.*
|
||||||
import java.math.BigDecimal
|
import java.math.BigDecimal
|
||||||
|
|
@ -85,49 +86,61 @@ class GetCurrencyWarningsUseCase(
|
||||||
currencyStatus: CryptoCurrencyStatus,
|
currencyStatus: CryptoCurrencyStatus,
|
||||||
): Flow<CryptoCurrencyWarning?> {
|
): Flow<CryptoCurrencyWarning?> {
|
||||||
val currency = currencyStatus.currency
|
val currency = currencyStatus.currency
|
||||||
val cryptoCurrencyStatuses = operations.getCurrenciesStatusesSync().getOrNull() ?: emptyList()
|
val cryptoStatuses = operations.getCurrenciesStatusesSync()
|
||||||
val promoBanner = promoRepository.getOkxPromoBanner()
|
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(
|
return combine(
|
||||||
showSwapPromoTokenUseCase().conflate(),
|
showSwapPromoTokenUseCase()
|
||||||
flowOf(marketCryptoCurrencyRepository.isExchangeable(userWalletId, currency)).conflate(),
|
.conflate()
|
||||||
|
.distinctUntilChanged(),
|
||||||
|
flowOf(marketCryptoCurrencyRepository.isExchangeable(userWalletId, currency))
|
||||||
|
.conflate()
|
||||||
|
.distinctUntilChanged(),
|
||||||
) { shouldShowSwapPromo, isExchangeable ->
|
) { shouldShowSwapPromo, isExchangeable ->
|
||||||
promoBanner ?: return@combine null
|
promoBanner ?: return@combine null
|
||||||
val showPromoBannerActive = promoBanner.isActive && shouldShowSwapPromo
|
val showPromoBanner = promoBanner.isActive && shouldShowSwapPromo
|
||||||
if (showPromoBannerActive && isExchangeable && currencyStatus.value !is CryptoCurrencyStatus.Unreachable) {
|
if (showPromoBanner && isExchangeable && currencyStatus.value !is CryptoCurrencyStatus.Unreachable) {
|
||||||
val filteredCurrencies = cryptoCurrencyStatuses.filterNot {
|
cryptoStatuses.fold(
|
||||||
it.currency.id == currency.id
|
ifLeft = { null },
|
||||||
}
|
ifRight = { cryptoCurrencyStatuses ->
|
||||||
val currencyPairs = pairs.filter {
|
val pairs = runCatching(dispatchers.io) {
|
||||||
it.from.network == currency.network.backendId ||
|
swapRepository.getPairsOnly(
|
||||||
it.to.network == currency.network.backendId
|
LeastTokenInfo(
|
||||||
}
|
contractAddress = (currency as? CryptoCurrency.Token)?.contractAddress ?: "0",
|
||||||
val showPromoForPair = currencyPairs.any { pair ->
|
network = currency.network.backendId,
|
||||||
val availablePair = if (currencyStatus.value.amount.isNullOrZero()) {
|
),
|
||||||
filteredCurrencies.filterNot { it.value.amount.isNullOrZero() }
|
cryptoCurrencyStatuses.map { it.currency },
|
||||||
} else {
|
)
|
||||||
filteredCurrencies
|
}.getOrNull()?.pairs ?: emptyList()
|
||||||
}
|
|
||||||
availablePair
|
val filteredCurrencies = cryptoCurrencyStatuses.filterNot {
|
||||||
.any {
|
it.currency.id == currency.id
|
||||||
it.currency.network.backendId == pair.to.network ||
|
|
||||||
it.currency.network.backendId == pair.from.network
|
|
||||||
}
|
}
|
||||||
}
|
val currencyPairs = pairs.filter {
|
||||||
if (showPromoForPair) {
|
it.from.network == currency.network.backendId ||
|
||||||
CryptoCurrencyWarning.SwapPromo(
|
it.to.network == currency.network.backendId
|
||||||
startDateTime = promoBanner.bannerState.timeline.start,
|
}
|
||||||
endDateTime = promoBanner.bannerState.timeline.end,
|
val showPromo = currencyPairs.any { pair ->
|
||||||
)
|
val availablePair = if (currencyStatus.value.amount.isNullOrZero()) {
|
||||||
} else {
|
filteredCurrencies.filterNot { it.value.amount.isNullOrZero() }
|
||||||
null
|
} 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 {
|
} else {
|
||||||
null
|
null
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue