Updated on 2026-08-14

This commit is contained in:
Tangem 2024-12-06 14:01:39 +05:00
parent db7aa562c1
commit 1d810d815f
3 changed files with 26 additions and 26 deletions

View file

@ -2,33 +2,35 @@ package com.tangem.domain.onramp
import arrow.core.Either
import com.tangem.domain.onramp.model.OnrampAvailability
import com.tangem.domain.onramp.model.OnrampCountry
import com.tangem.domain.onramp.repositories.OnrampRepository
import com.tangem.domain.tokens.model.CryptoCurrency
class CheckOnrampAvailabilityUseCase(private val repository: OnrampRepository) {
suspend operator fun invoke(cryptoCurrency: CryptoCurrency): Either<Throwable, OnrampAvailability> {
suspend operator fun invoke(): Either<Throwable, OnrampAvailability> {
return Either.catch {
repository.fetchPaymentMethodsIfAbsent()
repository.getDefaultCountrySync()?.let { savedCountry ->
return@catch if (savedCountry.onrampAvailable) {
val currency = repository.getDefaultCurrencySync() ?: run {
repository.saveDefaultCurrency(savedCountry.defaultCurrency)
savedCountry.defaultCurrency
}
repository.fetchPairs(
currency = currency,
country = savedCountry,
cryptoCurrency = cryptoCurrency,
)
OnrampAvailability.Available(country = savedCountry, currency = currency)
} else {
OnrampAvailability.NotSupported(savedCountry)
}
val savedCountry = repository.getDefaultCountrySync()
if (savedCountry != null) {
proceedWithSavedCountry(savedCountry = savedCountry)
} else {
val detectedCountry = repository.getCountryByIp()
OnrampAvailability.ConfirmResidency(detectedCountry)
}
}
}
val detectedCountry = repository.getCountryByIp()
OnrampAvailability.ConfirmResidency(detectedCountry)
private suspend fun proceedWithSavedCountry(savedCountry: OnrampCountry): OnrampAvailability {
val countries = repository.getCountries()
val onrampAvailable = countries.find { it == savedCountry }?.onrampAvailable ?: false
return if (onrampAvailable) {
val currency = repository.getDefaultCurrencySync() ?: run {
repository.saveDefaultCurrency(savedCountry.defaultCurrency)
savedCountry.defaultCurrency
}
OnrampAvailability.Available(country = savedCountry, currency = currency)
} else {
OnrampAvailability.NotSupported(savedCountry)
}
}
}

View file

@ -6,7 +6,7 @@ import com.tangem.domain.onramp.repositories.OnrampRepository
class OnrampSaveDefaultCountryUseCase(private val repository: OnrampRepository) {
suspend operator fun invoke(country: OnrampCountry) {
repository.saveDefaultCountry(country)
repository.saveDefaultCurrency(country.defaultCurrency)
repository.saveDefaultCountry(country)
}
}

View file

@ -110,7 +110,7 @@ internal class OnrampMainComponentModel @Inject constructor(
private fun checkResidenceCountry() {
modelScope.launch {
checkOnrampAvailabilityUseCase.invoke(params.cryptoCurrency)
checkOnrampAvailabilityUseCase.invoke()
.onRight(::handleOnrampAvailability)
.onLeft { Timber.e(it) }
}
@ -128,11 +128,9 @@ internal class OnrampMainComponentModel @Inject constructor(
private fun subscribeToCurrencyUpdates() {
getOnrampCurrencyUseCase.invoke()
.onEach { maybeCurrency ->
val currency = maybeCurrency.getOrNull()
if (currency != null) {
_state.update { amountStateFactory.getUpdatedCurrencyState(currency) }
updatePairsAndQuotes()
}
val currency = maybeCurrency.getOrNull() ?: return@onEach
_state.update { amountStateFactory.getUpdatedCurrencyState(currency) }
updatePairsAndQuotes()
}
.launchIn(modelScope)
}