PagingData

public final class PagingData<T extends Object>


Container for Paged data from a single generation of loads.

Each refresh of data (generally either pushed by local storage, or pulled from the network) will have a separate corresponding PagingData.

Summary

Nested types

PagingData.Companion

Extension functions

final @NonNull PagingData<@NonNull T>
<T extends Object> PagingDataFutures.filterAsync(
    @NonNull PagingData<@NonNull T> receiver,
    @NonNull AsyncFunction<@NonNull T, @NonNull Boolean> predicate,
    @NonNull Executor executor
)

Returns a PagingData containing only elements matching the given predicate.

final @NonNull PagingData<@NonNull R>
<T extends Object, R extends Object> PagingDataFutures.flatMapAsync(
    @NonNull PagingData<@NonNull T> receiver,
    @NonNull AsyncFunction<@NonNull T, @NonNull Iterable<@NonNull R>> transform,
    @NonNull Executor executor
)

Returns a PagingData of all elements returned from applying the given transform to each element, as it is loaded.

final @NonNull PagingData<@NonNull R>
<T extends R, R extends Object> PagingDataFutures.insertSeparatorsAsync(
    @NonNull PagingData<@NonNull T> receiver,
    @NonNull AsyncFunction<@NonNull AdjacentItems<@NonNull T>, @NonNull R> generator,
    @NonNull Executor executor
)

Returns a PagingData containing each original element, with an optional separator generated by generator, given the elements before and after (or null, in boundary conditions).

final @NonNull PagingData<@NonNull R>
<T extends Object, R extends Object> PagingDataFutures.mapAsync(
    @NonNull PagingData<@NonNull T> receiver,
    @NonNull AsyncFunction<@NonNull T, @NonNull R> transform,
    @NonNull Executor executor
)

Returns a PagingData containing the result of applying the given transform to each element, as it is loaded.

final @NonNull PagingData<@NonNull T>
<T extends Object> PagingDataTransforms.filter(
    @NonNull PagingData<@NonNull T> receiver,
    @NonNull Executor executor,
    @NonNull Function1<@NonNull T, @NonNull Boolean> predicate
)

Returns a PagingData containing only elements matching the given predicate.

final @NonNull PagingData<@NonNull R>
<T extends Object, R extends Object> PagingDataTransforms.flatMap(
    @NonNull PagingData<@NonNull T> receiver,
    @NonNull Executor executor,
    @NonNull Function1<@NonNull T, @NonNull Iterable<@NonNull R>> transform
)

Returns a PagingData of all elements returned from applying the given transform to each element, as it is loaded.

final @NonNull PagingData<@NonNull T>
<T extends Object> PagingDataTransforms.insertFooterItem(
    @NonNull PagingData<@NonNull T> receiver,
    @NonNull TerminalSeparatorType terminalSeparatorType,
    @NonNull T item
)

Returns a PagingData containing each original element, with the passed footer item added to the end of the list.

final @NonNull PagingData<@NonNull T>
<T extends Object> PagingDataTransforms.insertHeaderItem(
    @NonNull PagingData<@NonNull T> receiver,
    @NonNull TerminalSeparatorType terminalSeparatorType,
    @NonNull T item
)

Returns a PagingData containing each original element, with the passed header item added to the start of the list.

final @NonNull PagingData<@NonNull R>
<R extends Object, T extends R> PagingDataTransforms.insertSeparators(
    @NonNull PagingData<@NonNull T> receiver,
    @NonNull TerminalSeparatorType terminalSeparatorType,
    @NonNull Executor executor,
    @NonNull Function2<@NonNull T, @NonNull T, @NonNull R> generator
)

Returns a PagingData containing each original element, with an optional separator generated by generator, given the elements before and after (or null, in boundary conditions).

final @NonNull PagingData<@NonNull R>
<T extends Object, R extends Object> PagingDataTransforms.map(
    @NonNull PagingData<@NonNull T> receiver,
    @NonNull Executor executor,
    @NonNull Function1<@NonNull T, @NonNull R> transform
)

Returns a PagingData containing the result of applying the given transform to each element, as it is loaded.

final @NonNull PagingData<@NonNull T>
<T extends Object> PagingRx.filterAsync(
    @NonNull PagingData<@NonNull T> receiver,
    @NonNull Function1<@NonNull T, @NonNull Single<@NonNull Boolean>> predicate
)

Returns a PagingData containing only elements matching the given predicate.

final @NonNull PagingData<@NonNull T>
<T extends Object> PagingRx.filterAsync(
    @NonNull PagingData<@NonNull T> receiver,
    @NonNull Function1<@NonNull T, @NonNull Single<@NonNull Boolean>> predicate
)

Returns a PagingData containing only elements matching the given predicate.

final @NonNull PagingData<@NonNull R>
<T extends Object, R extends Object> PagingRx.flatMapAsync(
    @NonNull PagingData<@NonNull T> receiver,
    @NonNull Function1<@NonNull T, @NonNull Single<@NonNull Iterable<@NonNull R>>> transform
)

Returns a PagingData of all elements returned from applying the given transform to each element, as it is loaded.

final @NonNull PagingData<@NonNull R>
<T extends Object, R extends Object> PagingRx.flatMapAsync(
    @NonNull PagingData<@NonNull T> receiver,
    @NonNull Function1<@NonNull T, @NonNull Single<@NonNull Iterable<@NonNull R>>> transform
)

Returns a PagingData of all elements returned from applying the given transform to each element, as it is loaded.

final @NonNull PagingData<@NonNull R>
<T extends R, R extends Object> PagingRx.insertSeparatorsAsync(
    @NonNull PagingData<@NonNull T> receiver,
    @NonNull Function2<@NonNull T, @NonNull T, @NonNull Maybe<@NonNull R>> generator
)

Returns a PagingData containing each original element, with an optional separator generated by generator, given the elements before and after (or null, in boundary conditions).

final @NonNull PagingData<@NonNull R>
<T extends R, R extends Object> PagingRx.insertSeparatorsAsync(
    @NonNull PagingData<@NonNull T> receiver,
    @NonNull Function2<@NonNull T, @NonNull T, @NonNull Maybe<@NonNull R>> generator
)

Returns a PagingData containing each original element, with an optional separator generated by generator, given the elements before and after (or null, in boundary conditions).

final @NonNull PagingData<@NonNull R>
<T extends Object, R extends Object> PagingRx.mapAsync(
    @NonNull PagingData<@NonNull T> receiver,
    @NonNull Function1<@NonNull T, @NonNull Single<@NonNull R>> transform
)

Returns a PagingData containing the result of applying the given transform to each element, as it is loaded.

final @NonNull PagingData<@NonNull R>
<T extends Object, R extends Object> PagingRx.mapAsync(
    @NonNull PagingData<@NonNull T> receiver,
    @NonNull Function1<@NonNull T, @NonNull Single<@NonNull R>> transform
)

Returns a PagingData containing the result of applying the given transform to each element, as it is loaded.

Extension functions

PagingDataFutures.filterAsync

@NonNull
public final PagingData<@NonNull T> <T extends Object> PagingDataFutures.filterAsync(
    @NonNull PagingData<@NonNull T> receiver,
    @NonNull AsyncFunction<@NonNull T, @NonNull Boolean> predicate,
    @NonNull Executor executor
)

Returns a PagingData containing only elements matching the given predicate.

Parameters
@NonNull AsyncFunction<@NonNull T, @NonNull Boolean> predicate

AsyncFunction returning false for unmatched items which should be filtered.

@NonNull Executor executor

Executor to run the AsyncFunction in.

PagingDataFutures.flatMapAsync

@NonNull
public final PagingData<@NonNull R> <T extends Object, R extends Object> PagingDataFutures.flatMapAsync(
    @NonNull PagingData<@NonNull T> receiver,
    @NonNull AsyncFunction<@NonNull T, @NonNull Iterable<@NonNull R>> transform,
    @NonNull Executor executor
)

Returns a PagingData of all elements returned from applying the given transform to each element, as it is loaded.

Parameters
@NonNull AsyncFunction<@NonNull T, @NonNull Iterable<@NonNull R>> transform

AsyncFunction to transform an item of type T a list of items of type R.

@NonNull Executor executor

Executor to run the AsyncFunction in.

PagingDataFutures.insertSeparatorsAsync

@NonNull
public final PagingData<@NonNull R> <T extends R, R extends Object> PagingDataFutures.insertSeparatorsAsync(
    @NonNull PagingData<@NonNull T> receiver,
    @NonNull AsyncFunction<@NonNull AdjacentItems<@NonNull T>, @NonNull R> generator,
    @NonNull Executor executor
)

Returns a PagingData containing each original element, with an optional separator generated by generator, given the elements before and after (or null, in boundary conditions).

Note that this transform is applied asynchronously, as pages are loaded. Potential separators between pages are only computed once both pages are loaded.

import androidx.paging.insertSeparatorsAsync
import androidx.paging.rxjava2.insertSeparatorsAsync

/*
 * Create letter separators in an alphabetically sorted list.
 *
 * For example, if the input is:
 *     "apple", "apricot", "banana", "carrot"
 *
 * The operator would output:
 *     "A", "apple", "apricot", "B", "banana", "C", "carrot"
 */
pagingDataStream.map { pagingData ->
    // map outer stream, so we can perform transformations on each paging generation
    pagingData.insertSeparatorsAsync(
        AsyncFunction<AdjacentItems<String>, String?> {
            Futures.submit(
                Callable<String?> {
                    val (before, after) = it!!
                    if (after != null && before?.first() != after.first()) {
                        // separator - after is first item that starts with its first letter
                        after.first().uppercaseChar().toString()
                    } else {
                        // no separator - either end of list, or first letters of before/after are the same
                        null
                    }
                },
                executor
            )
        },
        executor
    )
}
import androidx.paging.insertSeparatorsAsync
import androidx.paging.map
import androidx.paging.rxjava2.insertSeparatorsAsync

open class UiModel
data class ItemUiModel(val item: Item) : UiModel()
data class SeparatorUiModel(val char: Char) : UiModel()

/*
 * Create letter separators in an alphabetically sorted list of Items, with UiModel objects.
 *
 * For example, if the input is (each an `Item`):
 *     "apple", "apricot", "banana", "carrot"
 *
 * The operator would output a list of UiModels corresponding to:
 *     "A", "apple", "apricot", "B", "banana", "C", "carrot"
 */
pagingDataStream.map { pagingData ->
    // map outer stream, so we can perform transformations on each paging generation
    pagingData
        .map { item ->
            ItemUiModel(item) // convert items in stream to ItemUiModel
        }
        .insertSeparatorsAsync(
            AsyncFunction<AdjacentItems<ItemUiModel>, UiModel?> {
                Futures.submit(
                    Callable<UiModel> {
                        val (before, after) = it!!
                        if (after != null &&
                            before?.item?.label?.first() != after.item.label.first()
                        ) {
                            // separator - after is first item that starts with its first letter
                            SeparatorUiModel(after.item.label.first().uppercaseChar())
                        } else {
                            // no separator - either end of list, or first letters of before/after are the same
                            null
                        }
                    },
                    executor
                )
            },
            executor
        )
}
Parameters
@NonNull AsyncFunction<@NonNull AdjacentItems<@NonNull T>, @NonNull R> generator

AsyncFunction used to generate separator between two AdjacentItems or the header or footer if either AdjacentItems.before or AdjacentItems.after is null.

@NonNull Executor executor

Executor to run the AsyncFunction in.

PagingDataFutures.mapAsync

@NonNull
public final PagingData<@NonNull R> <T extends Object, R extends Object> PagingDataFutures.mapAsync(
    @NonNull PagingData<@NonNull T> receiver,
    @NonNull AsyncFunction<@NonNull T, @NonNull R> transform,
    @NonNull Executor executor
)

Returns a PagingData containing the result of applying the given transform to each element, as it is loaded.

Parameters
@NonNull AsyncFunction<@NonNull T, @NonNull R> transform

AsyncFunction to transform an item of type T to R.

@NonNull Executor executor

Executor to run the AsyncFunction in.

PagingDataTransforms.filter

@NonNull
public final