Skip to content

Commit

Permalink
Merge pull request #30 from soil-kt/jb-navigation
Browse files Browse the repository at this point in the history
Supported androidx.Navigation Library for Compose Multiplatform
  • Loading branch information
ogaclejapan authored Jun 2, 2024
2 parents a949895 + 3a009aa commit 48443c1
Show file tree
Hide file tree
Showing 18 changed files with 432 additions and 235 deletions.
7 changes: 5 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ compose = "1.6.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 @@ -24,13 +26,14 @@ androidx-activity-compose = { module = "androidx.activity:activity-compose", ver
androidx-annotation = { module = "androidx.annotation:annotation", version.ref = "androidx-annotation" }
androidx-core = { module = "androidx.core:core-ktx", version.ref = "androidx-core" }
androidx-lifecycle-process = { module = "androidx.lifecycle:lifecycle-process", version.ref = "androidx-lifecycle" }
androidx-lifecycle-viewmodel-compose = { module = "androidx.lifecycle:lifecycle-viewmodel-compose", version.ref = "androidx-lifecycle" }
androidx-lifecycle-viewmodel-savedstate = { module = "androidx.lifecycle:lifecycle-viewmodel-savedstate", version.ref = "androidx-lifecycle" }
compose-ui-test-junit4-android = { module = "androidx.compose.ui:ui-test-junit4-android", version.ref = "compose" }
compose-ui-test-manifest = { module = "androidx.compose.ui:ui-test-manifest", version.ref = "compose" }
compose-ui-tooling = { module = "androidx.compose.ui:ui-tooling", version.ref = "compose" }
compose-ui-tooling-preview = { module = "androidx.compose.ui:ui-tooling-preview", version.ref = "compose" }
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.

Loading

0 comments on commit 48443c1

Please sign in to comment.