Koin: How to get viewModel from Koin with SavedStateHandle (new Google announced at I/O 2019)

Created on 26 May 2019  路  11Comments  路  Source: InsertKoinIO/koin

I'm concerning how to get ViewModel from Koin with SavedStateHandle.
Here is my DI class:
ViewModelModule.kt

viewModel { ProfileViewModel(userApi = get()) }

Now, I want to try using SavedState module for viewModule that is announced from Google I/O 2019.
Link: https://developer.android.com/topic/libraries/architecture/viewmodel-savedstate

So I declared in my ViewModel as below:

class ProfileViewModel(
    private val userApi: UserApi, 
    private val stateHandle: SavedStateHandle
) : ViewModel() {

Base on Koin concept, I just need define viewModel in ProfileActivity.kt class:

private val viewModel: ProfileViewModel by inject()

But, what should I do with my DI class ViewModelModule.kt to get SavedStateHandle ?

android check open-contribution

All 11 comments

I'm pretty sure that this isn't possible at the moment.

@IVIanuu
Thank for the reply.
Basically I think so

On dagger that's is solved using @AssistedInject, can we expect something similar coming to Koin?

have to check how to use this SavedStateVMFactory with constructor injection

Any updates on this?

This feature is open to contribution if you want to propose something 馃憤

I think we would need to add DSL which passes a savedStateParameter to the bean definition. I could look at it..

viewModelWithState { (savedState: SavedStateHandle, someArgument : String) -> MyViewModel(savedState, someArgument, get(), get()) }

I added a PR for this #496 would appreciate feedback. I know it forces the lifecycle dependencies to update, maybe we can discuss it.

@arnaudgiuliani I think this will be essential functionality. is there a bleeding edge branch you use to point to the latest versions of androidx? If so that would be a better target. I would also love to here your thoughts on my implementation. #496

ok, will check that

closing this, to go on the PR directly

Was this page helpful?
0 / 5 - 0 ratings

Related issues

AHarazim picture AHarazim  路  3Comments

dakuenjery picture dakuenjery  路  4Comments

caleb-allen picture caleb-allen  路  4Comments

mubarak1361 picture mubarak1361  路  3Comments

fmobus picture fmobus  路  4Comments