(Code in the thread) What would be the best way to...
# compose
b
(Code in the thread) What would be the best way to avoid re-composition in this case? or rather avoid pager restarts
Copy code
@Composable
internal fun SearchScreen(
    viewModel: SearchViewModel,
) {
    // if state is remembered here, pagers WILL restart
    val viewState by rememberFlowWithLifecycle(viewModel.state).collectAsState(initial = SearchViewState.Empty)
    
    val listState = rememberLazyListState()
    SearchList(viewModel, viewState, listState)
}
@Composable
internal fun SearchList(viewModel: SearchViewModel, viewState: SearchViewState, listState: LazyListState) {
    // if state is remembered here, pagers WILL restart
    //val viewState by rememberFlowWithLifecycle(viewModel.state).collectAsState(initial = SearchViewState.Empty)
    SearchList(
        //viewModel = viewModel,
        viewState = viewState
        // problem: viewState emissions cause these to restart (because SearchList is recomposed due to viewState changing?)
        pager = rememberFlowWithLifecycle(viewModel.paged).collectAsLazyPagingItems(),
        pager2 = rememberFlowWithLifecycle(viewModel.paged2).collectAsLazyPagingItems(), 
        pager3 = rememberFlowWithLifecycle(viewModel.paged3).collectAsLazyPagingItems(), 
        listState = listState
    )
}

@Composable
internal fun SearchList(
    // viewModel: SearchViewModel,
    viewState: SearchViewState,
    pager: LazyPagingItems<Item>,
    pager2: LazyPagingItems<Item>,
    pager3: LazyPagingItems<Item>,
    listState: LazyListState
) {
    // if state is remembered here, pagers WON'T restart
    //val viewState by rememberFlowWithLifecycle(viewModel.state).collectAsState(initial = SearchViewState.Empty)
    val searchFilter = viewState.searchFilter
}
c
You can always remember where your pager is with rememberSaveable
b
@Cicero doesn't
rememberSaveable
require a special Saver? Also why not the normal
remember
?
c
You are correct, I should have made my option clear. I assumed you have some navigation and you would like the user to come back to this place and be were he was before. remember loses its value as you navigate around. (Maybe I’m wrong on this one but I can’t test i) If your intention would be to remember where you are as you interact with the pager in the same navigation state, then yes, I would go with remember
b
I don't know how navigation is related to this, but I tried remembering the pager in lowest composable and viewState updates/recompositions still cause a restart in pagers
c
You are right, I just assumed because in most contexts I work there is always navigation.
But I believe you are right in remembering it inside of search list
b
I am using navigation (SearchScreen is called from navigation) but I don't know how it relates to this problem
c
Well, I didn’t said navigation was related to this issue. I mentioned using rememberSaveable. You questioned it. I excuse myself for not giving a feedback to my choice and explained why I thought about it. And then reinforced that the way to go is to leave the regular remember inside of SearchList 🙂 I hope this is clear now
b
Gotcha. So, I tried with remember (in SearchList) but still pagers restart (because viewState updates cause recomposition where SearchLists)
@Adam Powell Hey, could you take a look at this? I saw another thread related to this (link) and it seems like remembering
Flow<PagingData>
the way you mentioned should have been enough (
rememberFlowWithLifecycle
) for paging data to not restart. Could this be a bug or is this expected behavior?
a
I would need to look more closely at what paging-compose is doing here
b
The flow returned by
rememberFlowWithLifecycle(viewModel.pagedList)
is completing and starting again with the old config.