From 6ef85396728bf441d2eadde723869fcc2f02e220 Mon Sep 17 00:00:00 2001 From: Tangem Date: Mon, 11 Dec 2023 18:26:02 +0200 Subject: [PATCH] Updated on 2026-08-14 --- core/res/src/main/res/values/strings.xml | 2 +- .../tangem/feature/swap/SwapRepositoryImpl.kt | 72 +++++++++++-------- .../swap/domain/models/ExpressException.kt | 3 + .../tangem/feature/swap/ui/StateBuilder.kt | 10 ++- .../feature/swap/viewmodels/SwapViewModel.kt | 16 +++-- 5 files changed, 63 insertions(+), 40 deletions(-) create mode 100644 features/swap/domain/src/main/java/com/tangem/feature/swap/domain/models/ExpressException.kt diff --git a/core/res/src/main/res/values/strings.xml b/core/res/src/main/res/values/strings.xml index 345f06a0f6..cbd1bf2dab 100644 --- a/core/res/src/main/res/values/strings.xml +++ b/core/res/src/main/res/values/strings.xml @@ -681,7 +681,7 @@ No available tokens to swap To make a transaction you need to deposit some %1$s %2$s Unable to cover %s fee - Service temporary unavailable + Service temporarily unavailable Please change the amount to swap The amount to swap must be at least %s This card might be a production sample or counterfeit diff --git a/features/swap/data/src/main/java/com/tangem/feature/swap/SwapRepositoryImpl.kt b/features/swap/data/src/main/java/com/tangem/feature/swap/SwapRepositoryImpl.kt index 9e41c08329..042908ada3 100644 --- a/features/swap/data/src/main/java/com/tangem/feature/swap/SwapRepositoryImpl.kt +++ b/features/swap/data/src/main/java/com/tangem/feature/swap/SwapRepositoryImpl.kt @@ -9,6 +9,7 @@ import com.tangem.blockchain.common.Blockchain import com.tangem.blockchain.common.Token import com.tangem.blockchain.extensions.Result import com.tangem.data.tokens.utils.CryptoCurrencyFactory +import com.tangem.datasource.api.common.response.ApiResponse import com.tangem.datasource.api.common.response.ApiResponseError import com.tangem.datasource.api.common.response.getOrThrow import com.tangem.datasource.api.express.TangemExpressApi @@ -28,6 +29,7 @@ import com.tangem.domain.wallets.models.UserWalletId import com.tangem.feature.swap.converters.* import com.tangem.feature.swap.domain.SwapRepository import com.tangem.feature.swap.domain.models.DataError +import com.tangem.feature.swap.domain.models.ExpressException import com.tangem.feature.swap.domain.models.createFromAmountWithOffset import com.tangem.feature.swap.domain.models.data.AggregatedSwapDataModel import com.tangem.feature.swap.domain.models.domain.* @@ -62,49 +64,61 @@ internal class SwapRepositoryImpl @Inject constructor( currencyList: List, ): PairsWithProviders { return withContext(coroutineDispatcher.io) { - val initial = NetworkLeastTokenInfo( - contractAddress = initialCurrency.contractAddress, - network = initialCurrency.network, - ) - val currenciesList = currencyList.map { leastTokenInfoConverter.convert(it) } - - val pairs = async { - getPairsInternal( - from = arrayListOf(initial), - to = currenciesList, + try { + val initial = NetworkLeastTokenInfo( + contractAddress = initialCurrency.contractAddress, + network = initialCurrency.network, ) + val currenciesList = currencyList.map { leastTokenInfoConverter.convert(it) } + + val pairsDeferred = async { + getPairsInternal( + from = arrayListOf(initial), + to = currenciesList, + ) + } + + val reversedPairsDeferred = async { + getPairsInternal( + from = currenciesList, + to = arrayListOf(initial), + ) + } + + val pairs = pairsDeferred.await().getOrThrow() + val reversedPairs = reversedPairsDeferred.await().getOrThrow() + + val allPairs = pairs + reversedPairs + + val providers = tangemExpressApi.getProviders().getOrThrow() + + return@withContext swapPairInfoConverter.convert( + SwapPairsWithProviders( + swapPair = allPairs, + providers = providers, + ), + ) + } catch (exception: Exception) { + if (exception is ApiResponseError.HttpException) { + throw ExpressException(errorsDataConverter.convert(exception.errorBody ?: "")) + } else { + throw exception + } } - val reversedPairs = async { - getPairsInternal( - from = currenciesList, - to = arrayListOf(initial), - ) - } - - val allPairs = pairs.await() + reversedPairs.await() - - val providers = tangemExpressApi.getProviders().getOrThrow() - - return@withContext swapPairInfoConverter.convert( - SwapPairsWithProviders( - swapPair = allPairs, - providers = providers, - ), - ) } } private suspend fun getPairsInternal( from: List, to: List, - ): List { + ): ApiResponse> { return tangemExpressApi.getPairs( PairsRequestBody( from = from, to = to, ), - ).getOrThrow() + ) } override suspend fun getExchangeStatus(txId: String): Either { diff --git a/features/swap/domain/src/main/java/com/tangem/feature/swap/domain/models/ExpressException.kt b/features/swap/domain/src/main/java/com/tangem/feature/swap/domain/models/ExpressException.kt new file mode 100644 index 0000000000..cc416b82ca --- /dev/null +++ b/features/swap/domain/src/main/java/com/tangem/feature/swap/domain/models/ExpressException.kt @@ -0,0 +1,3 @@ +package com.tangem.feature.swap.domain.models + +class ExpressException(val dataError: DataError) : Exception() \ No newline at end of file diff --git a/features/swap/presentation/src/main/java/com/tangem/feature/swap/ui/StateBuilder.kt b/features/swap/presentation/src/main/java/com/tangem/feature/swap/ui/StateBuilder.kt index af2f4cb3db..28d54f59f5 100644 --- a/features/swap/presentation/src/main/java/com/tangem/feature/swap/ui/StateBuilder.kt +++ b/features/swap/presentation/src/main/java/com/tangem/feature/swap/ui/StateBuilder.kt @@ -579,14 +579,18 @@ internal class StateBuilder( } } - fun createInitialErrorState(uiState: SwapStateHolder, onRefreshClick: () -> Unit): SwapStateHolder { + fun createInitialErrorState( + uiState: SwapStateHolder, + code: Int, + onRefreshClick: () -> Unit, + ): SwapStateHolder { return uiState.copy( warnings = listOf( SwapWarning.GeneralWarning( notificationConfig = NotificationConfig( title = TextReference.Res(R.string.warning_express_refresh_required_title), - subtitle = TextReference.EMPTY, - iconResId = R.drawable.ic_alert_circle_24, + subtitle = TextReference.Res(R.string.generic_error_code, wrappedList(code)), + iconResId = R.drawable.ic_alert_triangle_20, buttonsState = NotificationConfig.ButtonsState.PrimaryButtonConfig( text = TextReference.Res(R.string.warning_button_refresh), onClick = onRefreshClick, 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 2f93839717..4f736b29f7 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 @@ -18,6 +18,7 @@ import com.tangem.feature.swap.analytics.SwapEvents import com.tangem.feature.swap.domain.BlockchainInteractor import com.tangem.feature.swap.domain.SwapInteractor import com.tangem.feature.swap.domain.models.DataError +import com.tangem.feature.swap.domain.models.ExpressException import com.tangem.feature.swap.domain.models.domain.PermissionOptions import com.tangem.feature.swap.domain.models.domain.SwapDataModel import com.tangem.feature.swap.domain.models.domain.SwapProvider @@ -174,13 +175,14 @@ internal class SwapViewModel @Inject constructor( selectedCurrency = null, ) - uiState = stateBuilder.createInitialErrorState(uiState) { - uiState = stateBuilder.createInitialLoadingState( - initialCurrency = initialCryptoCurrency, - networkInfo = blockchainInteractor.getBlockchainInfo(initialCryptoCurrency.network.backendId), - ) - initTokens() - } + uiState = + stateBuilder.createInitialErrorState(uiState, (it as? ExpressException)?.dataError?.code ?: DataError.UnknownError.code) { + uiState = stateBuilder.createInitialLoadingState( + initialCurrency = initialCryptoCurrency, + networkInfo = blockchainInteractor.getBlockchainInfo(initialCryptoCurrency.network.backendId), + ) + initTokens() + } } } }