Logan Knight
01/06/2021, 7:36 PMLogan Knight
01/06/2021, 7:37 PMclass UserFormViewModel {
val userRepository = UserRepository()
var userData: UserData? by mutableStateOf(null)
}
@Composable
fun userForm(
modifier: Modifier = Modifier,
) {
val vm = remember { UserFormViewModel() }
onActive {
MainScope().launch {
vm.userData = vm.userRepository.createNew()
}
}
Column(
modifier = modifier.padding(16.dp)
.border(BorderStroke(2.dp, Color.LightGray)),
) {
formRow(above = { Text("First Name: ") }) {
TextField(
value = vm.userData?.firstName ?: "",
onValueChange = { vm.userData = vm.userData?.copy(firstName = it) }
)
}
formRow(above = { Text("Last Name: ") }) {
TextField(
value = vm.userData?.lastName ?: "",
onValueChange = { vm.userData = vm.userData?.copy(lastName = it) }
)
}
formRow {
Button(
onClick = {
MainScope().launch {
vm.userData?.let { vm.userRepository.update(it) }
}
}
) {
Text("Save")
}
}
}
}
Logan Knight
01/06/2021, 7:37 PMuserData
. I would prefer this to not happen. I know I can break it out into individual fields in the UserFormViewModel
class but then would need to manually create the userData class for repository use. Is there a method to use a data class without having to replace the object on input like the above?Dominaezzz
01/06/2021, 9:59 PMLogan Knight
01/06/2021, 10:05 PMLogan Knight
01/06/2021, 10:07 PMDominaezzz
01/06/2021, 10:25 PMLogan Knight
01/06/2021, 11:06 PMDominaezzz
01/07/2021, 12:12 AMAlexandre Elias [G]
02/11/2021, 8:01 AMAlexandre Elias [G]
02/11/2021, 8:04 AMAlexandre Elias [G]
02/11/2021, 8:05 AMAlexandre Elias [G]
02/11/2021, 8:08 AMUserFormViewModel
class". it should also work to pull out a copy of individual fields in a calling composable and pass them as a parameter to a callee