-
Notifications
You must be signed in to change notification settings - Fork 0
/
AnimalDataSource.kt
49 lines (44 loc) · 1.63 KB
/
AnimalDataSource.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
40
41
42
43
44
45
46
47
48
49
package beepbeep.learningpaginglibrary
import android.arch.paging.PageKeyedDataSource
import beepbeep.pixelsforreddit.Animal
class AnimalDataSource : PageKeyedDataSource<Int, Animal>() {
val firstPage = 0
val animalList = listOf(
Animal("dog-1"),
Animal("dog-2"),
Animal("dog-3"),
Animal("dog-4"),
Animal("dog-5"),
Animal("dog-6"),
Animal("dog-7"),
Animal("dog-8"),
Animal("dog-9"),
Animal("dog-10"),
Animal("dog-11"),
Animal("dog-12"),
Animal("dog-13"),
Animal("dog-14"),
Animal("dog-15"),
Animal("dog-16"),
Animal("dog-17"),
Animal("dog-18"),
Animal("dog-19"),
Animal("dog-20"),
Animal("dog-21"),
Animal("dog-22"),
Animal("dog-23"))
override fun loadInitial(params: LoadInitialParams<Int>, callback: LoadInitialCallback<Int, Animal>) {
val initialData = animalList.subList(0, params.requestedLoadSize)
val pageBefore = firstPage - 1
val pageAfter = firstPage + 1
callback.onResult(initialData, pageBefore, pageAfter)
}
override fun loadAfter(params: LoadParams<Int>, callback: LoadCallback<Int, Animal>) {
val start = params.key * params.requestedLoadSize
val afterData = animalList.subList(start, start + params.requestedLoadSize)
callback.onResult(afterData, params.key + 1)
}
override fun loadBefore(params: LoadParams<Int>, callback: LoadCallback<Int, Animal>) {
// do nothing
}
}