### State in a ViewModel (Recommended) with PaginationState Source: https://github.com/ahmad-hamwi/lazy-pagination-compose/blob/main/README.md Recommended state management using a ViewModel. The PaginationState is created in the ViewModel with an Int key and onRequestPage delegates to loadPage. loadPage fetches a page and calls appendPage with items and isLastPage; on exception, setError is called. The composable accesses the state from the ViewModel. ```kotlin class MyViewModel : ViewModel() { // Int is passed as the KEY which represents your pagination fetching strategy // Int example here means the page number but could really by anything such as a cursor, time, etc... val paginationState = PaginationState( initialPageKey = 1, onRequestPage = { loadPage(it) } ) fun loadPage(pageKey: Int) { viewModelScope.launch { try { val page = DataSource.getPage(pageNumber = pageKey, pageSize = 10) paginationState.appendPage( items = page.items, isLastPage = page.isLastPage ) } catch (e: Exception) { paginationState.setError(e) } } } } @Composable fun Content(viewModel: MyViewModel) { val paginationState = viewModel.paginationState // Your Paginated composable here } ``` -------------------------------- ### Add lazy-pagination-compose dependency for Compose Multiplatform Source: https://github.com/ahmad-hamwi/lazy-pagination-compose/blob/main/README.md Declare the library version and dependency in the version catalog (libs.versions.toml) and add it to the commonMain source set for a Compose Multiplatform project. Use the -android artifact for Android-only projects. ```toml [versions] lazy-pagination-compose = "1.7.1" [libraries] lazyPaginationCompose = { module = "io.github.ahmad-hamwi:lazy-pagination-compose", version.ref = "lazy-pagination-compose" } ``` ```kotlin // Compose Multiplatform sourceSets { commonMain.dependencies { implementation(libs.lazyPaginationCompose) } } ``` -------------------------------- ### Create PaginationState with rememberPaginationState Source: https://github.com/ahmad-hamwi/lazy-pagination-compose/blob/main/README.md Create a PaginationState in a composable using rememberPaginationState. The first type parameter is the page key type (Int here) and the second is the item model type. ```kotlin // Int is the key which in this example represents the page number val paginationState = rememberPaginationState( initialPageKey = 1, ... ) ``` -------------------------------- ### Refresh pagination with refresh method Source: https://github.com/ahmad-hamwi/lazy-pagination-compose/blob/main/README.md Refresh the pagination list by calling refresh on the pagination state. The initialPageKey parameter is optional and defaults to the value provided when creating the state. ```kotlin paginationState.refresh( initialPageKey = 1 // optional, defaults to the value provided when creating the state ) ``` -------------------------------- ### Add Maven Central repository to root build.gradle Source: https://github.com/ahmad-hamwi/lazy-pagination-compose/blob/main/README.md Add the Maven Central repository to the root build.gradle file's repositories block to resolve the library dependency. ```kotlin allprojects { repositories { ... mavenCentral() } } ``` -------------------------------- ### Append page data with appendPage Source: https://github.com/ahmad-hamwi/lazy-pagination-compose/blob/main/README.md Append loaded items to the pagination state using appendPage. Provide the items, the next page key, and optionally isLastPage (defaults to false) to signal the end of the list. ```kotlin val scope = rememberCoroutineScope() val paginationState = rememberPaginationState( initialPageKey = 1, onRequestPage = { pageKey: Int -> scope.launch { val page = DataSource.getPage(pageNumber = pageKey, pageSize = 10,) appendPage( items = page.items, nextPageKey = page.nextPageNumber, isLastPage = page.isLastPage // optional, defaults to false ) } } ) ``` -------------------------------- ### Pass onRequestPage callback to load a page Source: https://github.com/ahmad-hamwi/lazy-pagination-compose/blob/main/README.md Pass an onRequestPage callback to rememberPaginationState that launches a coroutine and calls the data source with the requested page key. The callback receives the page key to load. ```kotlin val scope = rememberCoroutineScope() val paginationState = rememberPaginationState( initialPageKey = 1, onRequestPage = { pageKey: Int -> scope.launch { val page = DataSource.getPage(pageNumber = pageKey, pageSize = 10,) } } ) ``` -------------------------------- ### Define a paginated composable with PaginatedLazyColumn Source: https://github.com/ahmad-hamwi/lazy-pagination-compose/blob/main/README.md Define a paginated composable using PaginatedLazyColumn (or PaginatedLazyRow, PaginatedLazyVerticalGrid, PaginatedLazyHorizontalGrid). Pass the pagination state and provide indicators for progress, error, and empty states. Access paginationState.allItems safely inside the content lambda. ```kotlin @Composable fun Content() { val paginationState = ... // either remembered here or in ViewModel // Or any other paginated composable PaginatedLazyColumn( paginationState = paginationState, firstPageProgressIndicator = { ... }, newPageProgressIndicator = { ... }, firstPageErrorIndicator = { e -> ... }, newPageErrorIndicator = { e -> ... }, firstPageEmptyIndicator = { ... }, newPageEmptyIndicator = { ... }, ) { itemsIndexed( paginationState.allItems!!, // safe to access here ) { _, item -> Item(value = item) } } } ``` -------------------------------- ### State in UI (no ViewModel) with rememberPaginationState Source: https://github.com/ahmad-hamwi/lazy-pagination-compose/blob/main/README.md State management without a ViewModel. Uses rememberPaginationState with an Int key representing the page number. The onRequestPage lambda fetches a page and calls appendPage with the items, nextPageKey, and isLastPage; on exception, setError is called. ```kotlin // Int is passed as the KEY which represents your pagination fetching strategy // Int example here means the page number but could really by anything such as a cursor, time, etc... @Composable fun Content() { val paginationState = rememberPaginationState( initialPageKey = 1, onRequestPage = { pageKey: Int -> scope.launch { try { val page = DataSource.getPage(pageNumber = pageKey, pageSize = 10) appendPage( items = page.items, nextPageKey = page.nextPageNumber, isLastPage = page.isLastPage ) } catch (e: Exception) { setError(e) } } } ) // Your paginated composable here } ``` -------------------------------- ### Handle errors with setError Source: https://github.com/ahmad-hamwi/lazy-pagination-compose/blob/main/README.md Handle request errors by wrapping the data source call in a try-catch and calling setError(e) with the caught exception. This marks the state as errored for the current page request. ```kotlin val paginationState = rememberPaginationState( initialPageKey = 1, onRequestPage = { pageKey: Int -> scope.launch { try { val page = DataSource.getPage(pageNumber = pageKey, pageSize = 10,) appendPage( items = page.items, nextPageKey = page.nextPageNumber, isLastPage = page.isLastPage ) } catch (e: Exception) { setError(e) } } } ) ``` -------------------------------- ### PaginatedLazyVerticalGrid composable Source: https://github.com/ahmad-hamwi/lazy-pagination-compose/blob/main/README.md Use this composable to display a vertically scrolling grid with pagination. It requires a paginationState and provides indicators for loading, error, and empty states. The error indicator receives the error from setError and can call retryLastFailedRequest() to retry. ```kotlin PaginatedLazyVerticalGrid( paginationState = paginationState, firstPageProgressIndicator = { ... }, newPageProgressIndicator = { ... }, firstPageErrorIndicator = { e -> // from setError ... e.message ... ... onRetry = { paginationState.retryLastFailedRequest() } ... }, newPageErrorIndicator = { e -> ... }, firstPageEmptyIndicator = { ... }, newPageEmptyIndicator = { ... }, ... // The rest of LazyVerticalGrid params ) { itemsIndexed( paginationState.allItems!!, // safe to access here ) { _, item -> Item(value = item) } } ``` -------------------------------- ### PaginatedLazyRow composable Source: https://github.com/ahmad-hamwi/lazy-pagination-compose/blob/main/README.md Use this composable to display a horizontally scrolling list with pagination. It requires a paginationState and provides indicators for loading, error, and empty states. The error indicator receives the error from setError and can call retryLastFailedRequest() to retry. ```kotlin PaginatedLazyRow( paginationState = paginationState, firstPageProgressIndicator = { ... }, newPageProgressIndicator = { ... }, firstPageErrorIndicator = { e -> // from setError ... e.message ... ... onRetry = { paginationState.retryLastFailedRequest() } ... }, newPageErrorIndicator = { e -> ... }, firstPageEmptyIndicator = { ... }, newPageEmptyIndicator = { ... }, ... // The rest of LazyRow params ) { itemsIndexed( paginationState.allItems!!, // safe to access here ) { _, item -> Item(value = item) } } ``` -------------------------------- ### Retry last failed request with retryLastFailedRequest Source: https://github.com/ahmad-hamwi/lazy-pagination-compose/blob/main/README.md Retry the last failed request by calling retryLastFailedRequest on the pagination state. ```kotlin paginationState.retryLastFailedRequest() ``` -------------------------------- ### PaginatedLazyColumn composable Source: https://github.com/ahmad-hamwi/lazy-pagination-compose/blob/main/README.md Use this composable to display a vertically scrolling list with pagination. It requires a paginationState and provides indicators for loading, error, and empty states. The error indicator receives the error from setError and can call retryLastFailedRequest() to retry. ```kotlin PaginatedLazyColumn( paginationState = paginationState, firstPageProgressIndicator = { ... }, newPageProgressIndicator = { ... }, firstPageErrorIndicator = { e -> // from setError ... e.message ... ... onRetry = { paginationState.retryLastFailedRequest() } ... }, newPageErrorIndicator = { e -> ... }, firstPageEmptyIndicator = { ... }, newPageEmptyIndicator = { ... }, // The rest of LazyColumn params ) { itemsIndexed( paginationState.allItems!!, // safe to access here ) { _, item -> Item(value = item) } } ``` -------------------------------- ### PaginatedLazyHorizontalGrid composable Source: https://github.com/ahmad-hamwi/lazy-pagination-compose/blob/main/README.md Composable for a horizontally paginated grid. It takes a paginationState and provides indicators for first-page loading, new-page loading, errors, and empty states. The content lambda receives the paginated items via itemsIndexed, with allItems being safe to access. ```kotlin PaginatedLazyHorizontalGrid( paginationState = paginationState, firstPageProgressIndicator = { ... }, newPageProgressIndicator = { ... }, firstPageErrorIndicator = { e -> // from setError ... e.message ... ... onRetry = { paginationState.retryLastFailedRequest() } ... }, newPageErrorIndicator = { e -> ... }, firstPageEmptyIndicator = { ... }, newPageEmptyIndicator = { ... }, ... // The rest of LazyHorizontalGrid params ) { itemsIndexed( paginationState.allItems!!, // safe to access here ) { _, item -> Item(value = item) } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.