I have this code, which makes an api call But it d...
# compose
v
I have this code, which makes an api call But it does not get called, neither breakpoints are triggered nor anything is being logged
Copy code
LaunchedEffect(listState){
        snapshotFlow { listState.layoutInfo }
            .map { it.visibleItemsInfo }
            .map { items -> items.map { news[it.index] } }
            .onEach { it.log("Recording Viewed") }
            .map { it.filterNotNull() }
            .safeCatch()
            .collect { it.onEach(component::recordViewed) }
    }
here news is a
LazyPagingItems<T>
Sometimes it is logged but with emptyList
z
That looks fine to me. Can you post more surrounding code?
v
This is my whole component @Zach Klippenstein (he/him) [MOD]
Copy code
@Composable
fun NewsTab(
    component: NewsTabComponent,
    bottomNavVisibility:(Boolean) -> Unit
) {
    val news = component.newsFeed.collectAsLazyPagingItems()
    val listState = rememberLazyListState()

    LaunchedEffect(component){
        component.onTabReselected
            .onEach { listState.fastScrollToTop() }
            .safeCatch()
            .launchIn(this)
    }

    val isScrollingUp = listState.isScrollingUp()
    bottomNavVisibility(isScrollingUp)

    BaseScreenContent(
        component = component,
        onRefresh = { component.withSwipeRefresh { news.refresh() }},
    ){
        MedialLazyColumn(
            modifier = Modifier.fillMaxSize(),
            state = listState,
            loadState = news.loadState.mediator,
            items = news,
            contentType = { this.type },
            loader = remember { { item { Loader(Modifier.fillParentMaxSize()) } } }
        ) {
            when (it) {
                is NewsMedia.News -> NewsCard(it, Modifier.fillMaxWidth(), onAction = component::handleAction)
                is NewsMedia.Poll -> PollCard(it, Modifier.fillMaxWidth(), onAction = component::handleAction)
                is NewsMedia.Quiz -> QuizCard(it, Modifier.fillMaxWidth(), onAction = component::handleAction)
            }

        }
    }

    LaunchedEffect(listState){
        snapshotFlow { listState.layoutInfo }
            .map { it.visibleItemsInfo }
            .map { items -> items.map { news.peek(it.index) } }
            .onEach { it.log("Recording Viewed") }
            .map { it.filterNotNull() }
            .safeCatch()
            .collect { it.onEach(component::recordViewed) }
    }

}
z
Yea that looks fine. I’m not sure why nothing is getting logged. What if you put a log statement inside the
snapshotFlow
lambda itself and spit out something about the visible items, are you seeing anything then?
v
Nothing is being logged, breakpoint is not triggered
@Zach Klippenstein (he/him) [MOD] this seems to get called only the initial time, the list is drawn on screen and not again when list is being scrolled
Changing it to this fixed it
Copy code
LaunchedEffect(listState){
        snapshotFlow { listState.isScrollInProgress }
            .filterNot { it }
            .map { listState.layoutInfo.visibleItemsInfo.map { news.peek(it.index) } }
            .map { it.filterNotNull() }
            .safeCatch()
            .onEach { it.onEach(component::recordViewed) }
            .launchIn(this)
    }
z
what is safeCatch? Is that catching some exception that is cancelling the flow?
v
It catches all exceptions except CancellationException
z
So, was it catching some exception that was terminating the flow early?
v
No, I added a breakpoint there Did not hit When i made the change I sent above, it starting working perfectly