Skip to content

Instantly share code, notes, and snippets.

@sergenes
Last active July 3, 2021 16:21
Show Gist options
  • Save sergenes/b32eae8a8594853b58bc66c07cad16b5 to your computer and use it in GitHub Desktop.
Save sergenes/b32eae8a8594853b58bc66c07cad16b5 to your computer and use it in GitHub Desktop.
data class StudyCardDeckEvents(
val cardWidth: Float,
val cardHeight: Float,
val model: StudyCardDeckModel,
val peepHandler: () -> Unit,
val playHandler: (String, String) -> Unit,
val nextHandler: () -> Unit,
val actionCallback: (String) -> Unit = {}
) {
val cardSwipe: CardSwipeAnimation = CardSwipeAnimation(
model = model,
cardWidth = cardWidth,
cardHeight = cardHeight
)
val flipCard = FlipCardAnimation(peepHandler)
val cardsInDeck = CardsInDeckAnimation(paddingOffset, model.count)
@SuppressLint("ModifierFactoryExtensionFunction")
fun makeCardModifier(
coroutineScope: CoroutineScope,
topCardIndex: Int,
idx: Int
): Modifier {
return if (idx > topCardIndex) {
Modifier
.scale(cardsInDeck.scaleX(idx), 1f)
.offset { cardsInDeck.offset(idx) }
} else {
Modifier
.scale(flipCard.scaleX(), flipCard.scaleY())
.offset { cardSwipe.toIntOffset() }
.pointerInput(Unit) {
detectDragGestures(
onDragEnd = {
cardSwipe.animateToTarget(
coroutineScope,
CardSwipeState.DRAGGING
) {
if (it) {
nextHandler()
coroutineScope.launch {
flipCard.backToInitState()
}
}
cardsInDeck.backToInitState()
}
},
onDrag = { change, _ ->
cardSwipe.draggingCard(coroutineScope, change) {
cardsInDeck.pushBackToTheFront()
}
}
)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment