-
Notifications
You must be signed in to change notification settings - Fork 0
Dagger β multi binding IntoSet
Devrath edited this page Oct 8, 2023
·
7 revisions
Contents |
---|
Observations |
Output |
Code |
- There are 2 implementations of
television
interface namelySamsung
andLg
. - Both have
@Inject
annotation constructor annotated so we can use@Binds
instead of@Provides
annotation andInterface
instead ofnormal class
as a module. - Along with
@Binds
in the module, We need to add@IntoSet
indicating the dagger that this will be wrapped as a instance in set for the dagger to use it. - We use
fun provideTelivision() : Set<@JvmSuppressWildcards Telivision>
in yourComponent
that you use to inject the dependency. This is because the set returnsTelivision
and we need to add@JvmSuppressWildcards
in kotlin else it would give a error. - So in
component
we will haveinject
andprovides
functions. - Then with the instance in the activity which is of
Set
, We can loop it and compare with a each instance of a class and perform some funcitonality.
[com.istudio.di.modules.dagger.demos.multibindings.intoset.implementation.Samsung@9ce6cb2, com.istudio.di.modules.dagger.demos.multibindings.intoset.implementation.Lg@5aeb403]
Samsung ^-^ Output
Lg ^-^ Output
Contents |
---|
Implementations |
Modules |
Components |
Activity |
Telivision.kt
interface Telivision {
fun name() : String
}
Samsung.kt
class Samsung @Inject constructor() : Telivision {
override fun name(): String {
return "Samsung ^-^ Output"
}
}
Lg.kt
class Lg @Inject constructor() : Telivision {
override fun name(): String {
return "Lg ^-^ Output"
}
}
TelivisionModule.kt
@Module
@DisableInstallInCheck
interface TelivisionModule {
@Binds
@IntoSet
fun providesSamsungTv(implementation : Samsung) : Telivision
@Binds
@IntoSet
fun providesLgTv(implementation : Lg) : Telivision
}
TelivisionComponent.kt
@Component(modules = [TelivisionModule::class])
interface TelivisionComponent {
fun inject(activity : MyActivity)
fun provideTelivision() : Set<@JvmSuppressWildcards Telivision>
}
MyActivity.kt
class DaggerMultiBindingIntoSetActivity : AppCompatActivity() {
private lateinit var binding: ActivityDaggerMultiBindingIntoSetBinding
@Inject lateinit var telivision: Set<@JvmSuppressWildcards Telivision>
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityDaggerMultiBindingIntoSetBinding.inflate(layoutInflater)
setContentView(binding.root)
setOnClickListeners();
}
private fun setOnClickListeners() {
binding.apply {
initiateId.setOnClickListener {
// Inject the component into the activity
val comp = DaggerTelivisionComponent.builder().build()
comp.inject(this@DaggerMultiBindingIntoSetActivity)
// Print the entire set
PrintUtils.printLog(telivision.toString())
// Iterate the set of objects
telivision.forEach { tv ->
if(tv is Samsung){
// Access Samsung specific functionalities
PrintUtils.printLog(tv.name())
}else if(tv is Lg){
// Access Lg specific functionalities
PrintUtils.printLog(tv.name())
}
}
}
}
}
}