https://kotlinlang.org logo
#compose
Title
# compose
u

ursus

10/22/2023, 12:15 AM
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

Alejandro Rios

10/22/2023, 12:29 AM
You might wanna look for precompose or decompose libraries, they offer a way to use view models for other targets
u

ursus

10/22/2023, 12:30 AM
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

Ian Lake

10/22/2023, 5:41 AM
And star the official feature request for making ViewModel itself multiplatform: https://issuetracker.google.com/issues/214568825
a

Arjan van Wieringen

10/22/2023, 11:26 AM
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

Arkadii Ivanov

10/23/2023, 9:00 AM
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

ursus

10/23/2023, 9:10 AM
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

Arkadii Ivanov

10/23/2023, 9:12 AM
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

ursus

10/23/2023, 9:13 AM
well, host junit tests for start
a

Arkadii Ivanov

10/23/2023, 9:13 AM
Typealias should work, I guess
u

ursus

10/23/2023, 9:13 AM
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

Arkadii Ivanov

10/23/2023, 9:17 AM
Yep,
expect class ViewModel
+
actual typealias ViewModel = androidx.lifecycle.ViewModel
on Android.
u

ursus

10/23/2023, 9:19 AM
sure, but there are thing I dont like about their shape
how do you test it though? on emulator only?
a

Arkadii Ivanov

10/23/2023, 9:30 AM
It should be testable locally with Junit, there are no Android dependencies.
u

ursus

10/23/2023, 9:31 AM
hmm since it does pull in android, but doesnt touch any android apis, means it will work?
a

Arkadii Ivanov

10/23/2023, 9:44 AM
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

ursus

10/23/2023, 9:47 AM
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

Arkadii Ivanov

10/23/2023, 9:48 AM
What's "do work"?
u

ursus

10/23/2023, 9:49 AM
well, setup flows forexample
a

Arkadii Ivanov

10/23/2023, 9:49 AM
I don't get it
u

ursus

10/23/2023, 9:49 AM
unless you assume nothing is synchronous. but thats a dispatcher detail
a

Arkadii Ivanov

10/23/2023, 9:50 AM
The AndroidX ViewModel class is just a simple and almost empty class. It only contains some logic for closing.
u

ursus

10/23/2023, 9:51 AM
well if in the init you subscribe a flow that emits 2 values, youd only see the latest
a

Arkadii Ivanov

10/23/2023, 9:51 AM
That looks unrelated to ViewModel class, as there is nothing about coroutines in there.
u

ursus

10/23/2023, 9:51 AM
basically its missing some sort of "setup" lifecycle method, but yea I could probably bolt that on myself
a

Arkadii Ivanov

10/23/2023, 9:52 AM
So yeah, you can do something for your needs.
u

ursus

10/23/2023, 9:52 AM
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

Arkadii Ivanov

10/23/2023, 10:05 AM
Yeah, it should be in a separate ktx artifact
I think you can just write your own KMP variant, in a similar way
u

ursus

10/23/2023, 10:06 AM
how do they add a stored property as a extension.. thats not possible, is it?
u

ursus

10/23/2023, 10:17 AM
oh its the tagging hacks
2 Views