-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7a3c55e
commit c6d5080
Showing
6 changed files
with
169 additions
and
3 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
app/src/main/java/com/loodos/samplecomposeandroid/di/JankStatsModule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.loodos.samplecomposeandroid.di | ||
|
||
import android.app.Activity | ||
import android.util.Log | ||
import android.view.Window | ||
import androidx.metrics.performance.JankStats | ||
import dagger.Module | ||
import dagger.Provides | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.android.components.ActivityComponent | ||
|
||
@Module | ||
@InstallIn(ActivityComponent::class) | ||
object JankStatsModule { | ||
@Provides | ||
fun providesOnFrameListener(): JankStats.OnFrameListener { | ||
return JankStats.OnFrameListener { frameData -> | ||
// Make sure to only log janky frames. | ||
if (frameData.isJank) { | ||
// We're currently logging this but would better report it to a backend. | ||
Log.v("SampleCompose Jank", frameData.toString()) | ||
} | ||
} | ||
} | ||
|
||
@Provides | ||
fun providesWindow(activity: Activity): Window { | ||
return activity.window | ||
} | ||
|
||
@Provides | ||
fun providesJankStats( | ||
window: Window, | ||
frameListener: JankStats.OnFrameListener, | ||
): JankStats { | ||
return JankStats.createAndTrack(window, frameListener) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
core/ui/src/main/java/com/loodos/ui/JankStatsExtensions.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package com.loodos.ui | ||
|
||
import androidx.compose.foundation.gestures.ScrollableState | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.DisposableEffect | ||
import androidx.compose.runtime.DisposableEffectResult | ||
import androidx.compose.runtime.DisposableEffectScope | ||
import androidx.compose.runtime.LaunchedEffect | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.snapshotFlow | ||
import androidx.compose.ui.platform.LocalView | ||
import androidx.metrics.performance.PerformanceMetricsState | ||
import kotlinx.coroutines.CoroutineScope | ||
|
||
/** | ||
* Retrieves [PerformanceMetricsState.Holder] from current [LocalView] and | ||
* remembers it until the View changes. | ||
* @see PerformanceMetricsState.getHolderForHierarchy | ||
*/ | ||
|
||
@Composable | ||
fun rememberMetricsStateHolder(): PerformanceMetricsState.Holder { | ||
val localView = LocalView.current | ||
|
||
return remember(localView) { | ||
PerformanceMetricsState.getHolderForHierarchy(localView) | ||
} | ||
} | ||
|
||
/** | ||
* Convenience function to work with [PerformanceMetricsState] state. The side effect is | ||
* re-launched if any of the [keys] value is not equal to the previous composition. | ||
* @see TrackDisposableJank if you need to work with DisposableEffect to cleanup added state. | ||
*/ | ||
|
||
@Composable | ||
fun TrackJank( | ||
vararg keys: Any?, | ||
reportMetric: suspend CoroutineScope.(state: PerformanceMetricsState.Holder) -> Unit, | ||
) { | ||
val metrics = rememberMetricsStateHolder() | ||
LaunchedEffect(metrics, *keys) { | ||
reportMetric(metrics) | ||
} | ||
} | ||
|
||
/** | ||
* Convenience function to work with [PerformanceMetricsState] state that needs to be cleaned up. | ||
* The side effect is re-launched if any of the [keys] value is not equal to the previous composition. | ||
*/ | ||
@Composable | ||
fun TrackDisposableJank( | ||
vararg keys: Any?, | ||
reportMetric: DisposableEffectScope.(state: PerformanceMetricsState.Holder) -> DisposableEffectResult, | ||
) { | ||
val metrics = rememberMetricsStateHolder() | ||
DisposableEffect(metrics, *keys) { | ||
reportMetric(this, metrics) | ||
} | ||
} | ||
|
||
|
||
|
||
/** | ||
* Track jank while scrolling anything that's scrollable. | ||
*/ | ||
@Composable | ||
fun TrackScrollJank(scrollableState: ScrollableState, stateName: String) { | ||
TrackJank(scrollableState) { metricsHolder -> | ||
snapshotFlow { scrollableState.isScrollInProgress }.collect { isScrollInProgress -> | ||
metricsHolder.state?.apply { | ||
if (isScrollInProgress) { | ||
putState(stateName, "Scrolling=true") | ||
} else { | ||
removeState(stateName) | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters