Skip to content

Commit

Permalink
Per screen transition experimental api (#424)
Browse files Browse the repository at this point in the history
* Per Screen transition experimental API

* Update transitionSpec to avoid behavior change

* fix lint

* update api dump
  • Loading branch information
DevSrSouza authored May 21, 2024
1 parent 309e6c7 commit 764388a
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
5 changes: 5 additions & 0 deletions voyager-transitions/api/android/voyager-transitions.api
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ public final class cafe/adriel/voyager/transitions/ScaleTransitionKt {
public static final fun ScaleTransition (Lcafe/adriel/voyager/navigator/Navigator;Landroidx/compose/ui/Modifier;Landroidx/compose/animation/core/FiniteAnimationSpec;Lkotlin/jvm/functions/Function4;Landroidx/compose/runtime/Composer;II)V
}

public final class cafe/adriel/voyager/transitions/ScreenTransition$DefaultImpls {
public static fun enter (Lcafe/adriel/voyager/transitions/ScreenTransition;)Landroidx/compose/animation/EnterTransition;
public static fun exit (Lcafe/adriel/voyager/transitions/ScreenTransition;)Landroidx/compose/animation/ExitTransition;
}

public final class cafe/adriel/voyager/transitions/ScreenTransitionKt {
public static final fun ScreenTransition (Lcafe/adriel/voyager/navigator/Navigator;Lkotlin/jvm/functions/Function1;Landroidx/compose/ui/Modifier;Lkotlin/jvm/functions/Function4;Landroidx/compose/runtime/Composer;II)V
public static final fun ScreenTransition (Lcafe/adriel/voyager/navigator/Navigator;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;Landroidx/compose/ui/Modifier;Lkotlin/jvm/functions/Function4;Landroidx/compose/runtime/Composer;II)V
Expand Down
5 changes: 5 additions & 0 deletions voyager-transitions/api/desktop/voyager-transitions.api
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ public final class cafe/adriel/voyager/transitions/ScaleTransitionKt {
public static final fun ScaleTransition (Lcafe/adriel/voyager/navigator/Navigator;Landroidx/compose/ui/Modifier;Landroidx/compose/animation/core/FiniteAnimationSpec;Lkotlin/jvm/functions/Function4;Landroidx/compose/runtime/Composer;II)V
}

public final class cafe/adriel/voyager/transitions/ScreenTransition$DefaultImpls {
public static fun enter (Lcafe/adriel/voyager/transitions/ScreenTransition;)Landroidx/compose/animation/EnterTransition;
public static fun exit (Lcafe/adriel/voyager/transitions/ScreenTransition;)Landroidx/compose/animation/ExitTransition;
}

public final class cafe/adriel/voyager/transitions/ScreenTransitionKt {
public static final fun ScreenTransition (Lcafe/adriel/voyager/navigator/Navigator;Lkotlin/jvm/functions/Function1;Landroidx/compose/ui/Modifier;Lkotlin/jvm/functions/Function4;Landroidx/compose/runtime/Composer;II)V
public static final fun ScreenTransition (Lcafe/adriel/voyager/navigator/Navigator;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;Landroidx/compose/ui/Modifier;Lkotlin/jvm/functions/Function4;Landroidx/compose/runtime/Composer;II)V
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,34 @@ import androidx.compose.animation.AnimatedContent
import androidx.compose.animation.AnimatedContentTransitionScope
import androidx.compose.animation.AnimatedVisibilityScope
import androidx.compose.animation.ContentTransform
import androidx.compose.animation.EnterTransition
import androidx.compose.animation.ExitTransition
import androidx.compose.animation.togetherWith
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import cafe.adriel.voyager.core.annotation.ExperimentalVoyagerApi
import cafe.adriel.voyager.core.screen.Screen
import cafe.adriel.voyager.core.stack.StackEvent
import cafe.adriel.voyager.navigator.Navigator

@ExperimentalVoyagerApi
public interface ScreenTransition {

/**
* Defines the enter transition for the Screen.
*
* Returns null when it should not define a transition for this screen.
*/
public fun enter(): EnterTransition? = null

/**
* Defines the exit transition for the Screen.
*
* Returns null when it should not define a transition for this screen.
*/
public fun exit(): ExitTransition? = null
}

public typealias ScreenTransitionContent = @Composable AnimatedVisibilityScope.(Screen) -> Unit

@Composable
Expand Down Expand Up @@ -42,7 +64,30 @@ public fun ScreenTransition(
) {
AnimatedContent(
targetState = navigator.lastItem,
transitionSpec = transition,
transitionSpec = {
val contentTransform = transition()

val isPop = navigator.lastEvent == StackEvent.Pop

val screenEnterTransition = if (isPop) {
(targetState as? ScreenTransition)?.enter()
} else {
(targetState as? ScreenTransition)?.enter()
}

val screenExitTransition = if (isPop) {
(initialState as? ScreenTransition)?.exit()
} else {
(initialState as? ScreenTransition)?.exit()
}

if (screenExitTransition != null || screenEnterTransition != null) {
(screenEnterTransition ?: contentTransform.targetContentEnter) togetherWith
(screenExitTransition ?: contentTransform.initialContentExit)
} else {
contentTransform
}
},
modifier = modifier
) { screen ->
navigator.saveableState("transition", screen) {
Expand Down

0 comments on commit 764388a

Please sign in to comment.