Last active
February 26, 2024 16:33
-
-
Save nuhkoca/1bf28190dc71b00a2f32ce425f99924d to your computer and use it in GitHub Desktop.
A demonstration about how to inject BindingAdapters with Dagger Hilt.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
BindingScoped.kt | |
@Scope | |
@Retention(AnnotationRetention.BINARY) | |
annotation class BindingScoped | |
------------------------------------------------------- | |
CustomBindingComponent.kt | |
@BindingScoped | |
@DefineComponent(parent = SingletonComponent::class) | |
interface CustomBindingComponent | |
------------------------------------------------------- | |
CustomBindingComponentBuilder.kt | |
@DefineComponent.Builder | |
interface CustomBindingComponentBuilder { | |
fun build(): CustomBindingComponent | |
} | |
------------------------------------------------------- | |
CustomBindingEntryPoint.kt | |
@EntryPoint | |
@BindingScoped | |
@InstallIn(CustomBindingComponent::class) | |
interface CustomBindingEntryPoint: DataBindingComponent { | |
@BindingScoped | |
override fun getImageBindingAdapter(): ImageBindingAdapter | |
} | |
------------------------------------------------------- | |
MyApp.kt | |
@HiltAndroidApp | |
class MyApp : Application() { | |
@Inject | |
lateinit var bindingComponentProvider: Provider<CustomBindingComponentBuilder> | |
override fun onCreate() { | |
super.onCreate() | |
val dataBindingComponent = bindingComponentProvider.get().build() | |
val dataBindingEntryPoint = EntryPoints.get( | |
dataBindingComponent, CustomBindingEntryPoint::class.java | |
) | |
DataBindingUtil.setDefaultComponent(dataBindingEntryPoint) | |
} | |
} |
For those who are being stuck on this gist to fix issues running around Hilt and DataBinding implementation, I created a working sample repository for you! Please take a look at it if you want to see how it is working.
Repository: https://github.com/nuhkoca/HiltDataBindingSample
Thanks alot. its working now @nuhkoca
That's great to hear! @AhmMhd
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@ModerPage it is working well for me. The only difference is that
ApplicationComponent
was replaced withSingletonComponent
. Otherwise rest of the code remains the same. Please check and let me know if it works for you, too!