diff --git a/app/src/main/java/yokai/core/migration/MigrationContext.kt b/app/src/main/java/yokai/core/migration/MigrationContext.kt index 487ae5b6ae..ceb1f37971 100644 --- a/app/src/main/java/yokai/core/migration/MigrationContext.kt +++ b/app/src/main/java/yokai/core/migration/MigrationContext.kt @@ -5,6 +5,6 @@ import uy.kohesive.injekt.Injekt class MigrationContext(val dryRun: Boolean) { inline fun get(): T? { - return Injekt.getInstanceOrNull(T::class) + return Injekt.getInstanceOrNull(T::class.java) } } diff --git a/injekt-koin/src/commonMain/kotlin/uy/kohesive/injekt/Injekt.kt b/injekt-koin/src/commonMain/kotlin/uy/kohesive/injekt/Injekt.kt index 08680ce9ce..a68ac7485e 100644 --- a/injekt-koin/src/commonMain/kotlin/uy/kohesive/injekt/Injekt.kt +++ b/injekt-koin/src/commonMain/kotlin/uy/kohesive/injekt/Injekt.kt @@ -1,25 +1,9 @@ package uy.kohesive.injekt -import org.koin.core.parameter.ParametersDefinition -import org.koin.core.qualifier.Qualifier -import org.koin.mp.KoinPlatformTools import uy.kohesive.injekt.api.InjektScope +import uy.kohesive.injekt.api.fullType @Volatile var Injekt: InjektScope = InjektScope() -inline fun getKoinInstance( - qualifier: Qualifier? = null, - noinline parameters: ParametersDefinition? = null, -): T = KoinPlatformTools.defaultContext().get().get(qualifier, parameters) - -inline fun getKoinInstanceOrNull( - qualifier: Qualifier? = null, - noinline parameters: ParametersDefinition? = null, -): T? = KoinPlatformTools.defaultContext().getOrNull()?.getOrNull(qualifier, parameters) - @Suppress("unused") -inline fun injectLazy( - qualifier: Qualifier? = null, - mode: LazyThreadSafetyMode = KoinPlatformTools.defaultLazyMode(), - noinline parameters: ParametersDefinition? = null, -): Lazy = lazy(mode) { getKoinInstance(qualifier, parameters) } +inline fun injectLazy(): Lazy = lazy { Injekt.getInstance(fullType().type) } diff --git a/injekt-koin/src/commonMain/kotlin/uy/kohesive/injekt/api/InjektFactory.kt b/injekt-koin/src/commonMain/kotlin/uy/kohesive/injekt/api/InjektFactory.kt new file mode 100644 index 0000000000..9e072cb5a0 --- /dev/null +++ b/injekt-koin/src/commonMain/kotlin/uy/kohesive/injekt/api/InjektFactory.kt @@ -0,0 +1,27 @@ +package uy.kohesive.injekt.api + +import java.lang.reflect.Type +import org.koin.core.parameter.ParametersDefinition +import org.koin.core.qualifier.Qualifier + +interface InjektFactory { + fun getInstance(forType: Type): R + /* + fun getInstanceOrElse(forType: Type, default: R): R + fun getInstanceOrElse(forType: Type, default: ()->R): R + */ + fun getInstanceOrNull(forType: Type): R? + + /* + fun getKeyedInstance(forType: Type, key: K): R + fun getKeyedInstanceOrElse(forType: Type, key: K, default: R): R + fun getKeyedInstanceOrElse(forType: Type, key: K, default: ()->R): R + fun getKeyedInstanceOrNull(forType: Type, key: K): R? + */ +} + +@Suppress("unused") +inline fun InjektFactory.get( + qualifier: Qualifier? = null, + noinline parameters: ParametersDefinition? = null, +): T = getInstance(fullType().type) diff --git a/injekt-koin/src/commonMain/kotlin/uy/kohesive/injekt/api/InjektScope.kt b/injekt-koin/src/commonMain/kotlin/uy/kohesive/injekt/api/InjektScope.kt index c5ecd8e05d..60b8341393 100644 --- a/injekt-koin/src/commonMain/kotlin/uy/kohesive/injekt/api/InjektScope.kt +++ b/injekt-koin/src/commonMain/kotlin/uy/kohesive/injekt/api/InjektScope.kt @@ -1,23 +1,13 @@ package uy.kohesive.injekt.api -import kotlin.reflect.KClass -import org.koin.core.parameter.ParametersDefinition -import org.koin.core.qualifier.Qualifier +import java.lang.reflect.Type import org.koin.mp.KoinPlatformTools -import uy.kohesive.injekt.getKoinInstance -class InjektScope { +class InjektScope : InjektFactory { - @Suppress("unused") - fun getInstanceOrNull( - clazz: KClass<*>, - qualifier: Qualifier? = null, - parameters: ParametersDefinition? = null, - ): T? = KoinPlatformTools.defaultContext().getOrNull()?.getOrNull(clazz, qualifier, parameters) + override fun getInstance(forType: Type): R = + KoinPlatformTools.defaultContext().get().get(forType.kotlinClass) + + override fun getInstanceOrNull(forType: Type): R? = + KoinPlatformTools.defaultContext().getOrNull()?.getOrNull(forType.kotlinClass) } - -@Suppress("unused", "UnusedReceiverParameter") -inline fun InjektScope.get( - qualifier: Qualifier? = null, - noinline parameters: ParametersDefinition? = null, -): T = getKoinInstance(qualifier, parameters) diff --git a/injekt-koin/src/commonMain/kotlin/uy/kohesive/injekt/api/InjektTypeInfo.kt b/injekt-koin/src/commonMain/kotlin/uy/kohesive/injekt/api/InjektTypeInfo.kt new file mode 100644 index 0000000000..60570d104b --- /dev/null +++ b/injekt-koin/src/commonMain/kotlin/uy/kohesive/injekt/api/InjektTypeInfo.kt @@ -0,0 +1,44 @@ +package uy.kohesive.injekt.api + +import java.lang.reflect.GenericArrayType +import java.lang.reflect.ParameterizedType +import java.lang.reflect.Type +import java.lang.reflect.TypeVariable +import java.lang.reflect.WildcardType + +@Suppress("UNCHECKED_CAST") fun Type.erasedType(): Class { + return when (this) { + is Class<*> -> this as Class + is ParameterizedType -> this.rawType.erasedType() + is GenericArrayType -> { + val elementType = this.genericComponentType.erasedType() + val testArray = java.lang.reflect.Array.newInstance(elementType, 0) + testArray.javaClass + } + is TypeVariable<*> -> { + throw IllegalStateException("Not sure what to do here yet") + } + is WildcardType -> { + this.upperBounds[0].erasedType() + } + else -> throw IllegalStateException("Should not get here.") + } +} + +val Type.kotlinClass get() = erasedType().kotlin + +inline fun typeRef(): FullTypeReference = object : FullTypeReference() {} +inline fun fullType(): FullTypeReference = object : FullTypeReference() {} + +interface TypeReference { + val type: Type +} + +abstract class FullTypeReference protected constructor() : TypeReference { + override val type: Type = javaClass.getGenericSuperclass().let { superClass -> + if (superClass is Class<*>) { + throw IllegalArgumentException("Internal error: TypeReference constructed without actual type information") + } + (superClass as ParameterizedType).actualTypeArguments[0] + } +}