From 1e1f7df86de7f05726fd08b3b2ffb68401fe1226 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Wed, 14 Sep 2022 22:14:20 +0600 Subject: [PATCH] create koin module --- CHANGELOG.md | 3 ++ gradle/libs.versions.toml | 4 +++ koin/build.gradle | 23 ++++++++++++++ .../kotlin/FactoryWithRandomQualifier.kt | 15 ++++++++++ .../kotlin/FactoryWithStringQualifier.kt | 12 ++++++++ koin/src/commonMain/kotlin/GetAllDistinct.kt | 8 +++++ koin/src/commonMain/kotlin/GetAny.kt | 8 +++++ .../kotlin/SingleWithRandomQualifier.kt | 16 ++++++++++ .../kotlin/SingleWithStringQualifier.kt | 12 ++++++++ koin/src/jvmMain/kotlin/FactoryWithBinds.kt | 27 +++++++++++++++++ .../FactoryWithBindsAndRandomQualifier.kt | 16 ++++++++++ koin/src/jvmMain/kotlin/SignleWithBinds.kt | 30 +++++++++++++++++++ .../SingleWithBindsAndRandomQualifier.kt | 15 ++++++++++ koin/src/main/AndroidManifest.xml | 1 + settings.gradle | 1 + 15 files changed, 191 insertions(+) create mode 100644 koin/build.gradle create mode 100644 koin/src/commonMain/kotlin/FactoryWithRandomQualifier.kt create mode 100644 koin/src/commonMain/kotlin/FactoryWithStringQualifier.kt create mode 100644 koin/src/commonMain/kotlin/GetAllDistinct.kt create mode 100644 koin/src/commonMain/kotlin/GetAny.kt create mode 100644 koin/src/commonMain/kotlin/SingleWithRandomQualifier.kt create mode 100644 koin/src/commonMain/kotlin/SingleWithStringQualifier.kt create mode 100644 koin/src/jvmMain/kotlin/FactoryWithBinds.kt create mode 100644 koin/src/jvmMain/kotlin/FactoryWithBindsAndRandomQualifier.kt create mode 100644 koin/src/jvmMain/kotlin/SignleWithBinds.kt create mode 100644 koin/src/jvmMain/kotlin/SingleWithBindsAndRandomQualifier.kt create mode 100644 koin/src/main/AndroidManifest.xml diff --git a/CHANGELOG.md b/CHANGELOG.md index c6cc14c1f17..fc6233a42b6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ ## 0.12.13 +* `Koin`: + * Module inited :) + ## 0.12.12 * `Common`: diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 57445abebbf..13617be09b3 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -15,6 +15,8 @@ ktor = "2.1.1" gh-release = "2.4.1" +koin = "3.2.0" + android-gradle = "7.2.2" dexcount = "3.1.0" @@ -31,6 +33,7 @@ android-props-buildTools = "32.0.0" [libraries] kt-stdlib = { module = "org.jetbrains.kotlin:kotlin-stdlib", version.ref = "kt" } +kt-reflect = { module = "org.jetbrains.kotlin:kotlin-reflect", version.ref = "kt" } kt-serialization = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json", version.ref = "kt-serialization" } kt-serialization-cbor = { module = "org.jetbrains.kotlinx:kotlinx-serialization-cbor", version.ref = "kt-serialization" } @@ -59,6 +62,7 @@ ktor-server-content-negotiation = { module = "io.ktor:ktor-server-content-negoti klock = { module = "com.soywiz.korlibs.klock:klock", version.ref = "klock" } uuid = { module = "com.benasher44:uuid", version.ref = "uuid" } +koin = { module = "io.insert-koin:koin-core", version.ref = "koin" } jb-exposed = { module = "org.jetbrains.exposed:exposed-core", version.ref = "jb-exposed" } diff --git a/koin/build.gradle b/koin/build.gradle new file mode 100644 index 00000000000..d6b3efd2031 --- /dev/null +++ b/koin/build.gradle @@ -0,0 +1,23 @@ +plugins { + id "org.jetbrains.kotlin.multiplatform" + id "org.jetbrains.kotlin.plugin.serialization" + id "com.android.library" +} + +apply from: "$mppProjectWithSerializationPresetPath" + +kotlin { + sourceSets { + commonMain { + dependencies { + api libs.koin + api libs.uuid + } + } + jvmMain { + dependencies { + api libs.kt.reflect + } + } + } +} diff --git a/koin/src/commonMain/kotlin/FactoryWithRandomQualifier.kt b/koin/src/commonMain/kotlin/FactoryWithRandomQualifier.kt new file mode 100644 index 00000000000..f307eadc8d2 --- /dev/null +++ b/koin/src/commonMain/kotlin/FactoryWithRandomQualifier.kt @@ -0,0 +1,15 @@ +package dev.inmo.micro_utils.koin + +import com.benasher44.uuid.uuid4 +import org.koin.core.definition.Definition +import org.koin.core.instance.InstanceFactory +import org.koin.core.module.Module +import org.koin.core.qualifier.StringQualifier + +/** + * Will be useful in case you need to declare some singles with one type several types, but need to separate them and do + * not care about how :) + */ +inline fun Module.factoryWithRandomQualifier( + noinline definition: Definition +) = factory(uuid4().toString(), definition) diff --git a/koin/src/commonMain/kotlin/FactoryWithStringQualifier.kt b/koin/src/commonMain/kotlin/FactoryWithStringQualifier.kt new file mode 100644 index 00000000000..0bf7301b107 --- /dev/null +++ b/koin/src/commonMain/kotlin/FactoryWithStringQualifier.kt @@ -0,0 +1,12 @@ +package dev.inmo.micro_utils.koin + +import org.koin.core.definition.Definition +import org.koin.core.module.Module +import org.koin.core.qualifier.StringQualifier + +inline fun Module.factory( + qualifier: String, + noinline definition: Definition +) = factory(StringQualifier(qualifier), definition) + + diff --git a/koin/src/commonMain/kotlin/GetAllDistinct.kt b/koin/src/commonMain/kotlin/GetAllDistinct.kt new file mode 100644 index 00000000000..59a610217cb --- /dev/null +++ b/koin/src/commonMain/kotlin/GetAllDistinct.kt @@ -0,0 +1,8 @@ +package dev.inmo.micro_utils.koin + +import org.koin.core.Koin +import org.koin.core.scope.Scope + +inline fun Scope.getAllDistinct() = getAll().distinct() +inline fun Koin.getAllDistinct() = getAll().distinct() + diff --git a/koin/src/commonMain/kotlin/GetAny.kt b/koin/src/commonMain/kotlin/GetAny.kt new file mode 100644 index 00000000000..351a1272fe0 --- /dev/null +++ b/koin/src/commonMain/kotlin/GetAny.kt @@ -0,0 +1,8 @@ +package dev.inmo.micro_utils.koin + +import org.koin.core.Koin +import org.koin.core.scope.Scope + +inline fun Scope.getAny() = getAll().first() +inline fun Koin.getAny() = getAll().first() + diff --git a/koin/src/commonMain/kotlin/SingleWithRandomQualifier.kt b/koin/src/commonMain/kotlin/SingleWithRandomQualifier.kt new file mode 100644 index 00000000000..f39aef61aea --- /dev/null +++ b/koin/src/commonMain/kotlin/SingleWithRandomQualifier.kt @@ -0,0 +1,16 @@ +package dev.inmo.micro_utils.koin + +import com.benasher44.uuid.uuid4 +import org.koin.core.definition.Definition +import org.koin.core.instance.InstanceFactory +import org.koin.core.module.Module +import org.koin.core.qualifier.StringQualifier + +/** + * Will be useful in case you need to declare some singles with one type several types, but need to separate them and do + * not care about how :) + */ +inline fun Module.singleWithRandomQualifier( + createdAtStart: Boolean = false, + noinline definition: Definition +) = single(uuid4().toString(), createdAtStart, definition) diff --git a/koin/src/commonMain/kotlin/SingleWithStringQualifier.kt b/koin/src/commonMain/kotlin/SingleWithStringQualifier.kt new file mode 100644 index 00000000000..cdde37efcca --- /dev/null +++ b/koin/src/commonMain/kotlin/SingleWithStringQualifier.kt @@ -0,0 +1,12 @@ +package dev.inmo.micro_utils.koin + +import org.koin.core.definition.Definition +import org.koin.core.module.Module +import org.koin.core.qualifier.StringQualifier + +inline fun Module.single( + qualifier: String, + createdAtStart: Boolean = false, + noinline definition: Definition +) = single(StringQualifier(qualifier), createdAtStart, definition) + diff --git a/koin/src/jvmMain/kotlin/FactoryWithBinds.kt b/koin/src/jvmMain/kotlin/FactoryWithBinds.kt new file mode 100644 index 00000000000..de91acff060 --- /dev/null +++ b/koin/src/jvmMain/kotlin/FactoryWithBinds.kt @@ -0,0 +1,27 @@ +package dev.inmo.micro_utils.koin + +import org.koin.core.definition.Definition +import org.koin.core.instance.InstanceFactory +import org.koin.core.module.Module +import org.koin.core.qualifier.Qualifier +import org.koin.dsl.binds +import kotlin.reflect.KClass +import kotlin.reflect.full.allSuperclasses + + +inline fun Module.factoryWithBinds( + qualifier: Qualifier? = null, + bindFilter: (KClass<*>) -> Boolean = { true }, + noinline definition: Definition +): Pair> { + return factory(qualifier, definition) binds (T::class.allSuperclasses.filter(bindFilter).toTypedArray()) +} + +inline fun Module.factoryWithBinds( + qualifier: String, + bindFilter: (KClass<*>) -> Boolean = { true }, + noinline definition: Definition +): Pair> { + return factory(qualifier, definition) binds (T::class.allSuperclasses.filter(bindFilter).toTypedArray()) +} + diff --git a/koin/src/jvmMain/kotlin/FactoryWithBindsAndRandomQualifier.kt b/koin/src/jvmMain/kotlin/FactoryWithBindsAndRandomQualifier.kt new file mode 100644 index 00000000000..08144cf8861 --- /dev/null +++ b/koin/src/jvmMain/kotlin/FactoryWithBindsAndRandomQualifier.kt @@ -0,0 +1,16 @@ +package dev.inmo.micro_utils.koin + +import com.benasher44.uuid.uuid4 +import org.koin.core.definition.Definition +import org.koin.core.instance.InstanceFactory +import org.koin.core.module.Module +import org.koin.dsl.binds +import kotlin.reflect.KClass +import kotlin.reflect.full.allSuperclasses + +inline fun Module.factoryWithRandomQualifierAndBinds( + bindFilter: (KClass<*>) -> Boolean = { true }, + noinline definition: Definition +): Pair> { + return factoryWithBinds(uuid4().toString(), bindFilter, definition) +} diff --git a/koin/src/jvmMain/kotlin/SignleWithBinds.kt b/koin/src/jvmMain/kotlin/SignleWithBinds.kt new file mode 100644 index 00000000000..091ad6a8ca6 --- /dev/null +++ b/koin/src/jvmMain/kotlin/SignleWithBinds.kt @@ -0,0 +1,30 @@ +package dev.inmo.micro_utils.koin + +import org.koin.core.definition.Definition +import org.koin.core.instance.InstanceFactory +import org.koin.core.module.Module +import org.koin.core.qualifier.Qualifier +import org.koin.dsl.binds +import kotlin.reflect.KClass +import kotlin.reflect.full.allSuperclasses + + +inline fun Module.singleWithBinds( + qualifier: Qualifier? = null, + createdAtStart: Boolean = false, + bindFilter: (KClass<*>) -> Boolean = { true }, + noinline definition: Definition +): Pair> { + return single(qualifier, createdAtStart, definition) binds (T::class.allSuperclasses.filter(bindFilter).toTypedArray()) +} + + +inline fun Module.singleWithBinds( + qualifier: String, + createdAtStart: Boolean = false, + bindFilter: (KClass<*>) -> Boolean = { true }, + noinline definition: Definition +): Pair> { + return single(qualifier, createdAtStart, definition) binds (T::class.allSuperclasses.filter(bindFilter).toTypedArray()) +} + diff --git a/koin/src/jvmMain/kotlin/SingleWithBindsAndRandomQualifier.kt b/koin/src/jvmMain/kotlin/SingleWithBindsAndRandomQualifier.kt new file mode 100644 index 00000000000..9a5aeed6c79 --- /dev/null +++ b/koin/src/jvmMain/kotlin/SingleWithBindsAndRandomQualifier.kt @@ -0,0 +1,15 @@ +package dev.inmo.micro_utils.koin + +import com.benasher44.uuid.uuid4 +import org.koin.core.definition.Definition +import org.koin.core.instance.InstanceFactory +import org.koin.core.module.Module +import kotlin.reflect.KClass + +inline fun Module.singleWithRandomQualifierAndBinds( + createdAtStart: Boolean = false, + bindFilter: (KClass<*>) -> Boolean = { true }, + noinline definition: Definition +): Pair> { + return singleWithBinds(uuid4().toString(), createdAtStart, bindFilter, definition) +} diff --git a/koin/src/main/AndroidManifest.xml b/koin/src/main/AndroidManifest.xml new file mode 100644 index 00000000000..a96d3e1aa05 --- /dev/null +++ b/koin/src/main/AndroidManifest.xml @@ -0,0 +1 @@ + diff --git a/settings.gradle b/settings.gradle index d7471ac4ccc..ccd2dc06d84 100644 --- a/settings.gradle +++ b/settings.gradle @@ -5,6 +5,7 @@ String[] includes = [ ":common:compose", ":matrix", ":crypto", + ":koin", ":selector:common", ":pagination:common", ":pagination:exposed",