diff --git a/features/swap/domain/src/main/java/com/tangem/feature/swap/domain/SwapInteractorImpl.kt b/features/swap/domain/src/main/java/com/tangem/feature/swap/domain/SwapInteractorImpl.kt index ada40cc077..93a6fc6dff 100644 --- a/features/swap/domain/src/main/java/com/tangem/feature/swap/domain/SwapInteractorImpl.kt +++ b/features/swap/domain/src/main/java/com/tangem/feature/swap/domain/SwapInteractorImpl.kt @@ -60,46 +60,67 @@ internal class SwapInteractorImpl @Inject constructor( requireNotNull(selectedWallet) - val currencyStatuses = getMultiCryptoCurrencyStatusUseCase(selectedWallet.walletId) + val walletCurrencyStatuses = getMultiCryptoCurrencyStatusUseCase(selectedWallet.walletId) .first() .getOrElse { emptyList() } - // .filter { it.currency.network.backendId != currency.networkId } - val currencies = currencyStatuses.map { it.currency } + + val walletCurrencyStatusesExceptInitial = walletCurrencyStatuses.filter { + it.currency.network.backendId != currency.networkId || + it.currency.getContractAddress() != currency.getContractAddress() + } val pairsLeast = getPairs( initialCurrency = LeastTokenInfo( contractAddress = (currency as? Currency.NonNativeToken)?.contractAddress ?: "0", network = currency.networkId, ), - currenciesList = currencyStatuses.map { it.currency }, + currenciesList = walletCurrencyStatusesExceptInitial.map { it.currency }, ) - val pairs = createCryptoCurrencyPairs(pairsLeast, currencies) - - val initialCryptoCurrency = mapLegacyCurrencyToCryptoCurrency(currency, currencyStatuses) + val initialCryptoCurrency = mapLegacyCurrencyToCryptoCurrency(currency, walletCurrencyStatuses) ?: error("Initial crypto currency must not be null") return TokensDataStateExpress( initialCryptoCurrency = initialCryptoCurrency, - preselectTokens = getPreselectTokens(currency, currencyStatuses), - foundTokensState = FoundTokensStateExpress(emptyList(), emptyList()), - pairs = pairs, + fromGroup = getToCurrenciesGroup( + currency = initialCryptoCurrency, + leastPairs = pairsLeast, + cryptoCurrenciesList = walletCurrencyStatusesExceptInitial, + tokenInfoForFilter = { it.from }, + tokenInfoForAvailable = { it.to } + ), + toGroup = getToCurrenciesGroup( + currency = initialCryptoCurrency, + leastPairs = pairsLeast, + cryptoCurrenciesList = walletCurrencyStatusesExceptInitial, + tokenInfoForFilter = { it.to }, + tokenInfoForAvailable = { it.from } + ), ) } - private fun getPreselectTokens(currency: Currency, currencies: List): PreselectTokensExpress { - val from = mapLegacyCurrencyToCryptoCurrency(currency, currencies) - - val to = currencies.firstOrNull()?.currency // TODO choose of 3 variants - - if (from != null && to != null) { - return PreselectTokensExpress( - fromToken = from, - toToken = to, - ) - } else { - error("From and to currencies must not be null") + private fun getToCurrenciesGroup( + currency: CryptoCurrency, + leastPairs: List, + cryptoCurrenciesList: List, + tokenInfoForFilter: (SwapPairLeast) -> LeastTokenInfo, + tokenInfoForAvailable: (SwapPairLeast) -> LeastTokenInfo, + ): CurrenciesGroup { + val filteredPairs = leastPairs.filter { + tokenInfoForFilter(it).contractAddress == currency.getContractAddress() + && tokenInfoForFilter(it).network == currency.network.backendId } + + val availableCryptoCurrencies = filteredPairs.mapNotNull { + findCryptoCurrencyStatusByLeastInfo(tokenInfoForAvailable(it), cryptoCurrenciesList) + } + + val unavailableCryptoCurrencies = cryptoCurrenciesList - availableCryptoCurrencies.toSet() + + return CurrenciesGroup( + available = availableCryptoCurrencies, + unavailable = unavailableCryptoCurrencies + ) } private fun mapLegacyCurrencyToCryptoCurrency( @@ -113,32 +134,13 @@ internal class SwapInteractorImpl @Inject constructor( } } - private fun createCryptoCurrencyPairs( - swapPairLeasts: List, - cryptoCurrenciesList: List, - ): List { - return swapPairLeasts.mapNotNull { - val from = findCryptoCurrencyByLeastInfo(it.from, cryptoCurrenciesList) - val to = findCryptoCurrencyByLeastInfo(it.to, cryptoCurrenciesList) - if (from != null && to != null) { - SwapPair( - from = from, - to = to, - providers = it.providers, - ) - } else { - null - } - } - } - - private fun findCryptoCurrencyByLeastInfo( + private fun findCryptoCurrencyStatusByLeastInfo( leastTokenInfo: LeastTokenInfo, - cryptoCurrenciesList: List, - ): CryptoCurrency? { - return cryptoCurrenciesList.find { - it.network.backendId == leastTokenInfo.network && - it.getContractAddress() == leastTokenInfo.contractAddress + cryptoCurrencyStatusesList: List, + ): CryptoCurrencyStatus? { + return cryptoCurrencyStatusesList.find { + it.currency.network.backendId == leastTokenInfo.network && + it.currency.getContractAddress() == leastTokenInfo.contractAddress } } diff --git a/features/swap/domain/src/main/java/com/tangem/feature/swap/domain/models/ui/TokensDataStateExpress.kt b/features/swap/domain/src/main/java/com/tangem/feature/swap/domain/models/ui/TokensDataStateExpress.kt index e4909bfb51..d4c4f67494 100644 --- a/features/swap/domain/src/main/java/com/tangem/feature/swap/domain/models/ui/TokensDataStateExpress.kt +++ b/features/swap/domain/src/main/java/com/tangem/feature/swap/domain/models/ui/TokensDataStateExpress.kt @@ -1,13 +1,17 @@ package com.tangem.feature.swap.domain.models.ui import com.tangem.domain.tokens.model.CryptoCurrency -import com.tangem.feature.swap.domain.models.domain.SwapPair +import com.tangem.domain.tokens.model.CryptoCurrencyStatus data class TokensDataStateExpress( val initialCryptoCurrency: CryptoCurrency, - val preselectTokens: PreselectTokensExpress, - val foundTokensState: FoundTokensStateExpress, - val pairs: List, + val fromGroup: CurrenciesGroup, + val toGroup: CurrenciesGroup, +) + +data class CurrenciesGroup( + val available: List, + val unavailable: List, ) data class FoundTokensStateExpress( @@ -15,11 +19,6 @@ data class FoundTokensStateExpress( val loadedTokens: List, ) -data class PreselectTokensExpress( - val fromToken: CryptoCurrency, - val toToken: CryptoCurrency, -) - data class TokenWithBalanceExpress( val token: CryptoCurrency, val tokenBalanceData: TokenBalanceDataExpress? = null, diff --git a/features/swap/presentation/src/main/java/com/tangem/feature/swap/viewmodels/SwapViewModel.kt b/features/swap/presentation/src/main/java/com/tangem/feature/swap/viewmodels/SwapViewModel.kt index 03e7c8d26e..119f30fea1 100644 --- a/features/swap/presentation/src/main/java/com/tangem/feature/swap/viewmodels/SwapViewModel.kt +++ b/features/swap/presentation/src/main/java/com/tangem/feature/swap/viewmodels/SwapViewModel.kt @@ -139,10 +139,8 @@ internal class SwapViewModel @Inject constructor( runCatching(dispatchers.io) { swapInteractor.getTokensDataState(currency) }.onSuccess { state -> - dataState = dataState.copy( - fromCryptoCurrency = state.preselectTokens.fromToken, - toCryptoCurrency = state.preselectTokens.toToken, - ) + // dataState = dataState.copy( + // ) cryptoCurrency = state.initialCryptoCurrency // updateTokensState(dataState = state.foundTokensState) // startLoadingQuotes(