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

Add ability to using miscellaneous Navigator #326

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
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
1 change: 1 addition & 0 deletions samples/android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<activity android:name=".basicNavigation.BasicNavigationActivity"/>
<activity android:name=".tabNavigation.TabNavigationActivity"/>
<activity android:name=".nestedNavigation.NestedNavigationActivity"/>
<activity android:name=".transition.TransitionActivity"/>
<activity android:name=".parcelableScreen.ParcelableActivity"/>
<activity android:name=".screenModel.ScreenModelActivity"/>
<activity android:name=".androidViewModel.AndroidViewModelActivity"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import cafe.adriel.voyager.sample.rxJavaIntegration.RxJavaIntegrationActivity
import cafe.adriel.voyager.sample.screenModel.ScreenModelActivity
import cafe.adriel.voyager.sample.stateStack.StateStackActivity
import cafe.adriel.voyager.sample.tabNavigation.TabNavigationActivity
import cafe.adriel.voyager.sample.transition.TransitionActivity

class SampleActivity : ComponentActivity() {

Expand All @@ -58,6 +59,7 @@ class SampleActivity : ComponentActivity() {
StartSampleButton<TabNavigationActivity>("Tab Navigation")
StartSampleButton<BottomSheetNavigationActivity>("BottomSheet Navigation")
StartSampleButton<NestedNavigationActivity>("Nested Navigation")
StartSampleButton<TransitionActivity>("Transition")
StartSampleButton<AndroidViewModelActivity>("Android ViewModel")
StartSampleButton<ScreenModelActivity>("ScreenModel")
StartSampleButton<KoinIntegrationActivity>("Koin Integration")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package cafe.adriel.voyager.sample.transition

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.sp
import cafe.adriel.voyager.core.screen.Screen
import cafe.adriel.voyager.core.screen.uniqueScreenKey

data object FadeScreen : Screen {
override val key = uniqueScreenKey

@Composable
override fun Content() {
Box(modifier = Modifier.fillMaxSize()) {
Text(
text = "Fade Screen",
modifier = Modifier.align(alignment = Alignment.Center),
color = Color.Red,
fontSize = 30.sp
)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package cafe.adriel.voyager.sample.transition

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.sp
import cafe.adriel.voyager.core.screen.Screen
import cafe.adriel.voyager.core.screen.uniqueScreenKey

data object ScaleScreen : Screen {
override val key = uniqueScreenKey

@Composable
override fun Content() {
Box(modifier = Modifier.fillMaxSize()) {
Text(
text = "Scale Screen",
modifier = Modifier.align(alignment = Alignment.Center),
color = Color.Red,
fontSize = 30.sp
)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package cafe.adriel.voyager.sample.transition

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.sp
import cafe.adriel.voyager.core.screen.Screen
import cafe.adriel.voyager.core.screen.uniqueScreenKey

data object ShrinkScreen : Screen {
override val key = uniqueScreenKey

@Composable
override fun Content() {
Box(modifier = Modifier.fillMaxSize()) {
Text(
text = "Shrink Screen",
modifier = Modifier.align(alignment = Alignment.Center),
color = Color.Red,
fontSize = 30.sp
)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
package cafe.adriel.voyager.sample.transition

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.animation.AnimatedContentTransitionScope
import androidx.compose.animation.ContentTransform
import androidx.compose.animation.core.FiniteAnimationSpec
import androidx.compose.animation.core.LinearEasing
import androidx.compose.animation.core.keyframes
import androidx.compose.animation.core.tween
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.animation.scaleIn
import androidx.compose.animation.scaleOut
import androidx.compose.animation.shrinkVertically
import androidx.compose.animation.slideInHorizontally
import androidx.compose.animation.slideInVertically
import androidx.compose.animation.slideOutHorizontally
import androidx.compose.animation.slideOutVertically
import androidx.compose.animation.togetherWith
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.IntOffset
import androidx.compose.ui.unit.IntSize
import cafe.adriel.voyager.core.screen.Screen
import cafe.adriel.voyager.navigator.Navigator
import cafe.adriel.voyager.navigator.isPop
import cafe.adriel.voyager.navigator.isPush
import cafe.adriel.voyager.transitions.ScreenTransition
import cafe.adriel.voyager.transitions.ScreenTransitionContent

class TransitionActivity : ComponentActivity() {

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

setContent {
Navigator(TransitionScreen) {
TransitionDemo(it)
}
}
}
}

@Composable
fun TransitionDemo(
navigator: Navigator,
modifier: Modifier = Modifier,
content: ScreenTransitionContent = { it.Content() }
) {
val transition: AnimatedContentTransitionScope<Screen>.() -> ContentTransform = {
// Define any StackEvent you want transition to be
val isPush = navigator.lastAction.isPush()
val isPop = navigator.lastAction.isPop()
// Define any Screen you want transition must be from
val isInvokerTransitionScreen = navigator.lastAction?.invoker == TransitionScreen
val isInvokerFadeScreen = navigator.lastAction?.invoker == FadeScreen
val isInvokerShrinkScreen = navigator.lastAction?.invoker == ShrinkScreen
val isInvokerScaleScreen = navigator.lastAction?.invoker == ScaleScreen
// Define any Screen you want transition must be to
val isTargetTransitionScreen = navigator.lastItem == TransitionScreen
val isTargetFadeScreen = navigator.lastItem == FadeScreen
val isTargetShrinkScreen = navigator.lastItem == ShrinkScreen
val isTargetScaleScreen = navigator.lastItem == ScaleScreen
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
val isPush = navigator.lastAction.isPush()
val isPop = navigator.lastAction.isPop()
// Define any Screen you want transition must be from
val isInvokerTransitionScreen = navigator.lastAction?.invoker == TransitionScreen
val isInvokerFadeScreen = navigator.lastAction?.invoker == FadeScreen
val isInvokerShrinkScreen = navigator.lastAction?.invoker == ShrinkScreen
val isInvokerScaleScreen = navigator.lastAction?.invoker == ScaleScreen
// Define any Screen you want transition must be to
val isTargetTransitionScreen = navigator.lastItem == TransitionScreen
val isTargetFadeScreen = navigator.lastItem == FadeScreen
val isTargetShrinkScreen = navigator.lastItem == ShrinkScreen
val isTargetScaleScreen = navigator.lastItem == ScaleScreen
val invoker = this.initialState
val target = this.targetState
// Define any StackEvent you want transition to be
val isPush = navigator.lastEvent == StackEvent.Push
val isPop = navigator.lastEvent == StackEvent.Pop
// Define any Screen you want transition must be from
val isInvokerTransitionScreen = invoker == TransitionScreen
val isInvokerFadeScreen = invoker == FadeScreen
val isInvokerShrinkScreen = invoker == ShrinkScreen
val isInvokerScaleScreen = invoker == ScaleScreen
// Define any Screen you want transition must be to
val isTargetTransitionScreen = target == TransitionScreen
val isTargetFadeScreen = target == FadeScreen
val isTargetShrinkScreen = target == ShrinkScreen
val isTargetScaleScreen = target == ScaleScreen

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did not know about that API. I have checked, works as expected. Thank you!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like if you can open a PR with this transitions examples using the initialState and targetState API to we have on our oficial Voyager samples.
Could be also the sample base that we could use to create a new Transition API for Screens and test on it.


val tweenOffset: FiniteAnimationSpec<IntOffset> = tween(
durationMillis = 2000,
delayMillis = 100,
easing = LinearEasing
)
val tweenSize: FiniteAnimationSpec<IntSize> = tween(
durationMillis = 2000,
delayMillis = 100,
easing = LinearEasing
)

val sizeDefault = ({ size: Int -> size })
val sizeMinus = ({ size: Int -> -size })
val (initialOffset, targetOffset) = when {
isPush && isInvokerTransitionScreen -> {
if (isTargetFadeScreen || isTargetShrinkScreen) sizeMinus to sizeDefault
else sizeDefault to sizeMinus
}
isPop && isInvokerFadeScreen && isTargetTransitionScreen -> sizeDefault to sizeMinus
else -> sizeDefault to sizeMinus
}

val fadeInFrames = keyframes {
durationMillis = 2000
0.1f at 0 with LinearEasing
0.2f at 1800 with LinearEasing
1.0f at 2000 with LinearEasing
}
val fadeOutFrames = keyframes {
durationMillis = 2000
0.9f at 0 with LinearEasing
0.8f at 100 with LinearEasing
0.7f at 200 with LinearEasing
0.6f at 300 with LinearEasing
0.5f at 400 with LinearEasing
0.4f at 500 with LinearEasing
0.3f at 600 with LinearEasing
0.2f at 1000 with LinearEasing
0.1f at 1500 with LinearEasing
0.0f at 2000 with LinearEasing
}

val scaleInFrames = keyframes {
durationMillis = 2000
0.1f at 0 with LinearEasing
0.3f at 1500 with LinearEasing
1.0f at 2000 with LinearEasing
}
val scaleOutFrames = keyframes {
durationMillis = 2000
0.9f at 0 with LinearEasing
0.7f at 500 with LinearEasing
0.3f at 700 with LinearEasing
0.0f at 2000 with LinearEasing
}

when {
// Define any transition you want based on the StackEvent, invoker and target
isPush && isInvokerTransitionScreen && isTargetFadeScreen ||
isPop && isInvokerFadeScreen && isTargetTransitionScreen -> {
val enter = slideInHorizontally(tweenOffset, initialOffset) + fadeIn(fadeInFrames)
val exit = slideOutHorizontally(tweenOffset, targetOffset) + fadeOut(fadeOutFrames)
enter togetherWith exit
}
isPush && isInvokerTransitionScreen && isTargetShrinkScreen ||
isPop && isInvokerShrinkScreen && isTargetTransitionScreen -> {
val enter = slideInVertically(tweenOffset, initialOffset)
val exit = shrinkVertically(animationSpec = tweenSize, shrinkTowards = Alignment.Top)
enter togetherWith exit
}
isPush && isInvokerTransitionScreen && isTargetScaleScreen -> {
val enter = slideInVertically(tweenOffset, initialOffset) + fadeIn(fadeInFrames) + scaleIn(scaleInFrames)
val exit = slideOutVertically(tweenOffset, targetOffset) + fadeOut(fadeOutFrames) + scaleOut(scaleOutFrames)
enter togetherWith exit
}
isPop && isInvokerScaleScreen && isTargetTransitionScreen -> {
val enter = slideInHorizontally(tweenOffset, initialOffset) + fadeIn(fadeInFrames) + scaleIn(scaleInFrames)
val exit = slideOutHorizontally(tweenOffset, targetOffset) + fadeOut(fadeOutFrames) + scaleOut(scaleOutFrames)
enter togetherWith exit
}
else -> {
val animationSpec: FiniteAnimationSpec<IntOffset> = tween(
durationMillis = 500,
delayMillis = 100,
easing = LinearEasing
)
slideInHorizontally(animationSpec, initialOffset) togetherWith
slideOutHorizontally(animationSpec, targetOffset)
}
}
}
ScreenTransition(
navigator = navigator,
transition = transition,
modifier = modifier,
content = content,
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package cafe.adriel.voyager.sample.transition

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.height
import androidx.compose.foundation.layout.sizeIn
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.uniqueScreenKey
import cafe.adriel.voyager.navigator.LocalNavigator
import cafe.adriel.voyager.navigator.currentOrThrow

data object TransitionScreen : Screen {

override val key = uniqueScreenKey

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

Column(
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally,
modifier = Modifier.fillMaxSize()
) {
PushButton(text = "Push fade left\nPop fade right") {
navigator.push(FadeScreen)
}
Spacer(modifier = Modifier.height(50.dp))
PushButton(text = "Push shrink top\nPop shrink bottom") {
navigator.push(ShrinkScreen)
}
Spacer(modifier = Modifier.height(50.dp))
PushButton(text = "Push fade scale bottom\nPop scale right") {
navigator.push(ScaleScreen)
}
}
}
}

@Composable
private fun PushButton(
text: String,
onClick: () -> Unit
) {
Button(
onClick = onClick,
modifier = Modifier.sizeIn(minWidth = 200.dp, minHeight = 70.dp)
) {
Text(text = text)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import cafe.adriel.voyager.core.screen.Screen
* Navigator Saver that forces all Screens be [Parcelable], if not, it will throw a exception while trying to save
* the navigator state.
*/
public fun parcelableNavigatorSaver(): NavigatorSaver<Any> =
public fun parcelableNavigatorSaver(
navigatorCreator: NavigatorCreator = summonNavigatorCreator()
): NavigatorSaver<Any> =
NavigatorSaver { _, key, stateHolder, disposeBehavior, parent ->
listSaver(
save = { navigator ->
Expand All @@ -28,7 +30,7 @@ public fun parcelableNavigatorSaver(): NavigatorSaver<Any> =

screenAsParcelables
},
restore = { items -> Navigator(items as List<Screen>, key, stateHolder, disposeBehavior, parent) }
restore = { items -> navigatorCreator(items as List<Screen>, key, stateHolder, disposeBehavior, parent) }
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package cafe.adriel.voyager.navigator

import androidx.compose.runtime.saveable.SaveableStateHolder
import cafe.adriel.voyager.core.annotation.InternalVoyagerApi
import cafe.adriel.voyager.core.screen.Screen
import cafe.adriel.voyager.core.stack.Stack
import cafe.adriel.voyager.core.stack.toMutableStateStack

public class DefaultNavigator @InternalVoyagerApi constructor(
screens: List<Screen>,
key: String,
stateHolder: SaveableStateHolder,
disposeBehavior: NavigatorDisposeBehavior,
parent: Navigator? = null,
stack: Stack<Screen> = screens.toMutableStateStack(minSize = 1)
) : Navigator(screens, key, stateHolder, disposeBehavior, parent, stack) {

override var lastAction: StackLastAction<Screen>?
get() = TODO("Not yet implemented")
set(value) {}
}
Loading
Loading