Mobile Dev.
07/14/2022, 10:50 PMIgor Milakovic
07/14/2022, 10:54 PMMobile Dev.
07/14/2022, 11:10 PMJavier
07/15/2022, 12:03 AMtylerwilson
07/15/2022, 1:18 AMclass BookViewModel {
private val _loading = MutableStateFlow(false)
val loading = _loading.asStateFlow()
val nativeLoading = _loading.asFlowWrapper()
}
The Android side subscribes to .loading, and the iOS will use the .nativeLoading with either an RxSwift or Combine wrapper (depending on min iOS version).
Then in the kmm common code, we just set _loading.value to some state, and it gets sent where it needs to go...coolcat
07/15/2022, 12:46 PMdeviant
07/15/2022, 1:12 PMMobile Dev.
07/15/2022, 1:37 PMRacka N
07/15/2022, 1:46 PMcommonViewModel{}
for Koin, so that I just provide my ViewModels from commonMain
• Helper extension function commonViewModel()
for injecting the dependencies in the platform UI. In Android it just wraps viewModel()
functions provided by Koin. In jvm with Decompose it is an extension of ComponentContext
and will call the destroy()
function I mentioned earlier when lifecycle.onDestroy
is reached and automatically cleans up resources (like cancelling the scope).
Has worked beautifully so far and does not need me to add another library (like Mokko MVVM) where you'd have to deal with some platforms that aren't supported like jvm and js. If you add another platform you just add it's implementation of the ViewModel (or wrap the platforms existing code) and you are done
The only major downside is that you'd have to manually call the destroy () function if you are not using Koin or Decompose and the platform has no way of clearing them automatically.
I'll post the link to an open source project (when I get home) where I use this approach so you can review it yourselfMobile Dev.
07/15/2022, 2:34 PMRacka N
07/15/2022, 2:40 PMworking-kotlin-js-support
branch. It's the one that's the most up to date. Also ignore the :common:features
module. Everything in there has been moved to the :common:all-features
module after I added the commonViewModel stuff.Igor Milakovic
07/15/2022, 8:32 PMandroidx.lifecycle.ViewModel
), unless I'm missing something and there's a better way. With iOS it's easy, as you can instantiate the view model in a view controller, or a coordinator, depending on which architecture you like.
Also, I feel like view models, although they contain logic, this is just presentational logic. All the business logic remains in the domain, which lives in the shared module.
Hope this makes sense!Javier
07/15/2022, 8:34 PMIgor Milakovic
07/15/2022, 8:38 PMkpgalligan
07/16/2022, 9:25 AMArkadii Ivanov
07/16/2022, 1:07 PMRacka N
07/16/2022, 2:16 PMArkadii Ivanov
07/16/2022, 2:26 PMJavier
07/16/2022, 2:48 PM