Skip to content

Commit

Permalink
Replace voyager with androidx.navigation
Browse files Browse the repository at this point in the history
  • Loading branch information
ogaclejapan committed Jun 2, 2024
1 parent 49d011e commit bc2cae8
Show file tree
Hide file tree
Showing 16 changed files with 425 additions and 228 deletions.
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ compose-multiplatform = "1.6.10"
dokka = "1.9.20"
jbx-core-bundle = "1.0.0"
jbx-lifecycle = "2.8.0"
jbx-navigation = "2.7.0-alpha07"
jbx-savedstate = "1.2.0"
kotlin = "1.9.23"
kotlinx-coroutines = "1.8.0"
Expand All @@ -32,6 +33,7 @@ compose-ui-tooling-preview = { module = "androidx.compose.ui:ui-tooling-preview"
jbx-core-bundle = { module = "org.jetbrains.androidx.core:core-bundle", version.ref = "jbx-core-bundle" }
jbx-lifecycle-viewmodel-compose = { module = "org.jetbrains.androidx.lifecycle:lifecycle-viewmodel-compose", version.ref = "jbx-lifecycle" }
jbx-lifecycle-viewmodel-savedstate = { module = "org.jetbrains.androidx.lifecycle:lifecycle-viewmodel-savedstate", version.ref = "jbx-lifecycle" }
jbx-navigation-compose = { module = "org.jetbrains.androidx.navigation:navigation-compose", version.ref = "jbx-navigation" }
jbx-savedstate = { module = "org.jetbrains.androidx.savedstate:savedstate", version.ref = "jbx-savedstate" }
kotlin-test = { module = "org.jetbrains.kotlin:kotlin-test", version.ref = "kotlin" }
kotlinx-coroutines-core = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version.ref = "kotlinx-coroutines" }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package soil.playground.router

import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember

@Composable
inline fun NavLink(
to: String,
router: NavRouter = LocalNavRouter.current,
content: @Composable (NavLinkHandle) -> Unit
) {
val handle: NavLinkHandle = remember(to) { { router.push(to) } }
content(handle)
}

@Composable
inline fun <T : NavRoute> NavLink(
to: T,
router: NavRouter = LocalNavRouter.current,
content: @Composable (NavLinkHandle) -> Unit
) {
val handle: NavLinkHandle = remember(to) { { router.push<T>(to) } }
content(handle)
}

typealias NavLinkHandle = () -> Unit
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package soil.playground.router

import androidx.compose.runtime.Stable
import androidx.compose.runtime.staticCompositionLocalOf

@Stable
interface NavRouter {
fun push(route: String)

fun <T : NavRoute> push(route: T)

fun back(): Boolean

fun canBack(): Boolean
}

interface NavRoute

private val noRouter = object : NavRouter {
override fun push(route: String) = Unit
override fun <T : NavRoute> push(route: T) = Unit
override fun back() = false
override fun canBack() = false
}

val LocalNavRouter = staticCompositionLocalOf<NavRouter> {
noRouter
}
3 changes: 1 addition & 2 deletions sample/composeApp/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,7 @@ kotlin {
implementation(libs.ktor.core)
implementation(libs.ktor.client.content.negotiation)
implementation(libs.ktor.serialization.kotlinx.json)
implementation(libs.voyager.navigator)
implementation(libs.voyager.screenModel)
implementation(libs.jbx.navigation.compose)
}

androidMain.dependencies {
Expand Down

This file was deleted.

79 changes: 49 additions & 30 deletions sample/composeApp/src/commonMain/kotlin/App.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material.icons.Icons
Expand All @@ -12,11 +11,12 @@ import androidx.compose.material3.SnackbarHostState
import androidx.compose.material3.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import cafe.adriel.voyager.navigator.CurrentScreen
import cafe.adriel.voyager.navigator.Navigator
import soil.kmp.screen.HomeScreen
import androidx.navigation.NavHostController
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController
import soil.playground.FeedbackAction
import soil.playground.LocalFeedbackHost
import soil.playground.style.AppTheme
Expand All @@ -30,35 +30,54 @@ fun App() {
}
}

@Composable
private fun Content(
navController: NavHostController = rememberNavController()
) = withAppTheme {
val backStackEntry by navController.currentBackStackEntryAsState()
val navigator = remember(navController) { Navigator(navController) }
val canNavigateBack = remember(backStackEntry) { navigator.canBack() }
val hostState = remember { SnackbarHostState() }
val feedbackAction = remember { FeedbackAction(hostState) }
Scaffold(
modifier = Modifier.fillMaxSize(),
topBar = {
AppBar(
canNavigateBack = canNavigateBack,
navigateUp = { navigator.back() }
)
},
snackbarHost = {
SnackbarHost(hostState)
}
) { innerPadding ->
CompositionLocalProvider(LocalFeedbackHost provides feedbackAction) {
NavRouterHost(
navigator = navigator,
modifier = Modifier
.fillMaxSize()
.padding(innerPadding)
)
}
}
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
private fun Content() = withAppTheme {
Navigator(HomeScreen) { navigator ->
val hostState = remember { SnackbarHostState() }
val feedbackAction = remember { FeedbackAction(hostState) }
Scaffold(
modifier = Modifier.fillMaxSize(),
topBar = {
if (navigator.canPop) {
TopAppBar(
title = { },
navigationIcon = {
IconButton(onClick = navigator::pop) {
Icon(Icons.AutoMirrored.Filled.ArrowBack, contentDescription = "Back")
}
}
)
}
},
snackbarHost = {
SnackbarHost(hostState)
}
) { innerPadding ->
Box(modifier = Modifier.padding(innerPadding)) {
CompositionLocalProvider(LocalFeedbackHost provides feedbackAction) {
CurrentScreen()
fun AppBar(
canNavigateBack: Boolean,
navigateUp: () -> Unit,
modifier: Modifier = Modifier
) {
TopAppBar(
title = { },
modifier = modifier,
navigationIcon = {
if (canNavigateBack) {
IconButton(onClick = navigateUp) {
Icon(Icons.AutoMirrored.Filled.ArrowBack, contentDescription = "Back")
}
}
}
}
)
}
133 changes: 133 additions & 0 deletions sample/composeApp/src/commonMain/kotlin/Navigator.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.Stable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.navigation.NamedNavArgument
import androidx.navigation.NavHostController
import androidx.navigation.NavType
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.navArgument
import soil.kmp.screen.HelloFormScreen
import soil.kmp.screen.HelloQueryDetailScreen
import soil.kmp.screen.HelloQueryScreen
import soil.kmp.screen.HelloSpaceScreen
import soil.kmp.screen.HomeScreen
import soil.kmp.screen.NavScreen
import soil.playground.router.LocalNavRouter
import soil.playground.router.NavRoute
import soil.playground.router.NavRouter
import soil.space.compose.rememberViewModelStore

@Stable
class Navigator(
val navController: NavHostController
) : NavRouter {
override fun push(route: String) {
navController.navigate(route)
}

override fun <T : NavRoute> push(route: T) {
when (val screen = route as NavScreen) {
is NavScreen.Home -> push(NavScreenDestination.Home())
is NavScreen.HelloQuery -> push(NavScreenDestination.HelloQuery())
is NavScreen.HelloQueryDetail -> push(NavScreenDestination.HelloQueryDetail(screen.postId))
is NavScreen.HelloForm -> push(NavScreenDestination.HelloForm())
is NavScreen.HelloSpace -> push(NavScreenDestination.HelloSpace())
}
}

override fun back(): Boolean {
return navController.popBackStack()
}

override fun canBack(): Boolean {
return navController.previousBackStackEntry != null
}
}

@Composable
fun NavRouterHost(
navigator: Navigator,
modifier: Modifier
) {
val startDestination = remember(NavScreen.root) { NavScreen.root.destination.route }
CompositionLocalProvider(LocalNavRouter provides navigator) {
NavHost(
navController = navigator.navController,
startDestination = startDestination,
modifier = modifier
) {
composable(
route = NavScreenDestination.Home.route
) {
HomeScreen()
}
composable(
route = NavScreenDestination.HelloQuery.route
) {
HelloQueryScreen()
}
composable(
route = NavScreenDestination.HelloQueryDetail.route,
arguments = NavScreenDestination.HelloQueryDetail.arguments
) {
val id = checkNotNull(it.arguments?.getInt(NavScreenDestination.HelloQueryDetail.id.name))
HelloQueryDetailScreen(postId = id)
}
composable(
route = NavScreenDestination.HelloForm.route
) {
HelloFormScreen()
}
composable(
route = NavScreenDestination.HelloSpace.route
) {
val rootEntry = navigator.navController.getBackStackEntry(startDestination)
HelloSpaceScreen(
navStore = rememberViewModelStore(rootEntry)
)
}
}
}
}

private sealed class NavScreenDestination(
val route: String
) {
data object Home : NavScreenDestination("/home") {
operator fun invoke() = route
}

data object HelloQuery : NavScreenDestination("/helloQuery") {
operator fun invoke() = route
}

data object HelloQueryDetail : NavScreenDestination("/helloQuery/{id}") {
val arguments get() = listOf(id)
val id: NamedNavArgument
get() = navArgument("id") {
type = NavType.IntType
}

operator fun invoke(postId: Int) = "/helloQuery/$postId"
}

data object HelloForm : NavScreenDestination("/helloForm") {
operator fun invoke() = route
}

data object HelloSpace : NavScreenDestination("/helloSpace") {
operator fun invoke() = route
}
}

private val NavScreen.destination: NavScreenDestination
get() = when (this) {
is NavScreen.Home -> NavScreenDestination.Home
is NavScreen.HelloQuery -> NavScreenDestination.HelloQuery
is NavScreen.HelloQueryDetail -> NavScreenDestination.HelloQueryDetail
is NavScreen.HelloForm -> NavScreenDestination.HelloForm
is NavScreen.HelloSpace -> NavScreenDestination.HelloSpace
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.focus.focusRequester
import androidx.compose.ui.unit.dp
import cafe.adriel.voyager.core.screen.Screen
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import soil.form.compose.Controller
Expand All @@ -44,20 +43,16 @@ import soil.playground.form.compose.rememberAsRadio
import soil.playground.form.compose.rememberAsSelect
import soil.playground.style.withAppTheme


class HelloFormScreen : Screen {

@Composable
override fun Content() {
val feedback = LocalFeedbackHost.current
val coroutineScope = rememberCoroutineScope()
HelloFormContent(
onSubmitted = {
coroutineScope.launch { feedback.showAlert("Form submitted successfully") }
},
modifier = Modifier.fillMaxSize()
)
}
@Composable
fun HelloFormScreen() {
val feedback = LocalFeedbackHost.current
val coroutineScope = rememberCoroutineScope()
HelloFormContent(
onSubmitted = {
coroutineScope.launch { feedback.showAlert("Form submitted successfully") }
},
modifier = Modifier.fillMaxSize()
)
}

// The form input fields are based on the Live Demo used in React Hook Form.
Expand Down
Loading

0 comments on commit bc2cae8

Please sign in to comment.