Search code examples
androiddependency-injectiondagger-2

How can I provide and Inject a DividerItemDecoration in Dependency Injection and should I inject everything?


I believe that I should inject everything. (So we can discuss it also) I used a recycleview and added a dividerItemDecoration or LinearLayoutManager

so in my ActivityModule.java

public class ActivityModule {

    private AppCompatActivity mActivity;

    public ActivityModule(AppCompatActivity activity) {
        this.mActivity = activity;
    }

 @Provides
    DividerItemDecoration provideDividerItemDecoration() {
        return new DividerItemDecoration(mActivity, DividerItemDecoration.VERTICAL);
    }}

I provide my DividerItemDecoration like that.

and on my fragment I just inject

  @Inject
    DividerItemDecoration itemDecorator;

and use it like :

itemDecorator.setDrawable(Objects.requireNonNull(getDrawable(getBaseActivity(), R.drawable.language_separator)));

It is okay for a fragment.

PROBLEM :

I can use DividerItemDecoration next time in another fragment as Horizontal.. and I can not provide DividerItemDecoration again again.

I want to create general method or something . I will inject to my fragment for example

@Inject 
DividerItemDecoration itemDecorator(horizontal???).. ? 

I do not know how its syntax and logic.

How to provide and inject that kind of codes?

Waiting your supports.

Also it is a native view stuff, Maybe It can not need to inject.. Please tell your opinions.. I remind you that still I need an example and to learn for that kind of methods injection and providing..

Thanks a lot


Solution

  • I want to share a text about Dependency Injection, It makes us to change our mind using dependency injection :

    Do not (always) use DI: Injectables versus newables

    Something that was immensely useful to me when learning about DI frameworks was the realisation that using a DI framework does not mean that you have to DI to initialise all of your objects. As a rule of thumb: inject objects that you know of at compile time and that have static relations to other objects; do not inject runtime information.

    I think this is a good post on the subject. It introduces the concept of 'newables' and 'injectables'.

    Injectables are the classes near the root of your DI graph. Instances of these classes are the kind of objects that you expect your DI framework to provide and inject. Manager- or service-type objects are typical examples of injectables. Newables are objects at the fringes of your DI graph, or that are not even really part of your DI graph at all. Integer, Address etc. are examples of newables. Broadly speaking, newables are passive objects, and there is no point in injecting or mocking them. They typically contain the "data" that is in your application and that is only available at runtime (e.g. your address). Newables should not keep references to injectables or vice versa (something the author of the post refers to as "injectable/newable-separation").

    In reality, I have found that it is not always easy or possible to make a clear distinction between injectables and newables. Still, I think that they are nice concepts to use as part of your thinking process. Definitely think twice before adding yet another factory to your project!

    We decied to remove injection of ArrayList, LinearLayoutManager, DividerItemDecoration. We created these classes with "new" not inject