From c216dba69d7cb64a2883aefc4e37cd8eafd32262 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Mon, 3 Mar 2025 21:16:34 +0600 Subject: [PATCH] hotfix in InfinityPagedComponent and PagedComponent --- .../kotlin/InfinityPagedComponent.kt | 16 ++++++++-------- .../src/commonMain/kotlin/PagedComponent.kt | 18 +++++++++--------- .../kotlin/InfinityPagedComponentTests.kt | 4 ++-- 3 files changed, 19 insertions(+), 19 deletions(-) diff --git a/pagination/compose/src/commonMain/kotlin/InfinityPagedComponent.kt b/pagination/compose/src/commonMain/kotlin/InfinityPagedComponent.kt index b94590f80e8..6ac79e351a2 100644 --- a/pagination/compose/src/commonMain/kotlin/InfinityPagedComponent.kt +++ b/pagination/compose/src/commonMain/kotlin/InfinityPagedComponent.kt @@ -18,7 +18,7 @@ class InfinityPagedComponentContext internal constructor( size: Int ) { internal val startPage = SimplePagination(page, size) - internal val iterationState: MutableState> = mutableStateOf(0 to null) + internal val iterationState: MutableState = mutableStateOf(null) internal val dataState: MutableState?> = mutableStateOf(null) internal var lastPageLoaded = false @@ -27,11 +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 + 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 internal constructor( dataState.value = null lastPageLoaded = false iterationState.value = iterationState.value.let { - (it.first + 1) to null + null } } } @@ -66,12 +66,12 @@ internal fun InfinityPagedComponent( ) { val context = remember { InfinityPagedComponentContext(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 } diff --git a/pagination/compose/src/commonMain/kotlin/PagedComponent.kt b/pagination/compose/src/commonMain/kotlin/PagedComponent.kt index 88127bf0563..e901ab1def0 100644 --- a/pagination/compose/src/commonMain/kotlin/PagedComponent.kt +++ b/pagination/compose/src/commonMain/kotlin/PagedComponent.kt @@ -23,7 +23,7 @@ class PagedComponentContext internal constructor( initialPage: Int, size: Int ) { - internal val iterationState: MutableState> = mutableStateOf(0 to SimplePagination(preset?.page ?: initialPage, preset?.size ?: size)) + internal val iterationState: MutableState = mutableStateOf(SimplePagination(preset?.page ?: initialPage, preset?.size ?: size)) internal var dataOptional: PaginationResult? = preset private set @@ -35,7 +35,7 @@ class PagedComponentContext 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 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 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 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 { diff --git a/pagination/compose/src/jvmTest/kotlin/InfinityPagedComponentTests.kt b/pagination/compose/src/jvmTest/kotlin/InfinityPagedComponentTests.kt index 0dfb82a7fe8..62600116fdb 100644 --- a/pagination/compose/src/jvmTest/kotlin/InfinityPagedComponentTests.kt +++ b/pagination/compose/src/jvmTest/kotlin/InfinityPagedComponentTests.kt @@ -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)