Closed alexandru-calinoiu closed 5 years ago
Any updates on this? Thanks!
viewModel { FooBarImpl() as FooBar }
@alexandru-calinoiu It looks like you ar enot using the Android ViewModel
but instead define your own interface. In that case you don't need the viewModel {}
definitions at all - a simple factory {}
suffices. You inject them with by inject()
.
You can't use an interface for viewModel
since unfortunately the base ViewModel
is an abstract class and the ViewModelProvider.Factory
methods require the provided viewmodel inherits from ViewModel
.
But you can achieve the separation of interface and implementation by making the interface an abstract class instead.
// "interface"
abstract class FooViewModel {
abstract val events: LiveData<ViewModelEvent>
abstract fun getLastWeather()
}
// implementation
class FooViewModelImpl : FooViewModel() {
override val events = SingleLiveEvent<ViewModelEvent>()
override fun getLastWeather() {
}
}
and then
viewModel<FooViewModel> { FooViewModelImpl() }
That is probably the closest you will get without some hacky workarounds
this last message is the best way to do that IMO
I found a way to do this, by overriding Koin extension functions. Please check my stackoverflow answer. Although I think abstract class is the way to go.
Is your feature request related to a problem? Please describe. I don't want to inject the ViewModel implementations in my Activity/Fragment
Describe the solution you'd like Inject interfaces for my ViewModel
Describe alternatives you've considered So far my solution look like this
I will have an interface for my viewModel:
I will have my ViewModel inherit the interface and livecycle.ViewModel:
I will register my ViewModel like usual:
And use it in the Activity via this line:
Target Koin project not sure