mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2025-09-05 08:09:33 +00:00
improvements in InfinityPagedComponent
This commit is contained in:
@@ -2,6 +2,11 @@
|
|||||||
|
|
||||||
## 0.25.8
|
## 0.25.8
|
||||||
|
|
||||||
|
* `Pagination`:
|
||||||
|
* `Compose`:
|
||||||
|
* New function `rememberInfinityPagedComponentContext` to create `InfinityPagedComponentContext`
|
||||||
|
* New variants of `InfinityPagedComponent` component
|
||||||
|
|
||||||
## 0.25.7
|
## 0.25.7
|
||||||
|
|
||||||
* `Versions`:
|
* `Versions`:
|
||||||
|
@@ -65,6 +65,44 @@ class InfinityPagedComponentContext<T> internal constructor(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates and remembers an [InfinityPagedComponentContext] for managing infinite pagination in a Compose UI.
|
||||||
|
* This function is used to create a persistent pagination context that survives recompositions.
|
||||||
|
*
|
||||||
|
* @param size Number of items to load per page.
|
||||||
|
* @param page Initial page number to start pagination from (defaults to 0).
|
||||||
|
* @param scope [CoroutineScope] to launch pagination operations in. If not provided, a new scope will be created
|
||||||
|
* using [rememberCoroutineScope].
|
||||||
|
* @param loader Suspended function that loads paginated data. Receives the current pagination context and
|
||||||
|
* pagination parameters, and returns a [PaginationResult] containing the loaded data.
|
||||||
|
* @return An [InfinityPagedComponentContext] instance that manages the pagination state and operations.
|
||||||
|
*/
|
||||||
|
@Composable
|
||||||
|
fun <T> rememberInfinityPagedComponentContext(
|
||||||
|
size: Int,
|
||||||
|
page: Int = 0,
|
||||||
|
scope: CoroutineScope = rememberCoroutineScope(),
|
||||||
|
loader: suspend InfinityPagedComponentContext<T>.(Pagination) -> PaginationResult<T>
|
||||||
|
) = remember {
|
||||||
|
InfinityPagedComponentContext(page = page, size = size, scope = scope, loader = loader)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Composable function for managing an infinitely paged component.
|
||||||
|
*
|
||||||
|
* @param T The type of the paginated data.
|
||||||
|
* @param block Composable function that renders the UI with the loaded data. When data is in loading state, block will
|
||||||
|
* receive null as `it` parameter
|
||||||
|
*/
|
||||||
|
@Composable
|
||||||
|
internal fun <T> InfinityPagedComponent(
|
||||||
|
context: InfinityPagedComponentContext<T>,
|
||||||
|
block: @Composable InfinityPagedComponentContext<T>.(List<T>?) -> Unit
|
||||||
|
) {
|
||||||
|
val dataState = context.dataState.collectAsState()
|
||||||
|
context.block(dataState.value)
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Composable function for managing an infinitely paged component.
|
* Composable function for managing an infinitely paged component.
|
||||||
*
|
*
|
||||||
@@ -84,13 +122,8 @@ internal fun <T> InfinityPagedComponent(
|
|||||||
block: @Composable InfinityPagedComponentContext<T>.(List<T>?) -> Unit
|
block: @Composable InfinityPagedComponentContext<T>.(List<T>?) -> Unit
|
||||||
) {
|
) {
|
||||||
val scope = predefinedScope ?: rememberCoroutineScope()
|
val scope = predefinedScope ?: rememberCoroutineScope()
|
||||||
val context = remember { InfinityPagedComponentContext<T>(page, size, scope, loader) }
|
val context = rememberInfinityPagedComponentContext(page = page, size = size, scope = scope, loader = loader)
|
||||||
remember {
|
InfinityPagedComponent(context, block)
|
||||||
context.reload()
|
|
||||||
}
|
|
||||||
|
|
||||||
val dataState = context.dataState.collectAsState()
|
|
||||||
context.block(dataState.value)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Reference in New Issue
Block a user