Logan Knight
01/05/2021, 9:43 PMdata class
objects updated by the inputs. To use or not to use? Details will be in the thread.Logan Knight
01/05/2021, 9:43 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/05/2021, 9:46 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?Logan Knight
01/05/2021, 9:54 PMMainScope()
in a variable, but I am unsure if this would cause weird behavior. My gut tells me that I shouldn't do something like val scope = remember { MainScope() }
but I don't have a solid reason why and I have not yet seen an example of it being used in that way.Kirill Grouchnikov
01/05/2021, 9:55 PMLogan Knight
01/05/2021, 9:56 PMLogan Knight
01/05/2021, 9:56 PMLogan Knight
01/05/2021, 9:56 PMxetra11
01/05/2021, 11:58 PMLogan Knight
01/06/2021, 12:02 AMMainScope()
an example is given:
lass MyAndroidActivity {
private val scope = MainScope()
override fun onDestroy() {
super.onDestroy()
scope.cancel()
}
}
presumably to save having to call it every time. I'm open to correctionLogan Knight
01/06/2021, 12:03 AMMainScope()
is intended to be used in UI settings.xetra11
01/06/2021, 6:55 AMxetra11
01/06/2021, 6:55 AMxetra11
01/06/2021, 6:56 AMxetra11
01/06/2021, 6:57 AMLogan Knight
01/06/2021, 6:58 PM