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 new Stack that requires the caller #302

Closed
wants to merge 2 commits into from
Closed
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
4 changes: 4 additions & 0 deletions samples/android/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,7 @@ dependencies {

debugImplementation(libs.leakCanary)
}

tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile::class).all {
kotlinOptions.freeCompilerArgs = listOf("-Xcontext-receivers")
}
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 All @@ -76,7 +78,9 @@ class SampleActivity : ComponentActivity() {

Button(
onClick = { context.startActivity(Intent(this, T::class.java)) },
modifier = Modifier.fillMaxWidth().padding(vertical = 8.dp)
modifier = Modifier
.fillMaxWidth()
.padding(vertical = 8.dp)
) {
Text(text = text)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import cafe.adriel.voyager.core.screen.uniqueScreenKey
import cafe.adriel.voyager.hilt.getScreenModel
import cafe.adriel.voyager.navigator.LocalNavigator
import cafe.adriel.voyager.navigator.currentOrThrow
import cafe.adriel.voyager.navigator.push

class LegacyScreenOne : Screen {
override val key: ScreenKey = uniqueScreenKey
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import cafe.adriel.voyager.core.screen.uniqueScreenKey
import cafe.adriel.voyager.hilt.getScreenModel
import cafe.adriel.voyager.navigator.LocalNavigator
import cafe.adriel.voyager.navigator.currentOrThrow
import cafe.adriel.voyager.navigator.pop

class LegacyScreenTwo : Screen {
override val key: ScreenKey = uniqueScreenKey
Expand Down Expand Up @@ -47,7 +48,7 @@ class LegacyScreenTwo : Screen {
Spacer(modifier = Modifier.height(16.dp))

Button(
onClick = navigator::pop,
onClick = { navigator.pop() },
content = { Text(text = "Go to One") }
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import cafe.adriel.voyager.core.screen.Screen
import cafe.adriel.voyager.core.screen.ScreenKey
import cafe.adriel.voyager.core.screen.uniqueScreenKey
import cafe.adriel.voyager.navigator.LocalNavigator
import cafe.adriel.voyager.navigator.Navigator
import cafe.adriel.voyager.navigator.currentOrThrow
import cafe.adriel.voyager.navigator.pop
import cafe.adriel.voyager.sample.DetailsContent
import org.koin.androidx.compose.getViewModel
import org.koin.core.parameter.parametersOf
Expand All @@ -20,6 +22,8 @@ data class AndroidDetailsScreen(
val navigator = LocalNavigator.currentOrThrow
val viewModel = getViewModel<AndroidDetailsViewModel> { parametersOf(index) }

DetailsContent(viewModel, "Item #${viewModel.index}", navigator::pop)
DetailsContent(viewModel, "Item #${viewModel.index}") {
navigator.pop()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import cafe.adriel.voyager.core.screen.ScreenKey
import cafe.adriel.voyager.core.screen.uniqueScreenKey
import cafe.adriel.voyager.navigator.LocalNavigator
import cafe.adriel.voyager.navigator.currentOrThrow
import cafe.adriel.voyager.navigator.push
import cafe.adriel.voyager.sample.ListContent

class AndroidListScreen : Screen {
Expand All @@ -17,6 +18,8 @@ class AndroidListScreen : Screen {
val navigator = LocalNavigator.currentOrThrow
val viewModel = viewModel<AndroidListViewModel>()

ListContent(viewModel.items, onClick = { index -> navigator.push(AndroidDetailsScreen(index)) })
ListContent(viewModel.items, onClick = {
index -> navigator.push(AndroidDetailsScreen(index))
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ 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
import cafe.adriel.voyager.navigator.pop
import cafe.adriel.voyager.navigator.push
import cafe.adriel.voyager.navigator.replace

data class BasicNavigationScreen(
val index: Int,
Expand Down Expand Up @@ -62,7 +65,7 @@ data class BasicNavigationScreen(
) {
Button(
enabled = navigator.canPop,
onClick = navigator::pop,
onClick = { navigator.pop() },
modifier = Modifier.weight(.5f)
) {
Text(text = "Pop")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import cafe.adriel.voyager.core.screen.uniqueScreenKey
import cafe.adriel.voyager.hilt.getScreenModel
import cafe.adriel.voyager.navigator.LocalNavigator
import cafe.adriel.voyager.navigator.currentOrThrow
import cafe.adriel.voyager.navigator.pop
import cafe.adriel.voyager.sample.DetailsContent

data class HiltDetailsScreen(
Expand All @@ -28,6 +29,8 @@ data class HiltDetailsScreen(
factory.create(index)
}

DetailsContent(viewModel, "Item #${viewModel.index}", navigator::pop)
DetailsContent(viewModel, "Item #${viewModel.index}") {
navigator.pop()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import cafe.adriel.voyager.core.screen.Screen
import cafe.adriel.voyager.hilt.getViewModel
import cafe.adriel.voyager.navigator.LocalNavigator
import cafe.adriel.voyager.navigator.currentOrThrow
import cafe.adriel.voyager.navigator.push
import cafe.adriel.voyager.sample.ListContent

class HiltListScreen : Screen {
Expand All @@ -17,6 +18,6 @@ class HiltListScreen : Screen {
// Uncomment version below if you want to use ScreenModel
// val viewModel: HiltListScreenModel = getScreenModel()

ListContent(viewModel.items, onClick = { index -> navigator.push(HiltDetailsScreen(index)) })
ListContent(viewModel.items, onClick = { index -> navigator.push(HiltDetailsScreen(index)) } )
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class NestedNavigationActivity : ComponentActivity() {
NestedNavigation(backgroundColor = Color.White) { navigator ->
CurrentScreen()
Button(
onClick = { navigator.popUntilRoot() },
onClick = { navigator.popUntilRoot(navigator.lastItem) },
modifier = Modifier.padding(bottom = 16.dp)
) {
Text(text = "Pop Until Root")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ 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
import cafe.adriel.voyager.navigator.pop
import cafe.adriel.voyager.navigator.push
import cafe.adriel.voyager.navigator.replace
import kotlinx.parcelize.Parcelize

@Parcelize
Expand Down Expand Up @@ -70,7 +73,7 @@ data class SampleParcelableScreen(
) {
Button(
enabled = navigator.canPop,
onClick = navigator::pop,
onClick = { navigator.pop() },
modifier = Modifier.weight(.5f)
) {
Text(text = "Pop")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import cafe.adriel.voyager.core.screen.ScreenKey
import cafe.adriel.voyager.core.screen.uniqueScreenKey
import cafe.adriel.voyager.navigator.LocalNavigator
import cafe.adriel.voyager.navigator.currentOrThrow
import cafe.adriel.voyager.navigator.pop
import cafe.adriel.voyager.sample.DetailsContent
import cafe.adriel.voyager.sample.LoadingContent

Expand All @@ -28,7 +29,9 @@ data class DetailsScreen(

when (val result = state) {
is DetailsScreenModel.State.Loading -> LoadingContent()
is DetailsScreenModel.State.Result -> DetailsContent(screenModel, result.item, navigator::pop)
is DetailsScreenModel.State.Result -> DetailsContent(screenModel, result.item) {
navigator.pop()
}
}

LaunchedEffect(currentCompositeKeyHash) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import cafe.adriel.voyager.core.screen.ScreenKey
import cafe.adriel.voyager.core.screen.uniqueScreenKey
import cafe.adriel.voyager.navigator.LocalNavigator
import cafe.adriel.voyager.navigator.currentOrThrow
import cafe.adriel.voyager.navigator.push
import cafe.adriel.voyager.sample.ListContent

class ListScreen : Screen {
Expand All @@ -18,6 +19,8 @@ class ListScreen : Screen {
val navigator = LocalNavigator.currentOrThrow
val screenModel = rememberScreenModel { ListScreenModel() }

ListContent(screenModel.items, onClick = { index -> navigator.push(DetailsScreen(index)) })
ListContent(screenModel.items, onClick = {
index -> navigator.push(DetailsScreen(index)) }
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import androidx.compose.foundation.layout.RowScope
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.LazyItemScope
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.material.Button
import androidx.compose.material.Text
Expand Down Expand Up @@ -83,35 +82,35 @@ class StateStackActivity : ComponentActivity() {
if (stateStack.lastItemOrNull == selectedItem) {
selectItem("")
}
stateStack.pop()
stateStack.pop("Pop")
}
ActionButton(text = "Pop Until", enabled = stateStack.canPop) {
if (selectedItem.isBlank()) {
Toast.makeText(context, "Select an item first", Toast.LENGTH_SHORT).show()
return@ActionButton
}
selectItem("")
stateStack.popUntil { it == selectedItem }
stateStack.popUntil("Pop Until") { it == selectedItem }
}
ActionButton(text = "Push") {
stateStack.push(randomValue)
stateStack.push("Push", randomValue)
}
}
Row(
modifier = Modifier.weight(.1f)
) {
ActionButton(text = "Replace") {
stateStack.replace(randomValue)
stateStack.replace("Replace", randomValue)
}
ActionButton(text = "Replace All") {
stateStack.replaceAll(randomValue)
stateStack.replaceAll("Replace All", randomValue)
}
}
}
}

@Composable
private fun LazyItemScope.ListItem(
private fun ListItem(
index: Int,
item: String,
isLast: Boolean,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class TabNavigationActivity : ComponentActivity() {

BottomNavigationItem(
selected = tabNavigator.current.key == tab.key,
onClick = { tabNavigator.current = tab },
onClick = { tabNavigator.setCurrent(invoker = tabNavigator.current, newTab = tab) },
icon = { Icon(painter = tab.options.icon!!, contentDescription = tab.options.title) }
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ fun Tab.TabContent() {
Column {
InnerTabNavigation()
screen.Content()
Log.d("Navigator", "Last Event: ${navigator.lastEvent}")
Log.d("Navigator", "Last Event: ${navigator.lastAction.event}")
}
}
}
Expand Down Expand Up @@ -65,7 +65,7 @@ private fun RowScope.TabNavigationButton(

Button(
enabled = tabNavigator.current.key != tab.key,
onClick = { tabNavigator.current = tab },
onClick = { tabNavigator.setCurrent(invoker = tabNavigator.current, newTab = tab) },
modifier = Modifier.weight(1f)
) {
Text(text = tab.options.title)
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
)
}
}
}
Loading
Loading