Skip to content

Commit

Permalink
Merge pull request #571 from LionZXY/feature/localprovider
Browse files Browse the repository at this point in the history
Replace emptyStackAnimation with LocalStackAnimation
  • Loading branch information
arkivanov authored Dec 16, 2023
2 parents 683f8c6 + 67ccf1d commit 1a45926
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 4 deletions.
8 changes: 8 additions & 0 deletions extensions-compose/api/android/extensions-compose.api
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,14 @@ public final class com/arkivanov/decompose/extensions/compose/stack/animation/St
public static synthetic fun stackAnimation$default (ZLkotlin/jvm/functions/Function3;ILjava/lang/Object;)Lcom/arkivanov/decompose/extensions/compose/stack/animation/StackAnimation;
}

public abstract interface class com/arkivanov/decompose/extensions/compose/stack/animation/StackAnimationProvider {
public abstract fun provide ()Lcom/arkivanov/decompose/extensions/compose/stack/animation/StackAnimation;
}

public final class com/arkivanov/decompose/extensions/compose/stack/animation/StackAnimationProviderKt {
public static final fun getLocalStackAnimationProvider ()Landroidx/compose/runtime/ProvidableCompositionLocal;
}

public abstract interface class com/arkivanov/decompose/extensions/compose/stack/animation/StackAnimator {
public abstract fun invoke (Lcom/arkivanov/decompose/extensions/compose/stack/animation/Direction;ZLkotlin/jvm/functions/Function0;Lkotlin/jvm/functions/Function3;Landroidx/compose/runtime/Composer;I)V
}
Expand Down
8 changes: 8 additions & 0 deletions extensions-compose/api/jvm/extensions-compose.api
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,14 @@ public final class com/arkivanov/decompose/extensions/compose/stack/animation/St
public static synthetic fun stackAnimation$default (ZLkotlin/jvm/functions/Function3;ILjava/lang/Object;)Lcom/arkivanov/decompose/extensions/compose/stack/animation/StackAnimation;
}

public abstract interface class com/arkivanov/decompose/extensions/compose/stack/animation/StackAnimationProvider {
public abstract fun provide ()Lcom/arkivanov/decompose/extensions/compose/stack/animation/StackAnimation;
}

public final class com/arkivanov/decompose/extensions/compose/stack/animation/StackAnimationProviderKt {
public static final fun getLocalStackAnimationProvider ()Landroidx/compose/runtime/ProvidableCompositionLocal;
}

public abstract interface class com/arkivanov/decompose/extensions/compose/stack/animation/StackAnimator {
public abstract fun invoke (Lcom/arkivanov/decompose/extensions/compose/stack/animation/Direction;ZLkotlin/jvm/functions/Function0;Lkotlin/jvm/functions/Function3;Landroidx/compose/runtime/Composer;I)V
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import androidx.compose.runtime.saveable.SaveableStateHolder
import androidx.compose.runtime.saveable.rememberSaveableStateHolder
import androidx.compose.ui.Modifier
import com.arkivanov.decompose.Child
import com.arkivanov.decompose.extensions.compose.stack.animation.LocalStackAnimationProvider
import com.arkivanov.decompose.extensions.compose.stack.animation.StackAnimation
import com.arkivanov.decompose.extensions.compose.stack.animation.emptyStackAnimation
import com.arkivanov.decompose.extensions.compose.subscribeAsState
Expand All @@ -25,7 +26,8 @@ fun <C : Any, T : Any> Children(

holder.retainStates(stack.getConfigurations())

val anim = animation ?: emptyStackAnimation()
val animationProvider = LocalStackAnimationProvider.current
val anim = animation ?: remember(animationProvider, animationProvider::provide) ?: emptyStackAnimation()

anim(stack = stack, modifier = modifier) { child ->
holder.SaveableStateProvider(child.configuration.hashString()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.arkivanov.decompose.extensions.compose.stack.animation

import androidx.compose.foundation.layout.Box
import androidx.compose.runtime.Composable
import androidx.compose.runtime.compositionLocalOf
import androidx.compose.ui.Modifier
import com.arkivanov.decompose.Child
import com.arkivanov.decompose.router.stack.ChildStack
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.arkivanov.decompose.extensions.compose.stack.animation

import androidx.compose.runtime.compositionLocalOf

interface StackAnimationProvider {
fun <C : Any, T : Any> provide(): StackAnimation<C, T>?
}

val LocalStackAnimationProvider = compositionLocalOf<StackAnimationProvider> {
object : StackAnimationProvider {
override fun <C : Any, T : Any> provide(): StackAnimation<C, T>? = null
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import com.arkivanov.decompose.Child
import com.arkivanov.decompose.ExperimentalDecomposeApi
import com.arkivanov.decompose.extensions.compose.stack.animation.LocalStackAnimationProvider
import com.arkivanov.decompose.extensions.compose.stack.animation.StackAnimation
import com.arkivanov.decompose.extensions.compose.stack.animation.emptyStackAnimation
import com.arkivanov.decompose.router.stack.ChildStack
Expand Down Expand Up @@ -48,22 +49,24 @@ fun <C : Any, T : Any> predictiveBackAnimation(
): StackAnimation<C, T> =
PredictiveBackAnimation(
backHandler = backHandler,
animation = fallbackAnimation ?: emptyStackAnimation(),
animation = fallbackAnimation,
selector = selector,
onBack = onBack,
)

@OptIn(ExperimentalDecomposeApi::class)
private class PredictiveBackAnimation<C : Any, T : Any>(
private val backHandler: BackHandler,
private val animation: StackAnimation<C, T>,
private val animation: StackAnimation<C, T>?,
private val selector: (BackEvent, exitChild: Child.Created<C, T>, enterChild: Child.Created<C, T>) -> PredictiveBackAnimatable,
private val onBack: () -> Unit,
) : StackAnimation<C, T> {

@Composable
override fun invoke(stack: ChildStack<C, T>, modifier: Modifier, content: @Composable (child: Child.Created<C, T>) -> Unit) {
var activeConfigurations: Set<C> by remember { mutableStateOf(emptySet()) }
val animationProvider = LocalStackAnimationProvider.current
val fallBackAnimation = animation ?: remember(animationProvider, animationProvider::provide) ?: emptyStackAnimation()

val childContent =
remember(content) {
Expand Down Expand Up @@ -98,7 +101,7 @@ private class PredictiveBackAnimation<C : Any, T : Any>(
Box(modifier = modifier) {
items.forEach { item ->
key(item.key) {
animation(
fallBackAnimation(
stack = item.stack,
modifier = Modifier.fillMaxSize().then(item.modifier),
content = childContent,
Expand Down

0 comments on commit 1a45926

Please sign in to comment.