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")
}