Last active
March 7, 2019 17:42
-
-
Save Viska97/5359d8e59554a4a920995497c59b0758 to your computer and use it in GitHub Desktop.
PagedList.BoundaryCallback with coroutines
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TestBoundaryCallback( | |
private val pageSize: Int, | |
private val dataSource: BackendDataSource, | |
private val database : AppDatabase | |
) : PagedList.BoundaryCallback<?>(){ | |
var networkState = MutableLiveData<Int>() | |
private var isRequestInProgress = false | |
override fun onZeroItemsLoaded() { | |
loadSomething() | |
} | |
override fun onItemAtEndLoaded(itemAtEnd: Pack) { | |
loadSomething() | |
} | |
fun reload() { | |
loadSomething() | |
} | |
private fun loadSomething() { | |
if (isRequestInProgress) return | |
isRequestInProgress = true | |
GlobalScope.launch(Dispatchers.IO) { | |
//here code to load it and save to database | |
//val result = dataSource.load(pageSize) | |
//database.somethingDao().insert(result.data) | |
isRequestInProgress = false | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment