@@ -249,11 +249,11 @@ The `FirestorePagingAdapter` binds a `Query` to a `RecyclerView` by loading docu
249249This results in a time and memory efficient binding, however it gives up the real-time events
250250afforded by the ` FirestoreRecyclerAdapter ` .
251251
252- The ` FirestorePagingAdapter ` is built on top of the [ Android Paging Support Library] [ paging-support ] .
253- Before using the adapter in your application, you must add a dependency on the support library:
252+ The ` FirestorePagingAdapter ` is built on top of the [ Android Paging 3 Library] [ paging-support ] .
253+ Before using the adapter in your application, you must add a dependency on that library:
254254
255255``` groovy
256- implementation 'androidx.paging:paging-runtime:2 .x.x'
256+ implementation 'androidx.paging:paging-runtime:3 .x.x'
257257```
258258
259259First, configure the adapter by building ` FirestorePagingOptions ` . Since the paging adapter
@@ -262,16 +262,13 @@ an adapter that loads a generic `Item`:
262262
263263``` java
264264// The "base query" is a query with no startAt/endAt/limit clauses that the adapter can use
265- // to form smaller queries for each page. It should only include where() and orderBy() clauses
265+ // to form smaller queries for each page. It should only include where() and orderBy() clauses
266266Query baseQuery = mItemsCollection. orderBy(" value" , Query . Direction . ASCENDING );
267267
268- // This configuration comes from the Paging Support Library
269- // https://developer.android.com/reference/androidx/paging/PagedList.Config
270- PagedList . Config config = new PagedList .Config .Builder ()
271- .setEnablePlaceholders(false )
272- .setPrefetchDistance(10 )
273- .setPageSize(20 )
274- .build();
268+ // This configuration comes from the Paging 3 Library
269+ // https://developer.android.com/reference/kotlin/androidx/paging/PagingConfig
270+ PagingConfig config = new PagingConfig (/* page size */ 20 , /* prefetchDistance */ 10 ,
271+ /* enablePlaceHolders */ false );
275272
276273// The options for the adapter combine the paging configuration with query information
277274// and application-specific options for lifecycle, etc.
@@ -362,38 +359,103 @@ start and stop listening in `onStart()` and `onStop()`.
362359#### Paging events
363360
364361When using the ` FirestorePagingAdapter ` , you may want to perform some action every time data
365- changes or when there is an error. To do this, override the ` onLoadingStateChanged() `
366- method of the adapter:
362+ changes or when there is an error. To do this:
367363
368- ``` java
369- FirestorePagingAdapter<Item , ItemViewHolder > adapter =
370- new FirestorePagingAdapter<Item , ItemViewHolder > (options) {
364+ ##### In Java
371365
372- // ...
366+ Use the ` addLoadStateListener ` method from the adapter:
373367
368+ ``` java
369+ adapter. addLoadStateListener(new Function1<CombinedLoadStates , Unit > () {
374370 @Override
375- protected void onLoadingStateChanged (@NonNull LoadingState state ) {
376- switch (state) {
377- case LOADING_INITIAL :
378- // The initial load has begun
379- // ...
380- case LOADING_MORE :
381- // The adapter has started to load an additional page
371+ public Unit invoke (CombinedLoadStates states ) {
372+ LoadState refresh = states. getRefresh();
373+ LoadState append = states. getAppend();
374+
375+ if (refresh instanceof LoadState . Error || append instanceof LoadState . Error ) {
376+ // The previous load (either initial or additional) failed. Call
377+ // the retry() method in order to retry the load operation.
378+ // ...
379+ }
380+
381+ if (refresh instanceof LoadState . Loading ) {
382+ // The initial Load has begun
383+ // ...
384+ }
385+
386+ if (append instanceof LoadState . Loading ) {
387+ // The adapter has started to load an additional page
388+ // ...
389+ }
390+
391+ if (append instanceof LoadState . NotLoading ) {
392+ LoadState . NotLoading notLoading = (LoadState . NotLoading ) append;
393+ if (notLoading. getEndOfPaginationReached()) {
394+ // The adapter has finished loading all of the data set
382395 // ...
383- case LOADED :
396+ return null ;
397+ }
398+
399+ if (refresh instanceof LoadState . NotLoading ) {
384400 // The previous load (either initial or additional) completed
385401 // ...
386- case ERROR :
387- // The previous load (either initial or additional) failed. Call
388- // the retry() method in order to retry the load operation.
389- // ...
402+ return null ;
403+ }
390404 }
405+ return null ;
391406 }
392- };
407+ });
408+ ```
409+
410+ #### In Kotlin
411+
412+ Use the ` loadStateFlow ` exposed by the adapter, in a Coroutine Scope:
413+
414+ ``` kotlin
415+ // Activities can use lifecycleScope directly, but Fragments should instead use
416+ // viewLifecycleOwner.lifecycleScope.
417+ lifecycleScope.launch {
418+ pagingAdapter.loadStateFlow.collectLatest { loadStates ->
419+ when (loadStates.refresh) {
420+ is LoadState .Error -> {
421+ // The initial load failed. Call the retry() method
422+ // in order to retry the load operation.
423+ // ...
424+ }
425+ is LoadState .Loading -> {
426+ // The initial Load has begun
427+ // ...
428+ }
429+ }
430+
431+ when (loadStates.append) {
432+ is LoadState .Error -> {
433+ // The additional load failed. Call the retry() method
434+ // in order to retry the load operation.
435+ // ...
436+ }
437+ is LoadState .Loading -> {
438+ // The adapter has started to load an additional page
439+ // ...
440+ }
441+ is LoadState .NotLoading -> {
442+ if (loadStates.append.endOfPaginationReached) {
443+ // The adapter has finished loading all of the data set
444+ // ...
445+ }
446+ if (loadStates.refresh is LoadState .NotLoading ) {
447+ // The previous load (either initial or additional) completed
448+ // ...
449+ }
450+ }
451+ }
452+ }
453+ }
454+
393455```
394456
395457[ firestore-docs ] : https://firebase.google.com/docs/firestore/
396458[ firestore-custom-objects ] : https://firebase.google.com/docs/firestore/manage-data/add-data#custom_objects
397459[ recyclerview ] : https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView
398460[ arch-components ] : https://developer.android.com/topic/libraries/architecture/index.html
399- [ paging-support ] : https://developer.android.com/topic/libraries/architecture/paging.html
461+ [ paging-support ] : https://developer.android.com/topic/libraries/architecture/paging/v3-overview
0 commit comments