-
-
Notifications
You must be signed in to change notification settings - Fork 142
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
Velord
wants to merge
4
commits into
adrielcafe:main
Choose a base branch
from
Velord:feature/NavigatorExtended
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
samples/android/src/main/java/cafe/adriel/voyager/sample/transition/FadeScreen.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
samples/android/src/main/java/cafe/adriel/voyager/sample/transition/ScaleScreen.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
samples/android/src/main/java/cafe/adriel/voyager/sample/transition/ShrinkScreen.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) | ||
} | ||
} | ||
} |
165 changes: 165 additions & 0 deletions
165
samples/android/src/main/java/cafe/adriel/voyager/sample/transition/TransitionActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
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, | ||
) | ||
} |
59 changes: 59 additions & 0 deletions
59
samples/android/src/main/java/cafe/adriel/voyager/sample/transition/TransitionScreen.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
voyager-navigator/src/commonMain/kotlin/cafe/adriel/voyager/navigator/DefaultNavigator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) {} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
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
andtargetState
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.