Updated on 2026-08-14

This commit is contained in:
Tangem 2023-12-08 18:05:57 +03:00
parent 1d0d2aab1c
commit d74f3191bb
7 changed files with 40 additions and 20 deletions

View file

@ -60,7 +60,7 @@ internal class SwapRepositoryImpl @Inject constructor(
override suspend fun getPairs(
initialCurrency: LeastTokenInfo,
currencyList: List<CryptoCurrency>,
): List<SwapPairLeast> {
): PairsWithProviders {
return withContext(coroutineDispatcher.io) {
val initial = NetworkLeastTokenInfo(
contractAddress = initialCurrency.contractAddress,

View file

@ -2,18 +2,20 @@ package com.tangem.feature.swap.converters
import com.tangem.datasource.api.express.models.response.*
import com.tangem.feature.swap.domain.models.domain.LeastTokenInfo
import com.tangem.feature.swap.domain.models.domain.PairsWithProviders
import com.tangem.feature.swap.domain.models.domain.SwapProvider
import com.tangem.utils.converter.Converter
import com.tangem.feature.swap.domain.models.domain.ExchangeProviderType as ExchangeProviderTypeDomain
import com.tangem.feature.swap.domain.models.domain.SwapPairLeast as SwapPairDomain
import com.tangem.feature.swap.domain.models.domain.SwapProvider as SwapPairProviderDomain
class SwapPairInfoConverter : Converter<SwapPairsWithProviders, List<SwapPairDomain>> {
class SwapPairInfoConverter : Converter<SwapPairsWithProviders, PairsWithProviders> {
private val rateTypeConverter = RateTypeConverter()
override fun convert(value: SwapPairsWithProviders): List<SwapPairDomain> {
override fun convert(value: SwapPairsWithProviders): PairsWithProviders {
val providersAdditionalMap = value.providers.associateBy { it.id }
return value.swapPair.map { pair ->
val pairs = value.swapPair.map { pair ->
SwapPairDomain(
from = LeastTokenInfo(
contractAddress = pair.from.contractAddress,
@ -28,6 +30,20 @@ class SwapPairInfoConverter : Converter<SwapPairsWithProviders, List<SwapPairDom
},
)
}
return PairsWithProviders(
pairs = pairs,
allProviders = value.providers.map { convertLeastProvider(it) },
)
}
private fun convertLeastProvider(exchangeProvider: ExchangeProvider): SwapProvider {
return SwapProvider(
providerId = exchangeProvider.id,
rateTypes = emptyList(),
name = exchangeProvider.name,
type = convertExchangeType(exchangeProvider.type),
imageLarge = exchangeProvider.imageLargeUrl,
)
}
private fun convertProvider(

View file

@ -83,10 +83,7 @@ internal class SwapInteractorImpl @Inject constructor(
}
if (walletCurrencyStatusesExceptInitial.isEmpty()) {
return TokensDataStateExpress(
fromGroup = CurrenciesGroup(emptyList(), emptyList()),
toGroup = CurrenciesGroup(emptyList(), emptyList()),
)
return TokensDataStateExpress.EMPTY
}
val pairsLeast = getPairs(
@ -100,18 +97,19 @@ internal class SwapInteractorImpl @Inject constructor(
return TokensDataStateExpress(
fromGroup = getToCurrenciesGroup(
currency = currency,
leastPairs = pairsLeast,
leastPairs = pairsLeast.pairs,
cryptoCurrenciesList = walletCurrencyStatusesExceptInitial,
tokenInfoForFilter = { it.from },
tokenInfoForAvailable = { it.to },
),
toGroup = getToCurrenciesGroup(
currency = currency,
leastPairs = pairsLeast,
leastPairs = pairsLeast.pairs,
cryptoCurrenciesList = walletCurrencyStatusesExceptInitial,
tokenInfoForFilter = { it.to },
tokenInfoForAvailable = { it.from },
),
allProviders = pairsLeast.allProviders,
)
}
@ -177,7 +175,7 @@ internal class SwapInteractorImpl @Inject constructor(
private suspend fun getPairs(
initialCurrency: LeastTokenInfo,
currenciesList: List<CryptoCurrency>,
): List<SwapPairLeast> {
): PairsWithProviders {
return repository.getPairs(initialCurrency, currenciesList)
}

View file

@ -9,7 +9,7 @@ import java.math.BigDecimal
interface SwapRepository {
suspend fun getPairs(initialCurrency: LeastTokenInfo, currencyList: List<CryptoCurrency>): List<SwapPairLeast>
suspend fun getPairs(initialCurrency: LeastTokenInfo, currencyList: List<CryptoCurrency>): PairsWithProviders
suspend fun getRates(currencyId: String, tokenIds: List<String>): Map<String, Double>

View file

@ -0,0 +1,6 @@
package com.tangem.feature.swap.domain.models.domain
data class PairsWithProviders(
val pairs: List<SwapPairLeast>,
val allProviders: List<SwapProvider>,
)

View file

@ -1,16 +1,19 @@
package com.tangem.feature.swap.domain.models.ui
import com.tangem.feature.swap.domain.models.domain.CryptoCurrencySwapInfo
import com.tangem.feature.swap.domain.models.domain.SwapProvider
data class TokensDataStateExpress(
val fromGroup: CurrenciesGroup,
val toGroup: CurrenciesGroup,
val allProviders: List<SwapProvider>,
) {
companion object {
val EMPTY =
TokensDataStateExpress(
fromGroup = CurrenciesGroup(emptyList(), emptyList()),
toGroup = CurrenciesGroup(emptyList(), emptyList()),
allProviders = emptyList(),
)
}
}

View file

@ -791,7 +791,7 @@ internal class SwapViewModel @Inject constructor(
val fromToken = dataState.fromCryptoCurrency ?: return@UiActions
val amountToSwap = dataState.amount ?: return@UiActions
val selectedProvider = dataState.selectedProvider ?: return@UiActions
uiState = stateBuilder.updateSelectedFeeBottomSheet(uiState, it.feeType)
uiState = stateBuilder.dismissBottomSheet(uiState)
dataState = dataState.copy(selectedFee = it)
viewModelScope.launch(dispatchers.io) {
val updatedState = swapInteractor.updateQuotesStateWithSelectedFee(
@ -823,7 +823,7 @@ internal class SwapViewModel @Inject constructor(
val fromToken = dataState.fromCryptoCurrency
if (provider != null && swapState != null && fromToken != null) {
analyticsEventHandler.send(SwapEvents.ProviderChosen(provider))
uiState = stateBuilder.updateSelectedProvider(uiState, provider.providerId)
uiState = stateBuilder.dismissBottomSheet(uiState)
setupLoadedState(
provider = provider,
state = swapState,
@ -915,15 +915,12 @@ internal class SwapViewModel @Inject constructor(
}
private fun getAllProviders(): List<SwapProvider> {
dataState.tokensDataState?.let { data ->
val allProviders =
data.fromGroup.available.flatMap { it.providers } + data.toGroup.available.flatMap { it.providers }
return allProviders.distinct()
} ?: return emptyList()
return dataState.tokensDataState?.allProviders ?: emptyList()
}
private fun getUnavailableProvidersFor(state: Map<SwapProvider, SwapState>): List<SwapProvider> {
return getAllProviders().filterNot { it in state }
val availableProviders = state.keys.map { it.providerId }
return getAllProviders().filterNot { availableProviders.contains(it.providerId) }
}
private fun Map<SwapProvider, SwapState>.getLastLoadedSuccessStates(): SuccessLoadedSwapData {