diff --git a/core/utils/src/main/java/com/tangem/utils/coroutines/CoroutineExt.kt b/core/utils/src/main/java/com/tangem/utils/coroutines/CoroutineExt.kt index ebbe1dc50a..cfa0536804 100644 --- a/core/utils/src/main/java/com/tangem/utils/coroutines/CoroutineExt.kt +++ b/core/utils/src/main/java/com/tangem/utils/coroutines/CoroutineExt.kt @@ -10,6 +10,13 @@ suspend fun runCatching(dispatcher: CoroutineDispatcher, block: suspend () - } } +suspend fun waitForDelay(delay: Long, block: suspend CoroutineScope.() -> R): R = coroutineScope { + val minWaitingTime = async { delay(delay) } + val actionInvoke = async { block() } + minWaitingTime.await() + actionInvoke.await() +} + class Debouncer { private var debounceJob: Job? = null 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 5b9d5f336f..209e301af2 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 @@ -53,10 +53,7 @@ import com.tangem.features.send.impl.presentation.state.fee.* import com.tangem.features.send.impl.presentation.state.recipient.RecipientSendFactory import com.tangem.lib.crypto.BlockchainUtils import com.tangem.utils.Provider -import com.tangem.utils.coroutines.CoroutineDispatcherProvider -import com.tangem.utils.coroutines.DelayedWork -import com.tangem.utils.coroutines.JobHolder -import com.tangem.utils.coroutines.saveIn +import com.tangem.utils.coroutines.* import dagger.hilt.android.lifecycle.HiltViewModel import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.flow.* @@ -412,9 +409,11 @@ internal class SendViewModel @Inject constructor( private fun getUserWallets() { viewModelScope.launch(dispatchers.main) { runCatching { - getWalletsUseCase.invokeSync() - ?.toAvailableWallets() - .orEmpty() + waitForDelay(delay = RECENT_LOAD_DELAY) { + getWalletsUseCase.invokeSync() + ?.toAvailableWallets() + .orEmpty() + } }.onSuccess { result -> userWallets = result uiState = recipientStateFactory.onLoadedWalletsList(wallets = userWallets) @@ -455,10 +454,12 @@ internal class SendViewModel @Inject constructor( } private suspend fun getTxHistory() { - val txHistoryList = getFixedTxHistoryItemsUseCase.getSync( - userWalletId = userWalletId, - currency = cryptoCurrency, - ).getOrElse { emptyList() } + val txHistoryList = waitForDelay(delay = RECENT_LOAD_DELAY) { + getFixedTxHistoryItemsUseCase.getSync( + userWalletId = userWalletId, + currency = cryptoCurrency, + ).getOrElse { emptyList() } + } uiState = recipientStateFactory.onLoadedHistoryList(txHistory = txHistoryList) } @@ -971,6 +972,7 @@ internal class SendViewModel @Inject constructor( private companion object { const val CHECK_FEE_UPDATE_DELAY = 60_000L const val BALANCE_UPDATE_DELAY = 11_000L + const val RECENT_LOAD_DELAY = 500L const val RU_LOCALE = "ru" const val EN_LOCALE = "en"