Skip to content

Dagger ‐ multi binding IntoSet

Devrath edited this page Oct 8, 2023 · 7 revisions

Observations

  • There are 2 implementations of television interface namely Samsung and Lg.
  • Both have @Inject annotation constructor annotated so we can use @Binds instead of @Provides annotation and Interface instead of normal 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 your Component that you use to inject the dependency. This is because the set returns Telivision and we need to add @JvmSuppressWildcards in kotlin else it would give a error.
  • So in component we will have inject and provides 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.

Output

[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

Code

Implementations

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

Modules

TelivisionModule.kt

@Module
@DisableInstallInCheck
interface TelivisionModule {

    @Binds
    @IntoSet
    fun providesSamsungTv(implementation : Samsung) : Telivision

    @Binds
    @IntoSet
    fun providesLgTv(implementation : Lg) : Telivision

}

Components

TelivisionComponent.kt

@Component(modules = [TelivisionModule::class])
interface TelivisionComponent {
    fun inject(activity : MyActivity)
    fun provideTelivision() : Set<@JvmSuppressWildcards Telivision>
}

Activity

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

}
Clone this wiki locally