YASAN
04/16/2021, 7:01 PMby remember { mutableStateOf(X) }
Using remember takes less time but I am not sure if its recommended over livedata? Although I am not sure if it matters in the end of the day at all or notjim
04/16/2021, 7:18 PMby mutableStateOf(X)
or Flow<T>.collectAsState()
are both preferred over LiveData and/or AAC ViewModel for exactly the reason mentioned in the previous comment.Adam Powell
04/16/2021, 7:20 PMAdam Powell
04/16/2021, 7:24 PMAdam Powell
04/16/2021, 7:28 PMby mutableStateOf(...)
can give you a lot of the benefits associated with AAC ViewModel without actually subclassing ViewModel
to do it or introducing that dependency.YASAN
04/16/2021, 7:32 PMAdam Powell
04/16/2021, 7:33 PMAdam Powell
04/16/2021, 7:34 PMYASAN
04/16/2021, 7:44 PMYASAN
04/16/2021, 7:45 PMYASAN
04/16/2021, 7:49 PMAdam Powell
04/16/2021, 8:53 PMSo I should avoid using ViewModels and LiveData? Then it wont be MVVM?!MVVM is just a pattern, you don't need AAC
ViewModel
and LiveData
types to use the pattern. If you feel more comfortable using those types, you can, but you can often express things a bit more clearly/tersely in compose without them.Ian Lake
04/16/2021, 8:54 PMAdam Powell
04/16/2021, 8:55 PMViewModel
to cache data across config changes, but it's probably the simplest way to do so.Adam Powell
04/16/2021, 8:56 PMViewModel
subclasses entirely and just use them to reference other objects that I need to marshal across those config changes, putting the smarts in those other objects insteadAdam Powell
04/16/2021, 8:56 PMAdam Powell
04/16/2021, 8:57 PM: ViewModel()
from your class declaration and changing property pairs like this:
private val _myData = MutableLiveData<MyData>(...)
val myData: LiveData<MyData>
get() = _myData
with:
var myData by mutableStateOf(...)
private set
Adam Powell
04/16/2021, 9:02 PMsetValue
vs. postValue
based on what thread you're on, or explicitly observing it vs. just letting snapshot observation do the work for youYASAN
04/16/2021, 9:09 PM: ViewMode()
. It doesnt look like I can use them on my composables as well, I guess I would need viewmodels for those functions?
My main problem is that literally every single screen of my app needs to use my SettingsRepository
. I am working on a launcher so every single thing on the UI is based on the users configuration.
Without Compose I would just create a viewmodel for each activity/fragment and let the viewmodel talk to the repository but using compose I am just injecting the repo into my activity and using it everywhere I need that way without having to create new viewmodels for each screen. I have to send my activity as a param to most of the main composables to do this and even tho it is super easy to use it this way I feel like its not a good way of handling it.Adam Powell
04/16/2021, 9:37 PMViewModel
is working for you for various reasons, by all means keep using it. Compose doesn't mind one way or the other. πAdam Powell
04/16/2021, 9:37 PMViewModel
made drastically easier, and with compose, it's not the only way available to accomplish many of those things anymoreAdam Powell
04/16/2021, 9:39 PMviewModelScope
extensions at all; I think probably 95% of usages of it are better suited for either the UI scope (activity, fragment, compose, whatever) or a repository-level scopeAdam Powell
04/16/2021, 9:39 PMAdam Powell
04/16/2021, 9:41 PMviewModelScope
is an example of AAC ViewModel
being a local maximum for a lot of things. It's almost always within reach and there's a way to do a lot of things with it, but it pulls along a lot of its own quirks, dependencies, and lifecycle concerns that often pull other things into its gravity well that may work better outside of itAdam Powell
04/16/2021, 9:42 PMYASAN
04/16/2021, 10:05 PMYASAN
04/16/2021, 10:06 PMDaniele B
04/17/2021, 6:51 AMYASAN
04/17/2021, 6:58 AMJulianK
04/21/2021, 10:31 AMViewModel
- are there other possibilities to have backstackEntry
viewModel scoping together with compose navigation? Like when we navigate to sub screens, we'd like to keep the current viewmodel to cache state or avoid reloading lists of data when the user goes back.
I don't see why you want to give up the integration of compose-navigation, AAC ViewModels and SavedStateHandle
and I can't see how you would easily replace this without using AAC ViewModels and viewModel()
.