Thread
#compose
    b

    Berkeli Alashov

    1 year ago
    (Code in the thread) What would be the best way to avoid re-composition in this case? or rather avoid pager restarts
    @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
    }
    Cicero

    Cicero

    1 year ago
    You can always remember where your pager is with rememberSaveable
    b

    Berkeli Alashov

    1 year ago
    @Cicero doesn't
    rememberSaveable
    require a special Saver? Also why not the normal
    remember
    ?
    Cicero

    Cicero

    1 year ago
    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

    Berkeli Alashov

    1 year ago
    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
    Cicero

    Cicero

    1 year ago
    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

    Berkeli Alashov

    1 year ago
    I am using navigation (SearchScreen is called from navigation) but I don't know how it relates to this problem
    Cicero

    Cicero

    1 year ago
    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

    Berkeli Alashov

    1 year ago
    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?
    Adam Powell

    Adam Powell

    1 year ago
    I would need to look more closely at what paging-compose is doing here
    b

    Berkeli Alashov

    1 year ago
    The flow returned by
    rememberFlowWithLifecycle(viewModel.pagedList)
    is completing and starting again with the old config.