Updated on 2026-08-14

This commit is contained in:
Tangem 2022-04-22 23:21:12 +03:00
parent f784d9b33f
commit 4133bf4ab9
3 changed files with 17 additions and 11 deletions

View file

@ -11,7 +11,7 @@ import com.tangem.domain.common.util.ValueDebouncer
@Composable
fun <T> valueDebouncerAsState(
initialValue: T,
debounce: Long = 400,
debounce: Long = 600,
onEmitValueReceived: (T) -> Unit = {},
onValueChanged: (T) -> Unit,
): ValueDebouncer<T> {

View file

@ -89,7 +89,7 @@ private fun OutlinedProgressTextField(
onTextChanged: (String) -> Unit,
) {
val logger = remember {
CompositionLogger(label, "OutlinedProgressTextField", listOf("Адрес контракта"))
CompositionLogger(label, "OutlinedProgressTextField", listOf("Символ токена"))
}
logger.nextComposition()
@ -121,7 +121,7 @@ private fun OutlinedProgressTextField(
logger.log("$isNotUserInput: внешние данные ОДИНАКОВЫ с данными в поле")
} else {
logger.log("$isNotUserInput: внешние данные РАЗЛИЧАЮТСЯ с данными в поле")
if (textDebouncer.emittedValue != textDebouncer.debounced) {
if ((textDebouncer.emittedValue != textDebouncer.debounced) || textDebouncer.emitsCountBeforeDebounce > 0) {
logger.log("$isNotUserInput: пользователь ВВОДИТ данные -> внешние данные игнорируем, ждем RECOMPOSE")
} else {
logger.log("$isNotUserInput: пользователь НЕ вводит данные -> пытаемся обработать внешние данные")
@ -135,7 +135,7 @@ private fun OutlinedProgressTextField(
textValueState.value = fieldData.value
}
} else {
logger.log("$isNotUserInput: UNKNOWN -> start RECOMPOSE by new value for textValueState.value = [${fieldData.value}]")
logger.log("$isNotUserInput: в пустое поле вставляются данные -> start RECOMPOSE новые данные для textValueState.value = [${fieldData.value}]")
textValueState.value = fieldData.value
}
}

View file

@ -1,10 +1,10 @@
package com.tangem.domain.common.util
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.launch
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.debounce
import kotlinx.coroutines.flow.onEach
/**
[REDACTED_AUTHOR]
@ -18,6 +18,8 @@ class ValueDebouncer<T>(
var emittedValue: T = initialValue
private set
var emitsCountBeforeDebounce: Int = 0
private set
var debounced: T = initialValue
private set
@ -26,11 +28,14 @@ class ValueDebouncer<T>(
init {
debounceScope.launch {
flow.filter { if (debounced == null) true else debounced != it }
.debounce(debounceDuration)
flow.debounce(debounceDuration)
.onEach {
debounced = it
onValueChanged(it)
debounceScope.launch {
delay(500)
emitsCountBeforeDebounce = 0
}
}
.collect()
}
@ -39,6 +44,7 @@ class ValueDebouncer<T>(
fun isDebounced(value: T): Boolean = this.debounced == value
fun emmit(emmitValue: T) {
emitsCountBeforeDebounce++
emittedValue = emmitValue
onEmitValueReceived.invoke(emmitValue)
debounceScope.launch { flow.emit(emmitValue) }