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 all 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,122 @@
package cafe.adriel.voyager.sample.transition

import android.util.Log
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
import cafe.adriel.voyager.navigator.Navigator
import cafe.adriel.voyager.navigator.NavigatorCreator
import cafe.adriel.voyager.navigator.NavigatorDisposeBehavior
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.channels.BufferOverflow
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch

fun SelfResetNavigatorCreator(resetAfter: Long): NavigatorCreator = {
screenCollection: List<Screen>,
s: String,
saveableStateHolder: SaveableStateHolder,
navigatorDisposeBehavior: NavigatorDisposeBehavior,
navigator: Navigator?, ->
SelfResetNavigator(resetAfter, screenCollection, s, saveableStateHolder, navigatorDisposeBehavior, navigator)
}

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

private val timeStamp = MutableSharedFlow<Unit>(
extraBufferCapacity = 1,
onBufferOverflow = BufferOverflow.DROP_OLDEST
)

private val scope = CoroutineScope(Job() + Dispatchers.Default)

init {
Log.d("@@@", "Init")
observeTimeStamp()
}

private fun observeTimeStamp() {
scope.launch {
timeStamp.collectLatest {
Log.d("@@@", "Before delay")
delay(resetAfter)
Log.d("@@@", "After delay")
if (canPop) {
Log.d("@@@", "Reset")
popAll()
}
}
}
}

override fun push(item: Screen) {
timeStamp.tryEmit(Unit)
stack.push(item)
}

override fun push(items: List<Screen>) {
timeStamp.tryEmit(Unit)
stack.push(items)
}

override fun replace(item: Screen) {
timeStamp.tryEmit(Unit)
stack.replace(item)
}

override fun replaceAll(item: Screen) {
timeStamp.tryEmit(Unit)
stack.replaceAll(item)
}

override fun replaceAll(items: List<Screen>) {
timeStamp.tryEmit(Unit)
stack.replaceAll(items)
}

override fun pop(): Boolean {
if (canPop) {
timeStamp.tryEmit(Unit)
}
return stack.pop()
}

override fun popAll() {
timeStamp.tryEmit(Unit)
stack.popAll()
}

override fun popUntil(predicate: (Screen) -> Boolean): Boolean {
timeStamp.tryEmit(Unit)
return stack.popUntil(predicate)
}

override fun plusAssign(item: Screen) {
timeStamp.tryEmit(Unit)
stack.plusAssign(item)
}

override fun plusAssign(items: List<Screen>) {
timeStamp.tryEmit(Unit)
stack.plusAssign(items)
}

override fun clearEvent() {
timeStamp.tryEmit(Unit)
stack.clearEvent()
}
}
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
)
}
}
}
Loading