Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 ->
Expand All @@ -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)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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<Any>(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<RouteA> {
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<RouteA> {
ContentGreen("Welcome to Nav3") {
LazyColumn {
items(10) { i ->
Button(onClick = {
backStack.add(RouteB("$i"))
}) {
Text("$i")
}
}
}
}
}
}
entry<RouteB> { key ->
val viewModel = koinViewModel<RouteBViewModel> {
parametersOf(key)
entry<RouteB> { key ->
val viewModel = koinViewModel<RouteBViewModel> {
parametersOf(key)
}
ScreenB(viewModel = viewModel)
}
ScreenB(viewModel = viewModel)
}
}
)
)
}
}
}
}

// Local Koin Module
private val appModule = module {
viewModelOf(::RouteBViewModel)
}
Comment on lines +90 to +92
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

For improved readability and code organization, it's a common convention to declare top-level properties like appModule before the classes that use them. Consider moving this module definition to the top of the file, after the import statements. This allows developers reading the file to see the dependencies at a glance before diving into the component's implementation.


@Composable
fun ScreenB(viewModel: RouteBViewModel) {
ContentBlue("Route id: ${viewModel.navKey.id} ")
Expand Down
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down