Skip to content

Commit

Permalink
Merge pull request #2081 from InsertKoinIO/fix_parameters_stability_i…
Browse files Browse the repository at this point in the history
…nject

Fix parameters stability inject
  • Loading branch information
arnaudgiuliani authored Dec 17, 2024
2 parents a92770c + ab56091 commit afffe59
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 13 deletions.
2 changes: 1 addition & 1 deletion examples/gradle/versions.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
ext {
// Kotlin
kotlin_version = '2.0.20'
kotlin_version = '2.0.21'
// Koin Versions
koin_version = '4.0.1-Beta2'
koin_android_version = koin_version
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ class MainApplication : Application() {
super.onCreate()

startKoin {
// androidLogger(Level.DEBUG)
printLogger(Level.DEBUG)
androidLogger(Level.DEBUG)
// printLogger(Level.DEBUG)
androidContext(this@MainApplication)
modules(appModule)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@
package org.koin.compose

import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberUpdatedState
import org.koin.core.annotation.KoinInternalApi
import org.koin.core.parameter.ParametersDefinition
import org.koin.core.parameter.emptyParametersHolder
import org.koin.core.parameter.ParametersHolder
import org.koin.core.qualifier.Qualifier
import org.koin.core.scope.Scope

Expand All @@ -33,18 +32,15 @@ import org.koin.core.scope.Scope
*
* @author Arnaud Giuliani
*/
@OptIn(KoinInternalApi::class)
@Composable
inline fun <reified T> koinInject(
qualifier: Qualifier? = null,
scope: Scope = currentKoinScope(),
noinline parameters: ParametersDefinition? = null,
): T {
// This will always refer to the latest parameters
val currentParameters by rememberUpdatedState(parameters)

return remember(qualifier, scope) {
scope.get(qualifier) {
currentParameters?.invoke() ?: emptyParametersHolder()
}
val params: ParametersHolder? = parameters?.invoke()
return remember(qualifier, scope, params) {
scope.getWithParameters(T::class, qualifier,params)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,16 @@ open class ParametersHolder(
}

override fun toString(): String = "DefinitionParameters${_values.toList()}"

override fun equals(other: Any?): Boolean {
if (other is ParametersHolder){
return this.values == other.values
} else return false
}

override fun hashCode(): Int {
return values.hashCode()
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,15 @@ class ParametersHolderTest {
val p = parametersOf(Simple.Component1())
assertNotNull(p.get<Simple.ComponentInterface1>())
}

@Test
fun `equality check`() {
val p1 = parametersOf(1, 2, 3, 4)
val p2 = parametersOf(1, 2, 3, 4)
val p3 = parametersOf(1, 2, 3)

assertEquals(p1, p2)

assertNotEquals(p1,p3)
}
}

0 comments on commit afffe59

Please sign in to comment.