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

escodro

11/24/2020, 11:21 AM
Hello! Should we avoid using
ViewModel
in brand new Android projects or projects that we are able to refactor this part? I'm asking this because Compose seems to be growing over the Android Environment and I saw some projects using
ViewModel
and other ignoring it.
s

Sinan Gunes

11/24/2020, 11:32 AM
@escodro can you give me example projects? Since I am new I don’t see why Compose is not going well with ViewModel, or what is the alternative.
e

escodro

11/24/2020, 11:33 AM
At the moment, I'm studying the official ones: https://github.com/android/compose-samples
j

Joost Klitsie

11/24/2020, 11:34 AM
Well I just started a multiplatform project and in there I do not use the architecture components. However, I can see a way to use it, because you do get some stuff "for free" on Android side. If you only make an Android application, I do not see why you would not make use of ViewModel architecture components, as you get easy builders and navigation component integration
s

Sinan Gunes

11/24/2020, 11:35 AM
ah OK. I am not there yet. Bold guessing, they are just keeping samples simple.
j

Joost Klitsie

11/24/2020, 11:36 AM
Compose does try to integrate view models into composable methods, I just never tried it out because of my multiplatform adventure. But even in multiplatform, it would be possible to use android architecture ViewModels under the hood (for the android part)
s

Sinan Gunes

11/24/2020, 11:39 AM
hmm, this is new for me. Will have a deeper look.
n

nrobi

11/24/2020, 11:53 AM
I believe the current standpoint of the framework regarding architecture didn’t change with compose: while you’re still free to use any kind of pattern, the architectural ViewModel still remains a more convenient method of addressing android specific issues
Probably one of the clues for this is the
by viewModel()
support in composables
j

Javier

11/24/2020, 12:36 PM
if you remove ViewModel, where do you put all the logic it had? You can use
expect/actual
with the ViewModel to get it working in Multiplatform so you can use it in any UI (Android classic, Jetpack Compose, JetBrains Compose, SwiftUI, etc) sharing all this logic, instead of adding it to the View. Who says ViewModel, says Presenter or whatever
j

Joost Klitsie

11/24/2020, 12:42 PM
BTW many people/projects are in an experimental phase: what is compose and how to use it. I guess properly structuring apps will come later
b

Brandon Trautmann

11/24/2020, 1:24 PM
I thought one of those samples used a ViewModel but can't find it ATM. I'm using one in my little sample app and it's really nice to be able to just inject it into a Composable
The only annoying thing with doing so is if you want to Preview a whole screen that consumes a ViewModel you need to pass in fake params which is a bit boilerplate-ish