-
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 |
// -------- Application loaded for the first time and we initiated component building
Injected
analyticsService reference ->223348912
analyticsService1 reference ->223348912
imageService reference ->98140969
imageService1 reference ->98140969
// -------- Application is already loaded and we initiate component building a second time
Injected
analyticsService reference ->223348912
analyticsService1 reference ->223348912
imageService reference ->19202012
imageService1 reference ->19202012
// -------- Application is already loaded but we now rotate the screen and initiate component building a third time
Injected
analyticsService reference ->223348912
analyticsService1 reference ->223348912
imageService reference ->127919356
imageService1 reference ->127919356
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
}DiApplication.kt
@HiltAndroidApp
class DiApplication : Application() {
private lateinit var connComp : ConnectionComponent
override fun onCreate() {
super.onCreate()
connComp = DaggerConnectionComponent.builder().build()
}
fun provideApplicationComponent(): ApplicationComponent { return appComponent }
}class MyActivity : AppCompatActivity() {
private lateinit var binding: ActivityCustomScopeBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityCustomScopeBinding.inflate(layoutInflater)
setContentView(binding.root)
setOnClickListeners();
}
private fun setOnClickListeners() {
binding.apply {
initiateUsingModuleId.setOnClickListener {
val component = DaggerActivityComponent.builder()
.applicationComponent((application as DiApplication).provideApplicationComponent())
.imageProcessingServiceModule(ImageProcessingServiceModule())
.build()
val analyticsService = component.provideAnalytics()
val analyticsService1 = component.provideAnalytics()
val imageService = component.provideImageProcessingService()
val imageService1 = component.provideImageProcessingService()
PrintUtils.printLog("Injected")
PrintUtils.printLog("analyticsService reference ->" + analyticsService.hashCode().toString())
PrintUtils.printLog("analyticsService1 reference ->" + analyticsService1.hashCode().toString())
PrintUtils.printLog("imageService reference ->" + imageService.hashCode().toString())
PrintUtils.printLog("imageService1 reference ->" + imageService1.hashCode().toString())
}
}
}
}