Add SharedDelay feature to the AnimatedOffset

This commit is contained in:
Thomas Andres Gomez 2022-07-19 11:20:45 +02:00
parent 22abe55fce
commit 611374d800
2 changed files with 28 additions and 5 deletions

View file

@ -2,13 +2,14 @@ package com.pixelized.biblib.ui.composable.animation
import androidx.compose.animation.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.Stable
@Composable
fun AnimatedDelayer(
delay: Int = Delay.DELTA,
content: @Composable AnimatedDelayerScope.() -> Unit
) {
val scope = AnimatedDelayerScope(
val scope = IncreasedDelayerScope(
delay = Delay(value = delay)
)
scope.content()
@ -33,9 +34,31 @@ fun <T> AnimatedDelayer(
}
}
class AnimatedDelayerScope(
var delay: Delay = Delay()
)
@Composable
fun AnimatedDelayerScope.SharedDelay(content: @Composable AnimatedDelayerScope.() -> Unit) {
val localScope = SharedDelayerScope(delay = play())
localScope.content()
}
@Stable
interface AnimatedDelayerScope {
val delay: Delay
fun play(): Delay
}
@Stable
private class IncreasedDelayerScope(
override var delay: Delay
): AnimatedDelayerScope {
override fun play(): Delay = delay++
}
@Stable
private class SharedDelayerScope(
override var delay: Delay
): AnimatedDelayerScope {
override fun play(): Delay = delay
}
@JvmInline
value class Delay(

View file

@ -26,7 +26,7 @@ fun AnimatedDelayerScope.AnimatedOffset(
AnimatedOffset(
modifier = modifier,
transitionLabel = transitionLabel,
delay = delay++,
delay = play(),
contentAlignment = contentAlignment,
content = content,
)