Giter Site home page Giter Site logo

Comments (12)

JakeWharton avatar JakeWharton commented on June 17, 2024 14

This functionality is going to be covered by the upcoming Hilt tool in Jetpack which integrates Dagger with view models and work manager.

from inflationinject.

Ethan1983 avatar Ethan1983 commented on June 17, 2024

You might want to check @InflationInject @InflationModule which is used to avoid that another level of factories (LayoutInflator.Factory).

from inflationinject.

eerikk avatar eerikk commented on June 17, 2024

@mtiidla have you found more elegant solution to this?

from inflationinject.

mtiidla avatar mtiidla commented on June 17, 2024

@mtiidla have you found more elegant solution to this?

@eerikk No, unfortunately not. I have recently switched projects and am currently not using Dagger2, but Koin instead. Koin solves this exact issue as you can see here. However, I personally care much more about Dagger2 and I am planning to look into the LayoutInflater Factory solution mentioned by Ethan1983, but haven't gotten around to it yet.

from inflationinject.

alashow avatar alashow commented on June 17, 2024

My solution was to have a callback on ViewModel creation in ViewModelFactory:
https://gist.github.com/alashow/67cc52c606ddc7637e75185673b7f9a6

from inflationinject.

mtiidla avatar mtiidla commented on June 17, 2024

My solution was to have a callback on ViewModel creation in ViewModelFactory:
https://gist.github.com/alashow/67cc52c606ddc7637e75185673b7f9a6

Thanks for proposing your solution, however it is not solving the problem. The main idea is to avoid an init method on the viewmodel and use constructor to injection arguments. Otherwise we could very well write an init method on every viewmodel and just call it (which you have just abstracted away).

from inflationinject.

alashow avatar alashow commented on June 17, 2024

Yeah, the reason why I abstracted it is to init the ViewModel on creation.

from inflationinject.

evant avatar evant commented on June 17, 2024

I made https://github.com/evant/injectedvmprovider to try to deal with this, it lets you skip the ViewModelFactory. While we aren't currently using it with AssistedInject, we are using it with manual factories and it's working pretty well. Your example would be:

The ViewModel

class WordDetailViewModel  @AssistedInject constructor(
    private val repository: WordRepository,
    @Assisted argument: WordDetailScreenArg
) : ViewModel() {

    val wordDetail: LiveData<Word> = repository.word(argument.word)

    fun onDeleteWordClicked() {
        wordDetail.value?.let {
            repository.deleteWord(it.word)
        }
    }

    @AssistedInject.Factory
    interface Factory {
        fun create(argument: WordDetailScreenArg): WordDetailViewModel
    }
}

Injection

@Inject
lateinit var viewModelFactory: WordDetailViewModel.Factory;

override fun onAttach(context: Context?) {
    super.onAttach(context)

    val arguments: WordDetailScreenArg = ...
    Injection.appComponent.inject(this)

    viewModel = InjectedViewModelProviders.of(this).get(viewModelFactory, f -> f.create(arguments))
}

from inflationinject.

Mayank-Bhatnagar avatar Mayank-Bhatnagar commented on June 17, 2024

Well with this approach are we not gonna end with each individual ViewModel mapped to its own ViewModelFactory. Thus, we are sacrificing the cost of loosing a generic ViewModelFactory. Please correct me if I misunderstood.

from inflationinject.

permenko avatar permenko commented on June 17, 2024

https://github.com/tfcporciuncula/dagger-journey

from inflationinject.

eerikk avatar eerikk commented on June 17, 2024

We've started to use this kind of solution in our project. Dagger helps us out here 👇

  1. Generic code that lets us inject into Fragment and ViewModel. Important part here is the code inside companion object. We make fragment argument available for ViewModel.
@Module
interface ExampleModule {

    @PerFragment
    @ContributesAndroidInjector(modules = [ExampleViewModelModule::class])
    fun contributesExampleFragment(): ExampleFragment

    @Module(includes = [ViewModelFactoryModule::class])
    abstract class ExampleViewModelModule {

        @Binds
        @IntoMap
        @ViewModelKey(ExampleViewModel::class)
        abstract fun bindExampleViewModel(viewModel: ExampleViewModel): ViewModel

        @Module
        companion object {
            @Provides
            @JvmStatic
            fun providesExampleParam(fragment: ExampleFragment): String {
                return fragment.requireArguments()
                    .getString(ExampleFragment.ARG_EXAMPLE_STRING)!!
            }
        }
    }

}

This way we can inject the param simply to our ViewModel

class ExampleViewModel @Inject constructor(
    private val exampleString: String
): ViewModel{}

If you have multiple parameters with same type, you can use @Named annotation to prevent type conflicts. 👇

            @Provides
            @Named(ExampleFragment.EXAMPLE_STRING)
            @JvmStatic
            fun providesExampleParam(fragment: ExampleFragment): String {
                return fragment.requireArguments()
                    .getString(ExampleFragment.ARG_EXAMPLE_STRING)!!
            }

...and now you can simply access it from ViewModel

class ExampleViewModel @Inject constructor(
    @Named(ExampleFragment.EXAMPLE_STRING) private val exampleString: String
): ViewModel{}

from inflationinject.

hansenji avatar hansenji commented on June 17, 2024

This library uses Assisted Inject and SavedStateHandle to pass arguments into your viewmodel
https://github.com/hansenji/ViewModelInject.

from inflationinject.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.