kosi-libs / Kodein

Painless Kotlin Dependency Injection

Home Page:https://kosi-libs.org/kodein

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Use the new package kodein-di-framework-compose-android instead

PieterPicup opened this issue · comments

I am trying to call rememberViewModel on my VMs that inherit from androids ViewModel().
The call is giving me the title warning for deprecation of the method.

I have this in my gradle:

implementation("org.kodein.di:kodein-di-framework-compose-android:7.20.2")

If i try change that to:

implementation("org.kodein.di:kodein-di-framework-compose-android:7.21.1")

I then get a build error:

Could not find org.jetbrains.compose.runtime:runtime:1.5.10-dev-wasm02

Could you please let me know how to solve above and the correct way to implement VMs in compose that require savedStateHandle to be passed in.
I currently have this in my module:

bindFactory {savedStateHandle: SavedStateHandle ->
        DummyViewModel(
            savedStateHandle,
            instance() //DummySingletonRepository
        )
    }

but when I do try and retrieve it with rememberViewModel it says cannot find an instance of the type required(DummyViewModel).

I have managed to get the saved state handle to pass into the factories by using the below method which i created using the existing rememberViewModel methods as reference.

@Composable
inline fun <reified VM : ViewModel> rememberViewModelWithSavedStateHandle(tag: Any? = null): ViewModelLazy<VM> = with(
    localDI()
) {
    val viewModelStoreOwner = LocalViewModelStoreOwner.current ?: error("")

    val extras = if (viewModelStoreOwner is HasDefaultViewModelProviderFactory) {
        MutableCreationExtras(viewModelStoreOwner.defaultViewModelCreationExtras)
    } else {
        MutableCreationExtras(CreationExtras.Empty)
    }
    extras[ViewModelProvider.NewInstanceFactory.VIEW_MODEL_KEY] = VM::class.java.canonicalName!!

    val factory = object : ViewModelProvider.Factory {
        override fun <T : ViewModel> create(modelClass: Class<T>): T {
            @Suppress("UNCHECKED_CAST")
            return direct.instance<SavedStateHandle, VM>(tag, extras.createSavedStateHandle()) as T
        }
    }

    remember {
        ViewModelLazy(
            viewModelClass = VM::class,
            storeProducer = { viewModelStoreOwner.viewModelStore },
            factoryProducer = { factory }
        )
    }
}

I am not sure if this is the correct way but it seems to be working as expected.

Any feedback on this implementation would be appreciated.

This issue Could not find org.jetbrains.compose.runtime:runtime:1.5.10-dev-wasm02 is resovled by 7.21.2.

For the rest, I'd need to investigate more.

Thank you sir!

I will update versions and see if the existing lib code works as expected without my workaround. If needed feel free to use the code I have above if it is correct pattern. I am still getting the hang of compose so I am not sure if what I created is correct for the framework.

The version may take few minutes to be available through Maven Central.
The code seems okay, but perhaps some parts could be shortened.

I will most likely only get to checking next week. Project hopping right now with new years rush to get new features out.

This issue Could not find org.jetbrains.compose.runtime:runtime:1.5.10-dev-wasm02 is resovled by 7.21.2.

For the rest, I'd need to investigate more.

Import works now 👍