Hello. I'm using Rick's `KMMViewModel` library and...
# multiplatform
a
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
Should be able to. Try explicitly denoting the class name: val viewModel by lazy { ViewModelProvider(this).get(NativeViewModel::class.java) as NativeViewModel }
h
@William Walker can you please do not send you answers to the channel too?
šŸ‘ 1
a
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
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
Yeah ... no clue how to do that. I tried a couple of things with not luck. Any ideas?
w
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
@Rick Clephas ? am I doing something wrong by any chance ?! I'm literally out of ideas.
w
Did you expose Rickā€™s kmm vm as an api dependency?
a
Copy code
api("com.rickclephas.kmm:kmm-viewmodel-core:1.0.0-ALPHA-19")
i got this in commonMain
w
@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
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
image.png
hmm ... šŸ¤” ideas ?
r
What exactly is the full error message?
a
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
Ah the
viewModel
function should be the one from AndroidX
a
image.png
r
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
maybe i've got an older version of androidX ... ?
r
Yeah could be. You can update it with the correct parameters
a
Done! I finally got what you intended on doing. šŸ˜„ Looking good, no more compile errors. Thank you !
šŸ‘šŸ» 1
šŸ™Œ 1