Skip to content

Instantly share code, notes, and snippets.

@sharmadhiraj
Created August 3, 2018 07:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sharmadhiraj/a76a9b6c115066bee11f006cb90289a8 to your computer and use it in GitHub Desktop.
Save sharmadhiraj/a76a9b6c115066bee11f006cb90289a8 to your computer and use it in GitHub Desktop.
Android Paging Library
class NewsListViewModel : ViewModel() {
private val networkService = NetworkService.getService()
var newsList: LiveData<PagedList<News>>
private val compositeDisposable = CompositeDisposable()
private val pageSize = 5
private val newsDataSourceFactory: NewsDataSourceFactory
init {
newsDataSourceFactory = NewsDataSourceFactory(compositeDisposable, networkService)
val config = PagedList.Config.Builder()
.setPageSize(pageSize)
.setInitialLoadSizeHint(pageSize * 2)
.setEnablePlaceholders(false)
.build()
newsList = LivePagedListBuilder<Int, News>(newsDataSourceFactory, config).build()
}
fun getState(): LiveData<State> = Transformations.switchMap<NewsDataSource,
State>(newsDataSourceFactory.newsDataSourceLiveData, NewsDataSource::state)
fun retry() {
newsDataSourceFactory.newsDataSourceLiveData.value?.retry()
}
fun listIsEmpty(): Boolean {
return newsList.value?.isEmpty() ?: true
}
override fun onCleared() {
super.onCleared()
compositeDisposable.dispose()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment