How to porting Paging 3 from Kotlin Flow to Java? - Unable to find PagingDataFlow.create function

Viewed 1797

Paging 3 just released few days ago.

Currently, we are porting the following Paging 3 example code, from Kotlin to Java.

https://github.com/android/architecture-components-samples/blob/a2151cf66483560f76b041ba95ce96e931c50caf/PagingSample/app/src/main/java/paging/android/example/com/pagingsample/CheeseViewModel.kt

Kotlin

/**
 * We use the Kotlin API to construct a [Flow]<[PagingData]>. Java developers should use the
 * Java API: `PagingDataFlow.create`
 */
val allCheeses = Pager(
    PagingConfig(
        /**
         * A good page size is a value that fills at least a few screens worth of content on a
         * large device so the User is unlikely to see a null item.
         * You can play with this constant to observe the paging behavior.
         *
         * It's possible to vary this with list device size, but often unnecessary, unless a
         * user scrolling on a large device is expected to scroll through items more quickly
         * than a small device, such as when the large device uses a grid layout of items.
         */
        pageSize = 60,

        /**
         * If placeholders are enabled, PagedList will report the full size but some items might
         * be null in onBind method (PagedListAdapter triggers a rebind when data is loaded).
         *
         * If placeholders are disabled, onBind will never receive null but as more pages are
         * loaded, the scrollbars will jitter as new pages are loaded. You should probably
         * disable scrollbars if you disable placeholders.
         */
        enablePlaceholders = true,

        /**
         * Maximum number of items a PagedList should hold in memory at once.
         *
         * This number triggers the PagedList to start dropping distant pages as more are loaded.
         */
        maxSize = 200
    )
) {
    dao.allCheesesByName()
}.flow

Java

Constructing a PagingConfig isn't much issue.

final int pageSize = 60;
final int prefetchDistance = pageSize;
final boolean enablePlaceholders = false;
final int initialLoadSize = pageSize * PagingConfig.DEFAULT_INITIAL_PAGE_MULTIPLIER;
final int maxSize = PagingConfig.MAX_SIZE_UNBOUNDED;
final int jumpThreshold = PagingSource.LoadResult.Page.COUNT_UNDEFINED;

PagingConfig pagingConfig = new PagingConfig(
        pageSize,
        prefetchDistance,
        enablePlaceholders,
        initialLoadSize,
        maxSize,
        jumpThreshold
);

But, we are stuck then.

The following code comment draws our attention.

Java developers should use the Java API: PagingDataFlow.create

According to code comment, Java developer should use PagingDataFlow.create. But, from IDE, we really can't find a class named androidx.paging.PagingDataFlow.

We are expecting to have LiveData which we can observe. Kotlin's flow/ co-routine isn't something we are expecting in Java land.

Any idea how we can use PagingDataFlow.create?

1 Answers
Related