Skip to content

Commit 3b7e7e3

Browse files
committed
Use ReportDrawnWhen
Change-Id: I77b4db63f72f43c6c826e66358f7672792ef137d
1 parent 452ab50 commit 3b7e7e3

File tree

3 files changed

+5
-22
lines changed

3 files changed

+5
-22
lines changed

feature/foryou/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,5 @@ android {
2929
dependencies {
3030
implementation(libs.accompanist.flowlayout)
3131
implementation(libs.kotlinx.datetime)
32+
implementation(libs.androidx.activity.compose)
3233
}

feature/foryou/src/main/java/com/google/samples/apps/nowinandroid/feature/foryou/ForYouScreen.kt

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
package com.google.samples.apps.nowinandroid.feature.foryou
1818

19-
import android.app.Activity
19+
import androidx.activity.compose.ReportDrawnWhen
2020
import androidx.compose.animation.AnimatedVisibility
2121
import androidx.compose.animation.fadeIn
2222
import androidx.compose.animation.fadeOut
@@ -57,13 +57,11 @@ import androidx.compose.material3.MaterialTheme
5757
import androidx.compose.material3.Surface
5858
import androidx.compose.material3.Text
5959
import androidx.compose.runtime.Composable
60-
import androidx.compose.runtime.LaunchedEffect
6160
import androidx.compose.runtime.getValue
6261
import androidx.compose.ui.Alignment
6362
import androidx.compose.ui.Modifier
6463
import androidx.compose.ui.layout.layout
6564
import androidx.compose.ui.platform.LocalDensity
66-
import androidx.compose.ui.platform.LocalView
6765
import androidx.compose.ui.platform.testTag
6866
import androidx.compose.ui.res.painterResource
6967
import androidx.compose.ui.res.stringResource
@@ -73,7 +71,6 @@ import androidx.compose.ui.unit.dp
7371
import androidx.compose.ui.unit.max
7472
import androidx.compose.ui.unit.sp
7573
import androidx.compose.ui.util.trace
76-
import androidx.core.view.doOnPreDraw
7774
import androidx.hilt.navigation.compose.hiltViewModel
7875
import androidx.lifecycle.compose.collectAsStateWithLifecycle
7976
import com.google.samples.apps.nowinandroid.core.designsystem.component.DynamicAsyncImage
@@ -126,23 +123,8 @@ internal fun ForYouScreen(
126123
val isOnboardingLoading = onboardingUiState is OnboardingUiState.Loading
127124
val isFeedLoading = feedState is NewsFeedUiState.Loading
128125

129-
// Workaround to call Activity.reportFullyDrawn from Jetpack Compose.
130-
// This code should be called when the UI is ready for use
131-
// and relates to Time To Full Display.
132-
// TODO replace with ReportDrawnWhen { } once androidx.activity-compose 1.7.0 is used (currently alpha)
133-
if (!isSyncing && !isOnboardingLoading && !isFeedLoading) {
134-
val localView = LocalView.current
135-
// We use Unit to call reportFullyDrawn only on the first recomposition,
136-
// however it will be called again if this composable goes out of scope.
137-
// Activity.reportFullyDrawn() has its own check for this
138-
// and is safe to call multiple times though.
139-
LaunchedEffect(Unit) {
140-
// We're leveraging the fact, that the current view is directly set as content of Activity.
141-
val activity = localView.context as? Activity ?: return@LaunchedEffect
142-
// To be sure not to call in the middle of a frame draw.
143-
localView.doOnPreDraw { activity.reportFullyDrawn() }
144-
}
145-
}
126+
// This code should be called when the UI is ready for use and relates to Time To Full Display.
127+
ReportDrawnWhen { !isSyncing && !isOnboardingLoading && !isFeedLoading }
146128

147129
val state = rememberLazyGridState()
148130
TrackScrollJank(scrollableState = state, stateName = "forYou:feed")

gradle/libs.versions.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
accompanist = "0.28.0"
33
androidDesugarJdkLibs = "1.2.2"
44
androidGradlePlugin = "7.4.1"
5-
androidxActivity = "1.6.1"
5+
androidxActivity = "1.7.0"
66
androidxAppCompat = "1.5.1"
77
androidxBrowser = "1.4.0"
88
androidxComposeBom = "2023.01.00"

0 commit comments

Comments
 (0)