Skip to content

Commit

Permalink
Merge tag '3.5.3'
Browse files Browse the repository at this point in the history
  • Loading branch information
arnaudgiuliani committed Dec 19, 2023
2 parents 704f688 + 42192d8 commit e1f58a6
Show file tree
Hide file tree
Showing 63 changed files with 460 additions and 355 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: Build & Test

on: pull_request:
on: pull_request

concurrency:
group: build-${{ github.ref }}
Expand Down
2 changes: 1 addition & 1 deletion CONTRIBUTING.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ https://developer.android.com/studio/[AndroidStudio]

=== Building from source
To build the source you will need to install
https://gradle.org/[Gradle] v4.7 or more.
https://gradle.org/[Gradle] v7.4.2 or more.

We use Gradlew in the build scripts

Expand Down
6 changes: 1 addition & 5 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ buildscript {
mavenLocal()
mavenCentral()
google()
jcenter()
//TODO replace io.mockk
}
dependencies {
Expand All @@ -32,7 +31,6 @@ allprojects {
mavenLocal()
mavenCentral()
google()
jcenter()
}

group = 'io.insert-koin'
Expand All @@ -53,8 +51,6 @@ allprojects {

kotlinOptions {
jvmTarget = '1.8'
apiVersion = '1.5'
languageVersion = '1.5'
}
}
}
}
2 changes: 1 addition & 1 deletion android/gradle/versions.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
ext {
// Koin Versions
koin_android_version = '3.5.0'
koin_android_version = '3.5.3'

// build Tools
android_min_version = 14
Expand Down
6 changes: 3 additions & 3 deletions android/koin-android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ dependencies {

api "androidx.appcompat:appcompat:1.6.1"
api "androidx.activity:activity-ktx:1.7.2"
api "androidx.fragment:fragment-ktx:1.6.1"
api "androidx.lifecycle:lifecycle-viewmodel-ktx:2.6.1"
api("androidx.lifecycle:lifecycle-common-java8:2.6.1")
api "androidx.fragment:fragment-ktx:1.6.2"
api "androidx.lifecycle:lifecycle-viewmodel-ktx:2.6.2"
api("androidx.lifecycle:lifecycle-common-java8:2.6.2")

// tests
testImplementation "io.insert-koin:koin-test:$koin_version"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.koin.androidx.viewmodel

import androidx.annotation.MainThread
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelStore
Expand Down Expand Up @@ -38,13 +37,25 @@ fun <T : ViewModel> resolveViewModel(
val modelClass: Class<T> = vmClass.java
val factory = KoinViewModelFactory(vmClass, scope, qualifier, parameters)
val provider = ViewModelProvider(viewModelStore, factory, extras)
val vmKey = getViewModelKey(qualifier, scope, key)
return when {
qualifier != null -> provider[qualifier.value + (key?.let { "_$it" } ?: ""), modelClass]
key != null -> provider[key, modelClass]
vmKey != null -> provider[vmKey, modelClass]
else -> provider[modelClass]
}
}

@KoinInternalApi
internal fun getViewModelKey(qualifier: Qualifier?, scope: Scope, key: String?): String? {
return if (qualifier == null && key == null && scope.isRoot) {
null
} else {
val q = qualifier?.value ?: ""
val k = key ?: ""
val s = if (!scope.isRoot) scope.id else ""
"$q$k$s"
}
}

/**
* Resolve a Lazy ViewModel instance
* used in Main Thread
Expand All @@ -68,5 +79,15 @@ fun <T : ViewModel> lazyResolveViewModel(
scope: Scope,
parameters: (() -> ParametersHolder)? = null,
): Lazy<T> {
return lazy(LazyThreadSafetyMode.NONE) { resolveViewModel(vmClass, viewModelStore(), key, extras(), qualifier, scope, parameters) }
return lazy(LazyThreadSafetyMode.NONE) {
resolveViewModel(
vmClass,
viewModelStore(),
key,
extras(),
qualifier,
scope,
parameters
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package org.koin.androidx.viewmodel.ext.android

import android.app.Activity
import androidx.activity.ComponentActivity
import androidx.annotation.MainThread
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModel
Expand Down Expand Up @@ -66,10 +68,11 @@ inline fun <reified T : ViewModel> Fragment.getActivityViewModel(
noinline extrasProducer: (() -> CreationExtras)? = null,
noinline parameters: (() -> ParametersHolder)? = null,
): T {
val op = ownerProducer()
return resolveViewModel(
T::class,
ownerProducer().viewModelStore,
extras = extrasProducer?.invoke() ?: this.defaultViewModelCreationExtras,
op.viewModelStore,
extras = extrasProducer?.invoke() ?: (op as? ComponentActivity)?.defaultViewModelCreationExtras ?: this.defaultViewModelCreationExtras,
qualifier = qualifier,
parameters = parameters,
scope = getKoinScope()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package org.koin.test.android.viewmodel

import org.junit.Test
import org.koin.androidx.viewmodel.getViewModelKey
import org.koin.core.annotation.KoinInternalApi
import org.koin.core.qualifier.StringQualifier
import org.koin.core.scope.Scope
import org.koin.dsl.koinApplication
import kotlin.test.assertEquals

class ViewModelKeyTest {

@OptIn(KoinInternalApi::class)
@Test
fun generate_right_key() {
val koin = koinApplication().koin
val root = koin.scopeRegistry.rootScope

val q = StringQualifier("_qualifier_")
val scope = Scope(StringQualifier("_q_"), id = "_id_", _koin = koin, isRoot = false)
val key = "_KEY_"

assertEquals(
null, getViewModelKey(qualifier = null, scope = root, key = null)
)
assertEquals(
q.value, getViewModelKey(qualifier = q, scope = root, key = null)
)
assertEquals(
key, getViewModelKey(qualifier = null, scope = root, key = key)
)
assertEquals(
scope.id, getViewModelKey(qualifier = null, scope = scope, key = null)
)

assertEquals(
key + scope.id, getViewModelKey(qualifier = null, scope = scope, key = key)
)

assertEquals(
q.value + key + scope.id, getViewModelKey(qualifier = q, scope = scope, key = key)
)
}
}
2 changes: 1 addition & 1 deletion android/koin-androidx-navigation/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ dependencies {
api project(":koin-android")

// Android
api "androidx.navigation:navigation-fragment-ktx:2.7.1"
api "androidx.navigation:navigation-fragment-ktx:2.7.5"
}

apply from: '../../gradle/publish-to-central.gradle'
2 changes: 1 addition & 1 deletion bom/gradle/versions.gradle
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
ext {
koin_bom_version = '3.5.1'
koin_bom_version = '3.5.3'
}
2 changes: 2 additions & 0 deletions compose/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ buildscript {
mavenCentral()
mavenLocal()
maven { url "https://maven.pkg.jetbrains.space/public/p/kotlinx-html/maven" }
maven { url 'https://androidx.dev/storage/compose-compiler/repository/' }
}
dependencies {
// Kotlin
Expand Down Expand Up @@ -35,6 +36,7 @@ allprojects {
mavenCentral()
mavenLocal()
maven { url "https://maven.pkg.jetbrains.space/public/p/kotlinx-html/maven" }
maven { url 'https://androidx.dev/storage/compose-compiler/repository/' }
}

group = 'io.insert-koin'
Expand Down
14 changes: 7 additions & 7 deletions compose/gradle/versions.gradle
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
ext {

// Koin Versions
koin_androidx_compose_version = '3.5.0'
koin_compose_version = "1.1.0"
koin_androidx_compose_version = '3.5.3'
koin_compose_version = "1.1.2"

// Compose
compose_compiler = "1.5.0"
compose_compiler = "1.5.6"

// JB Compose
jb_compose_version = "1.5.0"
jb_compose_version = "1.5.11"

// Forced Version to keep compat
kotlin_version = '1.9.0'
koin_version = '3.5.0'
koin_android_version = '3.5.0'
// kotlin_version = '1.9.20'
// koin_version = '3.5.2-RC2'
// koin_android_version = '3.5.2-RC2'
}
2 changes: 1 addition & 1 deletion compose/koin-androidx-compose-navigation/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ dependencies {
// Koin
api project(":koin-androidx-compose")
// Navigation
api "androidx.navigation:navigation-compose:2.7.1"
api "androidx.navigation:navigation-compose:2.7.5"
}

apply from: '../../gradle/publish-to-central.gradle'
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import androidx.lifecycle.viewmodel.CreationExtras
import androidx.lifecycle.viewmodel.compose.LocalViewModelStoreOwner
import org.koin.androidx.viewmodel.resolveViewModel
import org.koin.compose.LocalKoinScope
import org.koin.compose.getKoinScope
import org.koin.compose.currentKoinScope
import org.koin.core.annotation.KoinInternalApi
import org.koin.core.parameter.ParametersDefinition
import org.koin.core.qualifier.Qualifier
Expand All @@ -46,7 +46,7 @@ inline fun <reified T : ViewModel> koinNavViewModel(
},
key: String? = null,
extras: CreationExtras = defaultNavExtras(viewModelStoreOwner),
scope: Scope = getKoinScope(),
scope: Scope = currentKoinScope(),
noinline parameters: ParametersDefinition? = null,
): T {
return resolveViewModel(
Expand Down
4 changes: 2 additions & 2 deletions compose/koin-androidx-compose/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ dependencies {
api "io.insert-koin:koin-android:$koin_android_version"
api project(":koin-compose")
// Compose
api "androidx.compose.runtime:runtime:1.5.0"
api "androidx.lifecycle:lifecycle-viewmodel-compose:2.6.1"
api "androidx.compose.runtime:runtime:1.5.4"
api "androidx.lifecycle:lifecycle-viewmodel-compose:2.6.2"
}

apply from: '../../gradle/publish-to-central.gradle'
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ package org.koin.androidx.compose

import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import org.koin.compose.getKoinScope
import org.koin.compose.currentKoinScope
import org.koin.compose.rememberCurrentKoinScope
import org.koin.core.Koin
import org.koin.core.annotation.KoinInternalApi
import org.koin.core.context.GlobalContext
Expand All @@ -40,7 +41,7 @@ import org.koin.core.scope.Scope
@Deprecated("use koinInject() instead")
inline fun <reified T> get(
qualifier: Qualifier? = null,
scope: Scope = getKoinScope(),
scope: Scope = currentKoinScope(),
noinline parameters: ParametersDefinition? = null,
): T = remember(qualifier, parameters, scope) {
scope.get(qualifier, parameters)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ import androidx.lifecycle.viewmodel.CreationExtras
import androidx.lifecycle.viewmodel.compose.LocalViewModelStoreOwner
import org.koin.androidx.viewmodel.resolveViewModel
import org.koin.compose.LocalKoinScope
import org.koin.compose.getKoinScope
import org.koin.compose.currentKoinScope
import org.koin.compose.rememberCurrentKoinScope
import org.koin.core.annotation.KoinInternalApi
import org.koin.core.parameter.ParametersDefinition
import org.koin.core.qualifier.Qualifier
Expand All @@ -39,14 +40,15 @@ import org.koin.core.scope.Scope
*/

@Composable
@Deprecated("use koinViewModel() instead", replaceWith = ReplaceWith("koinViewModel"))
inline fun <reified T : ViewModel> getViewModel(
qualifier: Qualifier? = null,
viewModelStoreOwner: ViewModelStoreOwner = checkNotNull(LocalViewModelStoreOwner.current) {
"No ViewModelStoreOwner was provided via LocalViewModelStoreOwner"
},
key: String? = null,
extras: CreationExtras = defaultExtras(viewModelStoreOwner),
scope: Scope = getKoinScope(),
scope: Scope = rememberCurrentKoinScope(),
noinline parameters: ParametersDefinition? = null,
): T {
return koinViewModel(qualifier, viewModelStoreOwner, key, extras, scope, parameters)
Expand All @@ -61,7 +63,7 @@ inline fun <reified T : ViewModel> koinViewModel(
},
key: String? = null,
extras: CreationExtras = defaultExtras(viewModelStoreOwner),
scope: Scope = getKoinScope(),
scope: Scope = currentKoinScope(),
noinline parameters: ParametersDefinition? = null,
): T {
return resolveViewModel(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.ui.platform.LocalContext
import org.koin.android.scope.AndroidScopeComponent
import org.koin.compose.LocalKoinScope
import org.koin.compose.getKoinScope


@Composable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,14 @@ import org.koin.core.scope.Scope
@Composable
inline fun <reified T> koinInject(
qualifier: Qualifier? = null,
scope: Scope = getKoinScope(),
scope: Scope = currentKoinScope(),
noinline parameters: ParametersDefinition? = null,
): T = rememberKoinInject(qualifier, scope, parameters)
): T {
val st = parameters?.let { rememberStableParametersDefinition(parameters) }
return remember(qualifier, scope) {
scope.get(qualifier, st?.parametersDefinition)
}
}

/**
* alias of koinInject()
Expand All @@ -46,13 +51,14 @@ inline fun <reified T> koinInject(
* @author Arnaud Giuliani
*/
@Composable
@Deprecated("")
inline fun <reified T> rememberKoinInject(
qualifier: Qualifier? = null,
scope: Scope = getKoinScope(),
scope: Scope = rememberCurrentKoinScope(),
noinline parameters: ParametersDefinition? = null,
): T {
val st = rememberStableParametersDefinition(parameters)
val st = parameters?.let { rememberStableParametersDefinition(parameters) }
return remember(qualifier, scope) {
scope.get(qualifier, st.parametersDefinition)
scope.get(qualifier, st?.parametersDefinition)
}
}
Loading

0 comments on commit e1f58a6

Please sign in to comment.