Updated on 2026-08-14

This commit is contained in:
Tangem 2024-08-30 15:15:07 +03:00
parent 248dab7303
commit 653cd7e72c
10 changed files with 121 additions and 238 deletions

View file

@ -25,7 +25,6 @@ import com.tangem.domain.tokens.UpdateDelayedNetworkStatusUseCase
import com.tangem.domain.tokens.model.CryptoCurrency
import com.tangem.domain.tokens.model.CryptoCurrencyStatus
import com.tangem.domain.tokens.model.Network
import com.tangem.domain.wallets.models.UserWallet
import com.tangem.domain.wallets.models.UserWalletId
import com.tangem.feature.swap.analytics.SwapEvents
import com.tangem.feature.swap.domain.BlockchainInteractor
@ -65,7 +64,7 @@ typealias SuccessLoadedSwapData = Map<SwapProvider, SwapState.QuotesLoadedState>
@Suppress("LargeClass", "LongParameterList")
@HiltViewModel
internal class SwapViewModel @Inject constructor(
private val swapInteractor: SwapInteractor,
private val swapInteractorFactory: SwapInteractor.Factory,
private val blockchainInteractor: BlockchainInteractor,
private val dispatchers: CoroutineDispatcherProvider,
private val analyticsEventHandler: AnalyticsEventHandler,
@ -79,7 +78,13 @@ internal class SwapViewModel @Inject constructor(
private val initialCryptoCurrency: CryptoCurrency = savedStateHandle.get<Bundle>(AppRoute.Swap.CURRENCY_BUNDLE_KEY)
?.unbundle(CryptoCurrency.serializer())
?: error("no expected parameter CryptoCurrency found`")
?: error("no expected parameter CryptoCurrency found")
private val userWalletId: UserWalletId = savedStateHandle.get<Bundle>(AppRoute.Swap.USER_WALLET_ID_KEY)
?.unbundle(UserWalletId.serializer())
?: error("no expected parameter UserWalletId found")
private val swapInteractor = swapInteractorFactory.create(userWalletId)
private lateinit var initialCryptoCurrencyStatus: CryptoCurrencyStatus
@ -126,15 +131,13 @@ internal class SwapViewModel @Inject constructor(
init {
viewModelScope.launch(dispatchers.io) {
swapInteractor.getSelectedWallet()?.let {
val cryptoCurrencyStatus =
getCryptoCurrencyStatusUseCase(it.walletId, initialCryptoCurrency.id).getOrNull()
if (cryptoCurrencyStatus == null) {
uiState = stateBuilder.addAlert(uiState = uiState, onClick = swapRouter::back)
} else {
initialCryptoCurrencyStatus = cryptoCurrencyStatus
initTokens()
}
val cryptoCurrencyStatus =
getCryptoCurrencyStatusUseCase(userWalletId, initialCryptoCurrency.id).getOrNull()
if (cryptoCurrencyStatus == null) {
uiState = stateBuilder.addAlert(uiState = uiState, onClick = swapRouter::back)
} else {
initialCryptoCurrencyStatus = cryptoCurrencyStatus
initTokens()
}
}
}
@ -191,7 +194,6 @@ internal class SwapViewModel @Inject constructor(
),
)
val userWalletId = swapInteractor.getSelectedWallet()?.walletId ?: return@launch
(dataState.fromCryptoCurrency?.currency as? CryptoCurrency.Coin)?.let {
subscribeToCoinBalanceUpdates(
userWalletId = userWalletId,
@ -707,8 +709,6 @@ internal class SwapViewModel @Inject constructor(
analyticsEventHandler.send(SwapEvents.ChooseTokenScreenResult(tokenChosen = true, token = it))
}
val userWalletId = swapInteractor.getSelectedWallet()?.walletId
if (foundToken != null) {
val fromToken: CryptoCurrencyStatus
val toToken: CryptoCurrencyStatus
@ -717,7 +717,7 @@ internal class SwapViewModel @Inject constructor(
toToken = initialCryptoCurrencyStatus
val newToken = fromToken.currency as? CryptoCurrency.Coin
if (userWalletId != null && newToken != null) {
if (newToken != null) {
subscribeToCoinBalanceUpdates(
userWalletId = userWalletId,
coin = newToken,
@ -729,7 +729,7 @@ internal class SwapViewModel @Inject constructor(
toToken = foundToken.currencyStatus
val newToken = toToken.currency as? CryptoCurrency.Coin
if (userWalletId != null && newToken != null) {
if (newToken != null) {
subscribeToCoinBalanceUpdates(
userWalletId = userWalletId,
coin = newToken,
@ -985,12 +985,10 @@ internal class SwapViewModel @Inject constructor(
}
},
onBuyClick = { currency ->
swapInteractor.getSelectedWallet()?.let { userWallet ->
swapRouter.openTokenDetails(
userWalletId = userWallet.walletId,
currency = currency,
)
}
swapRouter.openTokenDetails(
userWalletId = userWalletId,
currency = currency,
)
},
onRetryClick = {
startLoadingQuotesFromLastState()
@ -1162,20 +1160,18 @@ internal class SwapViewModel @Inject constructor(
}
private fun updateWalletBalance() {
swapInteractor.getSelectedWallet()?.let { userWallet ->
dataState.fromCryptoCurrency?.currency?.network?.let { network ->
viewModelScope.launch {
withContext(NonCancellable) {
updateForBalance(userWallet, network)
}
dataState.fromCryptoCurrency?.currency?.network?.let { network ->
viewModelScope.launch {
withContext(NonCancellable) {
updateForBalance(userWalletId, network)
}
}
}
}
private suspend fun updateForBalance(userWallet: UserWallet, network: Network) {
private suspend fun updateForBalance(userWalletId: UserWalletId, network: Network) {
updateDelayedCurrencyStatusUseCase(
userWalletId = userWallet.walletId,
userWalletId = userWalletId,
network = network,
delayMillis = UPDATE_BALANCE_DELAY_MILLIS,
refresh = true,