@@ -37,6 +37,7 @@ import com.getcode.utils.trace
3737import com.getcode.view.BaseViewModel2
3838import com.getcode.view.LoadingSuccessState
3939import dagger.hilt.android.lifecycle.HiltViewModel
40+ import kotlinx.coroutines.delay
4041import kotlinx.coroutines.flow.combine
4142import kotlinx.coroutines.flow.distinctUntilChanged
4243import kotlinx.coroutines.flow.filter
@@ -367,6 +368,8 @@ class BuySellSwapTokenViewModel @Inject constructor(
367368 usdf = balance,
368369 nativeAmount = balance.convertingTo(rate),
369370 )
371+ }.filter {
372+ stateFlow.value.buyProgress.isIdle && stateFlow.value.sellProgress.isIdle
370373 }.onEach {
371374 dispatchEvent(Event .OnReservesUpdated (TokenWithBalance (Token .usdf, it.nativeAmount)))
372375 }.launchIn(viewModelScope)
@@ -540,9 +543,9 @@ class BuySellSwapTokenViewModel @Inject constructor(
540543 of = token,
541544 ).onSuccess { swapId ->
542545 dispatchEvent(Event .OnPurchaseSubmitted (token, swapId))
546+ dispatchEvent(Event .UpdateBuyState (loading = false , success = true ))
543547 // buy submitted from reserves, drop reserves balance
544548 tokenController.subtract(Token .usdf, amount)
545- dispatchEvent(Event .UpdateBuyState (loading = false , success = true ))
546549 }.onFailure {
547550 dispatchEvent(Event .UpdateBuyState (loading = false , success = false ))
548551 BottomBarManager .showError(
@@ -569,9 +572,9 @@ class BuySellSwapTokenViewModel @Inject constructor(
569572 of = token,
570573 ).onSuccess { swapId ->
571574 dispatchEvent(Event .OnSellSubmitted (token, swapId))
575+ dispatchEvent(Event .UpdateSellState (loading = false , success = true ))
572576 // sell submitted, drop from balance
573577 tokenController.subtract(token, amount)
574- dispatchEvent(Event .UpdateSellState (loading = false , success = true ))
575578 }.onFailure {
576579 dispatchEvent(Event .UpdateSellState (loading = false , success = false ))
577580 BottomBarManager .showError(
@@ -598,15 +601,19 @@ class BuySellSwapTokenViewModel @Inject constructor(
598601 }.onResult(
599602 onSuccess = {
600603 val token = stateFlow.value.tokenWithBalance!! .token
604+ val isUsingReserves = stateFlow.value.purpose is TokenSwapPurpose .Buy ||
605+ stateFlow.value.purpose is TokenSwapPurpose .Sell
601606 viewModelScope.launch { tokenController.updateTokenAccount(token) }
607+ if (isUsingReserves) {
608+ viewModelScope.launch { tokenController.updateTokenAccount(Mint .usdf) }
609+ }
602610 viewModelScope.launch {
603611 // update activity feed to grab the tx as a result of this buy/sell
604612 feedCoordinator.fetchSinceLatest()
605613 }
606614 dispatchEvent(Event .UpdateProcessingState (loading = false , success = true ))
607615 },
608616 onError = {
609- // TODO: show error
610617 dispatchEvent(Event .UpdateProcessingState (loading = false , success = false , error = true ))
611618 }
612619 ).launchIn(viewModelScope)
0 commit comments