Pablo
12/16/2022, 7:07 PMPablichjenkov
12/16/2022, 7:16 PMPablo
12/16/2022, 7:20 PMPablichjenkov
12/16/2022, 7:26 PMPablichjenkov
12/16/2022, 7:31 PMPablo
12/16/2022, 7:53 PMPablo
12/16/2022, 7:54 PMPablo
12/16/2022, 7:55 PMPablo
12/16/2022, 7:59 PMPablo
12/16/2022, 7:59 PMPablichjenkov
12/16/2022, 8:33 PMPablichjenkov
12/16/2022, 8:40 PMenighma
12/16/2022, 9:36 PMenighma
12/16/2022, 9:37 PMPablo
12/16/2022, 10:20 PMenighma
12/16/2022, 10:25 PMenighma
12/16/2022, 10:27 PMPablo
12/16/2022, 10:30 PMenighma
12/16/2022, 10:32 PMPablo
12/16/2022, 10:32 PMenighma
12/16/2022, 10:35 PMPablo
12/16/2022, 10:37 PMenighma
12/16/2022, 10:37 PMPablo
12/16/2022, 10:37 PMenighma
12/16/2022, 10:37 PMPablo
12/16/2022, 10:38 PMclass MainViewModel {
var region by remember { mutableStateOf("") }
init {
region = "Europe"
}
fun changeRegion(newRegion: String) {
region = newRegion
}
}
Do you think it's correct? as I understand using remember state variables should do the job similar to the Android ViewModel feacturesenighma
12/16/2022, 10:39 PMPablo
12/16/2022, 10:39 PMPablo
12/16/2022, 10:39 PMPablo
12/16/2022, 10:41 PMPablo
12/16/2022, 10:41 PMclass MainViewModel {
var region: String
init {
region = "Europe"
}
fun changeRegion(newRegion: String) {
region = newRegion
}
}
Whad do you think?enighma
12/16/2022, 10:42 PMclass UiState(val region: String)
@Composable
fun MyViewModel(): MutableState<UiState> {
var region by remember { mutableStateOf("") }
var uiState = remember(region) {
mutableStateOf(UiState(region))
}
return uiState
}
enighma
12/16/2022, 10:43 PMenighma
12/16/2022, 10:46 PMPablo
12/16/2022, 10:47 PMvar vm: MainViewModel = MainViewModel()
Or how will the binding be achieved?enighma
12/16/2022, 10:50 PM@Composable
fun MyScreen() {
val viewModel by MyViewModel()
MyContent(viewModel.region)
}
The VM usually depend on various data sources, and you need to either inject those in the VM, or you lift it out of the Screen, and pass the VM as a parameter, and connect the VM with its data sources (repositories) in your App {}
composable instead.Pablo
12/16/2022, 10:51 PMPablo
12/16/2022, 10:52 PMenighma
12/16/2022, 10:53 PMPablo
12/16/2022, 10:55 PMPablo
12/16/2022, 10:55 PMenighma
12/16/2022, 10:56 PMPablo
12/16/2022, 10:56 PMPablichjenkov
12/16/2022, 10:58 PMPablo
12/16/2022, 10:59 PMPablo
12/16/2022, 10:59 PMPablichjenkov
12/16/2022, 11:00 PMPablo
12/16/2022, 11:01 PMMichael Paus
12/17/2022, 11:26 AMcollectAsState()
. That way I was able to even combine them with a GUI written in JavaFX.Pablo
12/17/2022, 5:15 PMPablo
12/17/2022, 5:23 PMclass MainViewModel {
var region: String
init {
region = "Europe"
}
fun changeRegion(newRegion: String) {
region = newRegion
}
}
And in a different .kt file:
var mainViewModel by remember { mutableStateOf(MainViewModel()) }
@Composable
fun Window1() {
MaterialTheme {
Column() {
OutlinedTextField(label = { Text("Region Window 1") }, value = mainViewModel.region,
onValueChange = { mainViewModel.region = it })
Text(text = mainViewModel.region)
}
}
}
@Composable
fun Window2() {
MaterialTheme {
Column() {
OutlinedTextField(label = { Text("Region Windows 2") }, value = mainViewModel.region,
onValueChange = { mainViewModel.region = it })
Text(text = mainViewModel.region)
}
}
}
I'm not sure where to place the "var mainViewModel" so I placed outside both composable functions. I suposse that as it is "by remember" it will work in both composables but... please, can you improve and correct this sample? Thank youPablo
12/17/2022, 5:27 PMMichael Paus
12/17/2022, 6:07 PMAlexander Maryanovsky
12/19/2022, 4:12 PM