Skip to content

Instantly share code, notes, and snippets.

@CristianMG
Created June 8, 2019 08:45
Show Gist options
  • Select an option

  • Save CristianMG/72b7fb445e4f32ca175d14065f30a402 to your computer and use it in GitHub Desktop.

Select an option

Save CristianMG/72b7fb445e4f32ca175d14065f30a402 to your computer and use it in GitHub Desktop.
Android paging Network + Database 6 snippet
class MainViewModel(
private val characterRepository: CharacterRepository
) : ViewModel() {
private val listing: LiveData<Listing<CharacterModel>> by lazy {
liveData(characterRepository.getListable())
}
private val boundaryCallback = switchMap(listing) { it.getBoundaryCallback() }!!
val dataSource = switchMap(listing) { it.getDataSource() }!!
val networkState = switchMap(listing) { it.getNetworkState() }!!
/**
* Retry all failed petitions boundary callback
*/
fun retry() {
boundaryCallback.value?.retryPetitions()
}
/**
* Cleared all references and petitions boundary callback
*/
override fun onCleared() {
boundaryCallback.value?.cleared()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment