mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2025-09-06 08:40:19 +00:00
add KDocs for InfinityPagedComponentContext and PagedComponent
This commit is contained in:
@@ -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,
|
||||
|
@@ -6,23 +6,42 @@ import dev.inmo.micro_utils.common.dataOrThrow
|
||||
import dev.inmo.micro_utils.common.optional
|
||||
import dev.inmo.micro_utils.pagination.*
|
||||
|
||||
/**
|
||||
* Context for managing paginated data in a Compose UI.
|
||||
*
|
||||
* @param T The type of data being paginated.
|
||||
* @property iterationState Holds the current pagination state and iteration count.
|
||||
* @property dataOptional Stores the optional preloaded pagination result.
|
||||
* @property dataState Stores the current pagination result.
|
||||
* @constructor Internal constructor for setting up pagination.
|
||||
* @param preset Optional preset pagination result.
|
||||
* @param initialPage Initial page number.
|
||||
* @param size Number of items per page.
|
||||
*/
|
||||
class PagedComponentContext<T> internal constructor(
|
||||
preset: PaginationResult<T>? = null,
|
||||
initialPage: Int,
|
||||
size: Int
|
||||
) {
|
||||
internal val iterationState: MutableState<Pair<Int, Pagination>> = mutableStateOf(0 to SimplePagination(preset ?.page ?: initialPage, preset ?.size ?: size))
|
||||
internal val iterationState: MutableState<Pair<Int, Pagination>> = mutableStateOf(0 to SimplePagination(preset?.page ?: initialPage, preset?.size ?: size))
|
||||
|
||||
internal var dataOptional: PaginationResult<T>? = preset
|
||||
private set
|
||||
internal val dataState: MutableState<PaginationResult<T>?> = mutableStateOf(dataOptional)
|
||||
|
||||
/**
|
||||
* Loads the next page of data. If the last page is reached, this function returns early.
|
||||
*/
|
||||
fun loadNext() {
|
||||
iterationState.value = iterationState.value.let {
|
||||
if (dataState.value ?.isLastPage == true) return
|
||||
(it.first + 1) to it.second.nextPage()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads the previous page of data if available.
|
||||
*/
|
||||
fun loadPrevious() {
|
||||
iterationState.value = iterationState.value.let {
|
||||
if (it.second.isFirstPage) return
|
||||
@@ -32,6 +51,10 @@ class PagedComponentContext<T> internal constructor(
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Reloads the current page, refreshing the data.
|
||||
*/
|
||||
fun reload() {
|
||||
iterationState.value = iterationState.value.let {
|
||||
it.copy(it.first + 1)
|
||||
@@ -39,6 +62,16 @@ class PagedComponentContext<T> internal constructor(
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Composable function for paginated data displaying in a Compose UI.
|
||||
*
|
||||
* @param T The type of paginated data.
|
||||
* @param preload Optional preloaded pagination result.
|
||||
* @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
|
||||
internal fun <T> PagedComponent(
|
||||
preload: PaginationResult<T>?,
|
||||
@@ -58,6 +91,14 @@ internal fun <T> PagedComponent(
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Overloaded composable function for paginated components with preloaded data.
|
||||
*
|
||||
* @param T The type of paginated data.
|
||||
* @param preload Preloaded pagination result.
|
||||
* @param loader Suspended function that loads paginated data.
|
||||
* @param block Composable function that renders the UI with the loaded data.
|
||||
*/
|
||||
@Composable
|
||||
fun <T> PagedComponent(
|
||||
preload: PaginationResult<T>,
|
||||
@@ -73,6 +114,14 @@ fun <T> PagedComponent(
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Overloaded composable function for paginated components with pagination info.
|
||||
*
|
||||
* @param T The type of 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> PagedComponent(
|
||||
pageInfo: Pagination,
|
||||
@@ -88,6 +137,15 @@ fun <T> PagedComponent(
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Overloaded composable function for paginated components with an initial page.
|
||||
*
|
||||
* @param T The type of 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> PagedComponent(
|
||||
initialPage: Int,
|
||||
@@ -98,6 +156,14 @@ fun <T> PagedComponent(
|
||||
PagedComponent(null, initialPage, size, loader, block)
|
||||
}
|
||||
|
||||
/**
|
||||
* Overloaded composable function for paginated components with only a size parameter.
|
||||
*
|
||||
* @param T The type of 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> PagedComponent(
|
||||
size: Int,
|
||||
|
Reference in New Issue
Block a user