PagingData
-
Cmn
class PagingData<T : Any>
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
Public companion functions |
||
|---|---|---|
PagingData<T> |
Create a |
Cmn
|
PagingData<T> |
<T : Any> empty(sourceLoadStates: LoadStates, mediatorLoadStates: LoadStates?)Create a |
Cmn
|
PagingData<T> |
Create a |
Cmn
|
PagingData<T> |
<T : Any> from(Create a |
Cmn
|
Extension functions |
||
|---|---|---|
PagingData<T> |
<T : Any> PagingData<T>.filterAsync(Returns a |
android
|
PagingData<R> |
<T : Any, R : Any> PagingData<T>.flatMapAsync(Returns a |
android
|
PagingData<R> |
<T : R, R : Any> PagingData<T>.insertSeparatorsAsync(Returns a |
android
|
PagingData<R> |
<T : Any, R : Any> PagingData<T>.mapAsync(Returns a |
android
|
PagingData<T> |
<T : Any> PagingData<T>.filter(predicate: suspend (T) -> Boolean)Returns a |
Cmn
|
PagingData<T> |
<T : Any> PagingData<T>.filter(executor: Executor, predicate: (T) -> Boolean)Returns a |
android
|
PagingData<R> |
<T : Any, R : Any> PagingData<T>.flatMap(transform: suspend (T) -> Iterable<R>)Returns a |
Cmn
|
PagingData<R> |
Returns a |
android
|
PagingData<T> |
<T : Any> PagingData<T>.insertFooterItem(Returns a |
Cmn
|
PagingData<T> |
<T : Any> PagingData<T>.insertHeaderItem(Returns a |
Cmn
|
PagingData<R> |
<T : R, R : Any> PagingData<T>.insertSeparators(Returns a |
Cmn
|
PagingData<R> |
<R : Any, T : R> PagingData<T>.insertSeparators(Returns a |
android
|
PagingData<R> |
<T : Any, R : Any> PagingData<T>.map(transform: suspend (T) -> R)Returns a |
Cmn
|
PagingData<R> |
<T : Any, R : Any> PagingData<T>.map(executor: Executor, transform: (T) -> R)Returns a |
android
|
PagingData<T> |
<T : Any> PagingData<T>.filterAsync(predicate: (T) -> Single<Boolean>)Returns a |
android
|
PagingData<T> |
<T : Any> PagingData<T>.filterAsync(predicate: (T) -> Single<Boolean>)Returns a |
android
|
PagingData<R> |
<T : Any, R : Any> PagingData<T>.flatMapAsync(Returns a |
android
|
PagingData<R> |
<T : Any, R : Any> PagingData<T>.flatMapAsync(Returns a |
android
|
PagingData<R> |
<T : R, R : Any> PagingData<T>.insertSeparatorsAsync(Returns a |
android
|
PagingData<R> |
<T : R, R : Any> PagingData<T>.insertSeparatorsAsync(Returns a |
android
|
PagingData<R> |
<T : Any, R : Any> PagingData<T>.mapAsync(transform: (T) -> Single<R>)Returns a |
android
|
PagingData<R> |
<T : Any, R : Any> PagingData<T>.mapAsync(transform: (T) -> Single<R>)Returns a |
android
|
Public companion functions
empty
fun <T : Any> empty(): PagingData<T>
Create a PagingData that immediately displays an empty list of items when submitted to a presenter. E.g., androidx.paging.AsyncPagingDataAdapter and dispatches LoadState.NotLoading on all LoadStates to the presenter.
empty
fun <T : Any> empty(
sourceLoadStates: LoadStates,
mediatorLoadStates: LoadStates? = null
): PagingData<T>
Create a PagingData that immediately displays an empty list of items when submitted to a presenter. E.g., androidx.paging.AsyncPagingDataAdapter.
| Parameters | |
|---|---|
sourceLoadStates: LoadStates |
|
mediatorLoadStates: LoadStates? = null |
|
from
fun <T : Any> from(
data: List<T>,
placeholdersBefore: Int = 0,
placeholdersAfter: Int = 0
): PagingData<T>
Create a PagingData that immediately displays a static list of items when submitted to a presenter. E.g., androidx.paging.AsyncPagingDataAdapter and dispatches LoadState.NotLoading on all LoadStates to the presenter.
| Parameters | |
|---|---|
data: List<T> |
Static list of |
placeholdersBefore: Int = 0 |
The count of placeholders before the list of |
placeholdersAfter: Int = 0 |
The count of placeholders after the list of |
from
fun <T : Any> from(
data: List<T>,
sourceLoadStates: LoadStates,
mediatorLoadStates: LoadStates? = null,
placeholdersBefore: Int = 0,
placeholdersAfter: Int = 0
): PagingData<T>
Create a PagingData that immediately displays a static list of items when submitted to a presenter. E.g., androidx.paging.AsyncPagingDataAdapter.
| Parameters | |
|---|---|
data: List<T> |
Static list of |
sourceLoadStates: LoadStates |
|
mediatorLoadStates: LoadStates? = null |
|
placeholdersBefore: Int = 0 |
The count of placeholders before the list of |
placeholdersAfter: Int = 0 |
The count of placeholders after the list of |
Extension functions
filterAsync
fun <T : Any> PagingData<T>.filterAsync(
predicate: AsyncFunction<T, Boolean>,
executor: Executor
): PagingData<T>
Returns a PagingData containing only elements matching the given predicate.
| Parameters | |
|---|---|
predicate: AsyncFunction<T, Boolean> |
|
executor: Executor |
|
flatMapAsync
fun <T : Any, R : Any> PagingData<T>.flatMapAsync(
transform: AsyncFunction<T, Iterable<R>>,
executor: Executor
): PagingData<R>
Returns a PagingData of all elements returned from applying the given transform to each element, as it is loaded.
| Parameters | |
|---|---|
transform: AsyncFunction<T, Iterable<R>> |
|
executor: Executor |
|
insertSeparatorsAsync
fun <T : R, R : Any> PagingData<T>.insertSeparatorsAsync(
generator: AsyncFunction<AdjacentItems<T>, R?>,
executor: Executor
): PagingData<R>
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.AdjacentItems import androidx.paging.insertSeparators 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.AdjacentItems import androidx.paging.insertSeparators 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 | |
|---|---|
generator: AsyncFunction<AdjacentItems<T>, R?> |
|
executor: Executor |
|
mapAsync
fun <T : Any, R : Any> PagingData<T>.mapAsync(
transform: AsyncFunction<T, R>,
executor: Executor
): PagingData<R>
Returns a PagingData containing the result of applying the given transform to each element, as it is loaded.
| Parameters | |
|---|---|
transform: AsyncFunction<T, R> |
|
executor: Executor |
|
filter
fun <T : Any> PagingData<T>.filter(predicate: suspend (T) -> Boolean): PagingData<T>
Returns a PagingData containing only elements matching the given predicate
filter
fun <T : Any> PagingData<T>.filter(executor: Executor, predicate: (T) -> Boolean): PagingData<T>
Returns a PagingData containing only elements matching the given predicate.
| See also | |
|---|---|
filter |
flatMap
fun <T : Any, R : Any> PagingData<T>.flatMap(transform: suspend (T) -> Iterable<R>): PagingData<R>
Returns a PagingData of all elements returned from applying the given transform to each element, as it is loaded.
flatMap
fun <T : Any, R : Any> PagingData<T>.flatMap(
executor: Executor,
transform: (T) -> Iterable<R>
): PagingData<R>
Returns a PagingData of all elements returned from applying the given transform to each element, as it is loaded.
| See also | |
|---|---|
flatMap |
insertFooterItem
fun <T : Any> PagingData<T>.insertFooterItem(
terminalSeparatorType: TerminalSeparatorType = FULLY_COMPLETE,
item: T
): PagingData<T>
Returns a PagingData containing each original element, with the passed footer item added to the end of the list.
The footer item is added to a loaded page which marks the end of the data stream in the LoadType.APPEND direction, either by returning null in PagingSource.LoadResult.Page.nextKey. It will be removed if the last page in the list is dropped, which can happen in the case of loaded pages exceeding PagingConfig.maxSize.
Note: This operation is not idempotent, calling it multiple times will continually add more footers to the end of the list, which can be useful if multiple footer items are required.
| Parameters | |
|---|---|
terminalSeparatorType: TerminalSeparatorType = FULLY_COMPLETE |
|
item: T |
The footer to add to the end of the list once it is fully loaded in the |
| See also | |
|---|---|
insertHeaderItem |
insertHeaderItem
fun <T : Any> PagingData<T>.insertHeaderItem(
terminalSeparatorType: TerminalSeparatorType = FULLY_COMPLETE,
item: T
): PagingData<T>
Returns a PagingData containing each original element, with the passed header item added to the start of the list.
The header item is added to a loaded page which marks the end of the data stream in the LoadType.PREPEND direction by returning null in PagingSource.LoadResult.Page.prevKey. It will be removed if the first page in the list is dropped, which can happen in the case of loaded pages exceeding PagingConfig.maxSize.
Note: This operation is not idempotent, calling it multiple times will continually add more headers to the start of the list, which can be useful if multiple header items are required.
| Parameters | |
|---|---|
terminalSeparatorType: TerminalSeparatorType = FULLY_COMPLETE |
|
item: T |
The header to add to the front of the list once it is fully loaded in the |
| See also | |
|---|---|
insertFooterItem |
insertSeparators
fun <T : R, R : Any> PagingData<T>.insertSeparators(
terminalSeparatorType: TerminalSeparatorType = FULLY_COMPLETE,
generator: suspend (T?, T?) -> R?
): PagingData<R>
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.insertSeparators /* * 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.insertSeparators { before: String?, after: String? -> 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 } } }
import androidx.paging.insertSeparators import androidx.paging.map 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 } .insertSeparators<ItemUiModel, UiModel> { before: ItemUiModel?, after: ItemUiModel? -> 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 } } }
| Parameters | |
|---|---|
terminalSeparatorType: TerminalSeparatorType = FULLY_COMPLETE |
|
generator: suspend (T?, T?) -> R? |
Generator function used to construct a separator item given the item before and the item after. For terminal separators (header and footer), the arguments passed to the generator, |
insertSeparators
fun <R : Any, T : R> PagingData<T>.insertSeparators(
terminalSeparatorType: TerminalSeparatorType = TerminalSeparatorType.FULLY_COMPLETE,
executor: Executor,
generator: (T?, T?) -> R?
): PagingData<R>
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.
Kotlin callers should instead use the suspending extension function variant of insertSeparators
/*
* 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
PagingDataTransforms.insertSeparators(pagingData, bgExecutor,
(@Nullable String before, @Nullable String after) -> {
if (after != null && (before == null
|| before.charAt(0) != after.charAt(0))) {
// separator - after is first item that starts with its first
// letter
return Character.toString(
Character.toUpperCase(after.charAt(0)));
} else {
// no separator - either end of list, or first
// letters of items are the same
return null;
}
}));
/*
* 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(itemPagingData -> {
// map outer stream, so we can perform transformations on each paging generation
// first convert items in stream to UiModel.Item
PagingData<UiModel.ItemModel> itemModelPagingData = PagingDataTransforms.map(
itemPagingData, bgExecutor, UiModel.ItemModel::new);
// Now insert UiModel.Separators, which makes the PagingData of generic type UiModel
return PagingDataTransforms.insertSeparators(
itemModelPagingData, bgExecutor,
(@Nullable UiModel.ItemModel before, @Nullable UiModel.ItemModel after) -> {
if (after != null && (before == null
|| before.item.label.charAt(0) != after.item.label.charAt(0))) {
// separator - after is first item that starts with its first letter
return new UiModel.SeparatorModel(
Character.toUpperCase(after.item.label.charAt(0)));
} else {
// no separator - either end of list, or first
// letters of items are the same
return null;
}
});
});
public class UiModel {
static class ItemModel extends UiModel {
public Item item;
ItemModel(Item item) {
this.item = item;
}
}
static class SeparatorModel extends UiModel {
public char character;
SeparatorModel(char character) {
this.character = character;
}
}
}
| Parameters | |
|---|---|
terminalSeparatorType: TerminalSeparatorType = TerminalSeparatorType.FULLY_COMPLETE |
|
executor: Executor |
|
generator: (T?, T?) -> R? |
Generator function used to construct a separator item given the item before and the item after. For terminal separators (header and footer), the arguments passed to the generator, |
map
fun <T : Any, R : Any> PagingData<T>.map(transform: suspend (T) -> R): PagingData<R>
Returns a PagingData containing the result of applying the given transform to each element, as it is loaded.
map
fun <T : Any, R : Any> PagingData<T>.map(executor: Executor, transform: (T) -> R): PagingData<R>
Returns a PagingData containing the result of applying the given transform to each element, as it is loaded.
| See also | |
|---|---|
map |
filterAsync
fun <T : Any> PagingData<T>.filterAsync(predicate: (T) -> Single<Boolean>): PagingData<T>
Returns a PagingData containing only elements matching the given predicate.
filterAsync
fun <T : Any> PagingData<T>.filterAsync(predicate: (T) -> Single<Boolean>): PagingData<T>
Returns a PagingData containing only elements matching the given predicate.
flatMapAsync
fun <T : Any, R : Any> PagingData<T>.flatMapAsync(
transform: (T) -> Single<Iterable<R>>
): PagingData<R>
Returns a PagingData of all elements returned from applying the given transform to each element, as it is loaded.
flatMapAsync
fun <T : Any, R : Any> PagingData<T>.flatMapAsync(
transform: (T) -> Single<Iterable<R>>
): PagingData<R>
Returns a PagingData of all elements returned from applying the given transform to each element, as it is loaded.
insertSeparatorsAsync
fun <T : R, R : Any> PagingData<T>.insertSeparatorsAsync(
generator: (T?, T?) -> Maybe<R>
): PagingData<R>
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.insertSeparators 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 { before: String?, after: String? -> Maybe.fromCallable<String> { 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 } } .subscribeOn(Schedulers.computation()) } }
import androidx.paging.insertSeparators 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 { before: ItemUiModel?, after: ItemUiModel? -> Maybe.fromCallable<UiModel> { 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 } } .subscribeOn(Schedulers.computation()) } }
insertSeparatorsAsync
fun <T : R, R : Any> PagingData<T>.insertSeparatorsAsync(
generator: (T?, T?) -> Maybe<R>
): PagingData<R>
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.insertSeparators 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 { before: String?, after: String? -> Maybe.fromCallable<String> { 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 } } .subscribeOn(Schedulers.computation()) } }
import androidx.paging.insertSeparators 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 { before: ItemUiModel?, after: ItemUiModel? -> Maybe.fromCallable<UiModel> { 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 } } .subscribeOn(Schedulers.computation()) } }
mapAsync
fun <T : Any, R : Any> PagingData<T>.mapAsync(transform: (T) -> Single<R>): PagingData<R>
Returns a PagingData containing the result of applying the given transform to each element, as it is loaded.
mapAsync
fun <T : Any, R : Any> PagingData<T>.mapAsync(transform: (T) -> Single<R>): PagingData<R>
Returns a PagingData containing the result of applying the given transform to each element, as it is loaded.