-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add AndroidInjection.inject(View) #720
Comments
There is both a philosophical point and logistical/implementation point to be made here. First, it's not fully clear to us that injecting views is the right thing to do. From the implementation perspective, there is also a problem in that there isn't a canonical way to retrieve the |
it might be possible to use https://github.com/InflationX/ViewPump in a way that allows you to use normal constructor injection for custom views as well, thus making special constructs for injecting a view unneeded at least in the context of dagger-android. |
while I would like to agree, fragments have their own issues, which has let to the rise of using custom views |
Custom views are not a replacement for fragments. You still need a coordination layer which is responsible for inflating, transitioning, and managing the stack. It should be that component's responsibility for injecting any views (or at the very least, providing the injector via the context hack). |
agreed, I was just pointing out the reason for the use case |
I currently inject Square's Coordinators via constructor and and I bind the coordinators to the custom views from inside the Activity. So the Activity becomes just a container for custom views and a coordination layer. Any opinions - is that correct? |
Would anyone elaborate the context hack approach? |
Not directly related, but discussed this a bit with @digitalbuddha here (and here's the gist) |
I'm open to being told this is bad design, but here's my situation and why I'd like the ability to easily inject My app's monetization strategy is based around subscriptions, and we have three tiers of membership -- call them Gold, Bronze, Silver. I have a custom view,
I can set the tier on a In my custom view, I take a peak at the In the current prod version of my app, I have a static reference to the relevant I am in the fortunate (?) position of rewriting my app from scratch, and I've adopted
Please note, the existing prod code was written in Java, works well, and I'm basically just trying to port it over as-is to the new app (and in Kotlin, but that's an aside). What is the recommended approach for getting an injectable object into my custom view? |
Time for us to just wait for Jake Wharton to solve this problem for us in https://github.com/square/AssistedInject 's |
Any news on this? |
You can inject anything you want with Dagger-Android as long as you create the dagger injection module just like AndroidInjectionModule.class for your target iirc |
I am a bit new with dagger, I searched a lot for an example of a custom view with injection but no luck. It would be great if you could refer me to an example or article for injection in a custom view. |
@ghahramani if you check the source code, @Module
public abstract class AndroidInjectionModule {
@Multibinds
abstract Map<Class<?>, AndroidInjector.Factory<?>> classKeyedInjectorFactories();
@Multibinds
abstract Map<String, AndroidInjector.Factory<?>> stringKeyedInjectorFactories();
private AndroidInjectionModule() {}
} So what you need to do is get a It might even work just by providing If not, this factory looks like this: @Module(
subcomponents =
MySubcomponent.class
)
public abstract class MyFragmentModule {
private MyFragmentModule() {}
@Binds
@IntoMap
@ClassKey(MyFragment.class)
abstract AndroidInjector.Factory<?> bindAndroidInjectorFactory(
MyFragmentSubcomponent.Factory builder);
@Subcomponent
@PerScreen
public interface MyFragmentSubcomponent
extends AndroidInjector<MyFragment> {
@Subcomponent.Factory
interface Factory extends AndroidInjector.Factory<MyFragment> {}
}
} So if You can check https://stackoverflow.com/questions/53889327/dagger-android-for-custom-class-possible which worked with Conductor Controllers, the idea should be the same. I hope you won't need to worry about |
Wow, |
That's pretty cool, I guess the 2.20 update made the library much more powerful. Mortar works independently of Dagger2, its development has been dead for 3 years, and I was happy to rip the Mortar itself is not based on Dagger2 at all btw, it holds |
Great. Thank you for the explanation. In that case I won't go with it as you mentioned it is a dead project (3 years no development 👎 ) |
@ghahramani , @Zhuinden , sorry but I am not quite following how you got the @ContributesAndroidInjector to work with a custom view, is there a code example you can point me at please? |
@CamiloVega I'll post the example tomorrow, Also I'm almost finishing a medium related to that. |
@CamiloVega The Module: @Module
internal abstract class CustomViewDiModule {
@ContributesAndroidInjector
abstract fun bindDictionaryTextView(): DictionaryTextView
} And here is the class DictionaryTextView : XXXText {
@Inject
protected lateinit var viewModel: XXXViewModel
init {
application.androidInjector().inject(this)
}
@Suppress("USELESS_CAST")
override val application: DaggerApplication by lazy {
val ctx = context.applicationContext
if (ctx is DaggerApplication) {
return@lazy ctx as DaggerApplication
}
throw IllegalStateException("Application context does not extend DaggerApplication: $context")
}
} Now Hope it helps, let me know if you need any help |
@ghahramani I have no way to thank you, this has saved me so much work and time, and now my code definitely looks cleaner. Thank you! |
@CamiloVega Glad that could help, I have finished the medium post, please check it for further information |
We have inject for Activity, Fragment, Receivers, etc., but one is required for custom views too.
The text was updated successfully, but these errors were encountered: