Skip to content

Instantly share code, notes, and snippets.

@pkliang
Forked from nuhkoca/HiltDataBinding.kt
Created September 17, 2020 12:34
Show Gist options
  • Save pkliang/a68dc2006a1e5cda87660f0d183bb8cc to your computer and use it in GitHub Desktop.
Save pkliang/a68dc2006a1e5cda87660f0d183bb8cc to your computer and use it in GitHub Desktop.
A demonstration about how to inject BindingAdapters with Dagger Hilt.
BindingScoped.kt
@Scope
@Retention(AnnotationRetention.BINARY)
annotation class BindingScoped
-------------------------------------------------------
CustomBindingComponent.kt
@BindingScoped
@DefineComponent(parent = ApplicationComponent::class)
interface CustomBindingComponent : DataBindingComponent
-------------------------------------------------------
CustomBindingComponentBuilder.kt
@DefineComponent.Builder
interface CustomBindingComponentBuilder {
fun build(): CustomBindingComponent
}
-------------------------------------------------------
CustomBindingEntryPoint.kt
@EntryPoint
@BindingScoped
@InstallIn(CustomBindingComponent::class)
interface CustomBindingEntryPoint {
@BindingScoped
fun getImageBindingAdapter(): ImageBindingAdapter
}
-------------------------------------------------------
MyApp.kt
@HiltAndroidApp
class MyApp : Application() {
@Inject
lateinit var bindingComponentProvider: Provider<CustomBindingComponentBuilder>
override fun onCreate() {
super.onCreate()
DataBindingUtil.setDefaultComponent(bindingComponentProvider.get().build())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment