Skip to content

Commit

Permalink
Add navigation
Browse files Browse the repository at this point in the history
  • Loading branch information
ksharma-xyz committed Dec 18, 2024
1 parent 1372b01 commit 8ea1773
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 3 deletions.
3 changes: 3 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -59,4 +60,6 @@ dependencies {

implementation (libs.koin.androidx.compose)
implementation(libs.koin.android)
implementation(libs.androidx.navigation.compose)
implementation(libs.kotlinx.serialization.json)
}
15 changes: 15 additions & 0 deletions app/src/main/java/xyz/ksharma/koinsingleton/HomeViewModel.kt
Original file line number Diff line number Diff line change
@@ -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()
}
}
37 changes: 34 additions & 3 deletions app/src/main/java/xyz/ksharma/koinsingleton/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<HomeRoute> {
val viewmodel = koinViewModel<HomeViewModel>()
HomeScreen(navController)
}
composable<DetailRoute> {
val viewmodel = koinViewModel<DetailViewModel>()
DetailScreen(navController)
}
}
}
}
}
Expand All @@ -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)
}
9 changes: 9 additions & 0 deletions app/src/main/java/xyz/ksharma/koinsingleton/Navigation.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package xyz.ksharma.koinsingleton

import kotlinx.serialization.Serializable

@Serializable
data object HomeRoute

@Serializable
data object DetailRoute
37 changes: 37 additions & 0 deletions app/src/main/java/xyz/ksharma/koinsingleton/Scree.kt
Original file line number Diff line number Diff line change
@@ -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)
})
}
}
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
5 changes: 5 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand All @@ -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" }

0 comments on commit 8ea1773

Please sign in to comment.