Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added LocalStackAnimationProvider for configuring a default stack animation #571

Merged
merged 4 commits into from
Dec 16, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,8 +7,9 @@ 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.LocalStackAnimation
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
import com.arkivanov.decompose.hashString
import com.arkivanov.decompose.router.stack.ChildStack
Expand All @@ -25,7 +26,8 @@ fun <C : Any, T : Any> Children(

holder.retainStates(stack.getConfigurations())

val anim = animation ?: LocalStackAnimation.current.invoke()
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 @@ -7,19 +7,7 @@ import androidx.compose.ui.Modifier
import com.arkivanov.decompose.Child
import com.arkivanov.decompose.router.stack.ChildStack


val LocalStackAnimation = compositionLocalOf<DefaultStackAnimationProvider> {
object : DefaultStackAnimationProvider {
override fun <C : Any, T : Any> invoke(): StackAnimation<C, T> =
emptyStackAnimation()
}
}

interface DefaultStackAnimationProvider {
operator fun <C : Any, T : Any> invoke(): StackAnimation<C, T>
}

private fun <C : Any, T : Any> emptyStackAnimation(): StackAnimation<C, T> =
internal fun <C : Any, T : Any> emptyStackAnimation(): StackAnimation<C, T> =
EmptyStackAnimation()

/*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
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> =
emptyStackAnimation()
arkivanov marked this conversation as resolved.
Show resolved Hide resolved
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ 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.LocalStackAnimation
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
import com.arkivanov.essenty.backhandler.BackEvent
import com.arkivanov.essenty.backhandler.BackHandler
Expand Down Expand Up @@ -64,7 +65,8 @@ private class PredictiveBackAnimation<C : Any, T : Any>(
@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 fallBackAnimationOrDefault = animation ?: LocalStackAnimation.current.invoke()
val animationProvider = LocalStackAnimationProvider.current
val fallBackAnimation = animation ?: remember(animationProvider, animationProvider::provide) ?: emptyStackAnimation()

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