Skip to content

Commit

Permalink
chore: Remove unneeded derivedStateOf calls + clarify
Browse files Browse the repository at this point in the history
Those are needed only if the given lambda reads another state.

It wasn't the case here, hence the removal.

This commit also introduces a single-use private function
that helps clarify that we want to show navigation while
we're in a top-level destination.
  • Loading branch information
LouisCAD committed Dec 16, 2024
1 parent 66f277b commit 519757d
Showing 1 changed file with 12 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ fun MainScaffold(
largeWindowTopAppBar: @Composable () -> Unit = {},
content: @Composable () -> Unit = {},
) {
val navType by rememberNavType(currentDestination)
val navType = rememberNavType(currentDestination)
MainScaffold(navType, currentDestination, navController::navigateToSelectedItem, largeWindowTopAppBar, content)
}

Expand Down Expand Up @@ -79,25 +79,25 @@ private fun MainScaffold(
private fun rememberNavType(
currentDestination: MainNavigation,
windowAdaptiveInfo: WindowAdaptiveInfo = LocalWindowAdaptiveInfo.current,
): State<NavigationSuiteType> {
): NavigationSuiteType {

val showNavigation by remember(currentDestination) {
derivedStateOf {
NavigationItem.entries.any { it.destination::class == currentDestination::class }
}
val showNavigation = remember(currentDestination) {
isDestinationInTopLevelNav(currentDestination)
}

return remember(showNavigation, windowAdaptiveInfo) {
derivedStateOf {
if (showNavigation) {
calculateFromAdaptiveInfo(windowAdaptiveInfo)
} else {
NavigationSuiteType.None
}
if (showNavigation) {
calculateFromAdaptiveInfo(windowAdaptiveInfo)
} else {
NavigationSuiteType.None
}
}
}

private fun isDestinationInTopLevelNav(destination: MainNavigation): Boolean {
return NavigationItem.entries.any { it.destination::class == destination::class }
}

private fun calculateFromAdaptiveInfo(windowAdaptiveInfo: WindowAdaptiveInfo): NavigationSuiteType {
return if (windowAdaptiveInfo.isWindowLarge()) NavigationSuiteType.NavigationRail else NavigationSuiteType.NavigationBar
}
Expand Down

0 comments on commit 519757d

Please sign in to comment.