Skip to content
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

Assisted injection? #6

Closed
Jeevuz opened this issue Oct 3, 2017 · 4 comments
Closed

Assisted injection? #6

Jeevuz opened this issue Oct 3, 2017 · 4 comments

Comments

@Jeevuz
Copy link
Contributor

Jeevuz commented Oct 3, 2017

Hi! Nice library, really like it!
But I can't get how we can do so called assisted injection. Imagine I have MyPresenter(val someId: Int, val interactor: SomeInteractor), where someId is dynamic parameter that is known only by the time MyView (a Fragment) is created.

How I can provide the MyPresenter in this case?

@arnaudgiuliani
Copy link
Member

Hi Jeevuz! Thanks :)

I think, you have 2 ways to do it:
In Koin module

  • declare a property injection for your MyPresenter component with:
    provide {MyPresenter(getProperty("someId"),get())
  • provide this property from your activity or your fragment, before using any injection of MyPresenter: getKoin().setProperty("someId",/* your value */)
  • when you will get the injected MyPresenter component, constructor will be filled with the given property - resolution will be done with the last value of your property. Be aware to drop it (release scope) if it needs any change.

Or if you use an MVP approach, you could directly set your id on your MyPresenter, by your fragment. Once your presenter is injected, fill your value presenter.someId = someId

try the first one if it fits your needs.

@Jeevuz
Copy link
Contributor Author

Jeevuz commented Oct 4, 2017

Thanks for the quick response!
Interesting way with the property.
Don't sure I understood your last point correctly. Is there the way to release the property? (It would be good for using the same property many times for different presenters. For example if I create first and put general PresenterParams for this and later release it and use for the second presenters creation.)

@arnaudgiuliani
Copy link
Member

Properties cannot be dropped for now. But you overwrite existing property, each time you use getKoin().setProperty() on the same key.

@Jeevuz
Copy link
Contributor Author

Jeevuz commented Oct 4, 2017

Ок, I got it. Thanks again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants