Michal Klimczak
03/29/2021, 8:01 PMLaunchedEffect(true)
. There's probably some other Effect
that I should use, right?
val pagerState = rememberPagerState(pageCount = pages.size)
LaunchedEffect(true) {
//SharedFlow
pageChangeFlow.collect {
pagerState.animateScrollToPage(it)
}
}
HorizontalPager(state = pagerState)
Dominaezzz
03/29/2021, 8:02 PMLaunchedEffect(Unit)
tbh.Dominaezzz
03/29/2021, 8:03 PMIan Lake
03/29/2021, 8:07 PMLaunchedEffect(pagerChangeFlow)
?cb
03/29/2021, 8:09 PMLaunchedEffect(pagerState)
is what I tend to usecb
03/29/2021, 8:11 PMDominaezzz
03/29/2021, 8:11 PMsnapshotFlow { pagerChangeFlow }.flattenConcat()
if you want to be super extra about it.Michal Klimczak
03/29/2021, 8:13 PMhttps://google.github.io/accompanist/pager/#reacting-to-page-changesButy my code is not indented to find out when page change happens, Instead I want to change page whenever my sharedFlow fires appropriate event.
cb
03/29/2021, 8:14 PMcb
03/29/2021, 8:15 PMsnapshotFlow
the pagerChangeFlow
, it’s not a stateful property. In fact, it uses snapshotFlow
underneathDominaezzz
03/29/2021, 8:17 PMMichal Klimczak
03/29/2021, 8:17 PMpageChangeFlow.collect
will then be tied to the pagerState
value changes. so whenever the page changes (e.g. user flings manually), it will cancel and relaunch the coroutine? Isn't that wasteful?Ian Lake
03/29/2021, 8:20 PMLaunchedEffect(pagerChangeFlow)
means is that if the pagerChangeFlow
object is replaced with a new object, the previous code block is cancelled and a new code block associated with the new flow is started. Using true
or null
would mean that they'd both runIan Lake
03/29/2021, 8:22 PMLaunchedEffect
should always take a key - in just about every case, your LaunchEffect
is operating on something - that something (or somethings) should be the keyMichal Klimczak
03/29/2021, 8:27 PMLaunchedEffect(*pagerState*)
which Chris suggested. which is external to the LaunchedEffect. LaunchedEffect(*pagerChangeFlow*)
is clearer,
But just to be sure I understand - in this case, since pagerChangeFlow is an immutable field of viewmodel, it will never be replaced, so technically the difference between that and true/null/Unit is purely semantic?Ian Lake
03/29/2021, 8:30 PMpagerState
, of which a pageChanges
flow is part of. I suspect this is in fact, your own custom thing, rather than that?cb
03/29/2021, 8:31 PMMichal Klimczak
03/29/2021, 8:31 PMpageChangeFlow
is my own 😄Ian Lake
03/29/2021, 8:33 PMUnit
as your key does not make any practical difference, but I'd certainly strongly suggest getting into the habit. You never know when the immutable Flow of today instead needs to be wrapped in a remember ala https://medium.com/androiddevelopers/a-safer-way-to-collect-flows-from-android-uis-23080b1f8bda#27a9Ian Lake
03/29/2021, 8:33 PMMichal Klimczak
03/29/2021, 8:35 PM