Paweł Cyroń
11/09/2022, 10:58 PMcurrentPage
is awful and get triggered way to often so I have made my own implementation. yet LaunchedEffect are called too often (not only when value is changed). I’m suspecting ViewPager issue in compose. Code in threadPaweł Cyroń
11/09/2022, 11:00 PM@Composable
private fun VideoReelPager(action: (VideoReelAction) -> Unit, videos: List<VideoReelEntity>) {
val pagerState = rememberPagerState()
val currentPageInPager = remember { mutableStateOf(0) }
LaunchedEffect(pagerState.currentPageOffset) {
if (pagerState.currentPageOffset.absoluteValue < 0.1 && currentPageInPager.value != pagerState.currentPage) {
Timber.e("Reel set page ${pagerState.currentPage}")
currentPageInPager.value = pagerState.currentPage
}
}
Box() {
VerticalPager(
count = videos.size,
state = pagerState,
modifier = Modifier.fillMaxSize()
) { pageIndex ->
VideoReelPage(
item = videos[pageIndex],
positionInPager = pageIndex,
currentIndexInPager = currentPageInPager,
pagerState = pagerState
)
}
}
}
}
Then a Page itself
@Composable
fun PagerScope.VideoReelPage(
item: VideoReelEntity,
positionInPager: Int,
currentIndexInPager: MutableState<Int>,
pagerState: PagerState
) {
LaunchedEffect(currentIndexInPager.value) {
//This is called often, even when no new value on currentIndexInPager, small fling and it's called
}
}
Paweł Cyroń
11/09/2022, 11:00 PMDaniele Segato
11/09/2022, 11:17 PMList
? this is considered unstable by compose by default. You can use kotlin ImmutableList
library to check if this is the casePaweł Cyroń
11/10/2022, 7:55 AMPaweł Cyroń
11/10/2022, 12:28 PMPaweł Cyroń
11/10/2022, 12:28 PMDaniele Segato
11/10/2022, 12:29 PMDaniele Segato
11/10/2022, 12:42 PMList
<--- the interface is considered UNSTABLE. So any composable receiving and using List will be considered unskippable.
Furthermore you might want to use viewpager keys for each page