From c931c3e8610ed87bc0016fb0fc021fa971f07199 Mon Sep 17 00:00:00 2001 From: Ilya Shulgin <13484957+Sateetas@users.noreply.github.com> Date: Tue, 29 Oct 2024 09:18:22 +0200 Subject: [PATCH] AND-8879 Fixed blinking on initial staking screen. (#3609) --- .../state/helpers/StakingBalanceUpdater.kt | 14 ++++++++++++-- .../state/helpers/StakingTransactionSender.kt | 2 +- .../presentation/viewmodel/StakingViewModel.kt | 9 +++++++-- 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/features/staking/impl/src/main/java/com/tangem/features/staking/impl/presentation/state/helpers/StakingBalanceUpdater.kt b/features/staking/impl/src/main/java/com/tangem/features/staking/impl/presentation/state/helpers/StakingBalanceUpdater.kt index 0c76f299b7..f275957a83 100644 --- a/features/staking/impl/src/main/java/com/tangem/features/staking/impl/presentation/state/helpers/StakingBalanceUpdater.kt +++ b/features/staking/impl/src/main/java/com/tangem/features/staking/impl/presentation/state/helpers/StakingBalanceUpdater.kt @@ -29,7 +29,7 @@ internal class StakingBalanceUpdater @AssistedInject constructor( @Assisted private val cryptoCurrencyStatus: CryptoCurrencyStatus, @Assisted private val yield: Yield, ) { - fun scheduleUpdates() { + fun fullUpdate() { coroutineScope.launch { listOf( // we should update network to find pending tx after 1 sec @@ -56,7 +56,7 @@ internal class StakingBalanceUpdater @AssistedInject constructor( } } - suspend fun instantUpdate() { + suspend fun partialUpdate() { coroutineScope { listOf( async { @@ -72,6 +72,16 @@ internal class StakingBalanceUpdater @AssistedInject constructor( } } + suspend fun initialUpdate() { + coroutineScope { + listOf( + async { + updateStakeBalance() + }, + ).awaitAll() + } + } + private suspend fun updateNetworkStatuses(delay: Long = BALANCE_UPDATE_DELAY) { updateDelayedNetworkStatusUseCase( userWalletId = userWallet.walletId, diff --git a/features/staking/impl/src/main/java/com/tangem/features/staking/impl/presentation/state/helpers/StakingTransactionSender.kt b/features/staking/impl/src/main/java/com/tangem/features/staking/impl/presentation/state/helpers/StakingTransactionSender.kt index 4014acae88..7866dc743a 100644 --- a/features/staking/impl/src/main/java/com/tangem/features/staking/impl/presentation/state/helpers/StakingTransactionSender.kt +++ b/features/staking/impl/src/main/java/com/tangem/features/staking/impl/presentation/state/helpers/StakingTransactionSender.kt @@ -227,7 +227,7 @@ internal class StakingTransactionSender @AssistedInject constructor( networkId = cryptoCurrencyStatus.currency.network.id, ).getOrElse { "" } - balanceUpdater.scheduleUpdates() + balanceUpdater.fullUpdate() onSendSuccess(txUrl) }, ) diff --git a/features/staking/impl/src/main/java/com/tangem/features/staking/impl/presentation/viewmodel/StakingViewModel.kt b/features/staking/impl/src/main/java/com/tangem/features/staking/impl/presentation/viewmodel/StakingViewModel.kt index affaf22281..7719fa6867 100644 --- a/features/staking/impl/src/main/java/com/tangem/features/staking/impl/presentation/viewmodel/StakingViewModel.kt +++ b/features/staking/impl/src/main/java/com/tangem/features/staking/impl/presentation/viewmodel/StakingViewModel.kt @@ -195,6 +195,7 @@ internal class StakingViewModel @Inject constructor( private var approvalJobHolder: JobHolder = JobHolder() private var feeJobHolder: JobHolder = JobHolder() private var sendTransactionJobHolder = JobHolder() + private var stepChangesJobHolder = JobHolder() init { subscribeOnSelectedAppCurrency() @@ -207,6 +208,7 @@ internal class StakingViewModel @Inject constructor( approvalJobHolder.cancel() feeJobHolder.cancel() sendTransactionJobHolder.cancel() + stepChangesJobHolder.cancel() } override fun onBackClick() { @@ -330,7 +332,7 @@ internal class StakingViewModel @Inject constructor( override fun onRefreshSwipe(isRefreshing: Boolean) { stateController.update(SetInitialLoadingStateTransformer(isRefreshing)) coroutineScope.launch { - balanceUpdater.instantUpdate() + balanceUpdater.partialUpdate() }.invokeOnCompletion { stateController.update(SetInitialLoadingStateTransformer(false)) } @@ -762,6 +764,8 @@ internal class StakingViewModel @Inject constructor( .onEach { maybeStatus -> maybeStatus.fold( ifRight = { status -> + if (status.value !is CryptoCurrencyStatus.Loaded) return@fold + if (!isInitialInfoAnalyticSent) { isInitialInfoAnalyticSent = true val balances = status.value.yieldBalance as? YieldBalance.Data @@ -822,7 +826,7 @@ internal class StakingViewModel @Inject constructor( when { isInitState() -> { updateInitialData() - onRefreshSwipe(isRefreshing = false) + balanceUpdater.initialUpdate() } isAssentState() -> { getFee() @@ -839,6 +843,7 @@ internal class StakingViewModel @Inject constructor( } .flowOn(dispatchers.main) .launchIn(viewModelScope) + .saveIn(stepChangesJobHolder) } private fun subscribeOnActionsUpdates() {