Ribs: [Question] How to inject dependencies into view class?

Created on 12 Jun 2018  路  8Comments  路  Source: uber/RIBs

How to inject dependencies into view class?
Should I create new Dagger component and module with view dependencies and than directly instantiate component inside view and call inject method?

question

Most helpful comment

@ermik looks like this is an Android question, needle is for iOS, the Android counterpart is uber/motif

All 8 comments

Do not do that! Always use a presenter to communicate with a view! You can pass through presenter methods whatever listener you want, or get an observable etc. And you already have a link on presenter inside your interactor ready to do the job. Also keep the view as dumb as possible.

"Also keep the view as dumb as possible" - What are you talking about, view implements presenter interface in this architecture! I need to provide view-related dependencies, such as adapter, that uses rx diff utils, with its own schedulers, how should I do this, create dependencies directly in the view - as for me, not good idea.

You must inject them through the presenter method, the view is being inflated inside the builder, you have no access to its constructor(sure you can create it manually in that method).
If you need a view that smart and decoupling to smaller ribs is not an option just put a method or property inside your presenter like

interface IWhateverPresenter {
    val title: String
    val adapter: IMySuperAdapter
    fun backflip(anotherStuff: Stuff? = null): Whatever
}

Inside your interactor

    @Inject
    lateinit var presenter: IWhateverPresenter

    @Inject
    lateinit var adapter: IMySuperAdapter

    override fun didBecomeActive() {
        super.didBecomeActive()
        presenter.adapter = adapter
        presenter.backflip()
    }

Thats it, easy, works, bulletproof

Whats the suggested way to do this?

~You guys should watch out for when uber/needle drops.~ See what @sbarow said below.

@ermik looks like this is an Android question, needle is for iOS, the Android counterpart is uber/motif

There is an android counterpart? :mind_blown:

Closing thread as stale.

Was this page helpful?
0 / 5 - 0 ratings