Anum Amin
02/22/2024, 9:44 PMOutlineTextField
. To maintain each textfield’s own state, I am using custom TextFieldState as in JetSurvey. As a result, I had to declare 10 states to remember in composable which I tried to solve by moving them into UILogic entity [ Code in thread ], because I think it is related to UI, rather than business. Apart from these textfields, there are also states maintained for bottom sheet and selections.
What is the rule of thumb to keep state in composable vs view model? How can I avoid clutter composable with so many states? I found articles by Google guys suggesting to put textfield states in view model. Need suggestions.. Thank you.Anum Amin
02/22/2024, 9:45 PM@Composable
fun SignupScreenContent(state: SignupState) {
val uiState = rememberSignupUIState(
firstNameState = getState(field = FirstName),
lastNameState = getState(field = LastName),
phoneState = getState(field = Phone),
emailState = getState(field = Email) as EmailState,
passwordState = passwordState,
confirmPasswordState = getConfirmPasswordState(passwordState = passwordState),
streetState = getState(field = Street),
.....)
var loginBottomSheetState by remember { mutableStateOf(LoginBottomSheetState()) }
}
Anum Amin
02/22/2024, 9:46 PM@Composable
private fun getState(field: SignupField): TextFieldState {
val errorName = stringResource(id = R.string.error_name)
val errorPhone = stringResource(id = R.string.error_phone)
....
return when (field) {
FirstName, LastName -> TextFieldState(
validator = { it.length >= 3 },
errorMessage = { errorName })
Phone -> TextFieldState(validator = { it.length == 10 }, errorMessage = { errorPhone })
.......
}
ursus
02/22/2024, 10:46 PMAnum Amin
02/23/2024, 6:21 AMursus
02/23/2024, 8:41 AM