Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add KOIN_USE_COMPOSE_VIEWMODEL key to replace USE_COMPOSE_VIEWMODEL #149

Merged
merged 1 commit into from
Jul 25, 2024
Merged
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
8 changes: 6 additions & 2 deletions docs/reference/koin-annotations/definitions.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,18 @@ For Scopes, check the [Declaring Scopes](/docs/reference/koin-core/scopes.md) se

### Generate Compose ViewModel for Kotlin Multipaltform (since 1.4.0)

The `@KoinViewModel` annotation can be used to generate either Android or Compsoe KMP ViewModel. To generate `viewModel` Koin definition with `org.koin.compose.viewmodel.dsl.viewModel` instead of regular `org.koin.androidx.viewmodel.dsl.viewModel`, you need to activate the `USE_COMPOSE_VIEWMODEL` option:
The `@KoinViewModel` annotation can be used to generate either Android or Compsoe KMP ViewModel. To generate `viewModel` Koin definition with `org.koin.compose.viewmodel.dsl.viewModel` instead of regular `org.koin.androidx.viewmodel.dsl.viewModel`, you need to activate the `KOIN_USE_COMPOSE_VIEWMODEL` option:

```groovy
ksp {
arg("USE_COMPOSE_VIEWMODEL","true")
arg("KOIN_USE_COMPOSE_VIEWMODEL","true")
}
```

:::note
`USE_COMPOSE_VIEWMODEL` key is deprecated in favor of `KOIN_USE_COMPOSE_VIEWMODEL`
:::

:::note
Koin 4.0 should bring merge of those 2 ViewModel DSL into only one, as the ViewModel type argiument comes from teh same library
:::
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class BuilderProcessor(
private val options: Map<String, String>
) : SymbolProcessor {

private val koinCodeGenerator = KoinGenerator(codeGenerator, logger, isComposeViewModelActive())
private val koinCodeGenerator = KoinGenerator(codeGenerator, logger, isComposeViewModelActive() || isKoinComposeViewModelActive())
private val koinMetaDataScanner = KoinMetaDataScanner(logger)
private val koinConfigVerification = KoinConfigVerification(codeGenerator, logger)

Expand Down Expand Up @@ -67,11 +67,17 @@ class BuilderProcessor(
}

//TODO Use Koin 4.0 ViewModel DSL
@Deprecated("use isKoinComposeViewModelActive")
private fun isComposeViewModelActive(): Boolean {
logger.warn("Use Compose ViewModel for @KoinViewModel generation")
logger.warn("[Deprecated] Please use KOIN_USE_COMPOSE_VIEWMODEL arg")

Choose a reason for hiding this comment

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

Why isn't this behind a check if the argument is actually set? I don't even use Compose and this deprecation notice is still printed.

Copy link
Member Author

Choose a reason for hiding this comment

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

is it still present?

Choose a reason for hiding this comment

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

As of 1.4.0-RC4, yes. Even when explicitly disabling USE_COMPOSE_VIEWMODEL.
the warnings
ksp config

Looking at the code, it appears the isComposeViewModelActive is called in any case, leading to the message being logged without any further checks.
https://github.com/InsertKoinIO/koin-annotations/blob/1.4.0-RC4/projects/koin-ksp-compiler/src/jvmMain/kotlin/org/koin/compiler/BuilderProcessor.kt#L83-L87

return options.getOrDefault(USE_COMPOSE_VIEWMODEL.name, "false") == true.toString()
}

private fun isKoinComposeViewModelActive(): Boolean {
logger.warn("Use Compose ViewModel for @KoinViewModel generation")
return options.getOrDefault(KOIN_USE_COMPOSE_VIEWMODEL.name, "false") == true.toString()
}

//TODO turn KOIN_DEFAULT_MODULE to false by default - Next Major version (breaking)
private fun isDefaultModuleActive(): Boolean {
return options.getOrDefault(KOIN_DEFAULT_MODULE.name, "true") == true.toString()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ enum class KspOptions {
KOIN_DEFAULT_MODULE,

//TODO Remove isComposeViewModelActive with Koin 4
USE_COMPOSE_VIEWMODEL
USE_COMPOSE_VIEWMODEL,
KOIN_USE_COMPOSE_VIEWMODEL
}
Loading