0

Dao class

@Dao
interface ShlokaDao {


    @Query("SELECT * FROM shlokas_table ORDER BY _Id ASC LIMIT :limit OFFSET :offset ")
    suspend fun getPagedShlokasList(limit: Int, offset: Int): List<Shloka>

}

Paging DataSource

class ShlokasDataSource(private val shlokasRepository: ShlokasRepository): PagingSource<Int, Shloka>() {


    override suspend fun load(params: LoadParams<Int>): LoadResult<Int, Shloka> {
        val page = params.key ?: 0

        return try {
            Log.d("MainPagingSource", "load: $page")
            val entities = shlokasRepository.getPagedShlokasList( params.loadSize, page * params.loadSize)
            if (page != 0) delay(1000)
            LoadResult.Page(
                data = entities,
                prevKey = if (page == 0) null else page - 1,
                nextKey = if (entities.isEmpty()) null else page + 1
            )
        } catch (e: Exception) {
            LoadResult.Error(e)
        }
    }

    override fun getRefreshKey(state: PagingState<Int, Shloka>): Int? {
        return state.anchorPosition?.let { anchorPosition ->
            val anchorPage = state.closestPageToPosition(anchorPosition)
            anchorPage?.prevKey?.plus(1) ?: anchorPage?.nextKey?.minus(1)
        }
    }

}

ViewModel class

class ShlokasViewModel(private val shlokasRepository: ShlokasRepository): ViewModel() {


    val data: Flow<PagingData<Shloka>> = Pager(
        PagingConfig(
            pageSize = 20,
            enablePlaceholders = false,
        )
    ) {
        ShlokasDataSource(shlokasRepository)

    }.flow.cachedIn(viewModelScope)

}

ShlokasScreen

@Composable
fun ShlokasScreen(
    canNavigateBack: Boolean,
    navigateUp: () -> Unit,
    onSearchBtnClick: () -> Unit,
    onShlokasClick: (Int) -> Unit,
    viewModel: ShlokasViewModel = viewModel(factory = AppViewModelProvider.Factory)
) {


    val shlokasData = viewModel.data.collectAsLazyPagingItems()

    Scaffold(

        contentPadding = PaddingValues(16.dp),
        topBar = {
            ShlokasAppBar(
                canNavigateBack = canNavigateBack,
                navigateUp = navigateUp,
                onSearchClicked = onSearchBtnClick
            )

        }

    ) { innerPadding ->


        LazyColumn(
            modifier = Modifier.fillMaxSize(),
            contentPadding = innerPadding
        ) {

            items(
                items = shlokasData.itemSnapshotList,
                key = { it!!._Id }
            ) {shlokas->


                shlokas?.let {
                    ShlokaCard(
                        shloka = shlokas,
                        onClick = { onShlokasClick(it._Id)
                        }
                    )
                }


                shlokasData.apply {


                    when (loadState.append) {
                        is LoadState.NotLoading -> Unit
                        LoadState.Loading -> {


                            LoadingItem()

                        }
                        is LoadState.Error -> {

                            ErrorItem(message = "Some error occurred")

                        }
                    }

                    when (loadState.refresh) {
                        is LoadState.NotLoading -> Unit
                        LoadState.Loading -> {

                            Box(
                                modifier = Modifier.fillMaxSize(),
                                contentAlignment = Center
                            ) {
                                CircularProgressIndicator()
                            }


                        }
                        is LoadState.Error -> TODO()
                    }

                }


            }
        }
    }

}

Room loading data in 0th page

i have more than 80+ words in pre-populated room database but it is only showing 60 words in 0th page.I tried to load data from pre-populated database but it is not paginating data as expected.

I followed this tutorials https://www.simplifiedcoding.net/pagination-in-jetpack-compose/ https://genicsblog.com/gouravkhunger/pagination-in-android-room-database-using-the-paging-3-library

Premjit
  • 37
  • 8

0 Answers0