From ce37903ea018a97c465d3f27713b10cfc9180e8b Mon Sep 17 00:00:00 2001 From: Harsh Shandilya Date: Sun, 13 Nov 2022 13:13:03 +0530 Subject: [PATCH] feat(di): kick off Anvil infra --- core/build.gradle.kts | 22 +++++++++++++++++++ .../claw/core/injection/ContextModule.kt | 20 +++++++++++++++++ di-scopes/build.gradle.kts | 18 +++++++++++++++ .../msfjarvis/claw/injection/Components.kt | 18 +++++++++++++++ .../claw/injection/scopes/AppScope.kt | 9 ++++++++ .../claw/injection/scopes/SingleIn.kt | 12 ++++++++++ 6 files changed, 99 insertions(+) create mode 100644 core/build.gradle.kts create mode 100644 core/src/main/kotlin/dev/msfjarvis/claw/core/injection/ContextModule.kt create mode 100644 di-scopes/build.gradle.kts create mode 100644 di-scopes/src/main/kotlin/dev/msfjarvis/claw/injection/Components.kt create mode 100644 di-scopes/src/main/kotlin/dev/msfjarvis/claw/injection/scopes/AppScope.kt create mode 100644 di-scopes/src/main/kotlin/dev/msfjarvis/claw/injection/scopes/SingleIn.kt diff --git a/core/build.gradle.kts b/core/build.gradle.kts new file mode 100644 index 00000000..55a62388 --- /dev/null +++ b/core/build.gradle.kts @@ -0,0 +1,22 @@ +/* + * Copyright © 2022 Harsh Shandilya. + * Use of this source code is governed by an MIT-style + * license that can be found in the LICENSE file or at + * https://opensource.org/licenses/MIT. + */ +@file:Suppress("DSL_SCOPE_VIOLATION", "UnstableApiUsage") + +plugins { + kotlin("android") + id("dev.msfjarvis.claw.kotlin-common") + id("dev.msfjarvis.claw.android-library") + alias(libs.plugins.anvil) +} + +android { namespace = "dev.msfjarvis.claw.core" } + +dependencies { + implementation(projects.diScopes) + implementation(libs.dagger) + implementation(libs.javax.inject) +} diff --git a/core/src/main/kotlin/dev/msfjarvis/claw/core/injection/ContextModule.kt b/core/src/main/kotlin/dev/msfjarvis/claw/core/injection/ContextModule.kt new file mode 100644 index 00000000..9655d41d --- /dev/null +++ b/core/src/main/kotlin/dev/msfjarvis/claw/core/injection/ContextModule.kt @@ -0,0 +1,20 @@ +/* + * Copyright © 2022 Harsh Shandilya. + * Use of this source code is governed by an MIT-style + * license that can be found in the LICENSE file or at + * https://opensource.org/licenses/MIT. + */ +package dev.msfjarvis.claw.core.injection + +import android.app.Application +import android.content.Context +import com.squareup.anvil.annotations.ContributesTo +import dagger.Binds +import dagger.Module +import dev.msfjarvis.claw.injection.scopes.AppScope + +@Module +@ContributesTo(AppScope::class) +interface ContextModule { + @get:Binds val Application.bindContext: Context +} diff --git a/di-scopes/build.gradle.kts b/di-scopes/build.gradle.kts new file mode 100644 index 00000000..5166eaea --- /dev/null +++ b/di-scopes/build.gradle.kts @@ -0,0 +1,18 @@ +/* + * Copyright © 2022 Harsh Shandilya. + * Use of this source code is governed by an MIT-style + * license that can be found in the LICENSE file or at + * https://opensource.org/licenses/MIT. + */ +@file:Suppress("DSL_SCOPE_VIOLATION", "UnstableApiUsage") + +plugins { + kotlin("jvm") + id("dev.msfjarvis.claw.kotlin-library") + alias(libs.plugins.anvil) +} + +dependencies { + implementation(libs.dagger) + implementation(libs.javax.inject) +} diff --git a/di-scopes/src/main/kotlin/dev/msfjarvis/claw/injection/Components.kt b/di-scopes/src/main/kotlin/dev/msfjarvis/claw/injection/Components.kt new file mode 100644 index 00000000..eebc56a0 --- /dev/null +++ b/di-scopes/src/main/kotlin/dev/msfjarvis/claw/injection/Components.kt @@ -0,0 +1,18 @@ +/* + * Copyright © 2022 Harsh Shandilya. + * Use of this source code is governed by an MIT-style + * license that can be found in the LICENSE file or at + * https://opensource.org/licenses/MIT. + */ +package dev.msfjarvis.claw.injection + +object Components { + + @PublishedApi @Suppress("ObjectPropertyName") internal val _components = mutableSetOf() + + fun add(component: Any) { + _components.add(component) + } + + inline fun get(): T = _components.filterIsInstance().single() +} diff --git a/di-scopes/src/main/kotlin/dev/msfjarvis/claw/injection/scopes/AppScope.kt b/di-scopes/src/main/kotlin/dev/msfjarvis/claw/injection/scopes/AppScope.kt new file mode 100644 index 00000000..0688fbff --- /dev/null +++ b/di-scopes/src/main/kotlin/dev/msfjarvis/claw/injection/scopes/AppScope.kt @@ -0,0 +1,9 @@ +/* + * Copyright © 2022 Harsh Shandilya. + * Use of this source code is governed by an MIT-style + * license that can be found in the LICENSE file or at + * https://opensource.org/licenses/MIT. + */ +package dev.msfjarvis.claw.injection.scopes + +abstract class AppScope private constructor() diff --git a/di-scopes/src/main/kotlin/dev/msfjarvis/claw/injection/scopes/SingleIn.kt b/di-scopes/src/main/kotlin/dev/msfjarvis/claw/injection/scopes/SingleIn.kt new file mode 100644 index 00000000..4bbe79be --- /dev/null +++ b/di-scopes/src/main/kotlin/dev/msfjarvis/claw/injection/scopes/SingleIn.kt @@ -0,0 +1,12 @@ +/* + * Copyright © 2022 Harsh Shandilya. + * Use of this source code is governed by an MIT-style + * license that can be found in the LICENSE file or at + * https://opensource.org/licenses/MIT. + */ +package dev.msfjarvis.claw.injection.scopes + +import javax.inject.Scope +import kotlin.reflect.KClass + +@Scope @Retention(AnnotationRetention.RUNTIME) annotation class SingleIn(val clazz: KClass<*>)