adeo-opensource / kviewmodel--mpp

Library to sharing view model
Apache License 2.0
48 stars 4 forks source link

DI in ViewModel #22

Closed lordsxrom closed 1 year ago

lordsxrom commented 1 year ago

Could you explain how I could inject dependecies into ViewModel constructor? I found a work around with Koin. I used KoinComponent interface and member injection. But I am not sure it is best way to do it

class RandomizeViewModel :
    BaseSharedViewModel<RandomizeViewState, RandomizeAction, RandomizeEvent>(
        initialState = RandomizeViewState.Idle
    ),
    KoinComponent {

    val getRandomDrinkUseCase: GetRandomDrinkUseCase by inject()

    override fun obtainEvent(viewEvent: RandomizeEvent) { }

}
Skeptick commented 1 year ago

Hello. We use this option with member injection. In general constructor injection – isn't the library's responsibility. You can add your viewmodel to DI graph and get it, for example, in compose screen