Updated on 2026-08-14

This commit is contained in:
Tangem 2022-04-22 18:13:05 +03:00
parent 79f35fe41e
commit eec0a5ccb8
9 changed files with 185 additions and 102 deletions

View file

@ -38,7 +38,7 @@ interface Field<T> {
data class Data<Data>(
val value: Data,
val isUserInput: Boolean = true
val isUserInput: Boolean
)
}

View file

@ -10,31 +10,37 @@ import kotlinx.coroutines.launch
[REDACTED_AUTHOR]
*/
class ValueDebouncer<T>(
private val debounce: Long = 400,
private val onValueChanged: (T?) -> Unit
private val initialValue: T,
private val debounceDuration: Long = 400,
private val onValueChanged: (T) -> Unit,
private val onEmitValueReceived: (T) -> Unit = {},
) {
private var value: T? = null
private val debounceScope = CoroutineScope(Job() + Dispatchers.Main)
private val flow = MutableStateFlow(value)
var emittedValue: T = initialValue
private set
var debounced: T = initialValue
private set
private val debounceScope: CoroutineScope = CoroutineScope(Job() + Dispatchers.Main)
private val flow = MutableStateFlow(debounced)
init {
initFlow()
}
private fun initFlow() {
debounceScope.launch {
flow.filter { if (value == null) true else value != it }
.debounce(debounce)
flow.filter { if (debounced == null) true else debounced != it }
.debounce(debounceDuration)
.onEach {
value = it
debounced = it
onValueChanged(it)
}
.collect()
}
}
fun isDebounced(value: T): Boolean = this.debounced == value
fun emmit(emmitValue: T) {
emittedValue = emmitValue
onEmitValueReceived.invoke(emmitValue)
debounceScope.launch { flow.emit(emmitValue) }
}
}

View file

@ -19,14 +19,14 @@ enum class CustomTokenFieldId : FieldId {
data class TokenField(
override val id: FieldId,
) : BaseDataField<String>(id, Field.Data(""))
) : BaseDataField<String>(id, Field.Data("", false))
data class TokenBlockchainField(
override val id: FieldId,
val itemList: List<Blockchain>,
) : BaseDataField<Blockchain>(id, Field.Data(Blockchain.Unknown))
) : BaseDataField<Blockchain>(id, Field.Data(Blockchain.Unknown, false))
data class TokenDerivationPathField(
override val id: FieldId,
val itemList: List<Blockchain>,
) : BaseDataField<Blockchain>(id, Field.Data(Blockchain.Unknown))
) : BaseDataField<Blockchain>(id, Field.Data(Blockchain.Unknown, false))