64 lines
2.1 KiB
Kotlin
64 lines
2.1 KiB
Kotlin
/*
|
|
* SPDX-FileCopyrightText: 2025 The Calyx Institute
|
|
* SPDX-License-Identifier: GPL-3.0-or-later
|
|
*/
|
|
|
|
package com.aurora.extensions
|
|
|
|
import androidx.compose.animation.core.FastOutSlowInEasing
|
|
import androidx.compose.animation.core.RepeatMode
|
|
import androidx.compose.animation.core.animateFloat
|
|
import androidx.compose.animation.core.infiniteRepeatable
|
|
import androidx.compose.animation.core.rememberInfiniteTransition
|
|
import androidx.compose.animation.core.tween
|
|
import androidx.compose.foundation.background
|
|
import androidx.compose.runtime.getValue
|
|
import androidx.compose.runtime.mutableStateOf
|
|
import androidx.compose.runtime.remember
|
|
import androidx.compose.runtime.setValue
|
|
import androidx.compose.ui.Modifier
|
|
import androidx.compose.ui.composed
|
|
import androidx.compose.ui.geometry.Offset
|
|
import androidx.compose.ui.graphics.Brush
|
|
import androidx.compose.ui.graphics.Color
|
|
import androidx.compose.ui.layout.onGloballyPositioned
|
|
import androidx.compose.ui.unit.IntSize
|
|
|
|
/**
|
|
* Modifier for composable to play shimmer animation
|
|
* @param toShow Whether to play shimmer animation
|
|
*/
|
|
fun Modifier.shimmer(toShow: Boolean): Modifier = composed {
|
|
if (toShow) {
|
|
val shimmerColors = listOf(
|
|
Color.LightGray.copy(alpha = 0.6f),
|
|
Color.LightGray.copy(alpha = 0.2f),
|
|
Color.LightGray.copy(alpha = 0.6f)
|
|
)
|
|
|
|
var size by remember { mutableStateOf(IntSize.Zero) }
|
|
|
|
val transition = rememberInfiniteTransition()
|
|
val transitionAnimation = transition.animateFloat(
|
|
initialValue = 0f,
|
|
targetValue = 1000f,
|
|
animationSpec = infiniteRepeatable(
|
|
animation = tween(
|
|
durationMillis = 1000,
|
|
easing = FastOutSlowInEasing
|
|
),
|
|
repeatMode = RepeatMode.Reverse
|
|
)
|
|
)
|
|
background(
|
|
brush = Brush.linearGradient(
|
|
colors = shimmerColors,
|
|
start = Offset.Zero,
|
|
end = Offset(x = transitionAnimation.value, y = transitionAnimation.value)
|
|
)
|
|
).onGloballyPositioned { size = it.size }
|
|
} else {
|
|
Modifier
|
|
}
|
|
}
|