Skip to content

Commit

Permalink
update screen transition
Browse files Browse the repository at this point in the history
  • Loading branch information
shpasha committed May 22, 2024
1 parent 6f8fdf0 commit 2a68388
Show file tree
Hide file tree
Showing 10 changed files with 191 additions and 15 deletions.
1 change: 1 addition & 0 deletions samples/android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
<activity android:name=".nestedNavigation.NestedNavigationActivity"/>
<activity android:name=".parcelableScreen.ParcelableActivity"/>
<activity android:name=".screenModel.ScreenModelActivity"/>
<activity android:name=".screenTransition.ScreenTransitionActivity"/>
<activity android:name=".androidViewModel.AndroidViewModelActivity"/>
<activity android:name=".bottomSheetNavigation.BottomSheetNavigationActivity"/>
<activity android:name=".rxJavaIntegration.RxJavaIntegrationActivity"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import cafe.adriel.voyager.sample.nestedNavigation.NestedNavigationActivity
import cafe.adriel.voyager.sample.parcelableScreen.ParcelableActivity
import cafe.adriel.voyager.sample.rxJavaIntegration.RxJavaIntegrationActivity
import cafe.adriel.voyager.sample.screenModel.ScreenModelActivity
import cafe.adriel.voyager.sample.screenTransition.ScreenTransitionActivity
import cafe.adriel.voyager.sample.stateStack.StateStackActivity
import cafe.adriel.voyager.sample.tabNavigation.TabNavigationActivity

Expand Down Expand Up @@ -66,6 +67,7 @@ class SampleActivity : ComponentActivity() {
StartSampleButton<LiveDataIntegrationActivity>("LiveData Integration")
StartSampleButton<HiltMainActivity>("Hilt Integration")
StartSampleButton<LegacyActivity>("Legacy Integration")
StartSampleButton<ScreenTransitionActivity>("ScreenTransitionActivity")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package cafe.adriel.voyager.sample.screenTransition

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.material.Button
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import cafe.adriel.voyager.core.screen.Screen
import cafe.adriel.voyager.core.screen.ScreenKey
import cafe.adriel.voyager.navigator.LocalNavigator
import cafe.adriel.voyager.navigator.currentOrThrow

abstract class BaseSampleScreen : Screen {

abstract val index: Int

override val key: ScreenKey get() = "SampleScreen$index"

@Composable
override fun Content() {
val navigator = LocalNavigator.currentOrThrow

Column(
modifier = Modifier
.fillMaxSize()
.padding(40.dp),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center
) {
Text(text = "Screen $index")

Spacer(modifier = Modifier.height(20.dp))

Button(
modifier = Modifier.fillMaxWidth(),
onClick = {
navigator.push(
when {
index % 2 == 0 -> NoCustomAnimationSampleScreen(index = index + 1)
else -> FadeAnimationSampleScreen(index = index + 1)
}
)
}
) {
Text(text = "Next")
}

Button(
modifier = Modifier.fillMaxWidth(),
onClick = { navigator.pop() }
) {
Text(text = "Back")
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package cafe.adriel.voyager.sample.screenTransition

import androidx.compose.animation.EnterTransition
import androidx.compose.animation.ExitTransition
import androidx.compose.animation.core.tween
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import cafe.adriel.voyager.transitions.ScreenTransition

class FadeTransition : ScreenTransition {

override fun enter(isPop: Boolean): EnterTransition {
return fadeIn(tween(500, delayMillis = 500))
}

override fun exit(isPop: Boolean): ExitTransition {
return fadeOut(tween(500))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package cafe.adriel.voyager.sample.screenTransition

import cafe.adriel.voyager.transitions.ScreenTransition

data class NoCustomAnimationSampleScreen(
override val index: Int
) : BaseSampleScreen()

data class FadeAnimationSampleScreen(
override val index: Int
) : BaseSampleScreen(), ScreenTransition by FadeTransition()
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package cafe.adriel.voyager.sample.screenTransition

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.runtime.Composable
import cafe.adriel.voyager.navigator.Navigator
import cafe.adriel.voyager.transitions.ScreenTransition

class ScreenTransitionActivity : ComponentActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

setContent {
Content()
}
}

@Composable
fun Content() {
Navigator(
screen = NoCustomAnimationSampleScreen(0)
) {
ScreenTransition(
navigator = it,
defaultTransition = SlideTransition()
)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package cafe.adriel.voyager.sample.screenTransition

import androidx.compose.animation.EnterTransition
import androidx.compose.animation.ExitTransition
import androidx.compose.animation.slideIn
import androidx.compose.animation.slideOut
import androidx.compose.ui.unit.IntOffset
import cafe.adriel.voyager.transitions.ScreenTransition

class SlideTransition : ScreenTransition {

override fun enter(isPop: Boolean): EnterTransition {
return slideIn { size ->
val x = if (isPop) -size.width else size.width
IntOffset(x = x, y = 0)
}
}

override fun exit(isPop: Boolean): ExitTransition {
return slideOut { size ->
val x = if (isPop) size.width else -size.width
IntOffset(x = x, y = 0)
}
}
}
7 changes: 5 additions & 2 deletions voyager-transitions/api/android/voyager-transitions.api
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ public final class cafe/adriel/voyager/transitions/ComposableSingletons$ScreenTr
public static final field INSTANCE Lcafe/adriel/voyager/transitions/ComposableSingletons$ScreenTransitionKt;
public static field lambda-1 Lkotlin/jvm/functions/Function4;
public static field lambda-2 Lkotlin/jvm/functions/Function4;
public static field lambda-3 Lkotlin/jvm/functions/Function4;
public fun <init> ()V
public final fun getLambda-1$voyager_transitions_release ()Lkotlin/jvm/functions/Function4;
public final fun getLambda-2$voyager_transitions_release ()Lkotlin/jvm/functions/Function4;
public final fun getLambda-3$voyager_transitions_release ()Lkotlin/jvm/functions/Function4;
}

public final class cafe/adriel/voyager/transitions/ComposableSingletons$SlideTransitionKt {
Expand All @@ -48,11 +50,12 @@ public final class cafe/adriel/voyager/transitions/ScaleTransitionKt {
}

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 static fun enter (Lcafe/adriel/voyager/transitions/ScreenTransition;Z)Landroidx/compose/animation/EnterTransition;
public static fun exit (Lcafe/adriel/voyager/transitions/ScreenTransition;Z)Landroidx/compose/animation/ExitTransition;
}

public final class cafe/adriel/voyager/transitions/ScreenTransitionKt {
public static final fun ScreenTransition (Lcafe/adriel/voyager/navigator/Navigator;Lcafe/adriel/voyager/transitions/ScreenTransition;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;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
7 changes: 5 additions & 2 deletions voyager-transitions/api/desktop/voyager-transitions.api
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ public final class cafe/adriel/voyager/transitions/ComposableSingletons$ScreenTr
public static final field INSTANCE Lcafe/adriel/voyager/transitions/ComposableSingletons$ScreenTransitionKt;
public static field lambda-1 Lkotlin/jvm/functions/Function4;
public static field lambda-2 Lkotlin/jvm/functions/Function4;
public static field lambda-3 Lkotlin/jvm/functions/Function4;
public fun <init> ()V
public final fun getLambda-1$voyager_transitions ()Lkotlin/jvm/functions/Function4;
public final fun getLambda-2$voyager_transitions ()Lkotlin/jvm/functions/Function4;
public final fun getLambda-3$voyager_transitions ()Lkotlin/jvm/functions/Function4;
}

public final class cafe/adriel/voyager/transitions/ComposableSingletons$SlideTransitionKt {
Expand All @@ -48,11 +50,12 @@ public final class cafe/adriel/voyager/transitions/ScaleTransitionKt {
}

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 static fun enter (Lcafe/adriel/voyager/transitions/ScreenTransition;Z)Landroidx/compose/animation/EnterTransition;
public static fun exit (Lcafe/adriel/voyager/transitions/ScreenTransition;Z)Landroidx/compose/animation/ExitTransition;
}

public final class cafe/adriel/voyager/transitions/ScreenTransitionKt {
public static final fun ScreenTransition (Lcafe/adriel/voyager/navigator/Navigator;Lcafe/adriel/voyager/transitions/ScreenTransition;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;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 @@ -22,14 +22,14 @@ public interface ScreenTransition {
*
* Returns null when it should not define a transition for this screen.
*/
public fun enter(): EnterTransition? = null
public fun enter(isPop: Boolean): 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 fun exit(isPop: Boolean): ExitTransition? = null
}

public typealias ScreenTransitionContent = @Composable AnimatedVisibilityScope.(Screen) -> Unit
Expand All @@ -55,6 +55,26 @@ public fun ScreenTransition(
)
}

@Composable
public fun ScreenTransition(
navigator: Navigator,
defaultTransition: ScreenTransition,
modifier: Modifier = Modifier,
content: ScreenTransitionContent = { it.Content() }
) {
ScreenTransition(
navigator = navigator,
transition = {
val isPop = navigator.lastEvent == StackEvent.Pop
val enter = defaultTransition.enter(isPop) ?: EnterTransition.None
val exit = defaultTransition.exit(isPop) ?: ExitTransition.None
enter togetherWith exit
},
modifier = modifier,
content = content
)
}

@Composable
public fun ScreenTransition(
navigator: Navigator,
Expand All @@ -69,17 +89,14 @@ public fun ScreenTransition(

val isPop = navigator.lastEvent == StackEvent.Pop

val screenEnterTransition = if (isPop) {
(targetState as? ScreenTransition)?.enter()
} else {
(targetState as? ScreenTransition)?.enter()
val source = when (navigator.lastEvent) {
StackEvent.Pop, StackEvent.Replace -> initialState
else -> targetState
}

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

val screenExitTransition = (source as? ScreenTransition)?.exit(isPop)

if (screenExitTransition != null || screenEnterTransition != null) {
(screenEnterTransition ?: contentTransform.targetContentEnter) togetherWith
Expand Down

0 comments on commit 2a68388

Please sign in to comment.