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 59452dbf7a..872aeb1c64 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 @@ -8,6 +8,8 @@ import kotlinx.coroutines.delay import kotlinx.coroutines.isActive import kotlinx.coroutines.launch import kotlinx.coroutines.withContext +import kotlin.coroutines.CoroutineContext +import kotlin.coroutines.EmptyCoroutineContext suspend fun runCatching(dispatcher: CoroutineDispatcher, block: suspend () -> R): Result { return runCatching { @@ -25,11 +27,20 @@ class Debouncer { private var debounceJob: Job? = null - fun debounce(waitMs: Long = 300L, coroutineScope: CoroutineScope, destinationFunction: () -> Unit) { + fun debounce( + coroutineScope: CoroutineScope, + waitMs: Long = 300L, + context: CoroutineContext = EmptyCoroutineContext, + destinationFunction: suspend () -> Unit, + ) { debounceJob?.cancel() - debounceJob = coroutineScope.launch { + debounceJob = coroutineScope.launch(context) { delay(waitMs) destinationFunction.invoke() } } + + fun release() { + debounceJob?.cancel() + } } \ No newline at end of file diff --git a/features/swap/presentation/src/main/java/com/tangem/feature/swap/viewmodels/SwapViewModel.kt b/features/swap/presentation/src/main/java/com/tangem/feature/swap/viewmodels/SwapViewModel.kt index 1c1ea1538f..ffd683f981 100644 --- a/features/swap/presentation/src/main/java/com/tangem/feature/swap/viewmodels/SwapViewModel.kt +++ b/features/swap/presentation/src/main/java/com/tangem/feature/swap/viewmodels/SwapViewModel.kt @@ -359,7 +359,7 @@ internal class SwapViewModel @Inject constructor( lastAmount.value = cutValue uiState = stateBuilder.updateSwapAmount(uiState, inputNumberFormatter.formatWithThousands(cutValue, decimals)) - amountDebouncer.debounce(DEBOUNCE_AMOUNT_DELAY, viewModelScope) { + amountDebouncer.debounce(viewModelScope, DEBOUNCE_AMOUNT_DELAY) { startLoadingQuotes(fromToken, toToken, lastAmount.value) } }