Answer the question
In order to leave comments, you need to log in
How to architecturally manage pagination?
Guys. Help with pagination.
The fragment has a Toolbar and a RecyclerView.
There is a SearchView on the toolbar.
On the QueryTextSubmit() event, I have to "reinitialize" the pagination
in such a way that it would completely clear the current data and
give query parameters to the DataSource to load new data.
Pagination is implemented completely according to this example:
https://medium.com/@sharmadhiraj.np/android-paging...
What is the best way to do this so that there are no conflicts
with the life cycles of the fragment and its activity?
My first thought is to move the body of the init {} block into a separate method,
say in public fun initPagination(searchRequest: SearchRequest? = null)
Inside this method, pass SearchRequest to the DataSourceFactory constructor and then
work with it in DataSource.loadInitial() / DataSource.loadAfter()
But this option seems to me a crutch.
class ImagesViewModel : ViewModel() {
private val imageService = API.getApi().create(ImageService::class.java)
var imageList: LiveData<PagedList<ImageItem>>
private val compositeDisposable = CompositeDisposable()
private val pageSize = 5
private var imageDataSourceFactory: ImageDataSourceFactory
init {
imageDataSourceFactory = ImageDataSourceFactory(compositeDisposable, imageService)
val config = PagedList.Config.Builder()
.setPageSize(pageSize)
.setInitialLoadSizeHint(pageSize * 2)
.setEnablePlaceholders(false)
.build()
imageList = LivePagedListBuilder(imageDataSourceFactory, config).build()
}
fun getState(): LiveData<State> = Transformations.switchMap<ImageDataSource,
State>(imageDataSourceFactory.imageDataSourceLiveData, ImageDataSource::state)
fun retry() {
imageDataSourceFactory.imageDataSourceLiveData.value?.retry()
}
fun listIsEmpty(): Boolean {
return imageList.value?.isEmpty() ?: true
}
override fun onCleared() {
super.onCleared()
compositeDisposable.dispose()
}
}
Answer the question
In order to leave comments, you need to log in
PagedListAdapter, seriously? Google once again gave birth to some kind of crap. It seems that the creators of RecyclerView did not tell the author of this lib that the adapter has nothing to do with receiving data (which, of course, is pagination).
In short, as it should. The data should arrive in the adapter as a list and it should display them. Somewhere in the view layer, there should be a subscription to the recycler's scroll events and a determination of whether it has scrolled to the end. When scrolled, you need to notify the presentation layer about this, which will ask the model for more data. The model will pull the data, give the presentation, the data will be converted and given to the view layer, which will pass it back to the adapter, which will show it.
End.
No lib that touches the adapter and generally hangs a bunch of entities is needed.
PS article uses Rx as a callback framework. Divine.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question