PagingState



Snapshot state of Paging system including the loaded pages, the last accessed anchorPosition, and the config used.

Summary

Public constructors

<Key : Any, Value : Any> PagingState(
    pages: List<PagingSource.LoadResult.Page<Key, Value>>,
    anchorPosition: Int?,
    config: PagingConfig,
    leadingPlaceholderCount: @IntRange(from = 0) Int
)
Cmn

Public functions

Value?
closestItemAroundPosition(
    anchorPosition: Int,
    predicate: (value) -> Boolean
)

Coerces anchorPosition to closest loaded value in pages that matches predicate.

Cmn
Value?
closestItemToPosition(anchorPosition: Int)

Coerces anchorPosition to closest loaded value in pages.

Cmn
PagingSource.LoadResult.Page<Key, Value>?
closestPageToPosition(anchorPosition: Int)

Coerces an index in the list, including placeholders, to closest loaded page in pages.

Cmn
open operator Boolean
equals(other: Any?)
Cmn
Value?
Cmn
open Int
Cmn
Boolean
Cmn
Value?
Cmn
open String
Cmn

Public properties

Int?

Most recently accessed index in the list, including placeholders.

Cmn
PagingConfig

PagingConfig that was given when initializing the PagingData stream.

Cmn
List<PagingSource.LoadResult.Page<Key, Value>>

Loaded pages of data in the list.

Cmn

Public constructors

PagingState

<Key : Any, Value : Any> PagingState(
    pages: List<PagingSource.LoadResult.Page<Key, Value>>,
    anchorPosition: Int?,
    config: PagingConfig,
    leadingPlaceholderCount: @IntRange(from = 0) Int
)

Public functions

closestItemAroundPosition

fun closestItemAroundPosition(
    anchorPosition: Int,
    predicate: (value) -> Boolean
): Value?

Coerces anchorPosition to closest loaded value in pages that matches predicate.

This function can be called with anchorPosition to fetch the loaded item that is closest to the last accessed index in the list.

This function searches for the matching predicate amongst items that are positioned both before and after the anchorPosition with this order of priority:

  1. item closest to anchorPosition

  2. item before anchorPosition

This means that given an anchorPosition of item10, if item8 and item11 both match the predicate, then item11 will be returned since it is closer to item10. If two matching items have the same proximity, then the item that comes first will be returned. So given an anchorPosition of item10, if item9 and item11 both match the predicate, then item9 will be returned since it comes before item11.

This method should be avoided if possible if used on Lists that do not support random access, otherwise performance will take a big hit.

Parameters
anchorPosition: Int

Index in the list, including placeholders.

predicate: (value) -> Boolean

the predicate that matches target item

Returns
Value?

The closest loaded Value in pages to the provided anchorPosition that matches the predicate. null if all loaded pages are empty or if none of the loaded values match predicate

closestItemToPosition

fun closestItemToPosition(anchorPosition: Int): Value?

Coerces anchorPosition to closest loaded value in pages.

This function can be called with anchorPosition to fetch the loaded item that is closest to the last accessed index in the list.

Parameters
anchorPosition: Int

Index in the list, including placeholders.

Returns
Value?

The closest loaded Value in pages to the provided anchorPosition. null if all loaded pages are empty.

closestPageToPosition

fun closestPageToPosition(anchorPosition: Int): PagingSource.LoadResult.Page<Key, Value>?

Coerces an index in the list, including placeholders, to closest loaded page in pages.

This function can be called with anchorPosition to fetch the loaded page that is closest to the last accessed index in the list.

Parameters
anchorPosition: Int

Index in the list, including placeholders.

Returns
PagingSource.LoadResult.Page<Key, Value>?

The closest loaded Value in pages to the provided anchorPosition. null if all loaded pages are empty.

equals

open operator fun equals(other: Any?): Boolean

firstItemOrNull

fun firstItemOrNull(): Value?
Returns
Value?

The first loaded item in the list or null if all loaded pages are empty or no pages were loaded when this PagingState was created.

hashCode

open fun hashCode(): Int

isEmpty

fun isEmpty(): Boolean
Returns
Boolean

true if all loaded pages are empty or no pages were loaded when this PagingState was created, false otherwise.

lastItemOrNull

fun lastItemOrNull(): Value?
Returns
Value?

The last loaded item in the list or null if all loaded pages are empty or no pages were loaded when this PagingState was created.

toString

open fun toString(): String

Public properties

anchorPosition

val anchorPositionInt?

Most recently accessed index in the list, including placeholders.

null if no access in the PagingData has been made yet. E.g., if this snapshot was generated before or during the first load.

config

val configPagingConfig

PagingConfig that was given when initializing the PagingData stream.

pages

val pagesList<PagingSource.LoadResult.Page<Key, Value>>

Loaded pages of data in the list.