Skip to content

Instantly share code, notes, and snippets.

@sergenes
Created June 29, 2021 16:29
Show Gist options
  • Save sergenes/1fafa1a3c5440f3b6d61c75ff4f38b63 to your computer and use it in GitHub Desktop.
Save sergenes/1fafa1a3c5440f3b6d61c75ff4f38b63 to your computer and use it in GitHub Desktop.
data class FlipCardAnimation(
val peepHandler: () -> Unit
) {
private lateinit var flipState: MutableState<CardFlipState>
private lateinit var scaleXAnimation: State<Float>
private lateinit var scaleYAnimation: State<Float>
@Composable
fun Init() {
flipState = remember { mutableStateOf(CardFlipState.FRONT_FACE) }
scaleXAnimation = animateFloatAsState(
if (flipState.value == CardFlipState.FRONT_FACE ||
flipState.value == CardFlipState.BACK_FACE
) 1f else 0.8f,
animationSpec = animationSpec,
finishedListener = {
}
)
scaleYAnimation = animateFloatAsState(
if (flipState.value == CardFlipState.FRONT_FACE ||
flipState.value == CardFlipState.BACK_FACE
) 1f else 0.1f,
animationSpec = animationSpec,
finishedListener = {
if (flipState.value == CardFlipState.FLIP_BACK) {
flipState.value = CardFlipState.BACK_FACE
} else if (flipState.value == CardFlipState.FLIP_FRONT) {
flipState.value = CardFlipState.FRONT_FACE
}
}
)
}
private val animationSpec: TweenSpec<Float> = tween(
durationMillis = animationTime,
easing = LinearEasing
)
fun flipToBackSide() {
flipState.value = CardFlipState.FLIP_BACK
peepHandler.invoke()
}
fun flipToFrontSide() {
flipState.value = CardFlipState.FLIP_FRONT
}
fun backToInitState() {
flipState.value = CardFlipState.FRONT_FACE
}
fun isFrontSide(): Boolean {
return flipState.value == CardFlipState.FRONT_FACE
}
fun scaleX(): Float {
return scaleXAnimation.value
}
fun scaleY(): Float {
return scaleYAnimation.value
}
fun cardSide(): CardFlipState {
return flipState.value
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment