From b639595bd0c7fe47c7eba75ed09f86e3f7257389 Mon Sep 17 00:00:00 2001 From: Romain Boisselle Date: Fri, 31 Mar 2023 13:42:06 +0200 Subject: [PATCH] add Jetpack Compose ViewModel factory retrieval --- build.gradle.kts | 2 +- doc/antora.yml | 4 +-- .../kodein/di/compose/retrievingAndroid.kt | 31 +++++++++++++++++++ settings.gradle.kts | 2 +- 4 files changed, 35 insertions(+), 4 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index b5cf67b9..190271de 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -5,5 +5,5 @@ plugins { allprojects { group = "org.kodein.di" - version = "7.19.0" + version = "7.19.1" } diff --git a/doc/antora.yml b/doc/antora.yml index 981cdb72..1ca70c75 100644 --- a/doc/antora.yml +++ b/doc/antora.yml @@ -1,7 +1,7 @@ name: kodein title: Kodein version: '7.19' -display_version: '7.19.0' +display_version: '7.19.1' nav: - modules/ROOT/nav.adoc - modules/core/nav.adoc @@ -11,6 +11,6 @@ nav: asciidoc: attributes: branch: '7.19' - version: '7.19.0' + version: '7.19.1' kotlin: '1.8.10' jdk: '1.8' \ No newline at end of file diff --git a/framework/compose/kodein-di-framework-compose/src/androidMain/kotlin/org/kodein/di/compose/retrievingAndroid.kt b/framework/compose/kodein-di-framework-compose/src/androidMain/kotlin/org/kodein/di/compose/retrievingAndroid.kt index 85f275dd..6edc2dfb 100644 --- a/framework/compose/kodein-di-framework-compose/src/androidMain/kotlin/org/kodein/di/compose/retrievingAndroid.kt +++ b/framework/compose/kodein-di-framework-compose/src/androidMain/kotlin/org/kodein/di/compose/retrievingAndroid.kt @@ -38,4 +38,35 @@ public inline fun rememberViewModel(tag: Any? = null): } ) } +} + +/** + * Gets an instance of a [VM] as an android [ViewModel] for the given [tag]. + * + * VM generic will be preserved! + * + * @param VM The type of the [ViewModel] to retrieve. + * @param tag The bound tag, if any. + * @return An instance of [VM]. + * @throws DI.NotFoundException If no provider was found. + * @throws DI.DependencyLoopException If the value construction triggered a dependency loop. + */ +@Composable +public inline fun rememberViewModel(tag: Any? = null, arg: A): ViewModelLazy = with(localDI()) { + val viewModelStoreOwner = LocalViewModelStoreOwner.current ?: error("") + + remember { + ViewModelLazy( + viewModelClass = VM::class, + storeProducer = { viewModelStoreOwner.viewModelStore }, + factoryProducer = { + object : ViewModelProvider.Factory { + override fun create(modelClass: Class): T { + @Suppress("UNCHECKED_CAST") + return direct.instance(tag, arg) as T + } + } + } + ) + } } \ No newline at end of file diff --git a/settings.gradle.kts b/settings.gradle.kts index ff7004ed..7d6a09fb 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -6,7 +6,7 @@ buildscript { maven(url = "https://raw.githubusercontent.com/kosi-libs/kodein-internal-gradle-plugin/mvn-repo") } dependencies { - classpath("org.kodein.internal.gradle:kodein-internal-gradle-settings:7.0.5") + classpath("org.kodein.internal.gradle:kodein-internal-gradle-settings:7.0.6") } }