diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 1026945..cbb441b 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -22,4 +22,4 @@ jobs: java-version: '21' - name: Build kinject - run: ./gradlew :kinject:build + run: ./gradlew :build diff --git a/README.md b/README.md index e65afcd..e2f92f2 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,20 @@ You can find all the posts [here](https://scottpierce.dev/categories/making-a-ko A multiplatform Dependency Injection library. +## Artifacts + +| Artifact | Description | +|----------------------------|------------------------------------------------------------------------------------------------------------------------------------------------| +| kinject-core | Provides core DI functionality. | +| kinject-android | Provides helper methods for working with `ObjectGraph`s embedded in `Context`s, and helper classes for embedding `ObjectGraph`s in `Contexts`. | +| kinject-android-appcompat | Provides `KinjectAppCompatActivity`. | +| kinject-android-compose | Provides `KinjectComponentActivity`. | +| kinject-compose | Provides the ability to provide an ObjectGraph in a Compose hierarchy. | +| kinject-compose-viewmodel | Provides the ability to get a ViewModel in a compose hierarchy. | +| kinject-coroutines | Provides a `KinjectCoroutineContextElement`, to easily allow passing an `ObjectGraph` via a `CoroutineContext`. +| kinject-viewmodel | Provides the ability to declare a view model dependency, and a factory to create it. | + + ## Usage ```kotlin diff --git a/benchmark-android/.gitignore b/benchmark/benchmark-android/.gitignore similarity index 100% rename from benchmark-android/.gitignore rename to benchmark/benchmark-android/.gitignore diff --git a/benchmark-android/.idea/workspace.xml b/benchmark/benchmark-android/.idea/workspace.xml similarity index 100% rename from benchmark-android/.idea/workspace.xml rename to benchmark/benchmark-android/.idea/workspace.xml diff --git a/benchmark-android/benchmark-proguard-rules.pro b/benchmark/benchmark-android/benchmark-proguard-rules.pro similarity index 100% rename from benchmark-android/benchmark-proguard-rules.pro rename to benchmark/benchmark-android/benchmark-proguard-rules.pro diff --git a/benchmark-android/build.gradle.kts b/benchmark/benchmark-android/build.gradle.kts similarity index 64% rename from benchmark-android/build.gradle.kts rename to benchmark/benchmark-android/build.gradle.kts index 21f92f3..4e222cc 100644 --- a/benchmark-android/build.gradle.kts +++ b/benchmark/benchmark-android/build.gradle.kts @@ -6,31 +6,14 @@ plugins { android { namespace = "dev.scottpierce.kinject.benchmark" - compileSdk = 34 - - compileOptions { - sourceCompatibility = JavaVersion.VERSION_1_8 - targetCompatibility = JavaVersion.VERSION_1_8 - } - - kotlinOptions { - jvmTarget = "1.8" - } - defaultConfig { - minSdk = 24 - targetSdk = 34 - testInstrumentationRunner = "androidx.benchmark.junit4.AndroidBenchmarkRunner" - - // TODO remove this after writing the benchmark is done - testInstrumentationRunnerArguments["androidx.benchmark.suppressErrors"] = "EMULATOR,LOW-BATTERY" } testBuildType = "release" buildTypes { debug { - // Since isDebuggable can"t be modified by gradle for library modules, + // Since isDebuggable can't be modified by gradle for library modules, // it must be done in a manifest - see src/androidTest/AndroidManifest.xml isMinifyEnabled = true proguardFiles( @@ -45,7 +28,7 @@ android { } dependencies { - androidTestImplementation(project(":benchmark-shared")) + androidTestImplementation(project(":benchmark:benchmark-shared")) androidTestImplementation(libs.androidx.runner) androidTestImplementation(libs.androidx.junit) diff --git a/benchmark-android/src/androidTest/AndroidManifest.xml b/benchmark/benchmark-android/src/androidTest/AndroidManifest.xml similarity index 100% rename from benchmark-android/src/androidTest/AndroidManifest.xml rename to benchmark/benchmark-android/src/androidTest/AndroidManifest.xml diff --git a/benchmark-android/src/androidTest/java/dev/scottpierce/kinject/benchmark/KinjectBenchmark.kt b/benchmark/benchmark-android/src/androidTest/java/dev/scottpierce/kinject/benchmark/KinjectBenchmark.kt similarity index 100% rename from benchmark-android/src/androidTest/java/dev/scottpierce/kinject/benchmark/KinjectBenchmark.kt rename to benchmark/benchmark-android/src/androidTest/java/dev/scottpierce/kinject/benchmark/KinjectBenchmark.kt diff --git a/benchmark-android/src/main/AndroidManifest.xml b/benchmark/benchmark-android/src/main/AndroidManifest.xml similarity index 100% rename from benchmark-android/src/main/AndroidManifest.xml rename to benchmark/benchmark-android/src/main/AndroidManifest.xml diff --git a/benchmark/build.gradle.kts b/benchmark/benchmark-kotlin/build.gradle.kts similarity index 88% rename from benchmark/build.gradle.kts rename to benchmark/benchmark-kotlin/build.gradle.kts index 1ed5a10..19424dd 100644 --- a/benchmark/build.gradle.kts +++ b/benchmark/benchmark-kotlin/build.gradle.kts @@ -13,7 +13,7 @@ kotlin { sourceSets { commonMain.dependencies { - implementation(project(":benchmark-shared")) + implementation(project(":benchmark:benchmark-shared")) implementation(libs.kotlinx.benchmark) } } diff --git a/benchmark/src/commonMain/kotlin/kinject/benchmark/CreateBenchmark.kt b/benchmark/benchmark-kotlin/src/commonMain/kotlin/kinject/benchmark/CreateBenchmark.kt similarity index 100% rename from benchmark/src/commonMain/kotlin/kinject/benchmark/CreateBenchmark.kt rename to benchmark/benchmark-kotlin/src/commonMain/kotlin/kinject/benchmark/CreateBenchmark.kt diff --git a/benchmark/src/commonMain/kotlin/kinject/benchmark/InjectionBenchmark.kt b/benchmark/benchmark-kotlin/src/commonMain/kotlin/kinject/benchmark/InjectionBenchmark.kt similarity index 100% rename from benchmark/src/commonMain/kotlin/kinject/benchmark/InjectionBenchmark.kt rename to benchmark/benchmark-kotlin/src/commonMain/kotlin/kinject/benchmark/InjectionBenchmark.kt diff --git a/benchmark/src/commonMain/kotlin/kinject/benchmark/Main.kt b/benchmark/benchmark-kotlin/src/commonMain/kotlin/kinject/benchmark/Main.kt similarity index 100% rename from benchmark/src/commonMain/kotlin/kinject/benchmark/Main.kt rename to benchmark/benchmark-kotlin/src/commonMain/kotlin/kinject/benchmark/Main.kt diff --git a/benchmark-shared/build.gradle.kts b/benchmark/benchmark-shared/build.gradle.kts similarity index 56% rename from benchmark-shared/build.gradle.kts rename to benchmark/benchmark-shared/build.gradle.kts index 476b1cd..46e8908 100644 --- a/benchmark-shared/build.gradle.kts +++ b/benchmark/benchmark-shared/build.gradle.kts @@ -3,13 +3,7 @@ plugins { } kotlin { - jvm { - compilations.all { - kotlinOptions { - jvmTarget = "1.8" - } - } - } + jvm() macosArm64() js { nodejs() @@ -17,7 +11,7 @@ kotlin { sourceSets { commonMain.dependencies { - api(project(":kinject")) + api(project(":kinject-core")) api(libs.koin.core) } } diff --git a/benchmark-shared/src/commonMain/kotlin/kinject/benchmark/Dependencies.kt b/benchmark/benchmark-shared/src/commonMain/kotlin/kinject/benchmark/Dependencies.kt similarity index 99% rename from benchmark-shared/src/commonMain/kotlin/kinject/benchmark/Dependencies.kt rename to benchmark/benchmark-shared/src/commonMain/kotlin/kinject/benchmark/Dependencies.kt index 540eddf..8196563 100644 --- a/benchmark-shared/src/commonMain/kotlin/kinject/benchmark/Dependencies.kt +++ b/benchmark/benchmark-shared/src/commonMain/kotlin/kinject/benchmark/Dependencies.kt @@ -312,5 +312,5 @@ class AllDependencies( val wwww: WWWW, val xxxx: XXXX, val yyyy: YYYY, - val zzzz: ZZZZ + val zzzz: ZZZZ, ) diff --git a/benchmark-shared/src/commonMain/kotlin/kinject/benchmark/LargeExample.kt b/benchmark/benchmark-shared/src/commonMain/kotlin/kinject/benchmark/LargeExample.kt similarity index 99% rename from benchmark-shared/src/commonMain/kotlin/kinject/benchmark/LargeExample.kt rename to benchmark/benchmark-shared/src/commonMain/kotlin/kinject/benchmark/LargeExample.kt index 515ffce..1835fed 100644 --- a/benchmark-shared/src/commonMain/kotlin/kinject/benchmark/LargeExample.kt +++ b/benchmark/benchmark-shared/src/commonMain/kotlin/kinject/benchmark/LargeExample.kt @@ -385,11 +385,10 @@ class LargeExample( get(), get(), get(), get(), get(), get(), get(), get(), get(), ) } - } + }, ) } return koinApp.koin } } } - diff --git a/benchmark-shared/src/commonMain/kotlin/kinject/benchmark/SmallExample.kt b/benchmark/benchmark-shared/src/commonMain/kotlin/kinject/benchmark/SmallExample.kt similarity index 92% rename from benchmark-shared/src/commonMain/kotlin/kinject/benchmark/SmallExample.kt rename to benchmark/benchmark-shared/src/commonMain/kotlin/kinject/benchmark/SmallExample.kt index 6383650..86483d5 100644 --- a/benchmark-shared/src/commonMain/kotlin/kinject/benchmark/SmallExample.kt +++ b/benchmark/benchmark-shared/src/commonMain/kotlin/kinject/benchmark/SmallExample.kt @@ -3,8 +3,6 @@ package kinject.benchmark import kinject.ObjectGraph import kinject.objectGraph import org.koin.core.Koin -import org.koin.core.context.startKoin -import org.koin.core.module.dsl.singleOf import org.koin.dsl.koinApplication import org.koin.dsl.module @@ -40,7 +38,7 @@ class SmallExample( single { D(get(), get(), get()) } single { E(get()) } single { SmallExample(get(), get(), get(), get(), get()) } - } + }, ) } return koinApp.koin diff --git a/build.gradle.kts b/build.gradle.kts index 9612124..443acd5 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,15 +1,145 @@ +import com.android.build.api.dsl.ApplicationExtension +import com.android.build.gradle.LibraryExtension +import com.android.build.gradle.TestExtension +import com.vanniktech.maven.publish.SonatypeHost +import org.jetbrains.kotlin.gradle.dsl.JvmTarget +import org.jetbrains.kotlin.gradle.dsl.KotlinTopLevelExtension +import org.jetbrains.kotlin.gradle.tasks.KotlinCompile + plugins { alias(libs.plugins.kotlin.multiplatform) apply false + alias(libs.plugins.kotlin.android) apply false + alias(libs.plugins.kotlin.compose) apply false alias(libs.plugins.gradleMavenPublish) apply false alias(libs.plugins.android.application) apply false - alias(libs.plugins.kotlin.android) apply false alias(libs.plugins.android.library) apply false alias(libs.plugins.androidx.benchmark) apply false } apply(rootProject.file("gradle/ktlint.gradle.kts")) +object AndroidConfig { + const val compileSdkVersion = 34 + const val targetSdkVersion = 34 + const val minSdkVersion = 23 + const val composeCompilerVersion = "1.5.14" +} + +val javaVersion = JavaVersion.VERSION_1_8 +val javaVersionInt = javaVersion.ordinal + 1 +val jvmTarget = JvmTarget.JVM_1_8 + +val ref = System.getenv()["GITHUB_REF"] +val releaseVersion: String? = if ( + System.getenv()["GITHUB_REF_TYPE"] == "tag" && + ref?.startsWith("refs/tags/v") == true +) { + ref.removePrefix("refs/tags/v") + .also { println("Releasing Version: $it") } +} else { + null +} + subprojects { group = "dev.scottpierce" version = "0.1.0" + + plugins.withId("com.android.application") { + configure { + compileOptions { + sourceCompatibility = javaVersion + targetCompatibility = javaVersion + } + + compileSdk = AndroidConfig.compileSdkVersion + + defaultConfig { + minSdk = AndroidConfig.minSdkVersion + targetSdk = AndroidConfig.targetSdkVersion + } + } + } + + plugins.withId("com.android.library") { + configure { + compileOptions { + sourceCompatibility = javaVersion + targetCompatibility = javaVersion + } + + compileSdk = AndroidConfig.compileSdkVersion + + defaultConfig { + minSdk = AndroidConfig.minSdkVersion + } + } + } + + plugins.withId("com.android.test") { + configure { + compileOptions { + sourceCompatibility = javaVersion + targetCompatibility = javaVersion + } + + compileSdk = AndroidConfig.compileSdkVersion + + defaultConfig { + minSdk = AndroidConfig.minSdkVersion + targetSdk = AndroidConfig.targetSdkVersion + } + } + } + + tasks.withType { + compilerOptions.jvmTarget.set(jvmTarget) + } + + plugins.withId("org.jetbrains.kotlin.jvm") { + configure { + jvmToolchain(javaVersionInt) + } + } + + if (releaseVersion != null) { + plugins.withId("com.vanniktech.maven.publish") { + configure { + publishToMavenCentral(SonatypeHost.CENTRAL_PORTAL, automaticRelease = true) + + signAllPublications() + + coordinates( + groupId = "dev.scottpierce", + artifactId = project.name, + version = releaseVersion, + ) + + pom { + name.set("Kinject") + description.set("A simple Kotlin Multiplatform dependency injection library.") + inceptionYear.set("2024") + url.set("https://github.com/ScottPierce/kinject") + licenses { + license { + name.set("The Apache License, Version 2.0") + url.set("http://www.apache.org/licenses/LICENSE-2.0.txt") + distribution.set("http://www.apache.org/licenses/LICENSE-2.0.txt") + } + } + developers { + developer { + id.set("ScottPierce") + name.set("Scott Pierce") + url.set("https://github.com/ScottPierce") + } + } + scm { + url.set("https://github.com/ScottPierce/kinject") + connection.set("scm:git:git://github.com/ScottPierce/kinject.git") + developerConnection.set("scm:git:ssh://git@github.com/ScottPierce/kinject.git") + } + } + } + } + } } diff --git a/gradle.properties b/gradle.properties index d844b09..4687e47 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,5 +1,7 @@ org.gradle.jvmargs=-Xmx3g -Dfile.encoding=UTF-8 -Dkotlin.daemon.jvm.options\="-Xmx2g" kotlin.code.style=official +kotlin.mpp.androidGradlePluginCompatibility.nowarn=true +kotlin.apple.xcodeCompatibility.nowarn=true android.useAndroidX=true diff --git a/gradle/ktlint.gradle.kts b/gradle/ktlint.gradle.kts index 10fcb66..b0b5708 100644 --- a/gradle/ktlint.gradle.kts +++ b/gradle/ktlint.gradle.kts @@ -8,7 +8,7 @@ dependencies { } } -val outputDir = "${project.buildDir}/reports/ktlint/" +val outputDir = "${project.layout.buildDirectory}/reports/ktlint/" val inputFiles = project.fileTree(mapOf("dir" to "src", "include" to "**/*.kt")) val commonArgs = listOf("**/src/**/*.kt", "!**/src/**/generated/**", "!**/build/**") diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 378b0c5..ee36045 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -1,21 +1,50 @@ [versions] +agp = "8.5.0" +compose = "1.6.8" +junit = "4.13.2" kotlin = "2.0.0" +kotlinx-coroutines = "1.8.1" kotlinxBenchmark = "0.4.10" - -agp = "8.2.0" -junit = "4.13.2" -junitVersion = "1.1.5" androidx-benchmark = "1.2.4" -runner = "1.5.2" +runner = "1.6.1" benchmarkJunit4 = "1.2.4" +androidx-lifecycle = "2.8.3" +compose-bom = "2024.06.00" +compose-compiler = "1.5.14" +coreKtx = "1.13.1" +espressoCore = "3.6.1" +lifecycleRuntimeKtx = "2.8.3" +activityCompose = "1.9.0" +appcompat = "1.7.0" +jetbrains-androidx-lifecycle = "2.8.0" [libraries] +androidx-appcompat = { group = "androidx.appcompat", name = "appcompat", version.ref = "appcompat" } androidx-benchmark-junit4 = { group = "androidx.benchmark", name = "benchmark-junit4", version.ref = "benchmarkJunit4" } -androidx-junit = { group = "androidx.test.ext", name = "junit", version.ref = "junitVersion" } +androidx-junit = { group = "androidx.test.ext", name = "junit", version = "1.2.1" } androidx-runner = { group = "androidx.test", name = "runner", version.ref = "runner" } +compose-bom = { group = "androidx.compose", name = "compose-bom", version = "2024.06.00" } +compose-runtime = { group = "androidx.compose.runtime", name = "runtime", version.ref = "compose" } junit = { group = "junit", name = "junit", version.ref = "junit" } -koin-core = { module = "io.insert-koin:koin-core", version = "3.2.0" } +koin-core = { module = "io.insert-koin:koin-core", version = "3.5.6" } kotlinx-benchmark = { module = "org.jetbrains.kotlinx:kotlinx-benchmark-runtime", version.ref = "kotlinxBenchmark" } +kotlinx-coroutines-core = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version.ref = "kotlinx-coroutines" } +androidx-lifecycle-viewmodel = { module = "androidx.lifecycle:lifecycle-viewmodel", version.ref = "androidx-lifecycle" } +androidx-lifecycle-viewmodel-compose = { module = "androidx.lifecycle:lifecycle-viewmodel-compose", version.ref = "androidx-lifecycle" } +jetbrains-androidx-lifecycle-viewmodel = { module = "org.jetbrains.androidx.lifecycle:lifecycle-viewmodel", version.ref = "jetbrains-androidx-lifecycle" } +jetbrains-androidx-lifecycle-viewmodel-compose = { module = "org.jetbrains.androidx.lifecycle:lifecycle-viewmodel-compose", version.ref = "jetbrains-androidx-lifecycle" } + +androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" } +androidx-espresso-core = { group = "androidx.test.espresso", name = "espresso-core", version.ref = "espressoCore" } +androidx-lifecycle-runtime-ktx = { group = "androidx.lifecycle", name = "lifecycle-runtime-ktx", version.ref = "lifecycleRuntimeKtx" } +androidx-activity-compose = { group = "androidx.activity", name = "activity-compose", version.ref = "activityCompose" } +androidx-ui = { group = "androidx.compose.ui", name = "ui" } +androidx-ui-graphics = { group = "androidx.compose.ui", name = "ui-graphics" } +androidx-ui-tooling = { group = "androidx.compose.ui", name = "ui-tooling" } +androidx-ui-tooling-preview = { group = "androidx.compose.ui", name = "ui-tooling-preview" } +androidx-ui-test-manifest = { group = "androidx.compose.ui", name = "ui-test-manifest" } +androidx-ui-test-junit4 = { group = "androidx.compose.ui", name = "ui-test-junit4" } +androidx-material3 = { group = "androidx.compose.material3", name = "material3" } [plugins] android-application = { id = "com.android.application", version.ref = "agp" } @@ -23,6 +52,7 @@ android-library = { id = "com.android.library", version.ref = "agp" } androidx-benchmark = { id = "androidx.benchmark", version.ref = "androidx-benchmark" } kotlin-allOpen = { id = "org.jetbrains.kotlin.plugin.allopen", version.ref = "kotlin" } kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" } +kotlin-compose = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" } kotlin-multiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" } kotlinx-benchmark = { id = "org.jetbrains.kotlinx.benchmark", version.ref = "kotlinxBenchmark" } -gradleMavenPublish = { id = "com.vanniktech.maven.publish", version = "0.28.0" } +gradleMavenPublish = { id = "com.vanniktech.maven.publish", version = "0.29.0" } diff --git a/kinject-android-appcompat/build.gradle.kts b/kinject-android-appcompat/build.gradle.kts new file mode 100644 index 0000000..40d05b7 --- /dev/null +++ b/kinject-android-appcompat/build.gradle.kts @@ -0,0 +1,14 @@ +plugins { + alias(libs.plugins.android.library) + alias(libs.plugins.kotlin.android) + alias(libs.plugins.gradleMavenPublish) +} + +android { + namespace = "kinject.android.appcompat" +} + +dependencies { + api(project(":kinject-android")) + implementation(libs.androidx.appcompat) +} diff --git a/kinject-android-appcompat/src/main/kotlin/kinject/android/KinjectAppCompatActivity.kt b/kinject-android-appcompat/src/main/kotlin/kinject/android/KinjectAppCompatActivity.kt new file mode 100644 index 0000000..e33854b --- /dev/null +++ b/kinject-android-appcompat/src/main/kotlin/kinject/android/KinjectAppCompatActivity.kt @@ -0,0 +1,27 @@ +package kinject.android + +import androidx.appcompat.app.AppCompatActivity +import kinject.HasObjectGraph +import kinject.ObjectGraph +import kinject.android.KinjectApplication.Companion.KINJECT_SERVICE + +abstract class KinjectAppCompatActivity : AppCompatActivity(), HasObjectGraph { + override val objectGraph: ObjectGraph by lazy { createObjectGraph() } + + protected abstract fun createObjectGraph(): ObjectGraph + + override fun getSystemService(name: String): Any { + return when (name) { + KINJECT_SERVICE -> objectGraph + else -> super.getSystemService(name) + } + } + + override fun getSystemServiceName(serviceClass: Class<*>): String? { + return if (serviceClass === ObjectGraph::class.java) { + KINJECT_SERVICE + } else { + super.getSystemServiceName(serviceClass) + } + } +} diff --git a/kinject-android-compose/build.gradle.kts b/kinject-android-compose/build.gradle.kts new file mode 100644 index 0000000..17d506f --- /dev/null +++ b/kinject-android-compose/build.gradle.kts @@ -0,0 +1,14 @@ +plugins { + alias(libs.plugins.android.library) + alias(libs.plugins.kotlin.android) + alias(libs.plugins.gradleMavenPublish) +} + +android { + namespace = "kinject.android.compose" +} + +dependencies { + api(project(":kinject-android")) + implementation(libs.androidx.activity.compose) +} diff --git a/kinject-android-compose/src/main/kotlin/kinject/android/KinjectComponentActivity.kt b/kinject-android-compose/src/main/kotlin/kinject/android/KinjectComponentActivity.kt new file mode 100644 index 0000000..aca4b3d --- /dev/null +++ b/kinject-android-compose/src/main/kotlin/kinject/android/KinjectComponentActivity.kt @@ -0,0 +1,27 @@ +package kinject.android + +import androidx.activity.ComponentActivity +import kinject.HasObjectGraph +import kinject.ObjectGraph +import kinject.android.KinjectApplication.Companion.KINJECT_SERVICE + +abstract class KinjectComponentActivity : ComponentActivity(), HasObjectGraph { + override val objectGraph: ObjectGraph by lazy { createObjectGraph() } + + protected abstract fun createObjectGraph(): ObjectGraph + + override fun getSystemService(name: String): Any { + return when (name) { + KINJECT_SERVICE -> objectGraph + else -> super.getSystemService(name) + } + } + + override fun getSystemServiceName(serviceClass: Class<*>): String? { + return if (serviceClass === ObjectGraph::class.java) { + KINJECT_SERVICE + } else { + super.getSystemServiceName(serviceClass) + } + } +} diff --git a/kinject-android/build.gradle.kts b/kinject-android/build.gradle.kts new file mode 100644 index 0000000..765aac6 --- /dev/null +++ b/kinject-android/build.gradle.kts @@ -0,0 +1,13 @@ +plugins { + alias(libs.plugins.android.library) + alias(libs.plugins.kotlin.android) + alias(libs.plugins.gradleMavenPublish) +} + +android { + namespace = "kinject.android" +} + +dependencies { + api(project(":kinject-core")) +} diff --git a/kinject-android/src/main/kotlin/kinject/android/ContextExt.kt b/kinject-android/src/main/kotlin/kinject/android/ContextExt.kt new file mode 100644 index 0000000..9b90f0b --- /dev/null +++ b/kinject-android/src/main/kotlin/kinject/android/ContextExt.kt @@ -0,0 +1,14 @@ +package kinject.android + +import android.annotation.SuppressLint +import android.content.Context +import kinject.ObjectGraph + +@SuppressLint("WrongConstant") +fun Context.objectGraphOrNull(): ObjectGraph? { + return getSystemService(KinjectApplication.KINJECT_SERVICE) as ObjectGraph? +} + +fun Context.objectGraph(): ObjectGraph { + return objectGraphOrNull() ?: error("ObjectGraph not found in Context.") +} diff --git a/kinject-android/src/main/kotlin/kinject/android/KinjectActivity.kt b/kinject-android/src/main/kotlin/kinject/android/KinjectActivity.kt new file mode 100644 index 0000000..e4f039a --- /dev/null +++ b/kinject-android/src/main/kotlin/kinject/android/KinjectActivity.kt @@ -0,0 +1,27 @@ +package kinject.android + +import android.app.Activity +import kinject.HasObjectGraph +import kinject.ObjectGraph +import kinject.android.KinjectApplication.Companion.KINJECT_SERVICE + +abstract class KinjectActivity : Activity(), HasObjectGraph { + override val objectGraph: ObjectGraph by lazy { createObjectGraph() } + + protected abstract fun createObjectGraph(): ObjectGraph + + override fun getSystemService(name: String): Any { + return when (name) { + KINJECT_SERVICE -> objectGraph + else -> super.getSystemService(name) + } + } + + override fun getSystemServiceName(serviceClass: Class<*>): String? { + return if (serviceClass === ObjectGraph::class.java) { + KINJECT_SERVICE + } else { + super.getSystemServiceName(serviceClass) + } + } +} diff --git a/kinject-android/src/main/kotlin/kinject/android/KinjectApplication.kt b/kinject-android/src/main/kotlin/kinject/android/KinjectApplication.kt new file mode 100644 index 0000000..62693c7 --- /dev/null +++ b/kinject-android/src/main/kotlin/kinject/android/KinjectApplication.kt @@ -0,0 +1,30 @@ +package kinject.android + +import android.app.Application +import kinject.HasObjectGraph +import kinject.ObjectGraph + +abstract class KinjectApplication : Application(), HasObjectGraph { + companion object { + const val KINJECT_SERVICE = "kinject" + } + + override val objectGraph: ObjectGraph by lazy { createObjectGraph() } + + protected abstract fun createObjectGraph(): ObjectGraph + + override fun getSystemService(name: String): Any { + return when (name) { + KINJECT_SERVICE -> objectGraph + else -> super.getSystemService(name) + } + } + + override fun getSystemServiceName(serviceClass: Class<*>): String? { + return if (serviceClass === ObjectGraph::class.java) { + KINJECT_SERVICE + } else { + super.getSystemServiceName(serviceClass) + } + } +} diff --git a/kinject-android/src/main/kotlin/kinject/android/KinjectContextWrapper.kt b/kinject-android/src/main/kotlin/kinject/android/KinjectContextWrapper.kt new file mode 100644 index 0000000..ad8ab38 --- /dev/null +++ b/kinject-android/src/main/kotlin/kinject/android/KinjectContextWrapper.kt @@ -0,0 +1,27 @@ +package kinject.android + +import android.content.Context +import android.content.ContextWrapper +import kinject.HasObjectGraph +import kinject.ObjectGraph +import kinject.android.KinjectApplication.Companion.KINJECT_SERVICE + +class KinjectContextWrapper( + base: Context, + override val objectGraph: ObjectGraph, +) : ContextWrapper(base), HasObjectGraph { + override fun getSystemService(name: String): Any { + return when (name) { + KINJECT_SERVICE -> objectGraph + else -> super.getSystemService(name) + } + } + + override fun getSystemServiceName(serviceClass: Class<*>): String? { + return if (serviceClass === ObjectGraph::class.java) { + KINJECT_SERVICE + } else { + super.getSystemServiceName(serviceClass) + } + } +} diff --git a/kinject-bom/build.gradle.kts b/kinject-bom/build.gradle.kts new file mode 100644 index 0000000..25e6ce0 --- /dev/null +++ b/kinject-bom/build.gradle.kts @@ -0,0 +1,21 @@ +plugins { + `java-platform` + alias(libs.plugins.gradleMavenPublish) +} + +javaPlatform { + allowDependencies() +} + +dependencies { + constraints { + api(project(":kinject-core")) + api(project(":kinject-android")) + api(project(":kinject-android-appcompat")) + api(project(":kinject-android-compose")) + api(project(":kinject-compose")) + api(project(":kinject-compose-viewmodel")) + api(project(":kinject-coroutines")) + api(project(":kinject-viewmodel")) + } +} diff --git a/kinject-compose-viewmodel/build.gradle.kts b/kinject-compose-viewmodel/build.gradle.kts new file mode 100644 index 0000000..a1fe300 --- /dev/null +++ b/kinject-compose-viewmodel/build.gradle.kts @@ -0,0 +1,16 @@ +plugins { + alias(libs.plugins.android.library) + alias(libs.plugins.kotlin.android) + alias(libs.plugins.kotlin.compose) + alias(libs.plugins.gradleMavenPublish) +} + +android { + namespace = "kinject.compose.viewmodel" +} + +dependencies { + api(project(":kinject-compose")) + api(project(":kinject-viewmodel")) + api(libs.androidx.lifecycle.viewmodel.compose) +} diff --git a/kinject-compose-viewmodel/src/main/kotlin/kinject/compose/viewmodel/GetViewModel.kt b/kinject-compose-viewmodel/src/main/kotlin/kinject/compose/viewmodel/GetViewModel.kt new file mode 100644 index 0000000..b8d9b37 --- /dev/null +++ b/kinject-compose-viewmodel/src/main/kotlin/kinject/compose/viewmodel/GetViewModel.kt @@ -0,0 +1,17 @@ +package kinject.compose.viewmodel + +import androidx.compose.runtime.Composable +import androidx.compose.runtime.remember +import androidx.lifecycle.ViewModel +import kinject.compose.LocalKinjectContext + +@Composable +inline fun getViewModel(): VM { + val vmClass = VM::class + val kinjectContext = LocalKinjectContext.current + val viewModelProvider = kinjectContext.viewModelProvider() + + return remember(vmClass, viewModelProvider) { + viewModelProvider[vmClass.java] + } +} diff --git a/kinject-compose-viewmodel/src/main/kotlin/kinject/compose/viewmodel/ViewModelProvider.kt b/kinject-compose-viewmodel/src/main/kotlin/kinject/compose/viewmodel/ViewModelProvider.kt new file mode 100644 index 0000000..cc9d4b2 --- /dev/null +++ b/kinject-compose-viewmodel/src/main/kotlin/kinject/compose/viewmodel/ViewModelProvider.kt @@ -0,0 +1,22 @@ +package kinject.compose.viewmodel + +import androidx.compose.runtime.Composable +import androidx.lifecycle.ViewModelProvider +import androidx.lifecycle.viewmodel.compose.LocalViewModelStoreOwner +import kinject.compose.KinjectContext +import kinject.viewmodel.KinjectViewModelFactory + +private const val KEY_VIEW_MODEL_PROVIDER = "view-model-provider" + +@Composable +fun KinjectContext.viewModelProvider(): ViewModelProvider { + get(KEY_VIEW_MODEL_PROVIDER) + ?.let { return@let it } + + val viewModelStoreOwner = LocalViewModelStoreOwner.current + ?: error("ViewModelStoreOwner not found") + val factory = KinjectViewModelFactory(objectGraph) + + return ViewModelProvider(viewModelStoreOwner, factory) + .also { put(KEY_VIEW_MODEL_PROVIDER, it) } +} diff --git a/kinject-compose/build.gradle.kts b/kinject-compose/build.gradle.kts new file mode 100644 index 0000000..9e4f472 --- /dev/null +++ b/kinject-compose/build.gradle.kts @@ -0,0 +1,19 @@ +plugins { + alias(libs.plugins.android.library) + alias(libs.plugins.kotlin.android) + alias(libs.plugins.kotlin.compose) + alias(libs.plugins.gradleMavenPublish) +} + +android { + namespace = "kinject.compose" +} + +composeCompiler { + stabilityConfigurationFile = project.layout.projectDirectory.file("compose-stability-config.conf") +} + +dependencies { + implementation(project(":kinject-core")) + implementation(libs.compose.runtime) +} diff --git a/kinject-compose/compose-stability-config.conf b/kinject-compose/compose-stability-config.conf new file mode 100644 index 0000000..a704b2e --- /dev/null +++ b/kinject-compose/compose-stability-config.conf @@ -0,0 +1,2 @@ +// Consider ObjectGraph stable +kinject.ObjectGraph \ No newline at end of file diff --git a/kinject-compose/src/main/kotlin/kinject/compose/KinjectContext.kt b/kinject-compose/src/main/kotlin/kinject/compose/KinjectContext.kt new file mode 100644 index 0000000..27211da --- /dev/null +++ b/kinject-compose/src/main/kotlin/kinject/compose/KinjectContext.kt @@ -0,0 +1,32 @@ +package kinject.compose + +import androidx.compose.runtime.Stable +import androidx.compose.runtime.compositionLocalOf +import kinject.ObjectGraph + +val LocalKinjectContext = compositionLocalOf { + error( + "No ObjectGraph or KinjectCache provided. Please use the `ObjectGraphProvider` " + + "Composable in a root Composable.", + ) +} + +@Stable +class KinjectContext( + val objectGraph: ObjectGraph, +) { + private var map: MutableMap? = null + + fun put(key: String, value: Any) { + val map = map + ?: HashMap() + .also { map = it } + + map[key] = value + } + + operator fun get(key: String): T? { + @Suppress("UNCHECKED_CAST") + return map?.get(key) as T? + } +} diff --git a/kinject-compose/src/main/kotlin/kinject/compose/LocalObjectGraph.kt b/kinject-compose/src/main/kotlin/kinject/compose/LocalObjectGraph.kt new file mode 100644 index 0000000..67aa986 --- /dev/null +++ b/kinject-compose/src/main/kotlin/kinject/compose/LocalObjectGraph.kt @@ -0,0 +1,19 @@ +package kinject.compose + +import androidx.compose.runtime.Composable +import androidx.compose.runtime.ProvidedValue +import kinject.ObjectGraph + +object LocalObjectGraph { + /** + * Returns current composition local value for an [ObjectGraph] or throws an + * [IllegalStateException] if one has not been provided. + */ + val current: ObjectGraph + @Composable + get() = LocalKinjectContext.current.objectGraph + + internal infix fun provides(objectGraph: ObjectGraph): ProvidedValue { + return LocalKinjectContext.provides(KinjectContext(objectGraph)) + } +} diff --git a/kinject-compose/src/main/kotlin/kinject/compose/ObjectGraphProvider.kt b/kinject-compose/src/main/kotlin/kinject/compose/ObjectGraphProvider.kt new file mode 100644 index 0000000..157ee12 --- /dev/null +++ b/kinject-compose/src/main/kotlin/kinject/compose/ObjectGraphProvider.kt @@ -0,0 +1,16 @@ +package kinject.compose + +import androidx.compose.runtime.Composable +import androidx.compose.runtime.CompositionLocalProvider +import kinject.ObjectGraph + +@Composable +fun ObjectGraphProvider( + objectGraph: ObjectGraph, + content: @Composable () -> Unit, +) { + CompositionLocalProvider( + LocalKinjectContext provides KinjectContext(objectGraph), + content = content, + ) +} diff --git a/kinject-core/build.gradle.kts b/kinject-core/build.gradle.kts new file mode 100644 index 0000000..3bfa2f9 --- /dev/null +++ b/kinject-core/build.gradle.kts @@ -0,0 +1,36 @@ +import org.jetbrains.kotlin.gradle.targets.js.dsl.ExperimentalWasmDsl + +plugins { + alias(libs.plugins.kotlin.multiplatform) + alias(libs.plugins.gradleMavenPublish) +} + +kotlin { + jvm() + js { + browser() + nodejs { + testTask { + useMocha { + timeout = "30000" + } + } + } + } + macosArm64() + macosX64() + iosX64() + iosArm64() + iosSimulatorArm64() + tvosX64() + tvosArm64() + tvosSimulatorArm64() + + applyDefaultHierarchyTemplate() + + sourceSets { + commonTest.dependencies { + implementation(kotlin("test")) + } + } +} diff --git a/kinject/src/appleMain/kotlin/kinject/KinjectPlatform.apple.kt b/kinject-core/src/appleMain/kotlin/kinject/KinjectPlatform.apple.kt similarity index 100% rename from kinject/src/appleMain/kotlin/kinject/KinjectPlatform.apple.kt rename to kinject-core/src/appleMain/kotlin/kinject/KinjectPlatform.apple.kt diff --git a/kinject/src/commonMain/kotlin/kinject/Binding.kt b/kinject-core/src/commonMain/kotlin/kinject/Binding.kt similarity index 89% rename from kinject/src/commonMain/kotlin/kinject/Binding.kt rename to kinject-core/src/commonMain/kotlin/kinject/Binding.kt index 1177833..0d77a8e 100644 --- a/kinject/src/commonMain/kotlin/kinject/Binding.kt +++ b/kinject-core/src/commonMain/kotlin/kinject/Binding.kt @@ -20,9 +20,11 @@ internal class SingletonLazyBinding( val resolve = resolve if (resolve != null) { if (isResolving) { - throw CyclicDependencyException("A cyclic dependency was found for '$key'" + + throw CyclicDependencyException( + "A cyclic dependency was found for '$key'" + "These should be avoided, but ObjectGraph.lazy() can be used as a work around if " + - "necessary.") + "necessary.", + ) } isResolving = true diff --git a/kinject/src/commonMain/kotlin/kinject/Exceptions.kt b/kinject-core/src/commonMain/kotlin/kinject/Exceptions.kt similarity index 100% rename from kinject/src/commonMain/kotlin/kinject/Exceptions.kt rename to kinject-core/src/commonMain/kotlin/kinject/Exceptions.kt diff --git a/kinject-core/src/commonMain/kotlin/kinject/HasObjectGraph.kt b/kinject-core/src/commonMain/kotlin/kinject/HasObjectGraph.kt new file mode 100644 index 0000000..0b50e57 --- /dev/null +++ b/kinject-core/src/commonMain/kotlin/kinject/HasObjectGraph.kt @@ -0,0 +1,5 @@ +package kinject + +interface HasObjectGraph { + val objectGraph: ObjectGraph +} diff --git a/kinject/src/commonMain/kotlin/kinject/KinjectPlatform.kt b/kinject-core/src/commonMain/kotlin/kinject/KinjectPlatform.kt similarity index 100% rename from kinject/src/commonMain/kotlin/kinject/KinjectPlatform.kt rename to kinject-core/src/commonMain/kotlin/kinject/KinjectPlatform.kt diff --git a/kinject/src/commonMain/kotlin/kinject/ObjectGraph.kt b/kinject-core/src/commonMain/kotlin/kinject/ObjectGraph.kt similarity index 79% rename from kinject/src/commonMain/kotlin/kinject/ObjectGraph.kt rename to kinject-core/src/commonMain/kotlin/kinject/ObjectGraph.kt index e1e8b08..b457f5a 100644 --- a/kinject/src/commonMain/kotlin/kinject/ObjectGraph.kt +++ b/kinject-core/src/commonMain/kotlin/kinject/ObjectGraph.kt @@ -19,7 +19,7 @@ class ObjectGraph private constructor( fun get(clazz: KClass): T = internalGet(clazz.className) @Suppress("UNCHECKED_CAST") - private fun internalGet(className: String): T { + internal fun internalGet(className: String): T { val binding = bindings[className] ?: throw BindingNotFoundException("Binding not found for class '$className'") return binding.resolve(this) as T @@ -45,7 +45,7 @@ class ObjectGraph private constructor( val bindingKey = clazz.className addBinding( key = bindingKey, - binding = SingletonLazyBinding(key = bindingKey, provider = provider) + binding = SingletonLazyBinding(key = bindingKey, provider = provider), ) } @@ -56,7 +56,22 @@ class ObjectGraph private constructor( val bindingKey = bindType.className addBinding( key = bindingKey, - binding = SingletonBinding(key = bindingKey, instance = instance) + binding = SingletonBinding(key = bindingKey, instance = instance), + ) + } + + inline fun factory( + noinline provider: ObjectGraph.() -> T, + ): Unit = singleton(T::class, provider) + + fun factory( + clazz: KClass, + provider: ObjectGraph.() -> T, + ) { + val bindingKey = clazz.className + addBinding( + key = bindingKey, + binding = FactoryBinding(key = bindingKey, provider = provider), ) } diff --git a/kinject/src/commonTest/kotlin/kinject/ObjectGraphTest.kt b/kinject-core/src/commonTest/kotlin/kinject/ObjectGraphTest.kt similarity index 100% rename from kinject/src/commonTest/kotlin/kinject/ObjectGraphTest.kt rename to kinject-core/src/commonTest/kotlin/kinject/ObjectGraphTest.kt diff --git a/kinject/src/jsMain/kotlin/kinject/KinjectPlatform.js.kt b/kinject-core/src/jsMain/kotlin/kinject/KinjectPlatform.js.kt similarity index 100% rename from kinject/src/jsMain/kotlin/kinject/KinjectPlatform.js.kt rename to kinject-core/src/jsMain/kotlin/kinject/KinjectPlatform.js.kt diff --git a/kinject/src/jvmMain/kotlin/kinject/KinjectPlatform.jvm.kt b/kinject-core/src/jvmMain/kotlin/kinject/KinjectPlatform.jvm.kt similarity index 100% rename from kinject/src/jvmMain/kotlin/kinject/KinjectPlatform.jvm.kt rename to kinject-core/src/jvmMain/kotlin/kinject/KinjectPlatform.jvm.kt diff --git a/kinject-core/src/jvmMain/kotlin/kinject/ObjectGraph.jvm.kt b/kinject-core/src/jvmMain/kotlin/kinject/ObjectGraph.jvm.kt new file mode 100644 index 0000000..94b35c5 --- /dev/null +++ b/kinject-core/src/jvmMain/kotlin/kinject/ObjectGraph.jvm.kt @@ -0,0 +1,5 @@ +package kinject + +fun ObjectGraph.get(clazz: Class): T { + return internalGet(clazz.name) +} diff --git a/kinject-coroutines/build.gradle.kts b/kinject-coroutines/build.gradle.kts new file mode 100644 index 0000000..1b0986b --- /dev/null +++ b/kinject-coroutines/build.gradle.kts @@ -0,0 +1,26 @@ +plugins { + alias(libs.plugins.kotlin.multiplatform) + alias(libs.plugins.gradleMavenPublish) +} + +kotlin { + jvm() + js() + macosArm64() + macosX64() + iosX64() + iosArm64() + iosSimulatorArm64() + tvosX64() + tvosArm64() + tvosSimulatorArm64() + + applyDefaultHierarchyTemplate() + + sourceSets { + commonMain.dependencies { + api(project(":kinject-core")) + api(libs.kotlinx.coroutines.core) + } + } +} diff --git a/kinject-coroutines/src/commonMain/kotlin/kinject/coroutines/KinjectCoroutineContextElement.kt b/kinject-coroutines/src/commonMain/kotlin/kinject/coroutines/KinjectCoroutineContextElement.kt new file mode 100644 index 0000000..393e68c --- /dev/null +++ b/kinject-coroutines/src/commonMain/kotlin/kinject/coroutines/KinjectCoroutineContextElement.kt @@ -0,0 +1,28 @@ +package kinject.coroutines + +import kinject.ObjectGraph +import kotlin.coroutines.AbstractCoroutineContextElement +import kotlin.coroutines.CoroutineContext +import kotlin.coroutines.coroutineContext + +class KinjectCoroutineContextElement( + val objectGraph: ObjectGraph +) : AbstractCoroutineContextElement(Key) { + companion object Key : CoroutineContext.Key +} + +fun CoroutineContext.objectGraphOrNull(): ObjectGraph? { + return this[KinjectCoroutineContextElement]?.objectGraph +} + +fun CoroutineContext.objectGraph(): ObjectGraph { + return this[KinjectCoroutineContextElement]?.objectGraph + ?: error("Can't find 'KinjectCoroutineContextElement' in the current CoroutineContext.") +} + +operator fun CoroutineContext.plus(objectGraph: ObjectGraph) = + this.plus(KinjectCoroutineContextElement(objectGraph)) + +suspend fun objectGraphOrNull(): ObjectGraph? = coroutineContext.objectGraphOrNull() + +suspend fun objectGraph(): ObjectGraph = coroutineContext.objectGraph() diff --git a/kinject-viewmodel/build.gradle.kts b/kinject-viewmodel/build.gradle.kts new file mode 100644 index 0000000..e3b2f02 --- /dev/null +++ b/kinject-viewmodel/build.gradle.kts @@ -0,0 +1,29 @@ +plugins { + alias(libs.plugins.kotlin.multiplatform) + alias(libs.plugins.android.library) + alias(libs.plugins.gradleMavenPublish) +} + +kotlin { + jvm() + js() + androidTarget() + macosArm64() + macosX64() + iosX64() + iosArm64() + iosSimulatorArm64() + + applyDefaultHierarchyTemplate() + + sourceSets { + commonMain.dependencies { + api(project(":kinject-core")) + implementation(libs.jetbrains.androidx.lifecycle.viewmodel) + } + } +} + +android { + namespace = "kinject.viewmodel" +} diff --git a/kinject-viewmodel/src/commonMain/kotlin/kinject/viewmodel/KinjectViewModelFactory.kt b/kinject-viewmodel/src/commonMain/kotlin/kinject/viewmodel/KinjectViewModelFactory.kt new file mode 100644 index 0000000..4a2661b --- /dev/null +++ b/kinject-viewmodel/src/commonMain/kotlin/kinject/viewmodel/KinjectViewModelFactory.kt @@ -0,0 +1,15 @@ +package kinject.viewmodel + +import androidx.lifecycle.ViewModel +import androidx.lifecycle.ViewModelProvider +import androidx.lifecycle.viewmodel.CreationExtras +import kinject.ObjectGraph +import kotlin.reflect.KClass + +class KinjectViewModelFactory( + private val objectGraph: ObjectGraph, +) : ViewModelProvider.Factory { + override fun create(modelClass: KClass, extras: CreationExtras): T { + return objectGraph.get(modelClass) + } +} diff --git a/kinject-viewmodel/src/commonMain/kotlin/kinject/viewmodel/ObjectGraphExt.kt b/kinject-viewmodel/src/commonMain/kotlin/kinject/viewmodel/ObjectGraphExt.kt new file mode 100644 index 0000000..b943373 --- /dev/null +++ b/kinject-viewmodel/src/commonMain/kotlin/kinject/viewmodel/ObjectGraphExt.kt @@ -0,0 +1,11 @@ +package kinject.viewmodel + +import androidx.lifecycle.ViewModel +import kinject.ObjectGraph + +/** + * A ViewModel representation that delegates to + */ +inline fun ObjectGraph.Builder.viewModel( + noinline provider: ObjectGraph.() -> T, +): Unit = factory(T::class, provider) diff --git a/kinject/build.gradle.kts b/kinject/build.gradle.kts deleted file mode 100644 index 9570da3..0000000 --- a/kinject/build.gradle.kts +++ /dev/null @@ -1,78 +0,0 @@ -import com.vanniktech.maven.publish.SonatypeHost - -plugins { - alias(libs.plugins.kotlin.multiplatform) - alias(libs.plugins.gradleMavenPublish) -} - -kotlin { - jvm { - compilations.all { - kotlinOptions { - jvmTarget = "1.8" - } - } - } - js { - browser() - nodejs() - } - macosArm64() - macosX64() - iosX64() - iosArm64() - iosSimulatorArm64() - tvosX64() - tvosArm64() - tvosSimulatorArm64() - - sourceSets { - commonTest.dependencies { - implementation(kotlin("test")) - } - } -} - -val ref = System.getenv()["GITHUB_REF"] -if (System.getenv()["GITHUB_REF_TYPE"] == "tag" && ref?.startsWith("refs/tags/v") == true) { - val version = ref.removePrefix("refs/tags/v") - println("Releasing Version: $version") - - mavenPublishing { - publishToMavenCentral(SonatypeHost.CENTRAL_PORTAL, automaticRelease = true) - - signAllPublications() - - coordinates( - groupId = "dev.scottpierce", - artifactId = "kinject", - version = version, - ) - - pom { - name.set("Kinject") - description.set("A simple Kotlin Multiplatform dependency injection library.") - inceptionYear.set("2024") - url.set("https://github.com/ScottPierce/kinject") - licenses { - license { - name.set("The Apache License, Version 2.0") - url.set("http://www.apache.org/licenses/LICENSE-2.0.txt") - distribution.set("http://www.apache.org/licenses/LICENSE-2.0.txt") - } - } - developers { - developer { - id.set("ScottPierce") - name.set("Scott Pierce") - url.set("https://github.com/ScottPierce") - } - } - scm { - url.set("https://github.com/ScottPierce/kinject") - connection.set("scm:git:git://github.com/ScottPierce/kinject.git") - developerConnection.set("scm:git:ssh://git@github.com/ScottPierce/kinject.git") - } - } - } -} diff --git a/sample/kinject-sample-android/.gitignore b/sample/kinject-sample-android/.gitignore new file mode 100644 index 0000000..42afabf --- /dev/null +++ b/sample/kinject-sample-android/.gitignore @@ -0,0 +1 @@ +/build \ No newline at end of file diff --git a/sample/kinject-sample-android/kinject-sample-android.gradle.kts b/sample/kinject-sample-android/kinject-sample-android.gradle.kts new file mode 100644 index 0000000..6763d67 --- /dev/null +++ b/sample/kinject-sample-android/kinject-sample-android.gradle.kts @@ -0,0 +1,66 @@ +plugins { + alias(libs.plugins.android.application) + alias(libs.plugins.kotlin.android) + alias(libs.plugins.kotlin.compose) +} + +android { + namespace = "dev.scottpierce.kinject.sample.android" + + defaultConfig { + applicationId = "dev.scottpierce.kinject.sample.android" + versionCode = 1 + versionName = "1.0.0" + + testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" + vectorDrawables { + useSupportLibrary = true + } + } + + buildTypes { + release { + isMinifyEnabled = false + proguardFiles( + getDefaultProguardFile("proguard-android-optimize.txt"), + "proguard-rules.pro" + ) + } + } + buildFeatures { + compose = true + } + composeOptions { + kotlinCompilerExtensionVersion = libs.versions.compose.compiler.get() + } + packaging { + resources { + excludes += "/META-INF/{AL2.0,LGPL2.1}" + } + } +} + +dependencies { + implementation(project(":kinject-core")) + implementation(project(":kinject-compose")) + implementation(project(":kinject-compose-viewmodel")) + + implementation(libs.androidx.core.ktx) + implementation(libs.androidx.lifecycle.runtime.ktx) + implementation(libs.androidx.activity.compose) + implementation(platform(libs.compose.bom)) + implementation(libs.androidx.ui) + implementation(libs.androidx.ui.graphics) + implementation(libs.androidx.ui.tooling.preview) + implementation(libs.androidx.material3) + + debugImplementation(libs.androidx.ui.tooling) + debugImplementation(libs.androidx.ui.test.manifest) + + testImplementation(libs.junit) + + androidTestImplementation(libs.androidx.junit) + androidTestImplementation(libs.androidx.espresso.core) + androidTestImplementation(platform(libs.compose.bom)) + androidTestImplementation(libs.androidx.ui.test.junit4) +} diff --git a/sample/kinject-sample-android/proguard-rules.pro b/sample/kinject-sample-android/proguard-rules.pro new file mode 100644 index 0000000..481bb43 --- /dev/null +++ b/sample/kinject-sample-android/proguard-rules.pro @@ -0,0 +1,21 @@ +# Add project specific ProGuard rules here. +# You can control the set of applied configuration files using the +# proguardFiles setting in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} + +# Uncomment this to preserve the line number information for +# debugging stack traces. +#-keepattributes SourceFile,LineNumberTable + +# If you keep the line number information, uncomment this to +# hide the original source file name. +#-renamesourcefileattribute SourceFile \ No newline at end of file diff --git a/sample/kinject-sample-android/src/androidTest/java/dev/scottpierce/kinject/sample/android/ExampleInstrumentedTest.kt b/sample/kinject-sample-android/src/androidTest/java/dev/scottpierce/kinject/sample/android/ExampleInstrumentedTest.kt new file mode 100644 index 0000000..a7beadb --- /dev/null +++ b/sample/kinject-sample-android/src/androidTest/java/dev/scottpierce/kinject/sample/android/ExampleInstrumentedTest.kt @@ -0,0 +1,22 @@ +package dev.scottpierce.kinject.sample.android + +import androidx.test.ext.junit.runners.AndroidJUnit4 +import androidx.test.platform.app.InstrumentationRegistry +import org.junit.Assert.* +import org.junit.Test +import org.junit.runner.RunWith + +/** + * Instrumented test, which will execute on an Android device. + * + * See [testing documentation](http://d.android.com/tools/testing). + */ +@RunWith(AndroidJUnit4::class) +class ExampleInstrumentedTest { + @Test + fun useAppContext() { + // Context of the app under test. + val appContext = InstrumentationRegistry.getInstrumentation().targetContext + assertEquals("dev.scottpierce.kinject.sample.android", appContext.packageName) + } +} diff --git a/sample/kinject-sample-android/src/main/AndroidManifest.xml b/sample/kinject-sample-android/src/main/AndroidManifest.xml new file mode 100644 index 0000000..ed351e0 --- /dev/null +++ b/sample/kinject-sample-android/src/main/AndroidManifest.xml @@ -0,0 +1,24 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/sample/kinject-sample-android/src/main/java/dev/scottpierce/kinject/sample/android/MainActivity.kt b/sample/kinject-sample-android/src/main/java/dev/scottpierce/kinject/sample/android/MainActivity.kt new file mode 100644 index 0000000..f2aba52 --- /dev/null +++ b/sample/kinject-sample-android/src/main/java/dev/scottpierce/kinject/sample/android/MainActivity.kt @@ -0,0 +1,69 @@ +package dev.scottpierce.kinject.sample.android + +import android.os.Bundle +import androidx.activity.ComponentActivity +import androidx.activity.compose.setContent +import androidx.activity.enableEdgeToEdge +import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.foundation.layout.padding +import androidx.compose.material3.Scaffold +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect +import androidx.compose.runtime.collectAsState +import androidx.compose.runtime.getValue +import androidx.compose.ui.Modifier +import androidx.compose.ui.tooling.preview.Preview +import dev.scottpierce.kinject.sample.android.ui.theme.KinjectTheme +import kinject.compose.ObjectGraphProvider +import kinject.compose.viewmodel.getViewModel +import kinject.objectGraph +import kinject.viewmodel.viewModel +import kotlinx.coroutines.delay + +private val objectGraph = objectGraph { + viewModel { TestViewModel() } +} + +class MainActivity : ComponentActivity() { + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + enableEdgeToEdge() + setContent { + KinjectTheme { + Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding -> + ObjectGraphProvider(objectGraph = objectGraph) { + val vm: TestViewModel = getViewModel() + val name by vm.state.collectAsState() + + LaunchedEffect(Unit) { + delay(1000) + vm.updateName("New Name") + } + + Greeting( + name = name, + modifier = Modifier.padding(innerPadding), + ) + } + } + } + } + } +} + +@Composable +fun Greeting(name: String, modifier: Modifier = Modifier) { + Text( + text = "Hello $name!", + modifier = modifier, + ) +} + +@Preview(showBackground = true) +@Composable +fun GreetingPreview() { + KinjectTheme { + Greeting("Android") + } +} diff --git a/sample/kinject-sample-android/src/main/java/dev/scottpierce/kinject/sample/android/TestViewModel.kt b/sample/kinject-sample-android/src/main/java/dev/scottpierce/kinject/sample/android/TestViewModel.kt new file mode 100644 index 0000000..b5d2f95 --- /dev/null +++ b/sample/kinject-sample-android/src/main/java/dev/scottpierce/kinject/sample/android/TestViewModel.kt @@ -0,0 +1,15 @@ +package dev.scottpierce.kinject.sample.android + +import androidx.lifecycle.ViewModel +import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.StateFlow + +class TestViewModel : ViewModel() { + private val _state: MutableStateFlow = MutableStateFlow("Android2") + val state: StateFlow + get() = _state + + fun updateName(name: String) { + _state.value = name + } +} diff --git a/sample/kinject-sample-android/src/main/java/dev/scottpierce/kinject/sample/android/ui/theme/Color.kt b/sample/kinject-sample-android/src/main/java/dev/scottpierce/kinject/sample/android/ui/theme/Color.kt new file mode 100644 index 0000000..61a4ec7 --- /dev/null +++ b/sample/kinject-sample-android/src/main/java/dev/scottpierce/kinject/sample/android/ui/theme/Color.kt @@ -0,0 +1,11 @@ +package dev.scottpierce.kinject.sample.android.ui.theme + +import androidx.compose.ui.graphics.Color + +val Purple80 = Color(0xFFD0BCFF) +val PurpleGrey80 = Color(0xFFCCC2DC) +val Pink80 = Color(0xFFEFB8C8) + +val Purple40 = Color(0xFF6650a4) +val PurpleGrey40 = Color(0xFF625b71) +val Pink40 = Color(0xFF7D5260) diff --git a/sample/kinject-sample-android/src/main/java/dev/scottpierce/kinject/sample/android/ui/theme/Theme.kt b/sample/kinject-sample-android/src/main/java/dev/scottpierce/kinject/sample/android/ui/theme/Theme.kt new file mode 100644 index 0000000..598871c --- /dev/null +++ b/sample/kinject-sample-android/src/main/java/dev/scottpierce/kinject/sample/android/ui/theme/Theme.kt @@ -0,0 +1,57 @@ +package dev.scottpierce.kinject.sample.android.ui.theme + +import android.os.Build +import androidx.compose.foundation.isSystemInDarkTheme +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.darkColorScheme +import androidx.compose.material3.dynamicDarkColorScheme +import androidx.compose.material3.dynamicLightColorScheme +import androidx.compose.material3.lightColorScheme +import androidx.compose.runtime.Composable +import androidx.compose.ui.platform.LocalContext + +private val DarkColorScheme = darkColorScheme( + primary = Purple80, + secondary = PurpleGrey80, + tertiary = Pink80, +) + +private val LightColorScheme = lightColorScheme( + primary = Purple40, + secondary = PurpleGrey40, + tertiary = Pink40, + + /* Other default colors to override + background = Color(0xFFFFFBFE), + surface = Color(0xFFFFFBFE), + onPrimary = Color.White, + onSecondary = Color.White, + onTertiary = Color.White, + onBackground = Color(0xFF1C1B1F), + onSurface = Color(0xFF1C1B1F), + */ +) + +@Composable +fun KinjectTheme( + darkTheme: Boolean = isSystemInDarkTheme(), + // Dynamic color is available on Android 12+ + dynamicColor: Boolean = true, + content: @Composable () -> Unit, +) { + val colorScheme = when { + dynamicColor && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> { + val context = LocalContext.current + if (darkTheme) dynamicDarkColorScheme(context) else dynamicLightColorScheme(context) + } + + darkTheme -> DarkColorScheme + else -> LightColorScheme + } + + MaterialTheme( + colorScheme = colorScheme, + typography = Typography, + content = content, + ) +} diff --git a/sample/kinject-sample-android/src/main/java/dev/scottpierce/kinject/sample/android/ui/theme/Type.kt b/sample/kinject-sample-android/src/main/java/dev/scottpierce/kinject/sample/android/ui/theme/Type.kt new file mode 100644 index 0000000..571b426 --- /dev/null +++ b/sample/kinject-sample-android/src/main/java/dev/scottpierce/kinject/sample/android/ui/theme/Type.kt @@ -0,0 +1,34 @@ +package dev.scottpierce.kinject.sample.android.ui.theme + +import androidx.compose.material3.Typography +import androidx.compose.ui.text.TextStyle +import androidx.compose.ui.text.font.FontFamily +import androidx.compose.ui.text.font.FontWeight +import androidx.compose.ui.unit.sp + +// Set of Material typography styles to start with +val Typography = Typography( + bodyLarge = TextStyle( + fontFamily = FontFamily.Default, + fontWeight = FontWeight.Normal, + fontSize = 16.sp, + lineHeight = 24.sp, + letterSpacing = 0.5.sp, + ), + /* Other default text styles to override + titleLarge = TextStyle( + fontFamily = FontFamily.Default, + fontWeight = FontWeight.Normal, + fontSize = 22.sp, + lineHeight = 28.sp, + letterSpacing = 0.sp + ), + labelSmall = TextStyle( + fontFamily = FontFamily.Default, + fontWeight = FontWeight.Medium, + fontSize = 11.sp, + lineHeight = 16.sp, + letterSpacing = 0.5.sp + ) + */ +) diff --git a/sample/kinject-sample-android/src/main/res/drawable/ic_launcher_background.xml b/sample/kinject-sample-android/src/main/res/drawable/ic_launcher_background.xml new file mode 100644 index 0000000..07d5da9 --- /dev/null +++ b/sample/kinject-sample-android/src/main/res/drawable/ic_launcher_background.xml @@ -0,0 +1,170 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/sample/kinject-sample-android/src/main/res/drawable/ic_launcher_foreground.xml b/sample/kinject-sample-android/src/main/res/drawable/ic_launcher_foreground.xml new file mode 100644 index 0000000..2b068d1 --- /dev/null +++ b/sample/kinject-sample-android/src/main/res/drawable/ic_launcher_foreground.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/sample/kinject-sample-android/src/main/res/mipmap-anydpi-v26/ic_launcher.xml b/sample/kinject-sample-android/src/main/res/mipmap-anydpi-v26/ic_launcher.xml new file mode 100644 index 0000000..6f3b755 --- /dev/null +++ b/sample/kinject-sample-android/src/main/res/mipmap-anydpi-v26/ic_launcher.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/sample/kinject-sample-android/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml b/sample/kinject-sample-android/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml new file mode 100644 index 0000000..6f3b755 --- /dev/null +++ b/sample/kinject-sample-android/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/sample/kinject-sample-android/src/main/res/mipmap-hdpi/ic_launcher.webp b/sample/kinject-sample-android/src/main/res/mipmap-hdpi/ic_launcher.webp new file mode 100644 index 0000000..c209e78 Binary files /dev/null and b/sample/kinject-sample-android/src/main/res/mipmap-hdpi/ic_launcher.webp differ diff --git a/sample/kinject-sample-android/src/main/res/mipmap-hdpi/ic_launcher_round.webp b/sample/kinject-sample-android/src/main/res/mipmap-hdpi/ic_launcher_round.webp new file mode 100644 index 0000000..b2dfe3d Binary files /dev/null and b/sample/kinject-sample-android/src/main/res/mipmap-hdpi/ic_launcher_round.webp differ diff --git a/sample/kinject-sample-android/src/main/res/mipmap-mdpi/ic_launcher.webp b/sample/kinject-sample-android/src/main/res/mipmap-mdpi/ic_launcher.webp new file mode 100644 index 0000000..4f0f1d6 Binary files /dev/null and b/sample/kinject-sample-android/src/main/res/mipmap-mdpi/ic_launcher.webp differ diff --git a/sample/kinject-sample-android/src/main/res/mipmap-mdpi/ic_launcher_round.webp b/sample/kinject-sample-android/src/main/res/mipmap-mdpi/ic_launcher_round.webp new file mode 100644 index 0000000..62b611d Binary files /dev/null and b/sample/kinject-sample-android/src/main/res/mipmap-mdpi/ic_launcher_round.webp differ diff --git a/sample/kinject-sample-android/src/main/res/mipmap-xhdpi/ic_launcher.webp b/sample/kinject-sample-android/src/main/res/mipmap-xhdpi/ic_launcher.webp new file mode 100644 index 0000000..948a307 Binary files /dev/null and b/sample/kinject-sample-android/src/main/res/mipmap-xhdpi/ic_launcher.webp differ diff --git a/sample/kinject-sample-android/src/main/res/mipmap-xhdpi/ic_launcher_round.webp b/sample/kinject-sample-android/src/main/res/mipmap-xhdpi/ic_launcher_round.webp new file mode 100644 index 0000000..1b9a695 Binary files /dev/null and b/sample/kinject-sample-android/src/main/res/mipmap-xhdpi/ic_launcher_round.webp differ diff --git a/sample/kinject-sample-android/src/main/res/mipmap-xxhdpi/ic_launcher.webp b/sample/kinject-sample-android/src/main/res/mipmap-xxhdpi/ic_launcher.webp new file mode 100644 index 0000000..28d4b77 Binary files /dev/null and b/sample/kinject-sample-android/src/main/res/mipmap-xxhdpi/ic_launcher.webp differ diff --git a/sample/kinject-sample-android/src/main/res/mipmap-xxhdpi/ic_launcher_round.webp b/sample/kinject-sample-android/src/main/res/mipmap-xxhdpi/ic_launcher_round.webp new file mode 100644 index 0000000..9287f50 Binary files /dev/null and b/sample/kinject-sample-android/src/main/res/mipmap-xxhdpi/ic_launcher_round.webp differ diff --git a/sample/kinject-sample-android/src/main/res/mipmap-xxxhdpi/ic_launcher.webp b/sample/kinject-sample-android/src/main/res/mipmap-xxxhdpi/ic_launcher.webp new file mode 100644 index 0000000..aa7d642 Binary files /dev/null and b/sample/kinject-sample-android/src/main/res/mipmap-xxxhdpi/ic_launcher.webp differ diff --git a/sample/kinject-sample-android/src/main/res/mipmap-xxxhdpi/ic_launcher_round.webp b/sample/kinject-sample-android/src/main/res/mipmap-xxxhdpi/ic_launcher_round.webp new file mode 100644 index 0000000..9126ae3 Binary files /dev/null and b/sample/kinject-sample-android/src/main/res/mipmap-xxxhdpi/ic_launcher_round.webp differ diff --git a/sample/kinject-sample-android/src/main/res/values/colors.xml b/sample/kinject-sample-android/src/main/res/values/colors.xml new file mode 100644 index 0000000..f8c6127 --- /dev/null +++ b/sample/kinject-sample-android/src/main/res/values/colors.xml @@ -0,0 +1,10 @@ + + + #FFBB86FC + #FF6200EE + #FF3700B3 + #FF03DAC5 + #FF018786 + #FF000000 + #FFFFFFFF + \ No newline at end of file diff --git a/sample/kinject-sample-android/src/main/res/values/strings.xml b/sample/kinject-sample-android/src/main/res/values/strings.xml new file mode 100644 index 0000000..2dae848 --- /dev/null +++ b/sample/kinject-sample-android/src/main/res/values/strings.xml @@ -0,0 +1,3 @@ + + kinject-sample-android + \ No newline at end of file diff --git a/sample/kinject-sample-android/src/main/res/values/themes.xml b/sample/kinject-sample-android/src/main/res/values/themes.xml new file mode 100644 index 0000000..34ee638 --- /dev/null +++ b/sample/kinject-sample-android/src/main/res/values/themes.xml @@ -0,0 +1,5 @@ + + + +