Skip to content

Instantly share code, notes, and snippets.

@BambangHeriSetiawan
Last active August 14, 2019 17:40
Show Gist options
  • Save BambangHeriSetiawan/1a16813a7e6cc838758732827708d024 to your computer and use it in GitHub Desktop.
Save BambangHeriSetiawan/1a16813a7e6cc838758732827708d024 to your computer and use it in GitHub Desktop.
MovieDataSource
package com.simx.paggingsample.data.paging
import androidx.paging.PageKeyedDataSource
import com.simx.paggingsample.data.ApiRequests
import com.simx.paggingsample.data.discover.ResponseMovies
import kotlinx.coroutines.*
import kotlin.coroutines.CoroutineContext
/**
* Created by simx on 14,August,2019
*/
class MovieDataSource(private var query:String, private var year:Int): PageKeyedDataSource<Int, ResponseMovies.ResultsItem>() {
private var scope: CoroutineScope? = null
private var job: Job? = SupervisorJob()
private val parentJob: CoroutineContext = CoroutineExceptionHandler{ _, throwable ->
run {
job = SupervisorJob()
}
}
init {
scope = CoroutineScope(parentJob)
}
override fun loadInitial(params: LoadInitialParams<Int>, callback: LoadInitialCallback<Int, ResponseMovies.ResultsItem>) {
scope?.launch {
var res = ApiRequests.search(1,year,query)
res.results?.let { callback.onResult(it,null,2) }
}
}
override fun loadAfter(params: LoadParams<Int>, callback: LoadCallback<Int, ResponseMovies.ResultsItem>) {
scope?.launch {
var res = ApiRequests.search(params.key,year,query)
res.results?.let { callback.onResult(it,params.key + 1) }
}
}
override fun loadBefore(params: LoadParams<Int>, callback: LoadCallback<Int, ResponseMovies.ResultsItem>) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment