mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2025-09-06 16:49:20 +00:00
hotfix in InfinityPagedComponent and PagedComponent
This commit is contained in:
@@ -18,7 +18,7 @@ class InfinityPagedComponentContext<T> internal constructor(
|
||||
size: Int
|
||||
) {
|
||||
internal val startPage = SimplePagination(page, size)
|
||||
internal val iterationState: MutableState<Pair<Int, Pagination?>> = mutableStateOf(0 to null)
|
||||
internal val iterationState: MutableState<Pagination?> = mutableStateOf(null)
|
||||
internal val dataState: MutableState<List<T>?> = mutableStateOf(null)
|
||||
internal var lastPageLoaded = false
|
||||
|
||||
@@ -27,11 +27,11 @@ class InfinityPagedComponentContext<T> internal constructor(
|
||||
*/
|
||||
fun loadNext() {
|
||||
if (lastPageLoaded) return
|
||||
if (iterationState.value.second is SimplePagination) return // Data loading has been inited but not loaded yet
|
||||
if (iterationState.value is SimplePagination) return // Data loading has been inited but not loaded yet
|
||||
|
||||
iterationState.value = iterationState.value.let {
|
||||
if ((it.second as? PaginationResult<*>) ?.isLastPage == true) return
|
||||
(it.first + 1) to (it.second ?: startPage).nextPage()
|
||||
if ((it as? PaginationResult<*>) ?.isLastPage == true) return
|
||||
(it ?: startPage).nextPage()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -42,7 +42,7 @@ class InfinityPagedComponentContext<T> internal constructor(
|
||||
dataState.value = null
|
||||
lastPageLoaded = false
|
||||
iterationState.value = iterationState.value.let {
|
||||
(it.first + 1) to null
|
||||
null
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -66,12 +66,12 @@ internal fun <T> InfinityPagedComponent(
|
||||
) {
|
||||
val context = remember { InfinityPagedComponentContext<T>(page, size) }
|
||||
|
||||
LaunchedEffect(context.iterationState.value.first) {
|
||||
val paginationResult = loader(context, context.iterationState.value.second ?: context.startPage)
|
||||
LaunchedEffect(context.iterationState.value ?.page) {
|
||||
val paginationResult = loader(context, context.iterationState.value ?: context.startPage)
|
||||
if (paginationResult.isLastPage) {
|
||||
context.lastPageLoaded = true
|
||||
}
|
||||
context.iterationState.value = context.iterationState.value.copy(second = paginationResult)
|
||||
context.iterationState.value = paginationResult
|
||||
context.dataState.value = (context.dataState.value ?: emptyList()) + paginationResult.results
|
||||
}
|
||||
|
||||
|
@@ -23,7 +23,7 @@ class PagedComponentContext<T> internal constructor(
|
||||
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<Pagination> = mutableStateOf(SimplePagination(preset?.page ?: initialPage, preset?.size ?: size))
|
||||
|
||||
internal var dataOptional: PaginationResult<T>? = preset
|
||||
private set
|
||||
@@ -35,7 +35,7 @@ class PagedComponentContext<T> internal constructor(
|
||||
fun loadNext() {
|
||||
iterationState.value = iterationState.value.let {
|
||||
if (dataState.value ?.isLastPage == true) return
|
||||
(it.first + 1) to it.second.nextPage()
|
||||
it.nextPage()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,10 +44,10 @@ class PagedComponentContext<T> internal constructor(
|
||||
*/
|
||||
fun loadPrevious() {
|
||||
iterationState.value = iterationState.value.let {
|
||||
if (it.second.isFirstPage) return
|
||||
(it.first - 1) to SimplePagination(
|
||||
it.second.page - 1,
|
||||
it.second.size
|
||||
if (it.isFirstPage) return
|
||||
SimplePagination(
|
||||
it.page - 1,
|
||||
it.size
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -57,7 +57,7 @@ class PagedComponentContext<T> internal constructor(
|
||||
*/
|
||||
fun reload() {
|
||||
iterationState.value = iterationState.value.let {
|
||||
it.copy(it.first + 1)
|
||||
SimplePagination(it.page, it.size)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -82,8 +82,8 @@ internal fun <T> PagedComponent(
|
||||
) {
|
||||
val context = remember { PagedComponentContext(preload, initialPage, size) }
|
||||
|
||||
LaunchedEffect(context.iterationState.value) {
|
||||
context.dataState.value = loader(context, context.iterationState.value.second)
|
||||
LaunchedEffect(context.iterationState.value.page, context.iterationState.value.hashCode()) {
|
||||
context.dataState.value = loader(context, context.iterationState.value)
|
||||
}
|
||||
|
||||
context.dataState.value ?.let {
|
||||
|
@@ -30,8 +30,8 @@ class InfinityPagedComponentTests {
|
||||
}
|
||||
) {
|
||||
if (it == null) {
|
||||
if (this.iterationState.value.second != null) {
|
||||
assertEquals(0, (this.iterationState.value.second as? SimplePagination) ?.page)
|
||||
if (this.iterationState.value != null) {
|
||||
assertEquals(0, (this.iterationState.value as? SimplePagination) ?.page)
|
||||
}
|
||||
} else {
|
||||
assertEquals(expectedList, it)
|
||||
|
Reference in New Issue
Block a user