From c68c5db506b10f46389b33c22d5778d3f76e4e7a Mon Sep 17 00:00:00 2001 From: Tangem Date: Mon, 8 Apr 2024 15:31:30 +0500 Subject: [PATCH] Updated on 2026-08-14 --- .../impl/presentation/state/SendAlertState.kt | 8 ++++ .../state/SendEventStateFactory.kt | 14 ++++++ .../state/amount/AmountNotificationFactory.kt | 38 ---------------- .../state/previewdata/SendClickIntentsStub.kt | 2 +- .../viewmodel/SendClickIntents.kt | 2 +- .../presentation/viewmodel/SendViewModel.kt | 43 ++++++++----------- 6 files changed, 41 insertions(+), 66 deletions(-) delete mode 100644 features/send/impl/src/main/java/com/tangem/features/send/impl/presentation/state/amount/AmountNotificationFactory.kt diff --git a/features/send/impl/src/main/java/com/tangem/features/send/impl/presentation/state/SendAlertState.kt b/features/send/impl/src/main/java/com/tangem/features/send/impl/presentation/state/SendAlertState.kt index 93bb812fae..d3adc2e74c 100644 --- a/features/send/impl/src/main/java/com/tangem/features/send/impl/presentation/state/SendAlertState.kt +++ b/features/send/impl/src/main/java/com/tangem/features/send/impl/presentation/state/SendAlertState.kt @@ -75,4 +75,12 @@ internal sealed class SendAlertState { override val message: TextReference = resourceReference(id = R.string.send_notification_invalid_reserve_amount_text) } + + data class FeeUnreachableError( + override val onConfirmClick: (() -> Unit), + ) : SendAlertState() { + override val title: TextReference = resourceReference(R.string.send_fee_unreachable_error_title) + override val message: TextReference = resourceReference(R.string.send_fee_unreachable_error_text) + override val confirmButtonText = resourceReference(R.string.warning_button_refresh) + } } \ No newline at end of file diff --git a/features/send/impl/src/main/java/com/tangem/features/send/impl/presentation/state/SendEventStateFactory.kt b/features/send/impl/src/main/java/com/tangem/features/send/impl/presentation/state/SendEventStateFactory.kt index 2928e3d1f6..58e0a97b65 100644 --- a/features/send/impl/src/main/java/com/tangem/features/send/impl/presentation/state/SendEventStateFactory.kt +++ b/features/send/impl/src/main/java/com/tangem/features/send/impl/presentation/state/SendEventStateFactory.kt @@ -123,4 +123,18 @@ internal class SendEventStateFactory( ), ) } + + fun getFeeUnreachableErrorState(onConsume: () -> Unit): SendUiState { + val state = currentStateProvider() + return state.copy( + event = triggeredEvent( + data = SendEvent.ShowAlert( + SendAlertState.FeeUnreachableError( + onConfirmClick = { clickIntents.feeReload(true) }, + ), + ), + onConsume = onConsume, + ), + ) + } } \ No newline at end of file diff --git a/features/send/impl/src/main/java/com/tangem/features/send/impl/presentation/state/amount/AmountNotificationFactory.kt b/features/send/impl/src/main/java/com/tangem/features/send/impl/presentation/state/amount/AmountNotificationFactory.kt deleted file mode 100644 index 222a8704cf..0000000000 --- a/features/send/impl/src/main/java/com/tangem/features/send/impl/presentation/state/amount/AmountNotificationFactory.kt +++ /dev/null @@ -1,38 +0,0 @@ -package com.tangem.features.send.impl.presentation.state.amount - -import com.tangem.features.send.impl.presentation.state.SendUiState -import com.tangem.features.send.impl.presentation.state.SendUiStateType -import com.tangem.features.send.impl.presentation.state.StateRouter -import com.tangem.features.send.impl.presentation.state.SendNotification -import com.tangem.features.send.impl.presentation.state.fee.FeeSelectorState -import com.tangem.features.send.impl.presentation.viewmodel.SendClickIntents -import com.tangem.utils.Provider -import kotlinx.collections.immutable.toImmutableList -import kotlinx.coroutines.flow.filter -import kotlinx.coroutines.flow.map - -internal class AmountNotificationFactory( - private val stateRouterProvider: Provider, - private val currentStateProvider: Provider, - private val clickIntents: SendClickIntents, -) { - - fun create() = stateRouterProvider().currentState - .filter { it.type == SendUiStateType.Amount } - .map { - buildList { - addFeeUnreachableNotification() - }.toImmutableList() - } - - private fun MutableList.addFeeUnreachableNotification() { - val state = currentStateProvider() - val feeState = state.feeState ?: return - - if (feeState.feeSelectorState is FeeSelectorState.Error) { - add( - SendNotification.Warning.NetworkFeeUnreachable(clickIntents::feeReload), - ) - } - } -} \ No newline at end of file diff --git a/features/send/impl/src/main/java/com/tangem/features/send/impl/presentation/state/previewdata/SendClickIntentsStub.kt b/features/send/impl/src/main/java/com/tangem/features/send/impl/presentation/state/previewdata/SendClickIntentsStub.kt index d70b281ffe..67d5b07642 100644 --- a/features/send/impl/src/main/java/com/tangem/features/send/impl/presentation/state/previewdata/SendClickIntentsStub.kt +++ b/features/send/impl/src/main/java/com/tangem/features/send/impl/presentation/state/previewdata/SendClickIntentsStub.kt @@ -33,7 +33,7 @@ internal object SendClickIntentsStub : SendClickIntents { override fun onRecipientMemoValueChange(value: String) {} - override fun feeReload() {} + override fun feeReload(isToNextState: Boolean) {} override fun onFeeSelectorClick(feeType: FeeType) {} diff --git a/features/send/impl/src/main/java/com/tangem/features/send/impl/presentation/viewmodel/SendClickIntents.kt b/features/send/impl/src/main/java/com/tangem/features/send/impl/presentation/viewmodel/SendClickIntents.kt index c35cf2ba2d..14ed1562af 100644 --- a/features/send/impl/src/main/java/com/tangem/features/send/impl/presentation/viewmodel/SendClickIntents.kt +++ b/features/send/impl/src/main/java/com/tangem/features/send/impl/presentation/viewmodel/SendClickIntents.kt @@ -38,7 +38,7 @@ internal interface SendClickIntents { // endregion // region Fee - fun feeReload() + fun feeReload(isToNextState: Boolean = false) fun onFeeSelectorClick(feeType: FeeType) diff --git a/features/send/impl/src/main/java/com/tangem/features/send/impl/presentation/viewmodel/SendViewModel.kt b/features/send/impl/src/main/java/com/tangem/features/send/impl/presentation/viewmodel/SendViewModel.kt index 4670b152cc..099bcd007f 100644 --- a/features/send/impl/src/main/java/com/tangem/features/send/impl/presentation/viewmodel/SendViewModel.kt +++ b/features/send/impl/src/main/java/com/tangem/features/send/impl/presentation/viewmodel/SendViewModel.kt @@ -46,7 +46,6 @@ import com.tangem.features.send.impl.presentation.analytics.SendScreenSource import com.tangem.features.send.impl.presentation.analytics.utils.SendOnNextScreenAnalyticSender import com.tangem.features.send.impl.presentation.domain.AvailableWallet import com.tangem.features.send.impl.presentation.state.* -import com.tangem.features.send.impl.presentation.state.amount.AmountNotificationFactory import com.tangem.features.send.impl.presentation.state.amount.AmountStateFactory import com.tangem.features.send.impl.presentation.state.confirm.SendNotificationFactory import com.tangem.features.send.impl.presentation.state.fee.* @@ -147,12 +146,6 @@ internal class SendViewModel @Inject constructor( feeStateFactory = feeStateFactory, ) - private val amountNotificationFactory = AmountNotificationFactory( - currentStateProvider = Provider { uiState }, - stateRouterProvider = Provider { stateRouter }, - clickIntents = this, - ) - private val feeNotificationFactory = FeeNotificationFactory( currentStateProvider = Provider { uiState }, stateRouterProvider = Provider { stateRouter }, @@ -194,7 +187,6 @@ internal class SendViewModel @Inject constructor( private var memoValidationJobHolder = JobHolder() private var sendNotificationsJobHolder = JobHolder() private var feeNotificationsJobHolder = JobHolder() - private var amountNotificationsJobHolder = JobHolder() private var qrScannerJobHolder = JobHolder() private var sendIdleTimer = 0L @@ -478,16 +470,6 @@ internal class SendViewModel @Inject constructor( .saveIn(feeNotificationsJobHolder) } - private fun updateAmountNotifications() { - amountNotificationFactory.create() - .conflate() - .distinctUntilChanged() - .onEach { uiState = amountStateFactory.getAmountNotificationState(notifications = it) } - .flowOn(dispatchers.io) - .launchIn(viewModelScope) - .saveIn(amountNotificationsJobHolder) - } - // region screen state navigation override fun popBackStack() = stateRouter.popBackStack() override fun onBackClick() { @@ -661,7 +643,7 @@ internal class SendViewModel @Inject constructor( // endregion // region fee - override fun feeReload() = loadFee() + override fun feeReload(isToNextState: Boolean) = loadFee(isToNextState = isToNextState) override fun onFeeSelectorClick(feeType: FeeType) { uiState = feeStateFactory.onFeeSelectedState(feeType) @@ -706,20 +688,30 @@ internal class SendViewModel @Inject constructor( } }, ifLeft = { - if (isShowStatus) uiState = feeStateFactory.onFeeOnErrorState() + onFeeLoadFailed(isShowStatus, isToNextState) }, ) - if (result == null && isShowStatus) { - uiState = feeStateFactory.onFeeOnErrorState() + if (result == null) { + onFeeLoadFailed(isShowStatus, isToNextState) } updateFeeNotifications() - updateAmountNotifications() }.saveIn(feeJobHolder) .invokeOnCompletion { uiState = amountStateFactory.getOnAmountFeeLoadingCancel() } } + private fun onFeeLoadFailed(isShowStatus: Boolean, isToNextState: Boolean) { + when { + isToNextState -> { + uiState = eventStateFactory.getFeeUnreachableErrorState { + uiState = eventStateFactory.onConsumeEventState() + } + } + isShowStatus -> uiState = feeStateFactory.onFeeOnErrorState() + } + } + private suspend fun checkIfSubtractAvailable() { isAmountSubtractAvailable = isAmountSubtractAvailableUseCase(userWalletId, cryptoCurrency).fold( ifRight = { it }, @@ -884,8 +876,7 @@ internal class SendViewModel @Inject constructor( }, ifLeft = { uiState = stateFactory.getSendingStateUpdate(isSending = false) - eventStateFactory.getGenericErrorState( - error = (it as? GetFeeError.DataError)?.cause, + eventStateFactory.getFeeUnreachableErrorState( onConsume = { uiState = eventStateFactory.onConsumeEventState() }, ) }, @@ -895,7 +886,7 @@ internal class SendViewModel @Inject constructor( feeUpdatedState } else { uiState = stateFactory.getSendingStateUpdate(isSending = false) - eventStateFactory.getGenericErrorState( + eventStateFactory.getFeeUnreachableErrorState( onConsume = { uiState = eventStateFactory.onConsumeEventState() }, ) }