Skip to content

Dagger ‐ Scoping using module

Devrath edited this page Oct 8, 2023 · 11 revisions

Observations

Output

Code

Scopes

ActivityScope.kt

@Scope
@MustBeDocumented
@Retention(AnnotationRetention.RUNTIME)
annotation class ActivityScope

Implementations

AnalyticsService.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")
    }
}

Modules

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();
    }
}

Components

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

}

Application

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 }
}

Activity

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())

            }
        }
    }
}
Clone this wiki locally