mars885 / hilt-binder Goto Github PK
View Code? Open in Web Editor NEWAn annotating processing library that automatically generates Dagger Hilt's `@Binds` methods.
License: Apache License 2.0
An annotating processing library that automatically generates Dagger Hilt's `@Binds` methods.
License: Apache License 2.0
For example:
// A custom component's scope annotation
@Scope
@Retention(value = AnnotationRetention.RUNTIME)
annotation class CustomScope
// Declaration of a custom component itself
@CustomScope
@DefineComponent(parent = SingletonComponent::class)
interface CustomComponent
interface ImageLoader
interface Logger
@CustomScope
@BindType
class PicassoImageLoader @Inject constructor(): ImageLoader
@BindType(
installIn = BindType.Component.CUSTOM,
customComponent = CustomComponent::class
)
class AndroidLogger @Inject constructor(): Logger
If the CustomScope
annotation is declared in different package than the CustomComponent
interface, the following error is thrown when building the project: Collection contains no element matching the predicate.
.
The above bug entails that having only custom scope annotation present on the binding type is not enough, since it's impossible to infer a class of the custom component from the scope annotation itself. This means that the installIn
and the customComponent
parameters should be set regardless if the type is scoped or unscoped. For example:
// A custom component's scope annotation
@Scope
@Retention(value = AnnotationRetention.RUNTIME)
annotation class CustomScope
// Declaration of a custom component itself
@CustomScope
@DefineComponent(parent = SingletonComponent::class)
interface CustomComponent
interface ImageLoader
interface Logger
// Binding unscoped type
@BindType(
installIn = BindType.Component.CUSTOM,
customComponent = CustomComponent::class
)
class PicassoImageLoader @Inject constructor(): ImageLoader
// Binding scoped type
@CustomScope
@BindType(
installIn = BindType.Component.CUSTOM,
customComponent = CustomComponent::class
)
class AndroidLogger @Inject constructor(): Logger
// Won't work, can't infer CustomComponent from CustomScope
// @CustomScope
// @BindType
// class AndroidLogger @Inject constructor(): Logger
Since Bintray is going away, are you planning to migrate to library to Maven Central or JitPack?
Great job btw, it is baffling to me why the Hilt does not have to built in
For now, installIn requires usage of Component
enum. I have to install my class in custom component. Can i do that right now? If not - i think it's crucial to have this. In my case - i'm trying to get rid of boilerplate when using Maverics with Hilt. I have such component:
@MavericksViewModelScoped
@DefineComponent(parent = SingletonComponent::class)
interface MavericksViewModelComponent
The latest version, 1.0.0-alpha02
, does not allow to do the following:
interface ImageLoader
@ActivityScoped
@BindType(installIn = BindType.Component.ACTIVITY)
class PicassoImageLoader @Inject constructor(): ImageLoader
When building the aforementioned snippet, the compiler throws the following error:
error: @BindType-using type specifies the component both by the scope annotation and in the 'installIn' parameter of the annotation.
The aforementioned requirement will be dropped, since there is nothing wrong when specyfing both the scope annotation and explicit predefined component as long as they match (e.g., ActivityScoped
and BindType.Component.ACTIVITY
, ServiceScoped
and BindType.Component.SERVICE
, and so on). In case they don't, an error will be raised by the compiler.
It would be nice to have documentation in the readme about what happens when you bind two types to the same interface without the multibinding annotation (@BindType(contributesTo = BindType.Collection.SET)
) and without qualifiers:
interface ImageLoader
@BindType
class PicassoImageLoader @Inject constructor(): ImageLoader
@BindType
class GlideImageLoader @Inject constructor(): ImageLoader
It's been a while since I've used dagger, but I would expect that not to work since the instance resolution becomes ambiguous.
I want to create multibinding for such collection:
interface HiltMavericksEntryPoint {
val viewModelFactories: Map<Class<out MavericksViewModel<*>>, AssistedViewModelFactory<*, *>>
}
I'm trying to use hilt binder like that:
@BindType(
to = AssistedViewModelFactory::class,
contributesTo = BindType.Collection.MAP
)
@ViewModelKey(DashboardViewModel::class)
@AssistedFactory
interface Factory : AssistedViewModelFactory<DashboardViewModel, DashboardState> {
override fun create(state: DashboardState): DashboardViewModel
}
this generated code like that:
@Module
@InstallIn(SingletonComponent.class)
public interface TestModule {
@Binds
@IntoMap
@ViewModelKey(DashboardViewModel.class)
AssistedViewModelFactory bind(
DashboardViewModel.Factory binding);
}
but dagger won't accept that as a member of Map<Class<out MavericksViewModel<*>>, AssistedViewModelFactory<*, *>>
. I tested very similar module and it's working fine:
@Module
@InstallIn(SingletonComponent.class)
public interface TestModule {
@Binds
@IntoMap
@ViewModelKey(DashboardViewModel.class)
AssistedViewModelFactory<?, ?> bind(
DashboardViewModel.Factory binding);
}
The only thing i added are the wildcards for the generic type, and now it's working great.
I even tried to workaround this by using @JvmSuppressWildcards
, but does not seem to help. Even this weird construct is failing:
@JvmSuppressWildcards
val viewModelFactories: @JvmSuppressWildcards Map<@JvmSuppressWildcards Class<out @JvmSuppressWildcards MavericksViewModel<*>>, @JvmSuppressWildcards AssistedViewModelFactory<*, *>>
Is is possible to use hilt-binder multibinding with generic types?
If i understand correctly - kotlin 1.6.0 is lifting the restriction on the @Repeatable
, so it can be used on any retention. I think multiple binds would be a great addition to this otherwise underestimated library
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.