I was currently in the process of implementing Pag...
# android
d
I was currently in the process of implementing Paging 3, for a specific API and don't need local room. Based on selectionState flow which is changed from FE we retrigger with different params the diffrent paging source instance. The issues when I do a state change, the page source is updates and sending new data with loading states working with custom loading adapter, but the issue is while the new data is fetching the old data in the adapter is retained, I want the data to. be empty the moment I change state
ConcatAdapter(header, this, footer)
Copy code
private fun getPagingFlow(
        selected: SState,
        listOfData: List<String>?
    ): Flow<PagingData<TDTO>> {
        return Pager(
            config = PagingConfig(
                pageSize = 10,
                prefetchDistance = 5,
                enablePlaceholders = false,
                initialLoadSize = 20
            ),
            pagingSourceFactory = {
                TPagingSource(
                    folioNumber = when (selected) {
                        SState.ALL -> null
                        is SState.OTHER -> {
                            listOfData?.getOrNull(selected.selection)
                        }
                    }
                )
            }
        ).flow
    }
Copy code
@OptIn(ExperimentalCoroutinesApi::class)
    val transactionsFlow = _selection
        .flatMapLatest { selected ->
            getPagingFlow(selected, listOfFolio)
        }.cachedIn(viewModelScope)
To consume in the FE
Copy code
viewLifecycleOwner.lifecycleScope.launch {
     viewModel.transactionsFlow.collectLatest { pagingData ->
         tPagingAdapter.submitData(pagingData)
     }
}
//aded custom. loader adapter
private fun <T : Any, V : RecyclerView.ViewHolder> PagingDataAdapter<T, V>.withLoadStateAdapters(
        header: LoadStateAdapter<*>,
        footer: LoadStateAdapter<*>
    ): ConcatAdapter {
        addLoadStateListener { loadStates ->
            header.loadState = loadStates.refresh
            footer.loadState = loadStates.append
        }
        return ConcatAdapter(header, this, footer)
    }
not kotlin but kotlin colored 1