thiennguyen0196 / survey

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Improvement] Extract email validation for common usage

hoangnguyen92dn opened this issue · comments

commented

Issue

There is the same logic to validate an email in ForgotViewModel and LoginViewModel so I'd suggest extracting this to a function for common usage.

fun resetPassword(email: String) {
when {
email.isBlank() || !Patterns.EMAIL_ADDRESS.matcher(email).matches() -> {
onResetPasswordChange.postValue(ResetPasswordState.EmailInvalid)
}

fun submitLogin(email: String, password: String) {
when {
email.isBlank() || !Patterns.EMAIL_ADDRESS.matcher(email).matches() -> {
onLoginStateChanged.postValue(LoginState.EmailInvalid)
}

Expected

The email validation function will be created and invoked in the ViewModel classes.

This is fixed in PR #22

commented

@thiennguyen0196 For this issue, I am not sure about your idea by using Inline class rather than extension 🤔 Can you please explain in detail why you would go with the current approach instead?

@hoangnguyen92dn
Indeed using extension was my first thought to extract the common usage function. However, in this case, I also want to distinguish between email text and password text so they cannot be passed wrongly in the arguments of viewModel.submitLogin() function. For example, if they are still string type, there is a chance that we could pass password text for email field and vice versa.

To sum up, I use inline class to have more strict usage in passing arguments with viewModel.submitLogin() function 😄

commented

Got your idea now. Thanks for your explanation 👍