Hello, I am creating same composable UI for both A...
# moko
j
Hello, I am creating same composable UI for both Android and iOS. I also want to have the same view-model, that why both UI and view-model logic is only one for both platforms. As I understand this library would be the correct approach? I have not found any information on GitHub repo, however, I found in some video that I could use
getViewModel
. This, however, requires to create an instance of view-model - what If I have some dependencies in my view-model that should be injected (by Koin). Is this correct thinking of mine or it's not quite possible to have same UI and view-model for Android and iOS? Thanks.
a
yes you can use moko-mvvm. to better handle of lifecycle on each platfroms you should use
getViewModel
from
mvvm-compose
, but inside
viewModelFactory
you can use koin to receive all required deps
🙌 1
hi. moko with koin can be used together. at first you should get
koin
object (just call
getKoin()
) then inside
viewModelFactory
you can create viewmodel with koin.
Copy code
viewModelFactory { koin.get<SomeViewModel>() }
p
Hi, does that mean that in Koin the viewModel injection declaration is declared as factory? and then the
viewModelFactory
will make sure the created viewModel is saved during configuration changes?
Copy code
factory { SomeViewModel(injectParam = get()) }
and:
Copy code
val koin = getKoin()
val viewModel = getViewModel(
  key = screenKey,
  factory = viewModelFactory { koin.get<SomeViewModel>() }
)
Thank you!