Skip to content

Commit

Permalink
Merge pull request #149 from InsertKoinIO/rename_key
Browse files Browse the repository at this point in the history
Add KOIN_USE_COMPOSE_VIEWMODEL key to replace USE_COMPOSE_VIEWMODEL
  • Loading branch information
arnaudgiuliani authored Jul 25, 2024
2 parents fc470eb + 427d65a commit 7215381
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
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")
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
}

0 comments on commit 7215381

Please sign in to comment.