hey guys does anybody use moko viewModel with koin...
# moko
d
hey guys does anybody use moko viewModel with koin di? how you declare your viewModels modules in common code? koin
viewModel
helper factory available only for android projects. so we use the
factory
extension temporarily. but it recreates viewModel each time.
w
We have created our own: In common we have a
BaseViewModel
Copy code
expect abstract class BaseViewModel()
And we create an
actual
for Android and another for iOS: Android:
Copy code
actual abstract class BaseViewModel : ViewModel()
Which the Android extends
androidx.lifecycle.ViewModel
, which will work nicely with the Lifecycles. And the iOS just the simple one:
Copy code
actual abstract class BaseViewModel
๐Ÿ™Œ 1
d
but moko mvvm already provides the common vm (which uses android vm for android). the problem is with providing via koin di. probably i should asj this question in the #koin ๐Ÿ™‚
w
We are also using Koin, so for Android we have the
viewModel
method, but for iOS we had to make it a
factory
.
d
i get it. so you use platform specific modules. interesting. probably we gonna do the same
w
So for Koin, we have our own method too In common:
Copy code
expect inline fun <reified T : BaseViewModel<*, *>> Module.viewModelDefinition(
    qualifier: Qualifier? = null,
    noinline definition: Definition<T>
): Pair<Module, InstanceFactory<T>>
While then for Android the implementation will call the Koin `viewModel`:
Copy code
actual inline fun <reified T : BaseViewModel<*, *>> Module.viewModelDefinition(
    qualifier: Qualifier?,
    noinline definition: Definition<T>,
): Pair<Module, InstanceFactory<T>> = viewModel(qualifier = qualifier, definition = definition)
And for iOS will use a `factory`:
Copy code
actual inline fun <reified T : BaseViewModel<*, *>> Module.viewModelDefinition(
    qualifier: Qualifier?,
    noinline definition: Definition<T>,
): Pair<Module, InstanceFactory<T>> = factory(qualifier = qualifier, definition = definition)
๐Ÿ‘ 2
d
thanks!
v
Hello @wbertan, I'm having trouble implementing these extensions in my KMM project, I'm getting error message when trying to implement actual viewModelDefinition for android. Error message: Module.viewModel(Qualifier? = ...) where T = TypeVariable(T) for inline fun <reified T : ViewModel> Module.viewModel(qualifier: Qualifier? = ...): Pair<Module, InstanceFactory<T>> defined in org.koin.androidx.viewmodel.dsl Module.viewModel(Qualifier? = ..., Definition<TypeVariable(T)> My implementation:
Copy code
actual inline fun <reified T : BaseViewModel> Module.viewModelDefinition(
    qualifier: Qualifier?,
    noinline definition: Definition<T>,
): Pair<Module, InstanceFactory<T>> = viewModel(qualifier = qualifier, definition = definition)
Also when I want to use this inside a fragment, how am I calling it?
Thank you ๐Ÿ™‚
w
๐Ÿค” how it is your
BaseViewModel
? Do you have any generics there as well? In the fragment we just do
Copy code
private val viewModel: MyViewModel by viewModel()
That
viewModel
is from Koin:
Copy code
import org.koin.androidx.viewmodel.ext.android.viewModel
v
This is my BaseViewModel
Copy code
open class BaseViewModel : ViewModel() {

    open fun onViewCreated() {}

    open fun onViewResumed() {}

    open fun onViewPaused() {}

    open fun onViewStopped() {}
}
I use no generics. Soo I still use the same implementation with koin in module right?
Copy code
val loginModule = module {
    viewModel {
        LoginViewModel(get())
    }
}
w
Sorry, did the test and forgot to post here, for me this is compiling fine:
Copy code
open class Asas : ViewModel() {

    open fun onViewCreated() {}

    open fun onViewResumed() {}

    open fun onViewPaused() {}

    open fun onViewStopped() {}
}

inline fun <reified T : Asas> Module.viewModelDefinition(
    qualifier: Qualifier?,
    noinline definition: Definition<T>
): Pair<Module, InstanceFactory<T>> = viewModel(qualifier = qualifier, definition = definition)
In the Koin Module, you should then not use the
viewModel
, but instead that
viewModelDefinition
Copy code
val loginModule = module {
    viewModelDefinition {
        MyViewModel()
    }
}
By the way, I'm using latest Canary Electric Eel, Versions:
Copy code
koin = "3.2.0"
    const val agp = "7.2.1"
    const val kotlin = "1.7.0"
v
I guess my IDE was messed up, sorry for late response but it is working like a charm ๐Ÿ™‚. Thank you very much for your help ๐Ÿ™‚
104 Views