Skip to content

Commit

Permalink
move to multiplatform viewmodel
Browse files Browse the repository at this point in the history
  • Loading branch information
ScottPierce committed Jul 6, 2024
1 parent fcd205e commit 4f68187
Show file tree
Hide file tree
Showing 11 changed files with 40 additions and 34 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ A multiplatform Dependency Injection library.

| Artifact | Description |
|---------------------------|------------------------------------------------------------------------------------------------------------------------------------------------|
| kinject | Provides core DI functionality. |
| 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`. |
Expand Down
4 changes: 2 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
alias(libs.plugins.kotlin.multiplatform) 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.kotlin.compose) apply false
alias(libs.plugins.gradleMavenPublish) apply false
alias(libs.plugins.android.application) apply false
alias(libs.plugins.android.library) apply false
alias(libs.plugins.androidx.benchmark) apply false
}
Expand Down
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
org.gradle.jvmargs=-Xmx3g -Dfile.encoding=UTF-8 -Dkotlin.daemon.jvm.options\="-Xmx2g"

kotlin.code.style=official
kotlin.mpp.androidGradlePluginCompatibility.nowarn=true

android.useAndroidX=true
12 changes: 8 additions & 4 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ kotlinxBenchmark = "0.4.10"
androidx-benchmark = "1.2.4"
runner = "1.6.1"
benchmarkJunit4 = "1.2.4"
lifecycleViewmodel = "2.8.2"
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.2"
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" }
Expand All @@ -26,8 +27,11 @@ compose-runtime = { group = "androidx.compose.runtime", name = "runtime", versio
junit = { group = "junit", name = "junit", version.ref = "junit" }
koin-core = { module = "io.insert-koin:koin-core", version = "3.5.6" }
kotlinx-benchmark = { module = "org.jetbrains.kotlinx:kotlinx-benchmark-runtime", version.ref = "kotlinxBenchmark" }
androidx-lifecycle-viewmodel-android = { group = "androidx.lifecycle", name = "lifecycle-viewmodel-android", version.ref = "lifecycleViewmodel" }
androidx-lifecycle-viewmodel-compose = { group = "androidx.lifecycle", name = "lifecycle-viewmodel-compose-android", version.ref = "lifecycleViewmodel" }
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" }
Expand Down
2 changes: 2 additions & 0 deletions kinject-android/src/main/kotlin/kinject/android/ContextExt.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
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?
}
Expand Down
9 changes: 1 addition & 8 deletions kinject-compose-viewmodel/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,10 @@ plugins {

android {
namespace = "kinject.compose.viewmodel"

buildFeatures {
compose = true
}
composeOptions {
kotlinCompilerExtensionVersion = libs.versions.compose.compiler.get()
}
}

dependencies {
api(project(":kinject-compose"))
api(project(":kinject-viewmodel"))
implementation(libs.androidx.lifecycle.viewmodel.compose)
api(libs.androidx.lifecycle.viewmodel.compose)
}
7 changes: 0 additions & 7 deletions kinject-compose/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,6 @@ plugins {

android {
namespace = "kinject.compose"

buildFeatures {
compose = true
}
composeOptions {
kotlinCompilerExtensionVersion = libs.versions.compose.compiler.get()
}
}

composeCompiler {
Expand Down
4 changes: 0 additions & 4 deletions kinject-core/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
@file:OptIn(ExperimentalWasmDsl::class)

import org.jetbrains.kotlin.gradle.targets.js.dsl.ExperimentalWasmDsl

plugins {
Expand All @@ -19,8 +17,6 @@ kotlin {
}
}
}
wasmJs()
wasmWasi()
macosArm64()
macosX64()
iosX64()
Expand Down
26 changes: 20 additions & 6 deletions kinject-viewmodel/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@
plugins {
alias(libs.plugins.kotlin.multiplatform)
alias(libs.plugins.android.library)
alias(libs.plugins.kotlin.android)
alias(libs.plugins.gradleMavenPublish)
}

android {
namespace = "kinject.viewmodel"
kotlin {
jvm()
androidTarget()
macosArm64()
macosX64()
iosX64()
iosArm64()
iosSimulatorArm64()

applyDefaultHierarchyTemplate()

sourceSets {
commonMain.dependencies {
api(project(":kinject-core"))
implementation(libs.androidx.lifecycle.viewmodel)
}
}
}

dependencies {
api(project(":kinject-core"))
implementation(libs.androidx.lifecycle.viewmodel.android)
android {
namespace = "kinject.viewmodel"
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ package kinject.viewmodel

import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.viewmodel.CreationExtras
import kinject.ObjectGraph
import kinject.get
import kotlin.reflect.KClass

class KinjectViewModelFactory(
private val objectGraph: ObjectGraph,
) : ViewModelProvider.Factory {
override fun <T : ViewModel> create(modelClass: Class<T>): T = objectGraph.get(modelClass)
override fun <T : ViewModel> create(modelClass: KClass<T>, extras: CreationExtras): T {
return objectGraph.get(modelClass)
}
}

0 comments on commit 4f68187

Please sign in to comment.