https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
a

AndreiBogdan

04/02/2024, 10:53 AM
Hello. I'm using Rick's
KMMViewModel
library and I've defined a view model in KMM but I'd like to subclass that viewModel on Android's side and add some more stuff to it. Problem is that I have no idea how to properly instantiate the view model. NativeViewModel -> CustomKMMViewModel -> Rick'sKMMViewModel Android Native -> KMM -> KMM Using
val mViewModel: NativeViewModel by viewModels()
does not work ... ideas on how this can be done ?! It shows a:
Type mismatch.
Required: NativeViewModel
Found: ViewModel
w

William Walker

04/02/2024, 3:55 PM
Should be able to. Try explicitly denoting the class name: val viewModel by lazy { ViewModelProvider(this).get(NativeViewModel::class.java) as NativeViewModel }
h

hfhbd

04/02/2024, 4:04 PM
@William Walker can you please do not send you answers to the channel too?
👍 1
a

AndreiBogdan

04/02/2024, 4:28 PM
image.png
So it doesn't see the SubscribeViewModel as a ViewModel ?!?! it's extending a SubscribeViewModel class in KMM which in return extends KMMViewModel ...
Android:
Copy code
class SubscribeViewModel : SubscribeViewModel() {
KMM
Copy code
open class SubscribeViewModel :
    BaseViewModel<SubscribeViewModel.UiState, SubscribeViewModel.RedirectType>(
also KMM
Copy code
open class BaseViewModel<U : Any, R> internal constructor(
    uiStateDefault: U,
    private val redirectTypeDefault: R
) : KMMViewModel() {
w

William Walker

04/02/2024, 5:11 PM
It is trying to infer the mViewModel as a regular androidx viewmodel. You need to explicitly denote it as SubscribeViewModel
You might also need to drop the lazy part- that delegate may only work for regular androidx viewmodels
a

AndreiBogdan

04/02/2024, 6:12 PM
Yeah ... no clue how to do that. I tried a couple of things with not luck. Any ideas?
w

William Walker

04/02/2024, 7:55 PM
val mViewmodel: SubscribeViewmodel = …
does that work?
Is rick’s viewmodel exposed as an api dependency by your kotlin multiplatform project?
If it’s implementation only, it won’t be able to be consumed by your android viewmodel
a

AndreiBogdan

04/03/2024, 8:10 AM
@Rick Clephas ? am I doing something wrong by any chance ?! I'm literally out of ideas.
w

William Walker

04/03/2024, 8:15 AM
Did you expose Rick’s kmm vm as an api dependency?
a

AndreiBogdan

04/04/2024, 4:33 AM
Copy code
api("com.rickclephas.kmm:kmm-viewmodel-core:1.0.0-ALPHA-19")
i got this in commonMain
w

William Walker

04/04/2024, 2:07 PM
@AndreiBogdan try updating your androidx viewmodel dependency it was just updated for KMP support and now takes Kclasses as parameters for viewmodel instantiation: https://developer.android.com/jetpack/androidx/releases/lifecycle
r

Rick Clephas

04/04/2024, 8:37 PM
This seems to be a linting issue. Using a small “helper” function removes the error:
Copy code
inline fun <reified VM : KMMViewModel> Fragment.viewModels(
    noinline ownerProducer: () -> ViewModelStoreOwner = { this },
    noinline extrasProducer: (() -> CreationExtras)? = null,
    noinline factoryProducer: (() -> ViewModelProvider.Factory)? = null
): Lazy<VM> = viewModels(ownerProducer, extrasProducer, factoryProducer)
a

AndreiBogdan

04/05/2024, 1:25 PM
image.png
hmm ... 🤔 ideas ?
r

Rick Clephas

04/05/2024, 1:50 PM
What exactly is the full error message?
a

AndreiBogdan

04/05/2024, 1:51 PM
image.png
aaah
wait
:))
i just noticed, yeah
Inline function 'public inline fun <reified VM : KMMViewModel> Fragment.viewModels(noinline ownerProducer: () -> ViewModelStoreOwner = ..., noinline extrasProducer: (() -> CreationExtras)? = ..., noinline factoryProducer: (() -> ViewModelProvider.Factory)? = ...): Lazy<VM> defined in com.pickatale.bookshelf.onboarding in file SubscribeFragment.kt' cannot be recursive
ah, well that explains it then :))
i've renamed the extension
image.png
r

Rick Clephas

04/05/2024, 1:53 PM
Ah the
viewModel
function should be the one from AndroidX
a

AndreiBogdan

04/05/2024, 1:54 PM
image.png
r

Rick Clephas

04/05/2024, 1:55 PM
Hmm maybe the signature has changed. The idea is to define the exact same inline function but restrict the generic to the
KMMViewModel
class
a

AndreiBogdan

04/05/2024, 1:55 PM
maybe i've got an older version of androidX ... ?
r

Rick Clephas

04/05/2024, 1:55 PM
Yeah could be. You can update it with the correct parameters
a

AndreiBogdan

04/05/2024, 2:12 PM
Done! I finally got what you intended on doing. 😄 Looking good, no more compile errors. Thank you !
👍🏻 1
🙌 1
8 Views