-
Notifications
You must be signed in to change notification settings - Fork 0
Hilt β multi binding ElementsIntoSet
Devrath edited this page Oct 9, 2023
·
3 revisions
Contents |
---|
Observations |
Output |
Code |
<------------------------------------>
PLANET INSTANCE:->125173523
PLANET:-> com.istudio.di.modules.hilt.demos.multibinding_demo.elements_into_set_demo.implementations.Earth@775ff13
<------------------------------------>
<------------------------------------>
PLANET INSTANCE:->212309328
PLANET:-> com.istudio.di.modules.hilt.demos.multibinding_demo.elements_into_set_demo.implementations.Mars@ca79550
<------------------------------------>
Contents |
---|
Implementation |
Modules |
Activity |
Planet.kt
interface Planet {
fun printInstance(name:String)
}
Earth.kt
class Earth @Inject constructor() : Planet {
override fun printInstance(name: String) {
}
}
Mars.kt
class Mars @Inject constructor() : Planet {
override fun printInstance(name: String) {
}
}
HiltPlanetModule.kt
@InstallIn(SingletonComponent::class)
@Module
object HiltPlanetModule {
@Provides
@ElementsIntoSet
fun providePlanets(): Set<@JvmSuppressWildcards Planet> {
return setOf(
Earth(),
Mars()
)
}
}
MyActivity.kt
@AndroidEntryPoint
class MyActivity : AppCompatActivity() {
private lateinit var binding: ActivityHiltMultiBindingsSelectionBinding
// IntoSet
@Inject lateinit var planets : Set<@JvmSuppressWildcards Planet>
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityHiltMultiBindingsSelectionBinding.inflate(layoutInflater)
setContentView(binding.root)
setOnClickListeners();
}
private fun setOnClickListeners() {
binding.apply {
usingIntoSetId.setOnClickListener {
// IntoSet
planets.forEach { planet ->
PrintUtils.printLog("<------------------------------------>")
PrintUtils.printLog("PLANET INSTANCE:->${planet.hashCode()}")
PrintUtils.printLog("PLANET:-> $planet")
PrintUtils.printLog("<------------------------------------>")
}
}
}
}
}