Track Jetpack Compose component impressions in a simple and flexible way
Text(
"text",
Modifier.impression(
key = item,
impression = { key ->
println("impressed:$key")
})
)
You can customize DefaultImpressionState
's parameters.
val impressionState = remember {
DefaultImpressionState(
lifecycle = lifecycle,
impressionDuration = 1000,
checkInterval = 1000,
visibleRatio = 0.5,
)
}
Text(
"text",
Modifier.impression(
key = item,
impressionState = impressionState,
impression = { key ->
println("impressed:$key")
})
)
You can implement your impression logic.
val impressionState = remember {
object: ImpressionState {
override val impressFlow: SharedFlow<Any>
get() = TODO("write your logic!")
override fun onLayoutCoordinatesChange(
key: Any,
size: IntSize,
boundsRect: Rect,
composeViewRect: Rect
) {
TODO("write your logic!")
}
override fun onDispose(key: Any) {
TODO("write your logic!")
}
}
}
You can share impression state by CompositionLocal
ProvideImpressionState(rememberDefaultImpressionState()) {
Text(
"text",
Modifier.
impression(
key = "key",
impression = { key ->
println("impressed:$key")
})
)
}