Say I have a Flow of Booleans and only want to emi...
# flow
j
Say I have a Flow of Booleans and only want to emit when they flip, what is the proper way to do that? What am I looking for?
Copy code
suspend fun Flow<Boolean>.flippedTo(value: Boolean, block: () -> Unit) {
    var previous: Boolean? = null
    collect { current ->
        if (previous == !value && current == value) {
            block()
        }
        previous = current
    }
}
boolFlow.flippedTo(false) {
    println("Flipped from true to false")
}
boolFlow.flippedTo(true) {
    println("Flipped from false to true")
}
j
StateFlow is not enough?
b
Maybe try
distinctUntilChanged
? Or that ☝️
1