Skip to content

Commit

Permalink
Scope Koin Modules to MainApplication
Browse files Browse the repository at this point in the history
  • Loading branch information
ksharma-xyz committed Dec 25, 2024
1 parent c6cb884 commit a3e91f5
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 18 deletions.
1 change: 1 addition & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 2 additions & 13 deletions app/src/main/java/xyz/ksharma/koinsingleton/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,20 @@ 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.KoinAndroidContext
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) {
KoinAndroidContext {
KoinSingletonBugTheme {
Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
NavHost(
Expand All @@ -53,14 +50,6 @@ class MainActivity : ComponentActivity() {
}
}

val koinConfig = koinConfiguration {
modules(
repoModule,
viewModelsModule,
)
androidContext(MainApplication.instance ?: error("No Android application context set"))
}

val viewModelsModule = module {
viewModelOf(::HomeViewModel)
viewModelOf(::DetailViewModel)
Expand Down
14 changes: 9 additions & 5 deletions app/src/main/java/xyz/ksharma/koinsingleton/MainApplication.kt
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
package xyz.ksharma.koinsingleton

import android.app.Application
import org.koin.android.ext.koin.androidContext
import org.koin.core.context.startKoin

class MainApplication : Application() {

override fun onCreate() {
super.onCreate()
instance = this
}

companion object {
var instance: Application? = null
startKoin {
modules(
repoModule,
viewModelsModule,
)
androidContext(this@MainApplication)
}
}
}

0 comments on commit a3e91f5

Please sign in to comment.