Thread
#moko
    deviant

    deviant

    11 months ago
    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.
    wbertan

    wbertan

    11 months ago
    We have created our own: In common we have a
    BaseViewModel
    expect abstract class BaseViewModel()
    And we create an
    actual
    for Android and another for iOS: Android:
    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:
    actual abstract class BaseViewModel
    deviant

    deviant

    11 months ago
    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 🙂
    wbertan

    wbertan

    11 months ago
    We are also using Koin, so for Android we have the
    viewModel
    method, but for iOS we had to make it a
    factory
    .
    deviant

    deviant

    11 months ago
    i get it. so you use platform specific modules. interesting. probably we gonna do the same
    wbertan

    wbertan

    11 months ago
    So for Koin, we have our own method too In common:
    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:
    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:
    actual inline fun <reified T : BaseViewModel<*, *>> Module.viewModelDefinition(
        qualifier: Qualifier?,
        noinline definition: Definition<T>,
    ): Pair<Module, InstanceFactory<T>> = factory(qualifier = qualifier, definition = definition)
    deviant

    deviant

    11 months ago
    thanks!
    Vladimir Vasic

    Vladimir Vasic

    2 months ago
    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:
    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 🙂
    wbertan

    wbertan

    2 months ago
    🤔 how it is your
    BaseViewModel
    ? Do you have any generics there as well? In the fragment we just do
    private val viewModel: MyViewModel by viewModel()
    That
    viewModel
    is from Koin:
    import org.koin.androidx.viewmodel.ext.android.viewModel
    Vladimir Vasic

    Vladimir Vasic

    2 months ago
    This is my BaseViewModel
    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?
    val loginModule = module {
        viewModel {
            LoginViewModel(get())
        }
    }
    wbertan

    wbertan

    2 months ago
    Sorry, did the test and forgot to post here, for me this is compiling fine:
    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
    val loginModule = module {
        viewModelDefinition {
            MyViewModel()
        }
    }
    By the way, I'm using latest Canary Electric Eel, Versions:
    koin = "3.2.0"
        const val agp = "7.2.1"
        const val kotlin = "1.7.0"
    Vladimir Vasic

    Vladimir Vasic

    2 months ago
    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 🙂