-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathFruitPagingSource.kt
39 lines (33 loc) · 1.34 KB
/
FruitPagingSource.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package com.ocnyang.recyclerviewevent.paging3
import androidx.paging.PagingSource
import androidx.paging.PagingState
import java.lang.Exception
class FruitPagingSource(
private val repository: FruitRepository,
private val queryFruitName: String
) : PagingSource<Int, PagingFruitBean>() {
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, PagingFruitBean> {
return try {
var nextPageNumber = params.key ?: 1
val baseBean = repository.requestFruitList(queryFruitName, nextPageNumber)
if (baseBean.isSuccess) {
val currentPage = baseBean.data.currentPage
val prevPageNumber = if (currentPage > 1) currentPage - 1 else null
val nextPageNumber = if (currentPage < baseBean.data.totalPage) currentPage + 1 else null
LoadResult.Page<Int, PagingFruitBean>(
data = baseBean.data.fruits,
prevKey = prevPageNumber,
nextKey = nextPageNumber
)
} else {
LoadResult.Error(Exception())
}
} catch (e: Exception) {
LoadResult.Error(e)
}
}
override fun getRefreshKey(state: PagingState<Int, PagingFruitBean>): Int? {
// TODO("Not yet implemented")
return 1
}
}