nglauber
05/20/2021, 3:05 AMTextField
… which one would be better: 1) expose a series of LiveData/StateFlow; or 2) encapsulate in a object and every change send a copy of the object just changing one property? (I mean myObj.copy(fieldThatChanged=newValue)
)
https://developer.android.com/jetpack/compose/state#viewmodel-stateColton Idle
05/20/2021, 3:30 AMclass MyViewModel : ViewModel() {
val state = MyViewState()
...
}
class MyViewState {
var textField1 by mutableStateOf("")
var textField2 by mutableStateOf("")
var textField3 by mutableStateOf("")
// etc
}
Since you're not consuming an observable from some other source, you get to use all of the primitives that compose gives you out of the box. Even if you wanted to use LD or SF you would still need to call the function to convert it to snapshot state, so you just saved yourself some resources.
Other pros of this is no backing property syntax, no livedata.value!! stuff, and setting values is super easy. You never have to call copy. You just set the value.
Adam Powell can correct me if I'm wrong, but using snapshot state gives you the immutability that you're probably used to, but you interface with it as if it was mutable. Win win.nglauber
05/20/2021, 3:42 AMnglauber
05/20/2021, 3:45 AMColton Idle
05/20/2021, 3:49 AMI understand your point, but I don’t want to tie my ViewModel to Compose… (edited)🤔 In a pure compose app you don't need to use AAC VMs, but at the end of the day... you have to "tie" together your AAC VM (or plain old VM/state class) to your activity/fragment/composable.
I’m also hoisting my callbacks to the ViewModel, which allows me to validate the data in the ViewModel as well…Everything I mentioned above works with what you want to do. Just write your onChange1, onChange2, etc methods and you're on your way.
nglauber
05/20/2021, 4:00 AMIan Lake
05/20/2021, 4:21 AMIan Lake
05/20/2021, 4:25 AMIan Lake
05/20/2021, 4:29 AMIan Lake
05/20/2021, 4:31 AMIan Lake
05/20/2021, 4:45 AMTash
05/20/2021, 6:22 AMnglauber
05/20/2021, 1:34 PMNeal Sanche
05/20/2021, 6:59 PMTash
05/20/2021, 10:40 PM