From a37c9328981dac6edcd146ab05441695cff9d113 Mon Sep 17 00:00:00 2001 From: Ben Weiss Date: Fri, 20 May 2022 21:18:25 +0100 Subject: [PATCH] Introduce view extension to track jank --- .../samples/apps/nowinandroid/ui/NiaApp.kt | 6 ++--- .../core/ui/JankStatsExtensions.kt | 24 +++++++++++++++++++ .../feature/interests/InterestsItem.kt | 3 +++ .../feature/interests/InterestsScreen.kt | 4 +++- 4 files changed, 33 insertions(+), 4 deletions(-) create mode 100644 core-ui/src/main/java/com/google/samples/apps/nowinandroid/core/ui/JankStatsExtensions.kt diff --git a/app/src/main/java/com/google/samples/apps/nowinandroid/ui/NiaApp.kt b/app/src/main/java/com/google/samples/apps/nowinandroid/ui/NiaApp.kt index d1b647a8b..991f09994 100644 --- a/app/src/main/java/com/google/samples/apps/nowinandroid/ui/NiaApp.kt +++ b/app/src/main/java/com/google/samples/apps/nowinandroid/ui/NiaApp.kt @@ -49,12 +49,12 @@ 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.metrics.performance.PerformanceMetricsState 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.theme.NiaTheme import com.google.samples.apps.nowinandroid.navigation.NiaNavHost @@ -66,10 +66,10 @@ import com.google.samples.apps.nowinandroid.navigation.TopLevelDestination @Composable fun NiaApp(windowSizeClass: WindowSizeClass) { NiaTheme { - val metricsStateHolder = PerformanceMetricsState.getForHierarchy(LocalView.current) + val localView = LocalView.current val navController = rememberNavController() navController.addOnDestinationChangedListener { _, destination, _ -> - metricsStateHolder.state?.addState("Navigation", "${destination.route}") + localView.addPerformanceMetricsState("Navigation", "${destination.route}") } val niaTopLevelNavigation = remember(navController) { NiaTopLevelNavigation(navController) diff --git a/core-ui/src/main/java/com/google/samples/apps/nowinandroid/core/ui/JankStatsExtensions.kt b/core-ui/src/main/java/com/google/samples/apps/nowinandroid/core/ui/JankStatsExtensions.kt new file mode 100644 index 000000000..f6064ec74 --- /dev/null +++ b/core-ui/src/main/java/com/google/samples/apps/nowinandroid/core/ui/JankStatsExtensions.kt @@ -0,0 +1,24 @@ +/* + * Copyright 2022 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.samples.apps.nowinandroid.core.ui + +import android.view.View +import androidx.metrics.performance.PerformanceMetricsState + +fun View.addPerformanceMetricsState(stateName: String, state: String) { + PerformanceMetricsState.getForHierarchy(this).state?.addState(stateName, state) +} diff --git a/feature-interests/src/main/java/com/google/samples/apps/nowinandroid/feature/interests/InterestsItem.kt b/feature-interests/src/main/java/com/google/samples/apps/nowinandroid/feature/interests/InterestsItem.kt index b9c2afadd..fc4f7d9ac 100644 --- a/feature-interests/src/main/java/com/google/samples/apps/nowinandroid/feature/interests/InterestsItem.kt +++ b/feature-interests/src/main/java/com/google/samples/apps/nowinandroid/feature/interests/InterestsItem.kt @@ -33,12 +33,14 @@ import androidx.compose.runtime.Composable import androidx.compose.ui.Alignment 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.tooling.preview.Preview import androidx.compose.ui.unit.Dp import androidx.compose.ui.unit.dp import coil.compose.AsyncImage import com.google.samples.apps.nowinandroid.core.ui.FollowButton +import com.google.samples.apps.nowinandroid.core.ui.addPerformanceMetricsState import com.google.samples.apps.nowinandroid.core.ui.theme.NiaTheme import com.google.samples.apps.nowinandroid.feature.interests.R.string @@ -54,6 +56,7 @@ fun InterestsItem( description: String = "", itemSeparation: Dp = 16.dp ) { + LocalView.current.addPerformanceMetricsState("InterestItem", name) Row( verticalAlignment = Alignment.CenterVertically, modifier = modifier diff --git a/feature-interests/src/main/java/com/google/samples/apps/nowinandroid/feature/interests/InterestsScreen.kt b/feature-interests/src/main/java/com/google/samples/apps/nowinandroid/feature/interests/InterestsScreen.kt index 7be087ccb..54fe3c9cf 100644 --- a/feature-interests/src/main/java/com/google/samples/apps/nowinandroid/feature/interests/InterestsScreen.kt +++ b/feature-interests/src/main/java/com/google/samples/apps/nowinandroid/feature/interests/InterestsScreen.kt @@ -33,10 +33,12 @@ import androidx.compose.runtime.collectAsState import androidx.compose.runtime.getValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier +import androidx.compose.ui.platform.LocalView import androidx.compose.ui.res.stringResource import androidx.compose.ui.unit.dp import androidx.hilt.navigation.compose.hiltViewModel import com.google.samples.apps.nowinandroid.core.ui.LoadingWheel +import com.google.samples.apps.nowinandroid.core.ui.addPerformanceMetricsState import com.google.samples.apps.nowinandroid.core.ui.component.NiaTab import com.google.samples.apps.nowinandroid.core.ui.component.NiaTabRow import com.google.samples.apps.nowinandroid.core.ui.component.NiaTopAppBar @@ -50,7 +52,6 @@ fun InterestsRoute( ) { val uiState by viewModel.uiState.collectAsState() val tabState by viewModel.tabState.collectAsState() - InterestsScreen( uiState = uiState, tabState = tabState, @@ -61,6 +62,7 @@ fun InterestsRoute( switchTab = viewModel::switchTab, modifier = modifier ) + LocalView.current.addPerformanceMetricsState("Interests", "$tabState") } @Composable