PaginatedResource

interface PaginatedResource<out T>

A list that arrives one page at a time.

Every all(…, scope) accessor returns one. It is a live object rather than a snapshot: observe state for what to draw, and call loadNextPage as the user reaches the end.

val issues = CGS.content().issues.all(scope = viewModelScope)
viewModelScope.launch { issues.state.collect { state -> render(state.items) } }

// From the list's scroll listener, as the user nears the end:
fun onNearEnd() = viewModelScope.launch { issues.loadNextPage() }

Hold on to the instance for as long as the list is on screen — it is what holds the pages already loaded. Calling all(…) again starts a new list from page one.

The first page is requested when state is first collected, not when the resource is created, and the whole thing follows the active profile — see Content.

Parameters

T

the item type, always the light form of an entity (IssueShort, ArticleShort, …).

Properties

Link copied to clipboard
abstract val state: StateFlow<PaginatedState<T>>

The items loaded so far and what the loading is doing, as one value.

Functions

Link copied to clipboard
abstract suspend fun loadNextPage()

Requests the next page and appends it to state.

Link copied to clipboard
abstract suspend fun refresh()

Loads the list again from the network, ignoring the cache: once the new first page arrives, it replaces every page loaded so far. A reload that fails keeps those pages and reports the error on PaginatedLoadStates.refresh.