From 8ea17732c281988c1eaecda0e46f5e9c0bffc7d1 Mon Sep 17 00:00:00 2001 From: Karan Sharma <55722391+ksharma-xyz@users.noreply.github.com> Date: Wed, 18 Dec 2024 13:20:07 +1100 Subject: [PATCH] Add navigation --- app/build.gradle.kts | 3 ++ .../ksharma/koinsingleton/HomeViewModel.kt | 15 ++++++++ .../xyz/ksharma/koinsingleton/MainActivity.kt | 37 +++++++++++++++++-- .../xyz/ksharma/koinsingleton/Navigation.kt | 9 +++++ .../java/xyz/ksharma/koinsingleton/Scree.kt | 37 +++++++++++++++++++ build.gradle.kts | 1 + gradle/libs.versions.toml | 5 +++ 7 files changed, 104 insertions(+), 3 deletions(-) create mode 100644 app/src/main/java/xyz/ksharma/koinsingleton/HomeViewModel.kt create mode 100644 app/src/main/java/xyz/ksharma/koinsingleton/Navigation.kt create mode 100644 app/src/main/java/xyz/ksharma/koinsingleton/Scree.kt diff --git a/app/build.gradle.kts b/app/build.gradle.kts index ef57d07..a55174b 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -2,6 +2,7 @@ plugins { alias(libs.plugins.android.application) alias(libs.plugins.kotlin.android) alias(libs.plugins.kotlin.compose) + alias(libs.plugins.kotlin.serialization) } android { @@ -59,4 +60,6 @@ dependencies { implementation (libs.koin.androidx.compose) implementation(libs.koin.android) + implementation(libs.androidx.navigation.compose) + implementation(libs.kotlinx.serialization.json) } diff --git a/app/src/main/java/xyz/ksharma/koinsingleton/HomeViewModel.kt b/app/src/main/java/xyz/ksharma/koinsingleton/HomeViewModel.kt new file mode 100644 index 0000000..a3db47c --- /dev/null +++ b/app/src/main/java/xyz/ksharma/koinsingleton/HomeViewModel.kt @@ -0,0 +1,15 @@ +package xyz.ksharma.koinsingleton + +import androidx.lifecycle.ViewModel + +class HomeViewModel(private val repository: Repository) : ViewModel() { + init { + repository.doWork() + } +} + +class DetailViewModel(private val repository: Repository): ViewModel() { + init { + repository.doWork() + } +} diff --git a/app/src/main/java/xyz/ksharma/koinsingleton/MainActivity.kt b/app/src/main/java/xyz/ksharma/koinsingleton/MainActivity.kt index 3c37546..a802c4b 100644 --- a/app/src/main/java/xyz/ksharma/koinsingleton/MainActivity.kt +++ b/app/src/main/java/xyz/ksharma/koinsingleton/MainActivity.kt @@ -7,22 +7,45 @@ import androidx.activity.enableEdgeToEdge import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.padding import androidx.compose.material3.Scaffold -import androidx.compose.material3.Text import androidx.compose.ui.Modifier +import androidx.navigation.compose.NavHost +import androidx.navigation.compose.composable +import androidx.navigation.compose.rememberNavController import org.koin.android.ext.koin.androidContext +import org.koin.androidx.compose.koinViewModel import org.koin.compose.KoinApplication +import org.koin.core.module.dsl.viewModelOf import org.koin.dsl.koinConfiguration +import org.koin.dsl.module import xyz.ksharma.koinsingleton.ui.theme.KoinSingletonBugTheme + class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) enableEdgeToEdge() setContent { + val navController = rememberNavController() + KoinApplication(application = koinConfig) { KoinSingletonBugTheme { Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding -> - Text(text = "Hello World!", modifier = Modifier.padding(innerPadding)) + NavHost( + navController = navController, + startDestination = HomeRoute, + modifier = Modifier + .fillMaxSize() + .padding(innerPadding) + ) { + composable { + val viewmodel = koinViewModel() + HomeScreen(navController) + } + composable { + val viewmodel = koinViewModel() + DetailScreen(navController) + } + } } } } @@ -31,6 +54,14 @@ class MainActivity : ComponentActivity() { } val koinConfig = koinConfiguration { - modules(repoModule) + modules( + repoModule, + viewModelsModule, + ) androidContext(MainApplication.instance ?: error("No Android application context set")) } + +val viewModelsModule = module { + viewModelOf(::HomeViewModel) + viewModelOf(::DetailViewModel) +} diff --git a/app/src/main/java/xyz/ksharma/koinsingleton/Navigation.kt b/app/src/main/java/xyz/ksharma/koinsingleton/Navigation.kt new file mode 100644 index 0000000..5cddfaa --- /dev/null +++ b/app/src/main/java/xyz/ksharma/koinsingleton/Navigation.kt @@ -0,0 +1,9 @@ +package xyz.ksharma.koinsingleton + +import kotlinx.serialization.Serializable + +@Serializable +data object HomeRoute + +@Serializable +data object DetailRoute diff --git a/app/src/main/java/xyz/ksharma/koinsingleton/Scree.kt b/app/src/main/java/xyz/ksharma/koinsingleton/Scree.kt new file mode 100644 index 0000000..11d9aac --- /dev/null +++ b/app/src/main/java/xyz/ksharma/koinsingleton/Scree.kt @@ -0,0 +1,37 @@ +package xyz.ksharma.koinsingleton + +import androidx.compose.foundation.clickable +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.padding +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.ui.Modifier +import androidx.compose.ui.unit.dp +import androidx.navigation.NavHostController + + +@Composable +fun HomeScreen(navController: NavHostController) { + Column { + Text("Home Screen", modifier = Modifier.padding(16.dp)) + + Text("Go to Detail", modifier = Modifier + .padding(16.dp) + .clickable { + navController.navigate(DetailRoute) + }) + } +} + +@Composable +fun DetailScreen(navController: NavHostController) { + Column { + Text("Detail Screen", modifier = Modifier.padding(16.dp)) + + Text("Go Back", modifier = Modifier + .padding(16.dp) + .clickable { + navController.navigate(HomeRoute) + }) + } +} diff --git a/build.gradle.kts b/build.gradle.kts index 5c98ad0..88ea69c 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -3,4 +3,5 @@ plugins { alias(libs.plugins.android.application) apply false alias(libs.plugins.kotlin.android) apply false alias(libs.plugins.kotlin.compose) apply false + alias(libs.plugins.kotlin.serialization) apply false } diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index faab588..52c6001 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -5,13 +5,16 @@ coreKtx = "1.15.0" junit = "4.13.2" junitVersion = "1.2.1" espressoCore = "3.6.1" +kotlinxSerializationJson = "1.7.3" lifecycleRuntimeKtx = "2.8.7" activityCompose = "1.9.3" composeBom = "2024.12.01" koin = "4.0.1-Beta1" +navigationComposeVersion = "2.8.5" [libraries] androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" } +androidx-navigation-compose = { module = "androidx.navigation:navigation-compose", version.ref = "navigationComposeVersion" } junit = { group = "junit", name = "junit", version.ref = "junit" } androidx-junit = { group = "androidx.test.ext", name = "junit", version.ref = "junitVersion" } androidx-espresso-core = { group = "androidx.test.espresso", name = "espresso-core", version.ref = "espressoCore" } @@ -27,8 +30,10 @@ androidx-ui-test-junit4 = { group = "androidx.compose.ui", name = "ui-test-junit androidx-material3 = { group = "androidx.compose.material3", name = "material3" } koin-android = { module = "io.insert-koin:koin-android", version.ref = "koin" } koin-androidx-compose = { module = "io.insert-koin:koin-androidx-compose", version.ref = "koin" } +kotlinx-serialization-json = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json", version.ref = "kotlinxSerializationJson" } [plugins] android-application = { id = "com.android.application", version.ref = "agp" } kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" } kotlin-compose = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" } +kotlin-serialization = { id = "org.jetbrains.kotlin.plugin.serialization", version.ref = "kotlin" }