Fix a animation on the RibbonRoll when navigateBack

This commit is contained in:
Andres Gomez, Thomas (ITDV RL) 2025-05-12 09:59:25 +02:00
parent 292374d547
commit 2c4519af3b

View file

@ -23,14 +23,12 @@ import androidx.compose.material.Text
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.Stable import androidx.compose.runtime.Stable
import androidx.compose.runtime.remember import androidx.compose.runtime.saveable.SaverScope
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Shadow
import androidx.compose.ui.graphics.graphicsLayer import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.DpSize import androidx.compose.ui.unit.DpSize
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
@ -39,6 +37,7 @@ import kotlinx.coroutines.launch
import lwacharactersheet.composeapp.generated.resources.Res import lwacharactersheet.composeapp.generated.resources.Res
import lwacharactersheet.composeapp.generated.resources.ic_d20_24dp import lwacharactersheet.composeapp.generated.resources.ic_d20_24dp
import org.jetbrains.compose.resources.painterResource import org.jetbrains.compose.resources.painterResource
import androidx.compose.runtime.saveable.Saver as ComposeSaver
@Stable @Stable
data class CharacterRibbonRollUio( data class CharacterRibbonRollUio(
@ -64,6 +63,24 @@ class CharacterRibbonRollAnimation(
animatedRotation = Animatable(rotation), animatedRotation = Animatable(rotation),
animatedScale = Animatable(scale), animatedScale = Animatable(scale),
) )
object Saver : ComposeSaver<CharacterRibbonRollAnimation, List<Float>> {
override fun restore(value: List<Float>): CharacterRibbonRollAnimation {
return CharacterRibbonRollAnimation(
animatedAlpha = Animatable(value[0]),
animatedRotation = Animatable(value[1]),
animatedScale = Animatable(value[2]),
)
}
override fun SaverScope.save(value: CharacterRibbonRollAnimation): List<Float> {
return listOf(
value.animatedAlpha.value,
value.animatedRotation.value,
value.animatedScale.value,
)
}
}
} }
@Composable @Composable
@ -85,10 +102,12 @@ fun CharacterRibbonRoll(
} }
) { ) {
val animation = diceIconAnimation( val animation = diceIconAnimation(
key = it?.rollId ?: Unit, key = it?.rollId,
rollDelay = it?.hideDelay ?: 1000, rollDelay = it?.hideDelay ?: 1000,
) )
val color = animateColorAsState(targetValue = it?.tint ?: Color.Transparent) val color = animateColorAsState(
targetValue = it?.tint ?: Color.Transparent,
)
Box( Box(
modifier = Modifier.graphicsLayer { modifier = Modifier.graphicsLayer {
@ -124,10 +143,13 @@ fun CharacterRibbonRoll(
@Composable @Composable
private fun diceIconAnimation( private fun diceIconAnimation(
key: Any = Unit, key: String?,
rollDelay: Int, rollDelay: Int,
): CharacterRibbonRollAnimation { ): CharacterRibbonRollAnimation {
val animation = remember(key) { val animation = rememberSaveable(
key = key,
saver = CharacterRibbonRollAnimation.Saver,
) {
CharacterRibbonRollAnimation() CharacterRibbonRollAnimation()
} }
LaunchedEffect(key) { LaunchedEffect(key) {