Updated on 2026-08-14

This commit is contained in:
Tangem 2024-12-13 15:18:41 +05:00
parent 1a8756bba5
commit 05a8cd600a
2 changed files with 43 additions and 34 deletions

View file

@ -74,7 +74,7 @@ internal class OnrampStateFactory(
)
}
private fun getErrorState(errorCode: String? = null): OnrampMainComponentUM {
fun getErrorState(errorCode: String? = null): OnrampMainComponentUM {
val state = currentStateProvider()
val endButton = state.topBarConfig.endButtonUM.copy(enabled = true)
@ -84,6 +84,7 @@ internal class OnrampStateFactory(
buyButtonConfig = state.buyButtonConfig.copy(enabled = false),
amountBlockState = state.amountBlockState.copy(
amountFieldModel = state.amountBlockState.amountFieldModel.copy(isError = true),
secondaryFieldModel = OnrampAmountSecondaryFieldUM.Content(TextReference.EMPTY),
),
providerBlockState = OnrampProviderBlockUM.Empty,
errorNotification = NotificationUM.Warning.OnrampErrorNotification(

View file

@ -199,39 +199,7 @@ internal class OnrampMainComponentModel @Inject constructor(
.onEach { maybeQuotes ->
maybeQuotes.fold(
ifLeft = ::handleOnrampError,
ifRight = { quotes ->
quotes.filterIsInstance<OnrampQuote.Error>().forEach { errorState ->
sendOnrampErrorAnalytic(errorState.error)
}
val quote = quotes.firstOrNull() ?: return@onEach
val bestProvider = quote as? OnrampQuote.Data
val isMultipleQuotes = !quotes.isSingleItem()
val isOtherQuotesHasData = quotes
.filter { it.paymentMethod == quote.paymentMethod }
.filterNot { it == bestProvider }
.any { it is OnrampQuote.Data }
val hasBestProvider = isMultipleQuotes && isOtherQuotesHasData
val isBestProvider = quote == bestProvider && hasBestProvider
if (quote is OnrampQuote.Data && lastAmount.value != quote.fromAmount.value) {
lastAmount.value = quote.fromAmount.value
analyticsEventHandler.send(
OnrampAnalyticsEvent.ProviderCalculated(
providerName = quote.provider.info.name,
tokenSymbol = params.cryptoCurrency.symbol,
paymentMethod = quote.paymentMethod.name,
),
)
}
_state.update {
amountStateFactory.getAmountSecondaryUpdatedState(
quote = quote,
isBestRate = isBestProvider,
)
}
},
ifRight = ::handleQuoteResult,
)
}
.launchIn(modelScope)
@ -300,6 +268,46 @@ internal class OnrampMainComponentModel @Inject constructor(
super.onDestroy()
}
private fun handleQuoteResult(quotes: List<OnrampQuote>) {
quotes.filterIsInstance<OnrampQuote.Error>().forEach { errorState ->
sendOnrampErrorAnalytic(errorState.error)
}
val quote = quotes.firstOrNull()
if (quote == null) {
_state.update { stateFactory.getErrorState() }
return
}
val bestProvider = quote as? OnrampQuote.Data
val isMultipleQuotes = !quotes.isSingleItem()
val isOtherQuotesHasData = quotes
.filter { it.paymentMethod == quote.paymentMethod }
.filterNot { it == bestProvider }
.any { it is OnrampQuote.Data }
val hasBestProvider = isMultipleQuotes && isOtherQuotesHasData
val isBestProvider = quote == bestProvider && hasBestProvider
if (quote is OnrampQuote.Data && lastAmount.value != quote.fromAmount.value) {
lastAmount.value = quote.fromAmount.value
analyticsEventHandler.send(
OnrampAnalyticsEvent.ProviderCalculated(
providerName = quote.provider.info.name,
tokenSymbol = params.cryptoCurrency.symbol,
paymentMethod = quote.paymentMethod.name,
),
)
}
_state.update {
amountStateFactory.getAmountSecondaryUpdatedState(
quote = quote,
isBestRate = isBestProvider,
)
}
}
private fun showDemoWarning() {
val alertUM = AlertDemoModeUM(onConfirmClick = {})