I am looking for an efficient way to trigger a callback for each item of a LazyColumn
as they become visible, but only once.
- The callback should happen only once as items become visible. It should not trigger if the user scrolls past the same item several times.
- The callback should only happen once per each item.
Is there a way Compose-y way of handling this?
I tried to use snapshotFlow
as below, but no matter which side effect I use, it gets triggered over and over as a user scrolls.
val listState = rememberLazyListState()
LaunchedEffect(listState) {
snapshotFlow { listState.layoutInfo.visibleItemsInfo}
.map { it.first() }
.collect {
MyAnalyticsService.someVisibleItemCallback()
}
}
Another way I can image is baking this into the model state as follows.
data class SomeObject(
val someStuff: SomeStuff,
val isSeen: Boolean = false
)
How can I handle this in an efficient way?