I am doing ```val isImeVisible = WindowInsets.isIm...
# compose
s
I am doing
Copy code
val isImeVisible = WindowInsets.isImeVisible
LaunchedEffect(Unit) {
  snapshotFlow { isImeVisible }.collectLatest {
    Log.d("Tag", "it:$it")
  }
}
But I am only getting this log to trigger once. As far as I can tell, isImeVisible comes from here which in turn is stored inside AndroidWindowInsets as a MutableState, so I should be getting new emissions on my snapshotFlow due to that, but I am not getting anything. is there something wrong with that mutable state, or could it be on my side? I do have
adjustResize
and
WindowCompat.setDecorFitsSystemWindows(window, false)
fwiw.
f
I guess it's because you are not reading a "state" value. You are reading your locally defined val
isImeVisible
. So the State read happens in your composable and not in the
snapshotFlow
Try making proper state out of it using
rememberUpdatedState
and then read it in your
snapshotFlow
. Something like this:
Copy code
val isImeVisible by rememberUpdatedState(WindowInsets.isImeVisible)
LaunchedEffect(Unit) {
    snapshotFlow { isImeVisible }.collectLatest {
        Log.d("Tag", "it:$it")
    }
}
s
Hmmm damn you are totally right! That’s really interesting! I haven’t encountered this before, since usually I’d just use the state directly inside the snapshotFlow lambda, like here it’d be
snapshotFlow { WindowInsets.isImeVisible }
but the problem in this specific case is that this getter is a composable function and you can’t do that in there. Just tested your suggestion and it works btw, thanks a lot!
a
Or just use
LauchedEffect(isImeVisible) {}
. Using
snapshotFlow
isn't more efficient here, as the change of
WindowInsets.isImeVisible
will trigger a recomposition anyway.
s
Yeah, and this is a result of that getter being a composable so I can’t do it inside the snapshotFlow lambda. I could however create a new composable function which contains all this and returns a State object to contain the recomposition in that block right? And in anyway, this was super important to understand regarding snapshowFlow, I am very happy to have asked this question 😄
a
That can reduce recomposition scope, yes.
183 Views