Skip to content

Instantly share code, notes, and snippets.

@nuhkoca
Created June 29, 2022 07:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nuhkoca/ede72676aeb47960a5990ebbfdaea381 to your computer and use it in GitHub Desktop.
Save nuhkoca/ede72676aeb47960a5990ebbfdaea381 to your computer and use it in GitHub Desktop.
PerformancePulse
@Composable
fun PerformancePulse(
performancePulseUpdate: PerformancePulseUpdate,
onDispose: (() -> Unit)? = null
) {
val alpha = remember { Animatable(0f) }
LaunchedEffect(performancePulseUpdate) {
alpha.snapTo(0f)
if (performancePulseUpdate !is PerformancePulseUpdate.None) {
// Fade in
alpha.animateTo(
targetValue = 1f,
animationSpec = tween(
durationMillis = 200,
easing = CubicBezierEasing(0.5f, 0f, 0f, 0.9f)
)
)
// Fade out
alpha.animateTo(
targetValue = 0f,
animationSpec = tween(
durationMillis = 800,
easing = CubicBezierEasing(0f, 0f, 0.6f, 1f)
)
)
}
}
DisposableEffect(Unit) {
onDispose {
onDispose?.invoke()
}
}
val tint = when (performancePulseUpdate) {
is PerformancePulseUpdate.Up -> TRTheme.colors.primaryGreen2
is PerformancePulseUpdate.Down -> TRTheme.colors.primaryRed2
is PerformancePulseUpdate.None -> Color.Transparent // Not visible due alpha 0f
}
Box(modifier = Modifier.alpha(alpha.value)) {
TRIcon(
iconResId = TRTheme.icons.indicatorDefaultXS,
tint = tint
)
}
}
@Composable
private fun PerformancePulseContainer(performancePulseUpdateObservable: Observable<PerformancePulseUpdate>) {
val performancePulseUpdate by performancePulseUpdateObservable.subscribeAsState(PerformancePulseUpdate.None)
PerformancePulse1(performancePulseUpdate)
}
private val performancePulseUpdateObservable1 = getPerformancePulseUpdateObservable(
periodInMilliseconds = 500,
pulse = { PerformancePulseUpdate.Up }
)
fun getPerformancePulseUpdateObservable(
periodInMilliseconds: Long = 2000,
pulse: (Long) -> PerformancePulseUpdate
): Observable<PerformancePulseUpdate> {
return Observable.interval(periodInMilliseconds, periodInMilliseconds, TimeUnit.MILLISECONDS)
.map { value ->
pulse(value)
}
}
sealed class PerformancePulseUpdate : Serializable {
object Up : PerformancePulseUpdate()
object Down : PerformancePulseUpdate()
object None : PerformancePulseUpdate()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment