-
Notifications
You must be signed in to change notification settings - Fork 0
Dagger β Scoping using module
Devrath edited this page Oct 8, 2023
·
11 revisions
Contents |
|---|
| Observations |
| Output |
| Code |
ActivityScope.kt
@Scope
@MustBeDocumented
@Retention(AnnotationRetention.RUNTIME)
annotation class ActivityScopeAnalyticsService.kt
class AnalyticsService {
fun trackEvent(action:String){
PrintUtils.printLog("Analytics service has tracked your action:-> $action")
}
}ImageProcessingService.kt
class ImageProcessingService {
fun initiateImageProcessing(){
PrintUtils.printLog("Image processing is initiated")
}
}AnalyticsServiceModule.kt
@Module
@DisableInstallInCheck
class AnalyticsServiceModule {
@Singleton
@Provides
fun provideAnalyticsService() : AnalyticsService {
return AnalyticsService()
}
}ImageProcessingServiceModule.kt
@Module
@DisableInstallInCheck
class ImageProcessingServiceModule {
@ActivityScope
@Provides
fun provideImageProcessingService() : ImageProcessingService {
return ImageProcessingService();
}
}ApplicationComponent.kt
@Singleton
@Component(modules = [AnalyticsServiceModule::class])
interface ApplicationComponent {
fun provideAnalytics() : AnalyticsService
}ActivityComponent.kt
@ActivityScope
@Component(dependencies = [ApplicationComponent::class], modules = [ImageProcessingServiceModule::class])
interface ActivityComponent {
fun provideAnalytics() : AnalyticsService
fun provideImageProcessingService() : ImageProcessingService
}