Created
August 3, 2018 07:17
-
-
Save sharmadhiraj/a76a9b6c115066bee11f006cb90289a8 to your computer and use it in GitHub Desktop.
Android Paging Library
This file contains 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 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