From c6f3cdb1e40eb07e1ea602248e165e636eabf7c2 Mon Sep 17 00:00:00 2001 From: Jonas Schneider Date: Sun, 12 Apr 2026 23:25:17 +0200 Subject: [PATCH] fix: reduce grid rerenders in home sections --- .../ui/component/home/HomePageSectionView.kt | 9 ++---- .../main/subroute/home/HomeDynamicLayout.kt | 29 ++++++++----------- 2 files changed, 14 insertions(+), 24 deletions(-) diff --git a/composeApp/src/commonMain/kotlin/de/kitshn/ui/component/home/HomePageSectionView.kt b/composeApp/src/commonMain/kotlin/de/kitshn/ui/component/home/HomePageSectionView.kt index ebc9ca26..aff495c7 100644 --- a/composeApp/src/commonMain/kotlin/de/kitshn/ui/component/home/HomePageSectionView.kt +++ b/composeApp/src/commonMain/kotlin/de/kitshn/ui/component/home/HomePageSectionView.kt @@ -38,7 +38,7 @@ fun HomePageSectionView( onClickRecipe: (recipe: TandoorRecipeOverview) -> Unit ) { val lazyGridState = - rememberForeverLazyGridState(key = "RouteMainSubrouteHome/lazyGridState/${section.hashCode()}") + rememberForeverLazyGridState(key = "RouteMainSubrouteHome/lazyGridState/${section?.title ?: "loading"}") Column { Text( @@ -59,12 +59,7 @@ fun HomePageSectionView( verticalArrangement = Arrangement.spacedBy(16.dp) ) { if(section != null) { - items(section.recipeIds.size, key = { index -> - val recipeId = section.recipeIds[index] - val recipe = client?.container?.recipeOverview?.get(recipeId) - - recipe?.let { "recipe-$it" } ?: "index-$index" - }) { index -> + items(section.recipeIds.size, key = {index -> section.recipeIds[index]}) { index -> val recipeId = section.recipeIds[index] val recipe = client?.container?.recipeOverview?.get(recipeId) ?: return@items diff --git a/composeApp/src/commonMain/kotlin/de/kitshn/ui/route/main/subroute/home/HomeDynamicLayout.kt b/composeApp/src/commonMain/kotlin/de/kitshn/ui/route/main/subroute/home/HomeDynamicLayout.kt index e9a632c8..5e67fb54 100644 --- a/composeApp/src/commonMain/kotlin/de/kitshn/ui/route/main/subroute/home/HomeDynamicLayout.kt +++ b/composeApp/src/commonMain/kotlin/de/kitshn/ui/route/main/subroute/home/HomeDynamicLayout.kt @@ -130,27 +130,22 @@ fun HomeDynamicLayout( } } - LaunchedEffect(homePageSectionList.toList()) { - if(homePageSectionList.size < 2) return@LaunchedEffect - pageLoadingState = ErrorLoadingSuccessState.SUCCESS - } + LaunchedEffect(homePage, homePage?.sectionsStateList?.size) { + if (homePage == null) return@LaunchedEffect - LaunchedEffect(homePage?.sectionsStateList?.toList()) { - if(homePage == null) return@LaunchedEffect + val newSections = homePage!!.sections.onEach { section -> + section.recipeIds.removeIf { !p.vm.tandoorClient!!.container.recipeOverview.contains(it) } + } - homePageSectionList.clear() - homePageSectionList.addAll(homePage!!.sections) + if (homePageSectionList != newSections) { + homePageSectionList.clear() + homePageSectionList.addAll(newSections) + } - // keep page from loading indefinitely (prob. only happens when user has less than two recipes in space) - if(homePage?.sectionsStateList?.size == 0) + if (homePage!!.sectionsStateList.isEmpty() || homePageSectionList.size >= 2) { pageLoadingState = ErrorLoadingSuccessState.SUCCESS - - // remove deleted recipes - homePageSectionList.forEach { section -> - section.recipeIds.removeIf { - !p.vm.tandoorClient!!.container.recipeOverview.contains(it) - } } + } val isScrollingUp = scrollState.isScrollingUp() @@ -195,7 +190,7 @@ fun HomeDynamicLayout( } } - if(homePageSectionList.size == 0 && pageLoadingState != ErrorLoadingSuccessState.SUCCESS) { + if(homePageSectionList.isEmpty() && pageLoadingState != ErrorLoadingSuccessState.SUCCESS) { repeat(5) { HomePageSectionView( client = p.vm.tandoorClient,