Skip to content

Commit

Permalink
Navigation multiplatform
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelbel committed May 5, 2024
1 parent b18b49d commit 0cd542e
Show file tree
Hide file tree
Showing 23 changed files with 102 additions and 220 deletions.
3 changes: 1 addition & 2 deletions core/common/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ kotlin {
api(libs.bundles.koin.common)
implementation(compose.material3)
implementation(libs.bundles.kotlinx.coroutines.common)
/*implementation(libs.bundles.compose.viewmodel.common)*/
implementation(libs.bundles.paging.common)
}
androidMain.dependencies {
api(libs.bundles.kotlinx.coroutines.android)
Expand All @@ -34,6 +32,7 @@ kotlin {
api(libs.bundles.koin.compose.work.android)
implementation(libs.bundles.appcompat.android)
implementation(libs.bundles.browser.android)
implementation(libs.bundles.paging.common) // fixme ломает navigation
}
val desktopMain by getting
desktopMain.dependencies {
Expand Down
2 changes: 1 addition & 1 deletion core/interactor/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ kotlin {
implementation(compose.runtime)
implementation(compose.runtimeSaveable)
implementation(libs.bundles.kotlinx.coroutines.common)
implementation(libs.bundles.paging.common)
implementation(libs.bundles.room.paging.common)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion core/navigation/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ kotlin {
val desktopMain by getting
desktopMain.dependencies {
api(libs.bundles.compose.navigation.common)
api(libs.bundles.precompose.desktop)
api(libs.bundles.compose.viewmodel.common)
}
}
}
Expand Down
1 change: 0 additions & 1 deletion core/persistence/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ kotlin {
api(project(":core:common"))
api(project(":core:network"))
implementation(libs.bundles.datastore.common)
implementation(libs.bundles.paging.common)
implementation(libs.bundles.room.common)
implementation(libs.bundles.sqlite.common)
implementation(libs.bundles.okio.common)
Expand Down
2 changes: 1 addition & 1 deletion core/repository/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ kotlin {
commonMain.dependencies {
api(project(":core:persistence"))
implementation(libs.bundles.kotlinx.coroutines.common)
implementation(libs.bundles.paging.common)
implementation(libs.bundles.room.paging.common)
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions core/ui/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@ kotlin {
sourceSets {
commonMain.dependencies {
implementation(project(":core:persistence"))
implementation(libs.bundles.paging.common)
implementation(libs.bundles.constraintlayout.common)
implementation(libs.bundles.coil.common)
implementation(libs.bundles.constraintlayout.common)
implementation(compose.components.resources)
implementation(compose.foundation)
implementation(compose.material)
Expand All @@ -43,6 +42,7 @@ kotlin {
api(libs.bundles.coil.android)
api(libs.bundles.compose.android)
api(libs.bundles.google.material.android)
implementation(libs.bundles.paging.common) // fixme ломает navigation
}
}
}
Expand Down
1 change: 0 additions & 1 deletion core/work/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ kotlin {
implementation(project(":core:interactor"))
implementation(project(":core:notifications"))
implementation(libs.bundles.kotlinx.serialization.common)
implementation(libs.bundles.paging.common)
}
androidMain.dependencies {
implementation(libs.bundles.work.android)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package org.michaelbel.movies

import androidx.compose.runtime.Composable
import moe.tlaster.precompose.navigation.NavHost
import moe.tlaster.precompose.navigation.rememberNavigator
import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.rememberNavController
import org.michaelbel.movies.account.accountGraph
import org.michaelbel.movies.account.navigateToAccount
import org.michaelbel.movies.auth.authGraph
Expand All @@ -18,44 +19,10 @@ import org.michaelbel.movies.settings.navigateToSettings
import org.michaelbel.movies.settings.settingsGraph

@Composable
internal fun MainWindowContent() {
val navHostController = rememberNavigator()

internal fun MainWindowContent(
navHostController: NavHostController = rememberNavController()
) {
NavHost(
navigator = navHostController,
initialRoute = "feed"
) {
authGraph(
navigateBack = navHostController::popBackStack
)
accountGraph(
navigateBack = navHostController::popBackStack
)
feedGraph(
navigateToSearch = navHostController::navigateToSearch,
navigateToAuth = navHostController::navigateToAuth,
navigateToAccount = navHostController::navigateToAccount,
navigateToSettings = navHostController::navigateToSettings,
navigateToDetails = navHostController::navigateToDetails
)
detailsGraph(
navigateBack = navHostController::popBackStack,
navigateToGallery = navHostController::navigateToGallery
)
galleryGraph(
navigateBack = navHostController::popBackStack
)
searchGraph(
navigateBack = navHostController::popBackStack,
navigateToDetails = navHostController::navigateToDetails,
)
settingsGraph(
navigateBack = navHostController::popBackStack
)
}

/*val navHostController: NavHostController = rememberNavController()
androidx.navigation.compose.NavHost(
navController = navHostController,
startDestination = "feed"
) {
Expand Down Expand Up @@ -86,5 +53,5 @@ internal fun MainWindowContent() {
settingsGraph(
navigateBack = navHostController::popBackStack
)
}*/
}
}
Original file line number Diff line number Diff line change
@@ -1,35 +1,66 @@
package org.michaelbel.movies

import androidx.compose.ui.window.singleWindowApplication
import moe.tlaster.precompose.PreComposeApp
import org.koin.core.context.startKoin
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.remember
import androidx.compose.ui.window.Window
import androidx.compose.ui.window.application
import androidx.lifecycle.ViewModelStore
import androidx.lifecycle.ViewModelStoreOwner
import androidx.lifecycle.viewmodel.compose.LocalViewModelStoreOwner
import org.michaelbel.movies.common.ThemeData
import org.michaelbel.movies.common.theme.AppTheme
import org.michaelbel.movies.di.appKoinModule
import org.michaelbel.movies.ui.theme.MoviesTheme

fun main() {
startKoin {
modules(appKoinModule)
fun main() = application {
Window(onCloseRequest = ::exitApplication, title = "Movies") {
App()
}
}

singleWindowApplication(
title = "Movies",
icon = null
@Composable
private fun App() = withViewModelStoreOwner {
MoviesTheme(
themeData = ThemeData(
appTheme = AppTheme.FollowSystem,
dynamicColors = false,
paletteKey = 0,
seedColor = 0
),
theme = AppTheme.FollowSystem,
enableEdgeToEdge = { _,_ -> }
) {
PreComposeApp {
MoviesTheme(
themeData = ThemeData(
appTheme = AppTheme.FollowSystem,
dynamicColors = false,
paletteKey = 0,
seedColor = 0
),
theme = AppTheme.FollowSystem,
enableEdgeToEdge = { _,_ -> }
) {
MainWindowContent()
}
}
MainWindowContent()
}
}

private class ComposeViewModelStoreOwner: ViewModelStoreOwner {
override val viewModelStore = ViewModelStore()
fun dispose() { viewModelStore.clear() }
}

@Composable
private fun rememberComposeViewModelStoreOwner(): ViewModelStoreOwner {
val viewModelStoreOwner = remember { ComposeViewModelStoreOwner() }
DisposableEffect(viewModelStoreOwner) {
onDispose { viewModelStoreOwner.dispose() }
}
return viewModelStoreOwner
}

@Composable
internal fun withViewModelStoreOwner(content: @Composable () -> Unit) {
if (LocalViewModelStoreOwner.current != null) {
// Normal case: use system-provided owner
content()
} else {
// Fallback case: use ViewModelStoreOwner with scope of this composable.
// It's required for Compose Multiplatform for now because it's not providing default value yet.
// Expected to be fixed in Compose Multiplatform 1.7.0
CompositionLocalProvider(
LocalViewModelStoreOwner provides rememberComposeViewModelStoreOwner(),
content = content
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,8 @@ package org.michaelbel.movies.account
import androidx.navigation.NavController
import androidx.navigation.NavGraphBuilder
import androidx.navigation.compose.dialog
import moe.tlaster.precompose.navigation.Navigator
import moe.tlaster.precompose.navigation.RouteBuilder
import org.michaelbel.movies.account.ui.AccountRoute

fun Navigator.navigateToAccount() {
navigate(AccountDestination.route)
}

fun RouteBuilder.accountGraph(
navigateBack: () -> Unit
) {
dialog(
route = AccountDestination.route
) {
AccountRoute(
onBackClick = navigateBack
)
}
}

fun NavController.navigateToAccount() {
navigate(AccountDestination.route)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,8 @@ package org.michaelbel.movies.auth
import androidx.navigation.NavController
import androidx.navigation.NavGraphBuilder
import androidx.navigation.compose.dialog
import moe.tlaster.precompose.navigation.Navigator
import moe.tlaster.precompose.navigation.RouteBuilder
import org.michaelbel.movies.auth.ui.AuthRoute

fun Navigator.navigateToAuth() {
navigate(AuthDestination.route)
}

fun RouteBuilder.authGraph(
navigateBack: () -> Unit
) {
dialog(
route = AuthDestination.route,
) {
AuthRoute(
onBackClick = navigateBack
)
}
}

fun NavController.navigateToAuth() {
navigate(AuthDestination.route)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,14 @@
package org.michaelbel.movies.details

import androidx.compose.foundation.clickable
import androidx.compose.material.Text
import androidx.compose.ui.Modifier
import androidx.navigation.NavController
import androidx.navigation.NavGraphBuilder
import androidx.navigation.compose.composable
import moe.tlaster.precompose.navigation.Navigator
import moe.tlaster.precompose.navigation.RouteBuilder
import org.michaelbel.movies.persistence.database.typealiases.MovieId
import org.michaelbel.movies.persistence.database.typealiases.PagingKey

fun Navigator.navigateToDetails(pagingKey: PagingKey, movieId: MovieId) {
navigate("movie?movieList=$pagingKey&movieId=$movieId")
}

fun RouteBuilder.detailsGraph(
navigateBack: () -> Unit,
navigateToGallery: (MovieId) -> Unit
) {
scene(
route = DetailsDestination.route
) {
Text("details")
}
}

fun NavController.navigateToDetails(pagingKey: PagingKey, movieId: MovieId) {
navigate("movie?movieList=$pagingKey&movieId=$movieId")
}
Expand All @@ -35,6 +20,9 @@ fun NavGraphBuilder.detailsGraph(
composable(
route = DetailsDestination.route
) {
Text("details")
Text(
text = "Details",
modifier = Modifier.clickable { navigateBack() }
)
}
}
8 changes: 5 additions & 3 deletions feature/feed-impl/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,17 @@ kotlin {

sourceSets {
commonMain.dependencies {
api(project(":core:navigation"))
api(project(":core:platform-services:interactor"))
api(project(":core:ui"))
api(project(":core:common"))
api(project(":core:navigation"))
api(project(":core:interactor"))
implementation(project(":core:notifications"))
implementation(libs.bundles.paging.common)
api(project(":core:platform-services:interactor"))
implementation(libs.bundles.constraintlayout.common)
}
androidMain.dependencies {
implementation(libs.bundles.paging.common) // fixme ломает navigation
}
val desktopMain by getting
desktopMain.dependencies {
implementation(compose.foundation)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,8 @@ package org.michaelbel.movies.feed

import androidx.navigation.NavGraphBuilder
import androidx.navigation.compose.composable
import moe.tlaster.precompose.navigation.RouteBuilder
import org.michaelbel.movies.feed.ui.FeedRoute

fun RouteBuilder.feedGraph(
navigateToSearch: () -> Unit,
navigateToAuth: () -> Unit,
navigateToAccount: () -> Unit,
navigateToSettings: () -> Unit,
navigateToDetails: (String, Int) -> Unit
) {
scene(
route = FeedDestination.route,
deepLinks = listOf(
"movies://redirect_url?request_token={request_token}&approved={approved}"
)
) {
FeedRoute(
onNavigateToSearch = navigateToSearch,
onNavigateToAccount = navigateToAccount,
onNavigateToAuth = navigateToAuth,
onNavigateToSettings = navigateToSettings,
onNavigateToDetails = navigateToDetails
)
}
}

fun NavGraphBuilder.feedGraph(
navigateToSearch: () -> Unit,
navigateToAuth: () -> Unit,
Expand Down
Loading

0 comments on commit 0cd542e

Please sign in to comment.