Last active
May 28, 2024 11:28
-
-
Save qamarelsafadi/80e1e8578c3c29cc15c03e598a04e00c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum class ValidationType { | |
Text, Phone, Email, Password | |
} | |
data class InputWrapper( | |
var inputValue: MutableState<String> = mutableStateOf(""), | |
var borderColor: String = Gray, | |
val validationType: ValidationType? = ValidationType.Text | |
) { | |
var validationMessageResourceId: Int = R.string.empty_lbl | |
fun onValueChange(input: String) { | |
inputValue.value = input | |
validationMessageResourceId = when (validationType) { | |
ValidationType.Email -> input.validateEmail() | |
ValidationType.Phone -> input.validatePhone() | |
else -> input.validateRequired() | |
} | |
borderColor = if (isValid) { | |
Gray // make border red if input is not valid | |
} else { | |
Red // make border gray if input is valid | |
} | |
} | |
val isValid: Boolean | |
get() = validationMessageResourceId == R.string.empty_lbl && inputValue.value.isNotEmpty() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class FormState() { | |
val mobile: InputWrapper = InputWrapper( | |
validationType = ValidationType.Phone | |
), | |
val name: InputWrapper = InputWrapper( | |
validationType = ValidationType.Text | |
), | |
val email: InputWrapper = InputWrapper( | |
validationType = ValidationType.Email | |
), | |
val password: InputWrapper = InputWrapper( | |
validationType = ValidationType.Password | |
) | |
val isSendEnabled = name.isValid && mobile.isValid && email.isValid && message.isValid | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// in viewmodel | |
val contactFormState = FormState() | |
// in your screen | |
@Composable | |
private fun ContactContent( | |
contactFormState: ContactFormState, | |
isLoading: Boolean = false, | |
onSaveUpdatesClick: () -> Unit, | |
) { | |
Column( | |
Modifier | |
.fillMaxSize() | |
.background(WalletBg) | |
.verticalScroll(rememberScrollState()) | |
.padding(horizontal = 24.dp), | |
horizontalAlignment = Alignment.CenterHorizontally | |
) { | |
Text( | |
text = stringResource(R.string.welcom), | |
style = lightWithoutLineStyle, | |
fontSize = 25.sp | |
) | |
InputText( | |
text = contactFormState.name, | |
onValueChange = contactFormState.name::onValueChange, | |
hint = stringResource(id = R.string.full_name), | |
) | |
Spacer(modifier = Modifier.height(15.dp)) | |
InputText( | |
text = contactFormState.mobile, | |
onValueChange = contactFormState.mobile::onValueChange, | |
hint = stringResource(id = R.string.mobile), | |
inputType = KeyboardType.Phone, | |
) | |
Spacer(modifier = Modifier.height(15.dp)) | |
InputText( | |
text = contactFormState.email, | |
onValueChange = contactFormState.email::onValueChange, | |
hint = stringResource(id = R.string.email), | |
inputType = KeyboardType.Email, | |
) | |
Spacer(modifier = Modifier.height(15.dp)) | |
Button( | |
onClick = onSaveUpdatesClick, title = stringResource(R.string.send_message), | |
hasBoarder = false, | |
modifier = Modifier.fillMaxWidth( | |
), | |
isEnabled = viewModel.contactFormState.isSendEnabled, | |
isLoading = isLoading | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment