-
-
Save ashar-7/2cb7e6da44960453f6a9619594a34f79 to your computer and use it in GitHub Desktop.
This file contains 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 sealed class MeterHeadState(val progress: Float) { | |
object Low : MeterHeadState(progress = 0f) | |
object High : MeterHeadState(progress = 100f) | |
class OnIndex(progress: Float) : MeterHeadState(progress) | |
} | |
@Composable | |
private fun rememberMeterHeadState(value: MeterHeadState): MutableState<MeterHeadState> { | |
return remember { mutableStateOf(value) } | |
} | |
@Composable | |
private fun updateProgressTransition(index: FGIndex?): State<Float> { | |
var currentState by rememberMeterHeadState(MeterHeadState.Low) | |
val transition = updateTransition(currentState, label = "meter_head") | |
val progress = transition.animateFloat( | |
label = "progress", | |
transitionSpec = { | |
if (currentState is MeterHeadState.OnIndex) { | |
spring( | |
Spring.DampingRatioMediumBouncy, | |
stiffness = Spring.StiffnessVeryLow | |
) | |
} else { | |
tween(800, easing = FastOutSlowInEasing) | |
} | |
} | |
) { state -> state.progress } | |
LaunchedEffect(progress, index) { | |
val state = getUpdatedState(index, progress.value) | |
if (state != null) { | |
currentState = state | |
} | |
} | |
return remember(transition) { progress } | |
} | |
private fun getUpdatedState(index: FGIndex?, progress: Float): MeterHeadState? { | |
if (index != null) { | |
return MeterHeadState.OnIndex(index.value.toFloat()) | |
} | |
return when (progress) { | |
MeterHeadState.Low.progress -> MeterHeadState.High | |
MeterHeadState.High.progress -> MeterHeadState.Low | |
else -> null | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment