Colton Idle
03/30/2022, 6:41 PMinit { myMutableState.collect { refresh() } }
Casey Brooks
03/30/2022, 6:42 PMsnapshotFlow { }
should do the trick https://developer.android.com/jetpack/compose/side-effects#snapshotFlowColton Idle
03/30/2022, 6:46 PMColton Idle
03/30/2022, 6:47 PMCasey Brooks
03/30/2022, 6:59 PMsnapshotFlow
just creates a normal Flow
which can be collected anywhere. It requires Compose to be running for it to emit values (well, really, just the global Snapshot), but it doesnât have to be collected within a LaunchedEffect
. You can see examples of it being used in non-Compose classes in the Compose stdlib, such as in SwipeableState
theapache64
03/30/2022, 6:59 PMState
? VM? or its being exposed via another source ?Colton Idle
03/30/2022, 7:05 PMtheapache64
03/30/2022, 7:08 PMcollect
?Casey Brooks
03/30/2022, 8:12 PMsnapshotFlow
when the UI is not present; it wonât ever emit values. But if you expose a Flow
, then you can easily unit-test collecting that flow without having that dependency on the UI stuffCasey Brooks
03/30/2022, 8:12 PMLiveData
from a Service/Repository layer, because it is also tied specifically to UIColton Idle
03/30/2022, 8:40 PMColton Idle
03/30/2022, 8:40 PMAlex Vanyo
03/30/2022, 8:56 PMsnapshotFlow
is exactly the tool youâre looking for to kick off ongoing suspending work in response to changing snapshot state outside of composition.
Just to make a comment against âtied to Composeâ: Using snapshot state in a data layer object isnât inherently wrong, itâs a tool like anything else. There are times where `Flow`s are more appropriate, but I wouldnât be so quick to say that snapshot state doesnât belong anywhere except in the UI.
Adam went into some more detail about this here as well: https://kotlinlang.slack.com/archives/CJLTWPH7S/p1623337962396700?thread_ts=1623313858.334200&cid=CJLTWPH7SColton Idle
03/31/2022, 2:40 AMsnapshotFlow
?
init { myMutableState.collect { refresh() } }
Alex Vanyo
03/31/2022, 3:32 AMinit { snapshotFlow { myMutableState }.collect { refresh() } }
Colton Idle
04/02/2022, 3:22 PM