Quick Start
Setup and Usage
Basically
Define your UI data
All items should have an identificator
data class YourData( // public ui data structure
val id: String,
val data: Something
)
Define your source, design its requirements
// this is a source that only allows to retrieve pages by index
interface YourSource {
fun getPage(pageIndex: Int): Flow<List<YourData>>
}
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 YourModel(source: YourSource, coroutineScope: CoroutineScope) {
val yourPageableageable = pageable(
coroutineScope,
onPage = { index -> source.getPage(index) },
strategy = prefetchPageAmount( // one of the default strategies
initialPage = 0,
pageAmountSurroundingVisible = 2
)
)
}
Convert the pageable to usable state in your UI
val lazyListState = rememberLazyListState()
val pageableState = yourModel.pageable.toState(
lazyListState,
key = { item -> item.id }
)
Optionally configure the strategy for pageable. See Pageable Strategies
Example Structure
yourFeature/YourModel.kt
class YourModel(source: YourSource, coroutineScope: CoroutineScope) {
// the default startegies use Int as page key/index,
// but any page key type you want is possible
val yourPageableageable = pageable(
coroutineScope,
onPage = { index -> source.getPage(index) },
strategy = prefetchPageAmount( // one of the default strategies
initialPage = 0,
pageAmountSurroundingVisible = 2
)
)
}