Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import retrofit2.http.Query
interface APIService {

@GET("api/users")
suspend fun getListData(@Query("page") pageNumber: Int): Response<ApiResponse>
suspend fun getListData(@Query("page") pageNumber: Int): ApiResponse

companion object {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,32 @@ package com.mindorks.example.paging3.data.datasource
import androidx.paging.PagingSource
import com.mindorks.example.paging3.data.APIService
import com.mindorks.example.paging3.data.response.Data
import java.io.IOException

class PostDataSource(private val apiService: APIService) : PagingSource<Int, Data>() {

private val DEFAULT_PAGE_INDEX= 1

override suspend fun load(params: LoadParams<Int>): LoadResult<Int, Data> {
try {
val currentLoadingPageKey = params.key ?: 1
val currentLoadingPageKey = params.key ?: DEFAULT_PAGE_INDEX
return try {
val response = apiService.getListData(currentLoadingPageKey)
val responseData = mutableListOf<Data>()
val data = response.body()?.myData ?: emptyList()
val data = response.myData ?: emptyList()
responseData.addAll(data)

val prevKey = if (currentLoadingPageKey == 1) null else currentLoadingPageKey - 1
val prevKey = if (currentLoadingPageKey == DEFAULT_PAGE_INDEX) null else currentLoadingPageKey - 1
val nextKey = if(responseData.isEmpty()) null else currentLoadingPageKey + 1

return LoadResult.Page(
data = responseData,
prevKey = prevKey,
nextKey = currentLoadingPageKey.plus(1)
nextKey = nextKey
)
} catch (e: Exception) {
return LoadResult.Error(e)
LoadResult.Error(e)
} catch (e: IOException) {
LoadResult.Error(e)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import com.squareup.moshi.Json

data class ApiResponse(
@Json(name = "ad")
val ad: Ad,
val ad: Ad?,
@Json(name = "data")
val myData: List<Data>,
val myData: List<Data>?,
@Json(name = "page")
val page: Int,
val page: Int?,
@Json(name = "per_page")
val per_page: Int,
val per_page: Int?,
@Json(name = "total")
val total: Int,
val total: Int?,
@Json(name = "total_pages")
val total_pages: Int
val total_pages: Int?
)