hi, I have this code in my LoginViewModel <https:/...
# android-architecture
o
hi, I have this code in my LoginViewModel https://hastebin.com/ujihufovil.kt and this code in my SignUpViewModel https://hastebin.com/uqilarefif.kt the functionality is duplicated and I’m not sure how to go about making them reusable and minimize the code as much as possible, I do know that validating the pattern checking for empty string and reacting to the results by setting the error message is doing too many things at once and that they should be separated but when I think of separating them I think then of having the same validateEmail (vaildatePassword) methods contain the new functions but still doing the 3 things at once, but now using functions ..how do I make this easier and not so jumbled?
s
don't use images to share code. It makes it unnecessarily difficult to read
please use a paste service like Pastebin or something like that to share code
o
sorry coming right up
s
Thank you. What you would want to do is make something that only does the validating. Split up deciding if something is valid, and then acting on that information
o
fine I can easily have this for validating email
Copy code
class IsEmailValid {
    operator fun invoke(email: String) = PatternsCompat.EMAIL_ADDRESS.matcher(email).matches()
}
but would I then go back to that same validateEmail function and replace
PatternsCompat...
with IsEmailValid(email), is that all the efficiency i gain
actually let me rework the function first and do it 1 by 1
s
you wouldn't necessarily need
validateEmail
to be it's own function there
o
yea I give up 😕
r
Assuming they are using the same activity you can use a sharedViewModel. Or you can extract to logic out into a delegate