refactor(deps): Use Injekt-Koin library

This commit is contained in:
Ahmad Ansori Palembani 2024-09-19 07:03:22 +07:00
parent 1ee48c91d3
commit 1300a9e7da
Signed by: null2264
GPG key ID: BA64F8B60AF3EFB6
10 changed files with 7 additions and 136 deletions

View file

@ -33,7 +33,9 @@ kotlin {
androidMain { androidMain {
dependencies { dependencies {
// Dependency injection // Dependency injection
api(projects.injektKoin) api(project.dependencies.platform(libs.koin.bom))
api(libs.koin.core)
api(libs.koin.injekt)
// Network client // Network client
api(libs.okhttp) api(libs.okhttp)

View file

@ -42,12 +42,12 @@ flexbox = { module = "com.google.android.flexbox:flexbox", version = "3.0.0" }
flexible-adapter-ui = { module = "com.github.arkon.FlexibleAdapter:flexible-adapter-ui", version.ref = "flexible-adapter" } flexible-adapter-ui = { module = "com.github.arkon.FlexibleAdapter:flexible-adapter-ui", version.ref = "flexible-adapter" }
flexible-adapter = { module = "com.github.arkon.FlexibleAdapter:flexible-adapter", version.ref = "flexible-adapter" } flexible-adapter = { module = "com.github.arkon.FlexibleAdapter:flexible-adapter", version.ref = "flexible-adapter" }
image-decoder = { module = "com.github.tachiyomiorg:image-decoder", version = "41c059e540" } image-decoder = { module = "com.github.tachiyomiorg:image-decoder", version = "41c059e540" }
injekt-core = { module = "com.github.null2264.injekt:injekt-core", version = "4135455a2a" }
kermit = { module = "co.touchlab:kermit", version.ref = "kermit" } kermit = { module = "co.touchlab:kermit", version.ref = "kermit" }
koin-bom = { module = "io.insert-koin:koin-bom", version.ref = "koin" } koin-bom = { module = "io.insert-koin:koin-bom", version.ref = "koin" }
koin-core = { module = "io.insert-koin:koin-core" } koin-core = { module = "io.insert-koin:koin-core" }
koin-injekt = { module = "com.github.null2264:injekt-koin", version = "7f944dd775" }
kotest-assertions = { module = "io.kotest:kotest-assertions-core", version = "5.9.1" } kotest-assertions = { module = "io.kotest:kotest-assertions-core", version = "5.9.1" }

View file

@ -1,38 +0,0 @@
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
alias(androidx.plugins.library)
alias(kotlinx.plugins.multiplatform)
alias(kotlinx.plugins.serialization)
}
kotlin {
androidTarget()
sourceSets {
val commonMain by getting {
dependencies {
api(project.dependencies.platform(libs.koin.bom))
api(libs.koin.core)
}
}
val androidMain by getting {
dependencies {
}
}
}
}
android {
namespace = "uy.kohesive.injekt"
defaultConfig {
consumerProguardFile("consumer-proguard.pro")
}
}
tasks {
withType<KotlinCompile> {
compilerOptions.freeCompilerArgs.addAll(
"-Xexpect-actual-classes",
)
}
}

View file

@ -1 +0,0 @@
-keep,allowoptimization class uy.kohesive.injekt.** { public protected *; }

View file

@ -1,9 +0,0 @@
package uy.kohesive.injekt
import uy.kohesive.injekt.api.InjektScope
import uy.kohesive.injekt.api.fullType
@Volatile var Injekt: InjektScope = InjektScope()
@Suppress("unused")
inline fun <reified T : Any> injectLazy(): Lazy<T> = lazy { Injekt.getInstance(fullType<T>().type) }

View file

@ -1,27 +0,0 @@
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 <R: Any> getInstance(forType: Type): R
/*
fun <R: Any> getInstanceOrElse(forType: Type, default: R): R
fun <R: Any> getInstanceOrElse(forType: Type, default: ()->R): R
*/
fun <R: Any> getInstanceOrNull(forType: Type): R?
/*
fun <R: Any, K: Any> getKeyedInstance(forType: Type, key: K): R
fun <R: Any, K: Any> getKeyedInstanceOrElse(forType: Type, key: K, default: R): R
fun <R: Any, K: Any> getKeyedInstanceOrElse(forType: Type, key: K, default: ()->R): R
fun <R: Any, K: Any> getKeyedInstanceOrNull(forType: Type, key: K): R?
*/
}
@Suppress("unused")
inline fun <reified T : Any> InjektFactory.get(
qualifier: Qualifier? = null,
noinline parameters: ParametersDefinition? = null,
): T = getInstance(fullType<T>().type)

View file

@ -1,13 +0,0 @@
package uy.kohesive.injekt.api
import java.lang.reflect.Type
import org.koin.mp.KoinPlatformTools
class InjektScope : InjektFactory {
override fun <R : Any> getInstance(forType: Type): R =
KoinPlatformTools.defaultContext().get().get(forType.kotlinClass)
override fun <R : Any> getInstanceOrNull(forType: Type): R? =
KoinPlatformTools.defaultContext().getOrNull()?.getOrNull(forType.kotlinClass)
}

View file

@ -1,44 +0,0 @@
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<Any> {
return when (this) {
is Class<*> -> this as Class<Any>
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 <reified T : Any> typeRef(): FullTypeReference<T> = object : FullTypeReference<T>() {}
inline fun <reified T : Any> fullType(): FullTypeReference<T> = object : FullTypeReference<T>() {}
interface TypeReference<T> {
val type: Type
}
abstract class FullTypeReference<T> protected constructor() : TypeReference<T> {
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]
}
}

View file

@ -35,7 +35,6 @@ include(":core")
include(":data") include(":data")
include(":domain") include(":domain")
include(":i18n") include(":i18n")
include(":injekt-koin")
include(":presentation:core") include(":presentation:core")
//include(":presentation:widget") //include(":presentation:widget")
include(":source:api") include(":source:api")

View file

@ -12,7 +12,9 @@ kotlin {
val commonMain by getting { val commonMain by getting {
dependencies { dependencies {
api(kotlinx.serialization.json) api(kotlinx.serialization.json)
api(projects.injektKoin) api(project.dependencies.platform(libs.koin.bom))
api(libs.koin.core)
api(libs.koin.injekt)
api(libs.rxjava) api(libs.rxjava)
api(libs.jsoup) api(libs.jsoup)
} }