Address review comments

* Introduce gradle plugin for firebase perf
* Create TrackScrollJank composable to mask tracking code
pull/145/head
Ben Weiss 2 years ago
parent c3ca38d31d
commit 9f929944d0
No known key found for this signature in database
GPG Key ID: 8424F9C1E763A74C

@ -24,7 +24,7 @@ plugins {
id("jacoco") id("jacoco")
id("dagger.hilt.android.plugin") id("dagger.hilt.android.plugin")
id("nowinandroid.spotless") id("nowinandroid.spotless")
id("com.google.firebase.firebase-perf") version "1.4.1" id("nowinandroid.firebase-perf")
} }
android { android {

@ -31,12 +31,12 @@ import javax.inject.Inject
@AndroidEntryPoint @AndroidEntryPoint
class MainActivity : ComponentActivity() { class MainActivity : ComponentActivity() {
/**
* Lazily inject [JankStats], which is used to track jank throughout the app.
*/
@Inject @Inject
lateinit var lazyStats: dagger.Lazy<JankStats> lateinit var lazyStats: dagger.Lazy<JankStats>
@Inject
lateinit var jankFrameListener: JankStats.OnFrameListener
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
@ -51,15 +51,11 @@ class MainActivity : ComponentActivity() {
override fun onResume() { override fun onResume() {
super.onResume() super.onResume()
isTrackingEnabled(true) lazyStats.get().isTrackingEnabled = true
} }
override fun onPause() { override fun onPause() {
super.onPause() super.onPause()
isTrackingEnabled(false) lazyStats.get().isTrackingEnabled = false
}
private fun isTrackingEnabled(enabled: Boolean) {
lazyStats.get().isTrackingEnabled = enabled
} }
} }

@ -67,7 +67,6 @@ import com.google.samples.apps.nowinandroid.navigation.TopLevelDestination
fun NiaApp(windowSizeClass: WindowSizeClass) { fun NiaApp(windowSizeClass: WindowSizeClass) {
NiaTheme { NiaTheme {
val navController = rememberNavController() val navController = rememberNavController()
val niaTopLevelNavigation = remember(navController) { val niaTopLevelNavigation = remember(navController) {
NiaTopLevelNavigation(navController) NiaTopLevelNavigation(navController)
} }

@ -69,5 +69,9 @@ gradlePlugin {
id = "nowinandroid.spotless" id = "nowinandroid.spotless"
implementationClass = "SpotlessConventionPlugin" implementationClass = "SpotlessConventionPlugin"
} }
register("firebase-perf") {
id = "nowinandroid.firebase-perf"
implementationClass = "FirebasePerfConventionPlugin"
}
} }
} }

@ -0,0 +1,29 @@
/*
* 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.
*/
import org.gradle.api.Plugin
import org.gradle.api.Project
class FirebasePerfConventionPlugin : Plugin<Project> {
override fun apply(target: Project) {
with(target) {
pluginManager.findPlugin("com.google.firebase.firebase-perf").apply {
version = "1.4.1"
}
}
}
}

@ -16,12 +16,14 @@
package com.google.samples.apps.nowinandroid.core.ui package com.google.samples.apps.nowinandroid.core.ui
import androidx.compose.foundation.gestures.ScrollableState
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.DisposableEffectResult import androidx.compose.runtime.DisposableEffectResult
import androidx.compose.runtime.DisposableEffectScope import androidx.compose.runtime.DisposableEffectScope
import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.remember import androidx.compose.runtime.remember
import androidx.compose.runtime.snapshotFlow
import androidx.compose.ui.platform.LocalView import androidx.compose.ui.platform.LocalView
import androidx.metrics.performance.PerformanceMetricsState import androidx.metrics.performance.PerformanceMetricsState
import androidx.metrics.performance.PerformanceMetricsState.MetricsStateHolder import androidx.metrics.performance.PerformanceMetricsState.MetricsStateHolder
@ -71,3 +73,18 @@ fun JankMetricDisposableEffect(
reportMetric(this, metrics) reportMetric(this, metrics)
} }
} }
@Composable
fun TrackScrollJank(scrollableState: ScrollableState, stateName: String) {
JankMetricEffect(scrollableState) { metricsHolder ->
snapshotFlow { scrollableState.isScrollInProgress }.collect { isScrollInProgress ->
metricsHolder.state?.apply {
if (isScrollInProgress) {
addState(stateName, "Scrolling=true")
} else {
removeState(stateName)
}
}
}
}
}

@ -38,7 +38,6 @@ import androidx.compose.material3.Surface
import androidx.compose.material3.Text import androidx.compose.material3.Text
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember import androidx.compose.runtime.remember
import androidx.compose.runtime.snapshotFlow
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip import androidx.compose.ui.draw.clip
@ -57,9 +56,7 @@ import com.google.samples.apps.nowinandroid.core.designsystem.icon.NiaIcons
import com.google.samples.apps.nowinandroid.core.designsystem.theme.NiaTheme import com.google.samples.apps.nowinandroid.core.designsystem.theme.NiaTheme
import com.google.samples.apps.nowinandroid.core.model.data.Author import com.google.samples.apps.nowinandroid.core.model.data.Author
import com.google.samples.apps.nowinandroid.core.model.data.FollowableAuthor import com.google.samples.apps.nowinandroid.core.model.data.FollowableAuthor
import com.google.samples.apps.nowinandroid.core.ui.JankMetricEffect import com.google.samples.apps.nowinandroid.core.ui.TrackScrollJank
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.combine
@Composable @Composable
fun AuthorsCarousel( fun AuthorsCarousel(
@ -69,21 +66,7 @@ fun AuthorsCarousel(
) { ) {
val lazyListState = rememberLazyListState() val lazyListState = rememberLazyListState()
JankMetricEffect(lazyListState) { metricsHolder -> TrackScrollJank(lazyListState, "ForYou:AuthorsCarousel")
combine(
snapshotFlow { lazyListState.isScrollInProgress },
snapshotFlow { lazyListState.firstVisibleItemIndex }
) { isScrollInProgress, firstVisibleItemIndex ->
if (isScrollInProgress) {
metricsHolder.state?.addState(
"ForYou:AuthorsCarousel:Scrolling",
"Index=$firstVisibleItemIndex"
)
} else {
metricsHolder.state?.removeState("ForYou:AuthorsCarousel:Scrolling")
}
}.collect()
}
LazyRow(modifier, lazyListState) { LazyRow(modifier, lazyListState) {
items(items = authors, key = { item -> item.author.id }) { followableAuthor -> items(items = authors, key = { item -> item.author.id }) { followableAuthor ->

@ -63,7 +63,6 @@ import androidx.compose.material3.windowsizeclass.WindowWidthSizeClass
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue import androidx.compose.runtime.getValue
import androidx.compose.runtime.snapshotFlow
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Color
@ -93,11 +92,9 @@ import com.google.samples.apps.nowinandroid.core.model.data.SaveableNewsResource
import com.google.samples.apps.nowinandroid.core.model.data.previewAuthors import com.google.samples.apps.nowinandroid.core.model.data.previewAuthors
import com.google.samples.apps.nowinandroid.core.model.data.previewNewsResources import com.google.samples.apps.nowinandroid.core.model.data.previewNewsResources
import com.google.samples.apps.nowinandroid.core.model.data.previewTopics import com.google.samples.apps.nowinandroid.core.model.data.previewTopics
import com.google.samples.apps.nowinandroid.core.ui.JankMetricEffect
import com.google.samples.apps.nowinandroid.core.ui.NewsResourceCardExpanded import com.google.samples.apps.nowinandroid.core.ui.NewsResourceCardExpanded
import com.google.samples.apps.nowinandroid.core.ui.TrackScrollJank
import kotlin.math.floor import kotlin.math.floor
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.combine
@Composable @Composable
fun ForYouRoute( fun ForYouRoute(
@ -168,21 +165,7 @@ fun ForYouScreen(
} }
val lazyListState = rememberLazyListState() val lazyListState = rememberLazyListState()
JankMetricEffect(lazyListState) { metricsHolder -> TrackScrollJank(lazyListState, "ForYou:Feed")
combine(
snapshotFlow { lazyListState.isScrollInProgress },
snapshotFlow { lazyListState.firstVisibleItemIndex },
) { isScrollInProgress, firstVisibleItemIndex ->
if (isScrollInProgress) {
metricsHolder.state?.addState(
"ForYou:Feed:Scrolling",
"index=$firstVisibleItemIndex"
)
} else {
metricsHolder.state?.removeState("ForYou:Feed:Scrolling")
}
}.collect()
}
LazyColumn( LazyColumn(
state = lazyListState, state = lazyListState,
@ -311,21 +294,8 @@ private fun TopicSelection(
modifier: Modifier = Modifier modifier: Modifier = Modifier
) { ) {
val lazyGridState = rememberLazyGridState() val lazyGridState = rememberLazyGridState()
JankMetricEffect(lazyGridState) { metricsHolder ->
combine( TrackScrollJank(scrollableState = lazyGridState, stateName = "ForYou:TopicSelection")
snapshotFlow { lazyGridState.isScrollInProgress },
snapshotFlow { lazyGridState.firstVisibleItemIndex },
) { isScrollInProgress, firstVisibleItemIndex ->
if (isScrollInProgress) {
metricsHolder.state?.addState(
"ForYou:TopicSelection:Scrolling",
"index=$firstVisibleItemIndex"
)
} else {
metricsHolder.state?.removeState("ForYou:TopicSelection:Scrolling")
}
}.collect()
}
LazyHorizontalGrid( LazyHorizontalGrid(
state = lazyGridState, state = lazyGridState,

Loading…
Cancel
Save