Skip to content

Commit 50a0388

Browse files
committed
fix: reduce unnecessary recompositions
1 parent 5c67c8d commit 50a0388

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

composeApp/src/wasmJsMain/kotlin/org/nsh07/nsh07/ui/homeScreen/AppHomeScreen.kt

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ fun AppHomeScreen(
3939
val motionScheme = motionScheme
4040

4141
val listState = rememberLazyListState()
42-
val firstVisibleItem by derivedStateOf { listState.firstVisibleItemIndex }
43-
val scrolledUp by derivedStateOf { listState.lastScrolledForward }
4442

4543
val paragraphs = remember {
4644
listOf(
@@ -74,11 +72,15 @@ fun AppHomeScreen(
7472
}
7573
val experienceCount = remember { experiences.size }
7674

77-
val cardPadding = remember { 16.dp }
75+
val cardPadding = 16.dp
7876

7977
val windowSizeClass = currentWindowAdaptiveInfo().windowSizeClass
8078

8179
if (windowSizeClass.isWidthAtLeastBreakpoint(WindowSizeClass.WIDTH_DP_EXPANDED_LOWER_BOUND)) {
80+
val aboutSectionVisible by remember { derivedStateOf { listState.firstVisibleItemIndex < paragraphCount + 1 } }
81+
val experienceSectionVisible by remember { derivedStateOf { listState.firstVisibleItemIndex in paragraphCount + 1..<paragraphCount + experienceCount + 2 } }
82+
val projectsSectionVisible by remember { derivedStateOf { listState.firstVisibleItemIndex >= paragraphCount + experienceCount + 2 } }
83+
8284
Row(
8385
modifier = Modifier
8486
.padding(horizontal = 48.dp)
@@ -92,23 +94,23 @@ fun AppHomeScreen(
9294
Spacer(Modifier.height(72.dp))
9395

9496
NavigationItem(
95-
selected = firstVisibleItem < paragraphCount + 1,
97+
selected = aboutSectionVisible,
9698
onClick = {
9799
scope.launch { listState.animateScrollToItem(0) }
98100
},
99101
label = { Text("About", style = typography.bodyMedium) },
100102
modifier = Modifier.offset(x = (-20).dp)
101103
)
102104
NavigationItem(
103-
selected = firstVisibleItem in paragraphCount + 1..<paragraphCount + experienceCount + 2,
105+
selected = experienceSectionVisible,
104106
onClick = {
105107
scope.launch { listState.animateScrollToItem(paragraphCount + 1) }
106108
},
107109
label = { Text("Experience", style = typography.bodyMedium) },
108110
modifier = Modifier.offset(x = (-20).dp)
109111
)
110112
NavigationItem(
111-
selected = firstVisibleItem >= paragraphCount + experienceCount + 2,
113+
selected = projectsSectionVisible,
112114
onClick = {
113115
scope.launch { listState.animateScrollToItem(paragraphCount + experienceCount + 2) }
114116
},
@@ -140,6 +142,12 @@ fun AppHomeScreen(
140142
}
141143
}
142144
} else {
145+
val scrolledUp by derivedStateOf { listState.lastScrolledForward }
146+
147+
val showTopBar by remember { derivedStateOf { listState.firstVisibleItemIndex > 1 } }
148+
val experienceSectionVisible by remember { derivedStateOf { listState.firstVisibleItemIndex in paragraphCount + 2..<paragraphCount + experienceCount + 3 } }
149+
val projectsSectionsVisible by remember { derivedStateOf { listState.firstVisibleItemIndex in paragraphCount + experienceCount + 3..1000 } }
150+
143151
val scrollBehavior = pinnedScrollBehavior()
144152
val hazeState = rememberHazeState(true)
145153
val topAppBarColors = topAppBarColors(
@@ -161,13 +169,13 @@ fun AppHomeScreen(
161169
Scaffold(
162170
topBar = {
163171
AnimatedVisibility(
164-
firstVisibleItem > 1,
172+
showTopBar,
165173
enter = slideInVertically(motionScheme.slowSpatialSpec(), initialOffsetY = { -it }),
166174
exit = slideOutVertically(motionScheme.slowSpatialSpec(), targetOffsetY = { -it })
167175
) {
168-
val topBarContent = when (firstVisibleItem) {
169-
in paragraphCount + 2..<paragraphCount + experienceCount + 3 -> 1
170-
in paragraphCount + experienceCount + 3..1000 -> 2
176+
val topBarContent = when {
177+
experienceSectionVisible -> 1
178+
projectsSectionsVisible -> 2
171179
else -> 0
172180
}
173181
TopAppBar(

0 commit comments

Comments
 (0)