Use rememberMetricsStateHolder for navigation

Change-Id: I3b038a9bbc6eed9b21343aa0fd75c044049386a4
pull/145/head
Tomáš Mlynarič 3 years ago
parent ccb08a1a06
commit a076a50010

@ -42,20 +42,21 @@ import androidx.compose.material3.windowsizeclass.WindowSizeClass
import androidx.compose.material3.windowsizeclass.WindowWidthSizeClass
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalView
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.navigation.NavController
import androidx.navigation.NavDestination
import androidx.navigation.NavDestination.Companion.hierarchy
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController
import com.google.samples.apps.nowinandroid.core.ui.ClearRippleTheme
import com.google.samples.apps.nowinandroid.core.ui.addPerformanceMetricsState
import com.google.samples.apps.nowinandroid.core.ui.component.NiaBackground
import com.google.samples.apps.nowinandroid.core.ui.rememberMetricsStateHolder
import com.google.samples.apps.nowinandroid.core.ui.theme.NiaTheme
import com.google.samples.apps.nowinandroid.navigation.NiaNavHost
import com.google.samples.apps.nowinandroid.navigation.NiaTopLevelNavigation
@ -66,11 +67,20 @@ import com.google.samples.apps.nowinandroid.navigation.TopLevelDestination
@Composable
fun NiaApp(windowSizeClass: WindowSizeClass) {
NiaTheme {
val localView = LocalView.current
val navController = rememberNavController()
navController.addOnDestinationChangedListener { _, destination, _ ->
localView.addPerformanceMetricsState("Navigation", "${destination.route}")
val metricsHolder = rememberMetricsStateHolder()
DisposableEffect(navController, metricsHolder) {
val listener = NavController.OnDestinationChangedListener { _, destination, _ ->
metricsHolder.state?.addState("Navigation", destination.route.toString())
}
navController.addOnDestinationChangedListener(listener)
onDispose {
navController.removeOnDestinationChangedListener(listener)
}
}
val niaTopLevelNavigation = remember(navController) {
NiaTopLevelNavigation(navController)
}

Loading…
Cancel
Save