Quick Start
Setup and Usage
Basically
Define your UI data
All items should have an identificator
data class SongData( // public ui data structure
val id: String,
val title: String,
val artist: String,
val durationMs: Long
)
Define your source, design its requirements
// this is a source that only allows to retrieve pages by index
interface SongSource {
fun getPage(pageIndex: Int): Flow<List<SongData>>
}
Use the source definition in pageable in your view model
// the source will be implemented by some part of your code that
// knows about the actual data source
class SongListModel(source: SongSource, coroutineScope: CoroutineScope) {
val songPageable = pageable(
coroutineScope,
onPage = { index -> source.getPage(index) },
pageItemKey = { it.id },
strategy = prefetchPageAmount( // one of the default strategies
initialPage = 0,
minimumPageAmount = 2
)
)
}
Convert the pageable to usable state in your UI
val lazyListState = rememberLazyListState()
val pageableState = songListModel.songPageable.toState(
lazyListState
)
Optionally configure the strategy for pageable. See Pageable Strategies
Example Structure
songs/SongListModel.kt
class SongListModel(
private val source: SongSource,
coroutineScope: CoroutineScope
) {
// the default strategies use Int as page key/index,
// but any page key type you want is possible
val songPageable = pageable(
coroutineScope,
onPage = { index -> source.getPage(index) },
pageItemKey = { it.id },
strategy = prefetchPageAmount( // one of the default strategies
initialPage = 0,
minimumPageAmount = 2
)
)
}