Skip to content

Instantly share code, notes, and snippets.

View livedata-compose.kt
/**
* 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) {
View dating-card-ui-props-instances.kt
// Scaled down to 0% (not visible)
private val queued = Props(
scale = 0f,
)
// Scaled down to 85%
private val bottom = Props(
scale = 0.85f,
)
View dating-cards-promote-all.kt
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
)
}
View dating-card-transition-handler.kt
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
View dating-card-ui-props.kt
data class Props(
val offsetX: Dp = 0.dp,
val scale: Float = 1f,
val rotationZ: Float = 0f,
val zIndex: Float = 0f,
)
View dating-cards-instance.kt
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"),
View dating-cards-demo.kt
lifecycle.coroutineScope.launchWhenStarted {
delay(1000)
repeat(3) {
delay(2000)
cards.indicateLike()
delay(1000)
cards.indicatePass()
delay(1000)
cards.votePass()
delay(1000)
View dating-card-ui-props-mapping.kt
private fun Cards.State.toProps() =
when (this) {
is Queued -> queued
is Bottom -> bottom
is Top -> top
is VoteLike -> voteLike
is VotePass -> votePass
}
View dating-cards-vote-like.kt
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
)
View dating-cards-screen-resolver.kt
internal object CardsOnScreenResolver : OnScreenStateResolver<State> {
override fun isOnScreen(state: State): Boolean =
when (state) {
is State.Bottom,
is State.Top -> true
is State.Queued,
is State.VoteLike,
is State.VotePass -> false
}
}