diff --git a/app/src/main/java/com/tangem/tap/di/domain/OnrampDomainModule.kt b/app/src/main/java/com/tangem/tap/di/domain/OnrampDomainModule.kt index 0c20fd0993..0f50feba84 100644 --- a/app/src/main/java/com/tangem/tap/di/domain/OnrampDomainModule.kt +++ b/app/src/main/java/com/tangem/tap/di/domain/OnrampDomainModule.kt @@ -179,6 +179,20 @@ internal object OnrampDomainModule { ) } + @Provides + @Singleton + fun provideGetOnrampV2QuotesUseCase( + settingsRepository: SettingsRepository, + onrampRepository: OnrampRepository, + onrampErrorResolver: OnrampErrorResolver, + ): GetOnrampV2QuotesUseCase { + return GetOnrampV2QuotesUseCase( + settingsRepository = settingsRepository, + repository = onrampRepository, + errorResolver = onrampErrorResolver, + ) + } + @Provides @Singleton fun provideGetOnrampProviderWithQuoteUseCase( diff --git a/domain/onramp/src/main/java/com/tangem/domain/onramp/GetOnrampV2QuotesUseCase.kt b/domain/onramp/src/main/java/com/tangem/domain/onramp/GetOnrampV2QuotesUseCase.kt new file mode 100644 index 0000000000..5039240734 --- /dev/null +++ b/domain/onramp/src/main/java/com/tangem/domain/onramp/GetOnrampV2QuotesUseCase.kt @@ -0,0 +1,73 @@ +package com.tangem.domain.onramp + +import arrow.core.Either +import arrow.core.left +import arrow.core.right +import com.tangem.domain.onramp.model.OnrampQuote +import com.tangem.domain.onramp.model.error.OnrampError +import com.tangem.domain.onramp.repositories.OnrampErrorResolver +import com.tangem.domain.onramp.repositories.OnrampRepository +import com.tangem.domain.settings.repositories.SettingsRepository +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.catch +import kotlinx.coroutines.flow.map +import java.math.BigDecimal +import java.util.Comparator + +class GetOnrampV2QuotesUseCase( + private val settingsRepository: SettingsRepository, + private val repository: OnrampRepository, + private val errorResolver: OnrampErrorResolver, +) { + + operator fun invoke(): Flow>> { + return repository.getQuotes() + .map, Either>> { quotes -> + val isGooglePayAvailable = settingsRepository.isGooglePayAvailability() + quotes.sortedWith( + Comparator.comparing { quote -> + getQuoteSortPriority(quote) + } + .thenComparingInt { quote -> + quote.paymentMethod.type.getPriority(isGooglePayAvailable) + }, + ).right() + } + .catch { + emit(errorResolver.resolve(it).left()) + } + } + + private class SortableBigDecimalWrapper( + val value: BigDecimal?, + val negateForSort: Boolean = false, + ) : Comparable { + override fun compareTo(other: SortableBigDecimalWrapper): Int { + return when { + value == null && other.value == null -> 0 + value == null -> 1 + other.value == null -> -1 + else -> { + val thisValue = if (negateForSort) value.negate() else value + val otherValue = if (other.negateForSort) other.value.negate() else other.value + thisValue.compareTo(otherValue) + } + } + } + } + + private fun getQuoteSortPriority(quote: OnrampQuote): SortableBigDecimalWrapper { + return when (quote) { + is OnrampQuote.Data -> SortableBigDecimalWrapper(quote.toAmount.value, negateForSort = true) + is OnrampQuote.Error -> SortableBigDecimalWrapper(null) + is OnrampQuote.AmountError -> { + when (val error = quote.error) { + is OnrampError.AmountError.TooSmallError -> + SortableBigDecimalWrapper((quote.fromAmount.value - error.requiredAmount).abs()) + is OnrampError.AmountError.TooBigError -> + SortableBigDecimalWrapper((error.requiredAmount - quote.fromAmount.value).abs()) + } + } + } + } +} \ No newline at end of file diff --git a/features/onramp/impl/src/main/kotlin/com/tangem/features/onramp/mainv2/model/OnrampV2MainComponentModel.kt b/features/onramp/impl/src/main/kotlin/com/tangem/features/onramp/mainv2/model/OnrampV2MainComponentModel.kt index 834518bbb3..959de53386 100644 --- a/features/onramp/impl/src/main/kotlin/com/tangem/features/onramp/mainv2/model/OnrampV2MainComponentModel.kt +++ b/features/onramp/impl/src/main/kotlin/com/tangem/features/onramp/mainv2/model/OnrampV2MainComponentModel.kt @@ -41,7 +41,7 @@ internal class OnrampV2MainComponentModel @Inject constructor( private val getOnrampCountryUseCase: GetOnrampCountryUseCase, private val clearOnrampCacheUseCase: ClearOnrampCacheUseCase, private val fetchQuotesUseCase: OnrampFetchQuotesUseCase, - private val getOnrampQuotesUseCase: GetOnrampQuotesUseCase, + private val getOnrampQuotesUseCase: GetOnrampV2QuotesUseCase, private val fetchPairsUseCase: OnrampFetchPairsUseCase, private val amountInputManager: InputManager, private val getOnrampOffersUseCase: GetOnrampOffersUseCase,