This file contains hidden or 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
| import androidx.compose.foundation.layout.Column | |
| import androidx.compose.foundation.layout.fillMaxWidth | |
| import androidx.compose.foundation.layout.height | |
| import androidx.compose.foundation.lazy.LazyColumn | |
| import androidx.compose.foundation.lazy.rememberLazyListState | |
| import androidx.compose.runtime.Composable | |
| import androidx.compose.runtime.remember | |
| import androidx.compose.ui.Modifier | |
| import androidx.compose.ui.layout.ContentScale | |
| import androidx.compose.ui.unit.dp |
This file contains hidden or 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
| private class VisibilityTrackerElement( | |
| private val thresholdPercentage: Float, | |
| private val onVisibilityChanged: (VisibilityInfo) -> Unit, | |
| ) : ModifierNodeElement<VisibilityTrackerNode>() { | |
| override fun create(): VisibilityTrackerNode { | |
| return VisibilityTrackerNode(thresholdPercentage, onVisibilityChanged) | |
| } | |
| override fun update(node: VisibilityTrackerNode) { | |
| node.thresholdPercentage = thresholdPercentage |
This file contains hidden or 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
| private class VisibilityTrackerNode( | |
| var thresholdPercentage: Float, | |
| var onVisibilityChanged: (VisibilityInfo) -> Unit, | |
| ) : Modifier.Node(), GlobalPositionAwareModifierNode { | |
| private var previousVisibilityPercentage: Float? = null | |
| // Minimum visibility difference that triggers an update | |
| private val minimumVisibilityDelta = 0.01f | |
| override fun onGloballyPositioned(coordinates: LayoutCoordinates) { |
This file contains hidden or 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
| data class VisibilityInfo( | |
| val isVisible: Boolean, | |
| val visiblePercentage: Float, | |
| val bounds: Rect, | |
| val isAboveThreshold: Boolean | |
| ) |
This file contains hidden or 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
| fun Modifier.trackVisibility( | |
| thresholdPercentage: Float = 0.5f, | |
| onVisibilityChanged: (VisibilityInfo) -> Unit, | |
| ): Modifier = this then VisibilityTrackerElement(thresholdPercentage, onVisibilityChanged) |
This file contains hidden or 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
| // Modifier factory | |
| fun Modifier.circle(color: Color) = this then CircleElement(color) | |
| // ModifierNodeElement | |
| private data class CircleElement(val color: Color) : ModifierNodeElement<CircleNode>() { | |
| override fun create() = CircleNode(color) | |
| override fun update(node: CircleNode) { | |
| node.color = color | |
| } |
This file contains hidden or 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
| @Composable | |
| fun Modifier.pulsatingScale( | |
| pulseFraction: Float = 1.2f, | |
| duration: Int = 1000 | |
| ): Modifier { | |
| val infiniteTransition = rememberInfiniteTransition() | |
| val scale = infiniteTransition.animateFloat( | |
| initialValue = 1f, | |
| targetValue = pulseFraction, | |
| animationSpec = infiniteRepeatable( |
This file contains hidden or 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
| fun Modifier.shimmerEffect() = composed { | |
| val size = remember { mutableStateOf(IntSize(0, 0)) } | |
| val transition = rememberInfiniteTransition() | |
| val startOffsetX = transition.animateFloat( | |
| initialValue = -2 * size.value.width.toFloat(), | |
| targetValue = 2 * size.value.width.toFloat(), | |
| animationSpec = infiniteRepeatable( | |
| animation = tween(1000) | |
| ) | |
| ) |