Skip to content

Instantly share code, notes, and snippets.

import androidx.compose.ui.graphics.Color
// reds
val md_red_50 = Color(0xFFFFEBEE)
val md_red_100 = Color(0xFFFFCDD2)
val md_red_200 = Color(0xFFEF9A9A)
val md_red_300 = Color(0xFFE57373)
val md_red_400 = Color(0xFFEF5350)
val md_red_500 = Color(0xFFF44336)
val md_red_600 = Color(0xFFE53935)
/**
* Based on https://medium.com/swlh/android-mvi-with-jetpack-compose-b0890f5156ac
* Adapted for 0.1.0-dev04
*/
@Composable
fun <T> observe(data: LiveData<T>): T? {
var result by state { data.value }
val observer = remember { Observer<T> { result = it } }
onCommit(data) {
// Scaled down to 0% (not visible)
private val queued = Props(
scale = 0f,
)
// Scaled down to 85%
private val bottom = Props(
scale = 0.85f,
)
internal class PromoteAll<T : Any> : CardsOperation<T> {
override fun invoke(elements: CardsElements<T>): CardsElements<T> =
elements.map {
it.transitionTo(
// This uses State.next() we defined earlier:
newTargetState = it.targetState.next(),
operation = this
)
}
class CardsTransitionHandler<T>(
private val transitionSpec: TransitionSpec<Cards.State, Float> = {
spring(stiffness = Spring.StiffnessVeryLow)
}
) : ModifierTransitionHandler<T, Cards.State>() {
private fun Cards.State.toProps() =
when (this) {
is Queued -> queued
is Bottom -> bottom
data class Props(
val offsetX: Dp = 0.dp,
val scale: Float = 1f,
val rotationZ: Float = 0f,
val zIndex: Float = 0f,
)
val cards = Cards(
initialItems = listOf(
Routing.ProfileCard(0, "Lara"), // Will be top
Routing.ProfileCard(1, "Kate"), // Will be bottom
Routing.ProfileCard(2, "Brad"), // Queued = 0
Routing.ProfileCard(3, "Megan"), // ...etc.
Routing.ProfileCard(4, "Michael"),
Routing.ProfileCard(0, "Lara"),
Routing.ProfileCard(1, "Kate"),
Routing.ProfileCard(2, "Brad"),
lifecycle.coroutineScope.launchWhenStarted {
delay(1000)
repeat(3) {
delay(2000)
cards.indicateLike()
delay(1000)
cards.indicatePass()
delay(1000)
cards.votePass()
delay(1000)
private fun Cards.State.toProps() =
when (this) {
is Queued -> queued
is Bottom -> bottom
is Top -> top
is VoteLike -> voteLike
is VotePass -> votePass
}
class VoteLike<T : Any> internal constructor() : CardsOperation<T> {
// Returns a modified list of elements with new target states applied
override fun invoke(elements: CardsElements<T>): CardsElements<T> =
elements.map { element ->
if (element.targetState == Cards.State.Top) {
element.transitionTo(
newTargetState = Cards.State.VoteLike,
operation = this
)