https://kotlinlang.org logo
#compose
Title
# compose
v

Vaibhav Jaiswal

03/09/2024, 7:34 AM
I have a
mutableStateOf
for a text field and a
snapshotFlow
version of it in my viewmodel, How can i create a
flow
for typing indicator type of feature Like when there are emissions in the snapshot flow, it should be true and when there are no emission for like 3seconds, it will be false Again snapshot flow got new emissions, and its again true Any idea?
a

Albert Chang

03/09/2024, 8:02 AM
Something like
Copy code
snapshotFlow { textFieldValue }
    .transformLatest {
        emit(true)
        delay(3000)
        emit(false)
    }
    .distinctUntilChanged()
v

Vaibhav Jaiswal

03/09/2024, 8:03 AM
What if user is typing for more than 3 seconds
It will drop the previous values?
a

Albert Chang

03/09/2024, 8:06 AM
https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/transform-latest.html
When the original flow emits a new value, the previous
transform
block is cancelled, thus the name
transformLatest
.
v

Vaibhav Jaiswal

03/09/2024, 8:06 AM
Thankyou
h

Halil Ozercan

03/09/2024, 3:30 PM
One thing to note is that if you are observing a
TextFieldValue
, the flow will also emit when the cursor or selection position changes. If you are only interested in typing, only observe the text contents of the value.
j

Jrichards1408

03/10/2024, 1:10 AM
Try denounce
Denounce