diff --git a/v4/feature/receipt/src/main/java/exchange/dydx/trading/feature/receipt/validation/DydxValidationViewModel.kt b/v4/feature/receipt/src/main/java/exchange/dydx/trading/feature/receipt/validation/DydxValidationViewModel.kt index 28eb0cca..97e15b11 100644 --- a/v4/feature/receipt/src/main/java/exchange/dydx/trading/feature/receipt/validation/DydxValidationViewModel.kt +++ b/v4/feature/receipt/src/main/java/exchange/dydx/trading/feature/receipt/validation/DydxValidationViewModel.kt @@ -3,14 +3,15 @@ package exchange.dydx.trading.feature.receipt.validation import androidx.lifecycle.ViewModel import dagger.hilt.android.lifecycle.HiltViewModel import exchange.dydx.abacus.output.input.ErrorType +import exchange.dydx.abacus.output.input.TransferInput import exchange.dydx.abacus.output.input.ValidationError import exchange.dydx.abacus.protocols.LocalizerProtocol import exchange.dydx.dydxstatemanager.AbacusStateManagerProtocol import exchange.dydx.dydxstatemanager.localizedString import exchange.dydx.trading.common.DydxViewModel import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.combine import kotlinx.coroutines.flow.distinctUntilChanged -import kotlinx.coroutines.flow.map import javax.inject.Inject @HiltViewModel @@ -20,15 +21,19 @@ class DydxValidationViewModel @Inject constructor( ) : ViewModel(), DydxViewModel { val state: Flow = - abacusStateManager.state.validationErrors - .map { - createViewState(it) - } + combine( + abacusStateManager.state.validationErrors, + abacusStateManager.state.transferInput, + ) { validationErrors, transferInput -> + createViewState(validationErrors, transferInput) + } .distinctUntilChanged() private fun createViewState( validationErrors: List?, + transferInput: TransferInput?, ): DydxValidationView.ViewState { + val transferError = transferInput?.errorMessage ?: transferInput?.errors val firstBlockingError = validationErrors?.firstOrNull { it.type == ErrorType.error } val firstWarning = validationErrors?.firstOrNull { it.type == ErrorType.warning } return DydxValidationView.ViewState( @@ -36,14 +41,13 @@ class DydxValidationViewModel @Inject constructor( state = when { firstBlockingError != null -> DydxValidationView.State.Error firstWarning != null -> DydxValidationView.State.Warning + transferError?.isNotEmpty() == true -> DydxValidationView.State.Error else -> DydxValidationView.State.None }, message = when { - firstBlockingError != null -> firstBlockingError.resources.text?.localizedString( - localizer, - ) - + firstBlockingError != null -> firstBlockingError.resources.text?.localizedString(localizer) firstWarning != null -> firstWarning.resources.text?.localizedString(localizer) + transferError?.isNotEmpty() == true -> transferError else -> null }, ) diff --git a/v4/feature/transfer/src/main/java/exchange/dydx/trading/feature/transfer/deposit/DydxTransferDepositViewModel.kt b/v4/feature/transfer/src/main/java/exchange/dydx/trading/feature/transfer/deposit/DydxTransferDepositViewModel.kt index e064885f..25ff9bf9 100644 --- a/v4/feature/transfer/src/main/java/exchange/dydx/trading/feature/transfer/deposit/DydxTransferDepositViewModel.kt +++ b/v4/feature/transfer/src/main/java/exchange/dydx/trading/feature/transfer/deposit/DydxTransferDepositViewModel.kt @@ -60,19 +60,29 @@ class DydxTransferDepositViewModel @Inject constructor( .distinctUntilChanged() init { - abacusStateManager.state.transferInput - .map { it?.depositOptions?.chains?.toList() } - .distinctUntilChanged() - .onEach { chains -> - selectedChainFlow.value = chains?.firstOrNull() + combine( + abacusStateManager.state.transferInput + .map { it?.depositOptions?.chains?.toList() } + .distinctUntilChanged(), + abacusStateManager.state.transferInput.map { it?.chain }.distinctUntilChanged(), + ) { chains, selected -> + chains?.firstOrNull { it.type == selected } ?: chains?.firstOrNull() + } + .onEach { chain -> + selectedChainFlow.value = chain } .launchIn(viewModelScope) - abacusStateManager.state.transferInput - .map { it?.depositOptions?.assets?.toList() } - .distinctUntilChanged() - .onEach { tokens -> - selectedTokenFlow.value = tokens?.firstOrNull() + combine( + abacusStateManager.state.transferInput + .map { it?.depositOptions?.assets?.toList() } + .distinctUntilChanged(), + abacusStateManager.state.transferInput.map { it?.token }.distinctUntilChanged(), + ) { tokens, selected -> + tokens?.firstOrNull { it.type == selected } ?: tokens?.firstOrNull() + } + .onEach { token -> + selectedTokenFlow.value = token } .launchIn(viewModelScope) diff --git a/v4/feature/transfer/src/main/java/exchange/dydx/trading/feature/transfer/withdrawal/DydxTransferWithdrawalViewModel.kt b/v4/feature/transfer/src/main/java/exchange/dydx/trading/feature/transfer/withdrawal/DydxTransferWithdrawalViewModel.kt index f438fdfa..37b2b2d0 100644 --- a/v4/feature/transfer/src/main/java/exchange/dydx/trading/feature/transfer/withdrawal/DydxTransferWithdrawalViewModel.kt +++ b/v4/feature/transfer/src/main/java/exchange/dydx/trading/feature/transfer/withdrawal/DydxTransferWithdrawalViewModel.kt @@ -55,20 +55,26 @@ class DydxTransferWithdrawalViewModel @Inject constructor( .distinctUntilChanged() init { - abacusStateManager.state.transferInput - .map { it?.withdrawalOptions?.chains?.toList() } - .distinctUntilChanged() - .onEach { chains -> - selectedChainFlow.value = chains?.firstOrNull() - } + combine( + abacusStateManager.state.transferInput + .map { it?.withdrawalOptions?.chains?.toList() } + .distinctUntilChanged(), + abacusStateManager.state.transferInput.map { it?.chain }.distinctUntilChanged(), + ) { chains, selected -> + chains?.firstOrNull { it.type == selected } ?: chains?.firstOrNull() + } + .onEach { selectedChainFlow.value = it } .launchIn(viewModelScope) - abacusStateManager.state.transferInput - .map { it?.withdrawalOptions?.assets?.toList() } - .distinctUntilChanged() - .onEach { tokens -> - selectedTokenFlow.value = tokens?.firstOrNull() - } + combine( + abacusStateManager.state.transferInput + .map { it?.withdrawalOptions?.assets?.toList() } + .distinctUntilChanged(), + abacusStateManager.state.transferInput.map { it?.token }.distinctUntilChanged(), + ) { tokens, selected -> + tokens?.firstOrNull { it.type == selected } ?: tokens?.firstOrNull() + } + .onEach { selectedTokenFlow.value = it } .launchIn(viewModelScope) }