Skip to content

Instantly share code, notes, and snippets.

@acious
Created December 12, 2024 17:39
Show Gist options
  • Select an option

  • Save acious/4b310b62502e9040e9f6ed5800f20400 to your computer and use it in GitHub Desktop.

Select an option

Save acious/4b310b62502e9040e9f6ed5800f20400 to your computer and use it in GitHub Desktop.
Android Compose - Composable(View) onExpose Implementation
@SuppressLint("CoroutineCreationDuringComposition", "UnnecessaryComposedModifier")
@Composable
fun Modifier.onExpose(
onVisibilityChanged: (Boolean) -> Unit,
exposeCriteriaSizeRate: Float = 1f,
): Modifier = composed {
this.onGloballyPositioned { layoutCoordinates ->
val boundsInWindow = layoutCoordinates.boundsInWindow()
val totalArea = layoutCoordinates.size.run { width * height }.toFloat()
val visibleArea = boundsInWindow.width * boundsInWindow.height
val visibilityRate = visibleArea / totalArea
onVisibilityChanged(visibilityRate >= exposeCriteriaSizeRate)
}
}
// Usage
Row(
modifier =
Modifier.onExpose(
onVisibilityChanged = { isVisible -> viewState.value = isVisible },
exposeCriteriaSizeRate = 0.5f,
),
) {
Text(
text = "이 뷰가 절반정도 보여지면 onVisibilityChanged 콜백 트리거링",
style = PrexTheme.typeScale.textXXS,
color = PrexTheme.fdsColor.contentAccentInfo,
maxLines = 1,
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment