https://kotlinlang.org logo
#dagger
Title
# dagger
m

miqbaldc

07/14/2021, 7:27 AM
https://github.com/google/dagger/issues/2752 anyone knows a workaround to provide a
FragmentActivity
for a qualifier of
@ActivityContext
using
@Binds
or it’s only possible to use the
@Provides
?
t

trevjones

07/14/2021, 6:51 PM
i’d expect a provides hiding the hard cast is the only way since it is a cast up and can’t be verified simply. though i’d guess you could validate it at compile time it would be expensive.
🙏 1
m

miqbaldc

07/15/2021, 2:34 AM
Ah I see, doing the hard cast was our current implementation 😞 something like this:
Copy code
// TheModule.kt
// `as?` or using the pre-conditions by calling: Preconditions.checkNotNull(...)
@InstallIn(ActivityComponent::class)
@Module
object TheModule {

    @Provides fun fragmentActivity(@ActivityContext context: Context) = context as? FragmentActivity

}

// the injected class: MyClass.kt
class MyClass @Inject constructor(
    val fragmentActivity: FragmentActivity?
)
nullable, in case it failed with
ClassCastException