Skip to content

Commit

Permalink
core: Expose LocalNavType with a compose provider
Browse files Browse the repository at this point in the history
  • Loading branch information
sirambd committed Aug 13, 2024
1 parent 91b4ff8 commit a7db537
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,21 @@ import androidx.compose.material3.adaptive.navigationsuite.NavigationSuiteDefaul
import androidx.compose.material3.adaptive.navigationsuite.NavigationSuiteScaffold
import androidx.compose.material3.adaptive.navigationsuite.NavigationSuiteScaffoldDefaults
import androidx.compose.material3.adaptive.navigationsuite.NavigationSuiteType
import androidx.compose.runtime.Composable
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.navigation.NavGraph.Companion.findStartDestination
import androidx.navigation.NavHostController
import com.infomaniak.swisstransfer.ui.navigation.MainNavigation
import com.infomaniak.swisstransfer.ui.navigation.NavigationItem
import com.infomaniak.swisstransfer.ui.theme.LocalCustomTypography
import com.infomaniak.swisstransfer.ui.theme.SwissTransferTheme
import com.infomaniak.swisstransfer.ui.theme.Typography
import com.infomaniak.swisstransfer.ui.utils.PreviewMobile
import com.infomaniak.swisstransfer.ui.utils.PreviewTablet

val LocalNavType = staticCompositionLocalOf { NavigationSuiteType.None }

@Composable
fun MainScaffold(
navController: NavHostController,
Expand All @@ -50,23 +51,11 @@ fun MainScaffold(
content: @Composable () -> Unit = {},
) {

val showNavigation by remember(currentDestination) {
derivedStateOf {
NavigationItem.entries.any { it.destination == currentDestination }
}
}
val navType by rememberNavType(currentDestination, windowAdaptiveInfo)

val navType by remember(showNavigation, windowAdaptiveInfo) {
derivedStateOf {
if (showNavigation) {
NavigationSuiteScaffoldDefaults.calculateFromAdaptiveInfo(windowAdaptiveInfo)
} else {
NavigationSuiteType.None
}
}
CompositionLocalProvider(LocalNavType provides navType) {
MainScaffold(navType, currentDestination, navController::navigateToSelectedItem, content)
}

MainScaffold(navType, currentDestination, navController::navigateToSelectedItem, content)
}

@Composable
Expand Down Expand Up @@ -123,6 +112,29 @@ private fun NavigationLabel(isNavigationBar: Boolean, navigationItem: Navigation
}
}

@Composable
private fun rememberNavType(
currentDestination: MainNavigation,
windowAdaptiveInfo: WindowAdaptiveInfo,
): State<NavigationSuiteType> {

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

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

private fun NavHostController.navigateToSelectedItem(destination: MainNavigation) {
navigate(destination) {
// Pop up to the start destination of the graph to avoid building up a large stack of destinations
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@ package com.infomaniak.swisstransfer.ui.screen.main.sent

import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import com.infomaniak.swisstransfer.ui.screen.main.LocalNavType

@Composable
fun SentScreen(navigateToDetails: (transferId: Int) -> Unit) {
Text("Sent screen")
val navType = LocalNavType.current

Text("Sent screen $navType")
}

0 comments on commit a7db537

Please sign in to comment.