Is there a way to report a value change in the exa...
# compose
d
Is there a way to report a value change in the example below assuming I have no way to modify
State
?
Copy code
class State(
  val value by mutableStateOf(false)
)

fun Component(state: State, reportValueChange: (Boolean) -> Unit)) {
  val v = state.value 
  // how to call reportValueChange(v) here
  // **only** when "v" changes?
}
I have tried
snapshotFlow { state.value }.collect(::reportValueChange)
but it emits nothing on value changes
a
Generally you want to avoid using composition as a change event dispatcher. Why is
Component
the thing responsible for telling the caller when
state
changes, when the caller is the one providing
state
to
Component
?
How have you tested your
snapshotFlow
?
d
My component uses the Swipeable modifier. When swipe finishes and settles I want to notify other parts of code about this. usecases: orchestration of some further ui changes or analytics. And
SwipeableState
has
isAnimating: Boolean
and that's how I plan to detect that swipe has finished: when
isAnimating
changes from true to false. There's a
confirmStateChange
labmbda, but it's invoked too soon, prior to swipe start.
As for snapshotFlow I have tried (inside the launched effect)
Copy code
snapshotFlow { swipeableState }.collect { log(it.isAnimating) }
// and
snapshotFlow { swipeableState.isAnimating }.collect { log(it) }
but the both emit the single value only
I'll ask a more specific question in a separate thread
z
Can you share more code around where you're calling collect? That looks right to me, and I thought I'd even written similar code in the past and it worked.
d
Copy code
val swipeableState = rememberSwipeableState(initialValue = findInitialAnchor())
Timber.e("is animating ${swipeableState.isAnimationRunning}")
val flow = snapshotFlow { swipeableState.isAnimationRunning }
LaunchedEffect(Unit) {
  flow.collect {
    Timber.e("animated changed to ${it}, state now is ${swipeableState.currentValue}")
  }
}
It's something like this.
a
this test passes:
Copy code
@Test
fun snapshotFlowTest(): Unit = runBlocking {
    class StateClass {
        var value by mutableStateOf(false)
    }

    suspend fun <T> Channel<T>.receiveWithTimeout(): T = withTimeout(1000) { receive() }

    val instance = StateClass()

    val ch = Channel<Boolean>(Channel.BUFFERED)
    val job = launch(start = CoroutineStart.UNDISPATCHED) {
        snapshotFlow { instance.value }.collect { ch.send(it) }
    }

    assertFalse("initial value", ch.receiveWithTimeout())

    Snapshot.withMutableSnapshot {
        instance.value = true
    }

    assertTrue("after change 1", ch.receiveWithTimeout())

    Snapshot.withMutableSnapshot {
        instance.value = false
    }

    assertFalse("after change 2", ch.receiveWithTimeout())

    job.cancel()
}
so yes, I'm also curious what's different
d
As I swipe my above example prints a lot of changes from
true
to
false
for the first Timber call and only one
false
for the second
hmm. maybe I should try to come up with a more minimal example then UPD: My code was wrong, Zach helped me in another thread. I didn't provide a key to the effect and was recreating flow on each recompose.
c
@dimsuz Hey. Quick question. So you mean if you don't pass a key into `LaunchedEffect(key)`except for
Unit
or `true`it will call the code inside your launchedEffect on every recomposition? I thought until you changed your key, it would keep the same LaunchedEffect?
d
Oops, I wasn't clear enough above. What I meant was that I did 2 things wrong: 1. provided a wrong key (Unit) 2. created the flow outside of the effect body.
LaunchedEffect
body will be called as you've said: on each key change, not on each recomposition. If you provide
Unit
or (literal)
true
it will be called only once.
c
Awesome. That's what I was thinking. Thanks for confirming!
👍 1
z
I could not repro the behavior you had initially either, i’m getting all the animation state updates from my
snapshotFlow
even if I create it outside the effect, don’t remember it, and pass
Unit
as the effect key.
d
Hmmm. I may have had another key mixed into
LaunchedEffect
at the time I had this problem: I was trying different things all at once. If I'll manage to catch this again, will try a minimal example and post here. Thanks for looking into this, after you hints everything works fine for me!