add KDocs for InfinityPagedComponentContext and PagedComponent

This commit is contained in:
2025-03-01 22:16:48 +06:00
parent 0a5cfaba18
commit 3b7dde3cb1
2 changed files with 119 additions and 3 deletions

View File

@@ -3,20 +3,36 @@ package dev.inmo.micro_utils.pagination.compose
import androidx.compose.runtime.*
import dev.inmo.micro_utils.pagination.*
/**
* Context for managing infinite pagination in a Compose UI.
*
* @param T The type of the data being paginated.
* @property iterationState Holds the current pagination state and iteration count.
* @property dataState Stores the loaded data, initially null.
* @constructor Internal constructor to initialize pagination.
* @param page Initial page number.
* @param size Number of items per page.
*/
class InfinityPagedComponentContext<T> internal constructor(
page: Int,
size: Int
) {
internal val iterationState: MutableState<Pair<Int, Pagination>> = mutableStateOf(0 to SimplePagination(page, size))
internal val dataState: MutableState<List<T>?> = mutableStateOf(null)
/**
* Loads the next page of data. If the current page is the last one, the function returns early.
*/
fun loadNext() {
iterationState.value = iterationState.value.let {
if ((dataState.value as? PaginationResult<*>) ?.isLastPage == true) return
(it.first + 1) to it.second.nextPage()
}
}
/**
* Reloads the pagination from the first page, clearing previously loaded data.
*/
fun reload() {
dataState.value = null
iterationState.value = iterationState.value.let {
@@ -25,6 +41,15 @@ class InfinityPagedComponentContext<T> internal constructor(
}
}
/**
* Composable function for managing an infinitely paged component.
*
* @param T The type of the paginated data.
* @param page Initial page number.
* @param size Number of items per page.
* @param loader Suspended function that loads paginated data.
* @param block Composable function that renders the UI with the loaded data.
*/
@Composable
internal fun <T> InfinityPagedComponent(
page: Int,
@@ -43,6 +68,14 @@ internal fun <T> InfinityPagedComponent(
}
}
/**
* Overloaded composable function for an infinitely paged component.
*
* @param T The type of the paginated data.
* @param pageInfo Initial pagination information.
* @param loader Suspended function that loads paginated data.
* @param block Composable function that renders the UI with the loaded data.
*/
@Composable
fun <T> InfinityPagedComponent(
pageInfo: Pagination,
@@ -58,6 +91,15 @@ fun <T> InfinityPagedComponent(
)
}
/**
* Overloaded composable function for an infinitely paged component.
*
* @param T The type of the paginated data.
* @param initialPage Initial page number.
* @param size Number of items per page.
* @param loader Suspended function that loads paginated data.
* @param block Composable function that renders the UI with the loaded data.
*/
@Composable
fun <T> InfinityPagedComponent(
initialPage: Int,
@@ -68,6 +110,14 @@ fun <T> InfinityPagedComponent(
PagedComponent(null, initialPage, size, loader, block)
}
/**
* Overloaded composable function for an infinitely paged component.
*
* @param T The type of the paginated data.
* @param size Number of items per page.
* @param loader Suspended function that loads paginated data.
* @param block Composable function that renders the UI with the loaded data.
*/
@Composable
fun <T> InfinityPagedComponent(
size: Int,