From 3bf2ed5168195cee53645cd4c6be3dd8c2dd4844 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Mon, 3 Mar 2025 08:57:40 +0600 Subject: [PATCH] fix of InfinityPagedComponent --- pagination/compose/build.gradle | 1 + .../src/commonMain/kotlin/InfinityPagedComponent.kt | 13 ++++++++----- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/pagination/compose/build.gradle b/pagination/compose/build.gradle index c3885146e2b..7087b3e456d 100644 --- a/pagination/compose/build.gradle +++ b/pagination/compose/build.gradle @@ -14,6 +14,7 @@ kotlin { dependencies { api project(":micro_utils.pagination.common") api project(":micro_utils.common.compose") + api libs.kt.coroutines } } } diff --git a/pagination/compose/src/commonMain/kotlin/InfinityPagedComponent.kt b/pagination/compose/src/commonMain/kotlin/InfinityPagedComponent.kt index 1af1fe28ab6..44a0259252f 100644 --- a/pagination/compose/src/commonMain/kotlin/InfinityPagedComponent.kt +++ b/pagination/compose/src/commonMain/kotlin/InfinityPagedComponent.kt @@ -17,7 +17,8 @@ class InfinityPagedComponentContext internal constructor( page: Int, size: Int ) { - internal val iterationState: MutableState> = mutableStateOf(0 to SimplePagination(page, size)) + internal val startPage = SimplePagination(page, size) + internal val iterationState: MutableState> = mutableStateOf(0 to null) internal val dataState: MutableState?> = mutableStateOf(null) internal var lastPageLoaded = false @@ -26,10 +27,11 @@ class InfinityPagedComponentContext internal constructor( */ fun loadNext() { if (lastPageLoaded) return + if (iterationState.value.second is SimplePagination) return // Data loading has been inited but not loaded yet iterationState.value = iterationState.value.let { - if ((dataState.value as? PaginationResult<*>) ?.isLastPage == true) return - (it.first + 1) to it.second.nextPage() + if ((it.second as? PaginationResult<*>) ?.isLastPage == true) return + (it.first + 1) to (it.second ?: startPage).nextPage() } } @@ -40,7 +42,7 @@ class InfinityPagedComponentContext internal constructor( dataState.value = null lastPageLoaded = false iterationState.value = iterationState.value.let { - (it.first + 1) to (it.second.firstPage()) + (it.first + 1) to null } } } @@ -64,10 +66,11 @@ internal fun InfinityPagedComponent( val context = remember { InfinityPagedComponentContext(page, size) } LaunchedEffect(context.iterationState.value.first) { - val paginationResult = loader(context, context.iterationState.value.second) + val paginationResult = loader(context, context.iterationState.value.second ?: context.startPage) if (paginationResult.isLastPage) { context.lastPageLoaded = true } + context.iterationState.value = context.iterationState.value.copy(second = paginationResult) context.dataState.value = (context.dataState.value ?: emptyList()) + paginationResult.results }