diff --git a/app/src/main/java/com/example/nav3recipes/modular/koin/KoinModularActivity.kt b/app/src/main/java/com/example/nav3recipes/modular/koin/KoinModularActivity.kt index a77c0370..238d1266 100644 --- a/app/src/main/java/com/example/nav3recipes/modular/koin/KoinModularActivity.kt +++ b/app/src/main/java/com/example/nav3recipes/modular/koin/KoinModularActivity.kt @@ -9,19 +9,14 @@ import androidx.compose.ui.Modifier import androidx.navigation3.ui.NavDisplay import com.example.nav3recipes.ui.setEdgeToEdgeConfig import org.koin.android.ext.android.inject -import org.koin.android.ext.koin.androidContext -import org.koin.android.ext.koin.androidLogger import org.koin.android.scope.AndroidScopeComponent import org.koin.androidx.compose.navigation3.getEntryProvider import org.koin.androidx.scope.activityRetainedScope -import org.koin.androidx.scope.activityScope +import org.koin.core.Koin import org.koin.core.annotation.KoinExperimentalAPI import org.koin.core.component.KoinComponent -import org.koin.core.context.startKoin -import org.koin.core.context.stopKoin -import org.koin.core.logger.Level import org.koin.core.scope.Scope -import org.koin.mp.KoinPlatform +import org.koin.dsl.koinApplication /** * This recipe demonstrates how to use a modular approach with Navigation 3, @@ -38,16 +33,21 @@ import org.koin.mp.KoinPlatform * to the rest of the app module (i.e. MainActivity) and the feature modules. */ @OptIn(KoinExperimentalAPI::class) -class KoinModularActivity : ComponentActivity(), AndroidScopeComponent { - +class KoinModularActivity : ComponentActivity(), AndroidScopeComponent, KoinComponent { + // Local Koin Context Instance + companion object { + private val localKoin = koinApplication { + modules(appModule) + }.koin + } + // Override default Koin context to use the local one + override fun getKoin(): Koin = localKoin override val scope : Scope by activityRetainedScope() val navigator: Navigator by inject() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - initKoin() - setEdgeToEdgeConfig() setContent { Scaffold { paddingValues -> @@ -61,14 +61,4 @@ class KoinModularActivity : ComponentActivity(), AndroidScopeComponent { } } - private fun initKoin() { - if (KoinPlatform.getKoinOrNull() == null) { - // The startKoin block should be placed in Application.onCreate. - startKoin { - androidContext(this@KoinModularActivity) - androidLogger(Level.DEBUG) - modules(appModule) - } - } - } } diff --git a/app/src/main/java/com/example/nav3recipes/passingarguments/viewmodels/koin/KoinViewModelsActivity.kt b/app/src/main/java/com/example/nav3recipes/passingarguments/viewmodels/koin/KoinViewModelsActivity.kt index a38598bd..dcdbaf62 100644 --- a/app/src/main/java/com/example/nav3recipes/passingarguments/viewmodels/koin/KoinViewModelsActivity.kt +++ b/app/src/main/java/com/example/nav3recipes/passingarguments/viewmodels/koin/KoinViewModelsActivity.kt @@ -18,12 +18,14 @@ import com.example.nav3recipes.content.ContentBlue import com.example.nav3recipes.content.ContentGreen import com.example.nav3recipes.ui.setEdgeToEdgeConfig import org.koin.android.ext.koin.androidContext +import org.koin.compose.KoinApplication import org.koin.compose.viewmodel.koinViewModel import org.koin.core.context.GlobalContext import org.koin.core.context.startKoin import org.koin.core.context.stopKoin import org.koin.core.module.dsl.viewModelOf import org.koin.core.parameter.parametersOf +import org.koin.dsl.koinConfiguration import org.koin.dsl.module import org.koin.mp.KoinPlatform @@ -34,63 +36,61 @@ class KoinViewModelsActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { - //prevent any already launched Koin instance with other config - if (KoinPlatform.getKoinOrNull() != null) { - stopKoin() - } - // The startKoin block should be placed in Application.onCreate. - startKoin { - androidContext(this@KoinViewModelsActivity) - modules( - module { - viewModelOf(::RouteBViewModel) - } - ) - } - setEdgeToEdgeConfig() super.onCreate(savedInstanceState) setContent { val backStack = remember { mutableStateListOf(RouteA) } - NavDisplay( - backStack = backStack, - onBack = { backStack.removeLastOrNull() }, + // Koin Compose Entry point + KoinApplication( + configuration = koinConfiguration { + modules(appModule) + } + ) { + NavDisplay( + backStack = backStack, + onBack = { backStack.removeLastOrNull() }, - // In order to add the `ViewModelStoreNavEntryDecorator` (see comment below for why) - // we also need to add the default `NavEntryDecorator`s as well. These provide - // extra information to the entry's content to enable it to display correctly - // and save its state. - entryDecorators = listOf( - rememberSaveableStateHolderNavEntryDecorator(), - rememberViewModelStoreNavEntryDecorator() - ), - entryProvider = entryProvider { - entry { - ContentGreen("Welcome to Nav3") { - LazyColumn { - items(10) { i -> - Button(onClick = { - backStack.add(RouteB("$i")) - }) { - Text("$i") + // In order to add the `ViewModelStoreNavEntryDecorator` (see comment below for why) + // we also need to add the default `NavEntryDecorator`s as well. These provide + // extra information to the entry's content to enable it to display correctly + // and save its state. + entryDecorators = listOf( + rememberSaveableStateHolderNavEntryDecorator(), + rememberViewModelStoreNavEntryDecorator() + ), + entryProvider = entryProvider { + entry { + ContentGreen("Welcome to Nav3") { + LazyColumn { + items(10) { i -> + Button(onClick = { + backStack.add(RouteB("$i")) + }) { + Text("$i") + } } } } } - } - entry { key -> - val viewModel = koinViewModel { - parametersOf(key) + entry { key -> + val viewModel = koinViewModel { + parametersOf(key) + } + ScreenB(viewModel = viewModel) } - ScreenB(viewModel = viewModel) } - } - ) + ) + } } } } +// Local Koin Module +private val appModule = module { + viewModelOf(::RouteBViewModel) +} + @Composable fun ScreenB(viewModel: RouteBViewModel) { ContentBlue("Route id: ${viewModel.navKey.id} ") diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 08d9cd9e..ca7ade19 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -31,7 +31,7 @@ nav3Material = "1.3.0-alpha03" ksp = "2.3.2" hilt = "2.57.2" hiltNavigationCompose = "1.3.0" -koin = "4.2.0-alpha2" +koin = "4.2.0-beta2" [libraries] androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" }