Updated on 2026-08-14
This commit is contained in:
parent
db7aa562c1
commit
1d810d815f
3 changed files with 26 additions and 26 deletions
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue