Anyone have ViewModels that work in compose but *d...
# compose
u
Anyone have ViewModels that work in compose but *dont have the android*/android architecture dependency? It is possible to mimic what it does, with my own type, right? (to survive orientation changes + stay alive while on backstack)
a
You might wanna look for precompose or decompose libraries, they offer a way to use view models for other targets
u
but, why does it have android dependency at all? ViewModel is basically just a interface its the retaining bit that has the android dependency, right?
i
And star the official feature request for making ViewModel itself multiplatform: https://issuetracker.google.com/issues/214568825
a
For our own HTML and Desktop projects we just rolled our own viewmodel. It’s pretty easy and works well with a good DI framework
a
I believe the AndroidX ViewModel class itself doesn't depend on anything Android-specific. It only depends on AndroidX Annotations (that are already multiplatform) and a bunch of Java things. See here: https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:lifecycle/lifecycle-viewmodel/src/main/java/androidx/lifecycle/ViewModel.java I think the integration with the platform is Android-specific, that's why the entire library is currently only for Android.
u
yea so if they broke it apart into 2 artifacts (jvm + android), it should be able multiplatform ready, right?
well, ready-ish, obviously jvm is not multiplatform, but shedding android is a start
a
Perhaps, but would there be any benefit to just having the ViewModel class? You can create your own expect/actual class, and typealias it to AndroidX ViewModel for Android, and just simple class for other platforms. But it's the integration with the platform and navigation that does the magic.
u
well, host junit tests for start
a
Typealias should work, I guess
u
hence why I have my own ViewModel type even on android
but that was dependant on Conductor handling config changes retaining, which is now obsolete with compose navigation
typealias?
a
Yep,
expect class ViewModel
+
actual typealias ViewModel = androidx.lifecycle.ViewModel
on Android.
u
sure, but there are thing I dont like about their shape
how do you test it though? on emulator only?
a
It should be testable locally with Junit, there are no Android dependencies.
u
hmm since it does pull in android, but doesnt touch any android apis, means it will work?
a
The library is only for Android, perhaps because there are things that depend on Android platform APIs. But the ViewModel class itself is a pure Java class. You can try, it should work.
Unless I'm missing something, but looking at the code there is nothing Android-related.
u
thats good to know it will work for teste regardless their shape is wrong, the "do work" in init bit, makes flows untestable, if they conflate you need to instantiate the class, then setup observer, then cause the testable bit.. and here you cant since its all in ini
a
What's "do work"?
u
well, setup flows forexample
a
I don't get it
u
unless you assume nothing is synchronous. but thats a dispatcher detail
a
The AndroidX ViewModel class is just a simple and almost empty class. It only contains some logic for closing.
u
well if in the init you subscribe a flow that emits 2 values, youd only see the latest
a
That looks unrelated to ViewModel class, as there is nothing about coroutines in there.
u
basically its missing some sort of "setup" lifecycle method, but yea I could probably bolt that on myself
a
So yeah, you can do something for your needs.
u
yea didnt occur to me
btw what about the
viewModelScope
.. is that not baked in as well?
looking at the sources I dont see it but how else could they have.. a stored property extension..oxymoron
a
Yeah, it should be in a separate ktx artifact
I think you can just write your own KMP variant, in a similar way
u
how do they add a stored property as a extension.. thats not possible, is it?
u
oh its the tagging hacks