From b02f35a8b44819e7c40d275a528b720b8fb4f914 Mon Sep 17 00:00:00 2001 From: Ralf Wondratschek Date: Thu, 12 Sep 2024 14:41:58 -0700 Subject: [PATCH] Adopt pre-defined scopes in sample app Adopt the best practices of `@SingleIn` in the sample application. --- sample/app/build.gradle | 1 + .../inject/anvil/sample/AndroidAppComponent.kt | 8 +++++--- .../sample/ApplicationIdProviderComponent.kt | 6 +++--- .../inject/anvil/sample/IosAppComponent.kt | 8 +++++--- sample/lib/build.gradle | 1 + .../sample/AndroidApplicationIdProvider.kt | 6 ++++-- .../inject/anvil/sample/SingleInAppScope.kt | 17 ----------------- .../anvil/sample/IosApplicationIdProvider.kt | 6 ++++-- 8 files changed, 23 insertions(+), 30 deletions(-) delete mode 100644 sample/lib/src/commonMain/kotlin/software/amazon/lastmile/kotlin/inject/anvil/sample/SingleInAppScope.kt diff --git a/sample/app/build.gradle b/sample/app/build.gradle index 9a9a0fc..d4751ed 100644 --- a/sample/app/build.gradle +++ b/sample/app/build.gradle @@ -20,6 +20,7 @@ dependencies { kspIosSimulatorArm64 project(':compiler') commonMainImplementation project(':runtime') + commonMainImplementation project(':runtime-optional') commonMainImplementation libs.kotlin.inject.runtime commonMainImplementation project(':sample:lib') diff --git a/sample/app/src/androidMain/kotlin/software/amazon/lastmile/kotlin/inject/anvil/sample/AndroidAppComponent.kt b/sample/app/src/androidMain/kotlin/software/amazon/lastmile/kotlin/inject/anvil/sample/AndroidAppComponent.kt index e446fe6..e588232 100644 --- a/sample/app/src/androidMain/kotlin/software/amazon/lastmile/kotlin/inject/anvil/sample/AndroidAppComponent.kt +++ b/sample/app/src/androidMain/kotlin/software/amazon/lastmile/kotlin/inject/anvil/sample/AndroidAppComponent.kt @@ -3,10 +3,12 @@ package software.amazon.lastmile.kotlin.inject.anvil.sample import android.app.Application import me.tatarka.inject.annotations.Component import me.tatarka.inject.annotations.Provides +import software.amazon.lastmile.kotlin.inject.anvil.AppScope import software.amazon.lastmile.kotlin.inject.anvil.MergeComponent +import software.amazon.lastmile.kotlin.inject.anvil.SingleIn /** - * Concrete application component for Android using the scope [SingleInAppScope]. + * Concrete application component for Android using the scope [SingleIn] [AppScope]. * [AndroidAppComponentMerged] is a generated interface. Through this merged interface * [ApplicationIdProviderComponent], other contributed component interfaces and contributed * bindings such as from [AndroidApplicationIdProvider] are implemented. @@ -15,8 +17,8 @@ import software.amazon.lastmile.kotlin.inject.anvil.MergeComponent * [Application] can be provided in the object graph. */ @Component -@MergeComponent -@SingleInAppScope +@MergeComponent(AppScope::class) +@SingleIn(AppScope::class) abstract class AndroidAppComponent( /** * The Android application that is provided to this object graph. diff --git a/sample/app/src/commonMain/kotlin/software/amazon/lastmile/kotlin/inject/anvil/sample/ApplicationIdProviderComponent.kt b/sample/app/src/commonMain/kotlin/software/amazon/lastmile/kotlin/inject/anvil/sample/ApplicationIdProviderComponent.kt index f7ed1ac..22f1b54 100644 --- a/sample/app/src/commonMain/kotlin/software/amazon/lastmile/kotlin/inject/anvil/sample/ApplicationIdProviderComponent.kt +++ b/sample/app/src/commonMain/kotlin/software/amazon/lastmile/kotlin/inject/anvil/sample/ApplicationIdProviderComponent.kt @@ -1,13 +1,13 @@ package software.amazon.lastmile.kotlin.inject.anvil.sample +import software.amazon.lastmile.kotlin.inject.anvil.AppScope import software.amazon.lastmile.kotlin.inject.anvil.ContributesTo /** * This component interface is added to the kotlin-inject component using the scope - * [SingleInAppScope] automatically. + * [AppScope] automatically. */ -@ContributesTo -@SingleInAppScope +@ContributesTo(AppScope::class) interface ApplicationIdProviderComponent { /** * Provides the [ApplicationIdProvider] from the kotlin-inject object graph. diff --git a/sample/app/src/iosSimulatorArm64Main/kotlin/software/amazon/lastmile/kotlin/inject/anvil/sample/IosAppComponent.kt b/sample/app/src/iosSimulatorArm64Main/kotlin/software/amazon/lastmile/kotlin/inject/anvil/sample/IosAppComponent.kt index 41ba335..b32ab28 100644 --- a/sample/app/src/iosSimulatorArm64Main/kotlin/software/amazon/lastmile/kotlin/inject/anvil/sample/IosAppComponent.kt +++ b/sample/app/src/iosSimulatorArm64Main/kotlin/software/amazon/lastmile/kotlin/inject/anvil/sample/IosAppComponent.kt @@ -3,10 +3,12 @@ package software.amazon.lastmile.kotlin.inject.anvil.sample import me.tatarka.inject.annotations.Component import me.tatarka.inject.annotations.Provides import platform.UIKit.UIApplication +import software.amazon.lastmile.kotlin.inject.anvil.AppScope import software.amazon.lastmile.kotlin.inject.anvil.MergeComponent +import software.amazon.lastmile.kotlin.inject.anvil.SingleIn /** - * Concrete application component for iOS using the scope [SingleInAppScope]. + * Concrete application component for iOS using the scope [SingleIn] [AppScope]. * [IosAppComponentMerged] is a generated interface. Through this merged interface * [ApplicationIdProviderComponent], other contributed component interfaces and contributed * bindings such as from [IosApplicationIdProvider] are implemented. @@ -15,8 +17,8 @@ import software.amazon.lastmile.kotlin.inject.anvil.MergeComponent * [UIApplication] can be provided in the object graph. */ @Component -@MergeComponent -@SingleInAppScope +@MergeComponent(AppScope::class) +@SingleIn(AppScope::class) abstract class IosAppComponent( /** * The iOS application that is provided to this object graph. diff --git a/sample/lib/build.gradle b/sample/lib/build.gradle index 4699cc4..872de75 100644 --- a/sample/lib/build.gradle +++ b/sample/lib/build.gradle @@ -20,5 +20,6 @@ dependencies { kspIosSimulatorArm64 project(':compiler') commonMainImplementation project(':runtime') + commonMainImplementation project(':runtime-optional') commonMainImplementation libs.kotlin.inject.runtime } diff --git a/sample/lib/src/androidMain/kotlin/software/amazon/lastmile/kotlin/inject/anvil/sample/AndroidApplicationIdProvider.kt b/sample/lib/src/androidMain/kotlin/software/amazon/lastmile/kotlin/inject/anvil/sample/AndroidApplicationIdProvider.kt index 8e34812..49e2a03 100644 --- a/sample/lib/src/androidMain/kotlin/software/amazon/lastmile/kotlin/inject/anvil/sample/AndroidApplicationIdProvider.kt +++ b/sample/lib/src/androidMain/kotlin/software/amazon/lastmile/kotlin/inject/anvil/sample/AndroidApplicationIdProvider.kt @@ -2,7 +2,9 @@ package software.amazon.lastmile.kotlin.inject.anvil.sample import android.app.Application import me.tatarka.inject.annotations.Inject +import software.amazon.lastmile.kotlin.inject.anvil.AppScope import software.amazon.lastmile.kotlin.inject.anvil.ContributesBinding +import software.amazon.lastmile.kotlin.inject.anvil.SingleIn /** * Android implementation for [ApplicationIdProvider] and provides the package name as application @@ -12,8 +14,8 @@ import software.amazon.lastmile.kotlin.inject.anvil.ContributesBinding * inject [ApplicationIdProvider] through the [ContributesBinding] annotation. */ @Inject -@SingleInAppScope -@ContributesBinding +@SingleIn(AppScope::class) +@ContributesBinding(AppScope::class) class AndroidApplicationIdProvider( application: Application, ) : ApplicationIdProvider { diff --git a/sample/lib/src/commonMain/kotlin/software/amazon/lastmile/kotlin/inject/anvil/sample/SingleInAppScope.kt b/sample/lib/src/commonMain/kotlin/software/amazon/lastmile/kotlin/inject/anvil/sample/SingleInAppScope.kt deleted file mode 100644 index b6c506f..0000000 --- a/sample/lib/src/commonMain/kotlin/software/amazon/lastmile/kotlin/inject/anvil/sample/SingleInAppScope.kt +++ /dev/null @@ -1,17 +0,0 @@ -package software.amazon.lastmile.kotlin.inject.anvil.sample - -import me.tatarka.inject.annotations.Scope - -/** - * A scope annotation for DI to make classes singletons in the app scope: - * ``` - * @Inject - * @SingleInAppScope - * class MyClass(..) : SuperType { - * ... - * } - * ``` - */ -@Scope -@Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY_GETTER) -annotation class SingleInAppScope diff --git a/sample/lib/src/iosMain/kotlin/software/amazon/lastmile/kotlin/inject/anvil/sample/IosApplicationIdProvider.kt b/sample/lib/src/iosMain/kotlin/software/amazon/lastmile/kotlin/inject/anvil/sample/IosApplicationIdProvider.kt index 7f8fab6..c1c3df6 100644 --- a/sample/lib/src/iosMain/kotlin/software/amazon/lastmile/kotlin/inject/anvil/sample/IosApplicationIdProvider.kt +++ b/sample/lib/src/iosMain/kotlin/software/amazon/lastmile/kotlin/inject/anvil/sample/IosApplicationIdProvider.kt @@ -2,7 +2,9 @@ package software.amazon.lastmile.kotlin.inject.anvil.sample import me.tatarka.inject.annotations.Inject import platform.UIKit.UIApplication +import software.amazon.lastmile.kotlin.inject.anvil.AppScope import software.amazon.lastmile.kotlin.inject.anvil.ContributesBinding +import software.amazon.lastmile.kotlin.inject.anvil.SingleIn /** * iOS implementation for [ApplicationIdProvider] and provides a fake string as application @@ -12,8 +14,8 @@ import software.amazon.lastmile.kotlin.inject.anvil.ContributesBinding * inject [ApplicationIdProvider] through the [ContributesBinding] annotation. */ @Inject -@SingleInAppScope -@ContributesBinding +@SingleIn(AppScope::class) +@ContributesBinding(AppScope::class) class IosApplicationIdProvider( application: UIApplication, ) : ApplicationIdProvider {