Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

P2022-1557 Common validation #41

Merged
merged 6 commits into from
Mar 25, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
P2022-1557 Common validation (not working):
- creates new class Validation.kt
- changes id of EditTexts in Sign In and Sign Up (there were the same id)
  • Loading branch information
AdrianPat committed Mar 13, 2022
commit 0f73340246685423fef9baaddd374334bd81e17f
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,8 @@ import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.EditText
import com.intive.patronage22.intivi.MainActivity
import com.intive.patronage22.intivi.R
import com.intive.patronage22.intivi.databinding.FragmentSignInBinding
import java.util.regex.Pattern

class SignInFragment : Fragment() {

Expand All @@ -21,44 +18,13 @@ class SignInFragment : Fragment() {
val bind = FragmentSignInBinding.inflate(layoutInflater)

bind.signInButton.setOnClickListener {
if (isLoginFormValid()) {
if (isValid()) {
val intent = Intent(this.requireContext(), MainActivity::class.java)
startActivity(intent)
}
}
return bind.root
}

private fun isEmailOK(): Boolean {
val editText = requireView().findViewById<EditText>(R.id.loginEditText)
val email = editText.text.toString()
val localPartLength = email.split("@").first().length
val domainPartLength = email.split("@").last().length
if (!isEmailValid(email) || localPartLength > 64 || domainPartLength > 255) {
editText.error = getString(R.string.emailValidMessage)
return false
} else {
return true
}
}

private fun isEmailValid(email: String): Boolean {
return Pattern.compile(
"""(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9]))\.){3}(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9])|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])"""
).matcher(email).matches()
}

private fun isPasswordOK(): Boolean {
val editText = requireView().findViewById<EditText>(R.id.passwordEditText)
if (editText.text.length !in (5..40) || (editText.text.toString()
.filterNot { it.isWhitespace() }.length != editText.text.length)
) {
editText.error = getString(R.string.passwordValidMessage)
return false
} else {
return true
}
}

private fun isLoginFormValid() = isEmailOK() and isPasswordOK()
private fun isValid() = Validation().isLoginFormValid()
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,8 @@ import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.EditText
import com.intive.patronage22.intivi.MainActivity
import com.intive.patronage22.intivi.R
import com.intive.patronage22.intivi.databinding.FragmentSignUpBinding
import java.util.regex.Pattern

class SignUpFragment : Fragment() {

Expand All @@ -29,47 +26,5 @@ class SignUpFragment : Fragment() {
return bind.root
}

private fun isEmailOK(): Boolean {
val editText = requireView().findViewById<EditText>(R.id.loginEditText)
val email = editText.text.toString()
val localPartLength = email.split("@").first().length
val domainPartLength = email.split("@").last().length
if (!isEmailValid(email) || localPartLength > 64 || domainPartLength > 255) {
editText.error = getString(R.string.emailValidMessage)
return false
} else {
return true
}
}

private fun isEmailValid(email: String): Boolean {
return Pattern.compile(
"""(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9]))\.){3}(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9])|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])"""
).matcher(email).matches()
}

private fun isPasswordOK(): Boolean {
val editText = requireView().findViewById<EditText>(R.id.passwordEditText)
if (editText.text.length !in (5..40) || (editText.text.toString()
.filterNot { it.isWhitespace() }.length != editText.text.length)
) {
editText.error = getString(R.string.passwordValidMessage)
return false
} else {
return true
}
}

private fun isRepeatPasswordOK(): Boolean {
val password = requireView().findViewById<EditText>(R.id.passwordEditText)
val repeatPassword = requireView().findViewById<EditText>(R.id.repeatPassword)
if (password.text.toString() != repeatPassword.text.toString()) {
repeatPassword.error = getString(R.string.repeatPassValidMessage)
return false
} else {
return true
}
}

private fun isValid() = isEmailOK() and isPasswordOK() and isRepeatPasswordOK()
private fun isValid() = Validation().isRegisterFormValid()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.intive.patronage22.intivi.Fragments

import android.widget.EditText
import androidx.fragment.app.Fragment
import com.intive.patronage22.intivi.R
import java.util.regex.Pattern

class Validation : Fragment() {

fun isLoginFormValid() =
isEmailOK(R.id.editTextLoginEmail) and isPasswordOK(R.id.editTextLoginPassword)

fun isRegisterFormValid() =
isEmailOK(R.id.editTextRegisterEmail) and isPasswordOK(R.id.editTextRegisterPassword) and isRepeatPasswordOK()

private fun isEmailOK(emailTextId: Int): Boolean {
val editText = requireView().findViewById<EditText>(emailTextId)
val email = editText.text.toString()
val localPartLength = email.split("@").first().length
val domainPartLength = email.split("@").last().length
if (!isEmailValid(email) || localPartLength > 64 || domainPartLength > 255) {
editText.error = getString(R.string.emailValidMessage)
return false
} else {
return true
}
}

private fun isEmailValid(email: String): Boolean {
return Pattern.compile(
"""(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9]))\.){3}(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9])|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])"""
).matcher(email).matches()
}

private fun isPasswordOK(passwordTextId: Int): Boolean {
val editText = requireView().findViewById<EditText>(passwordTextId)
if (editText.text.length !in (5..40) || (editText.text.toString()
.filterNot { it.isWhitespace() }.length != editText.text.length)
) {
editText.error = getString(R.string.passwordValidMessage)
return false
} else {
return true
}
}

private fun isRepeatPasswordOK(): Boolean {
val password = requireView().findViewById<EditText>(R.id.editTextRegisterPassword)
val repeatPassword = requireView().findViewById<EditText>(R.id.editTextRegisterRepeatPassword)
if (password.text.toString() != repeatPassword.text.toString()) {
repeatPassword.error = getString(R.string.repeatPassValidMessage)
return false
} else {
return true
}
}
}
10 changes: 5 additions & 5 deletions app/src/main/res/layout/fragment_sign_in.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@
tools:layout_editor_absoluteY="-134dp">

<EditText
android:id="@+id/loginEditText"
android:id="@+id/editTextLoginEmail"
android:layout_marginHorizontal="@dimen/loginMarginHorizontal"
android:layout_marginTop="@dimen/loginEmailMarginTop"
android:hint = "@string/loginEditTextHint"
android:inputType="text"
android:inputType="textEmailAddress"
style="@style/loginEditText"/>

<EditText
android:id="@+id/passwordEditText"
android:layout_below="@id/loginEditText"
android:id="@+id/editTextLoginPassword"
android:layout_below="@id/editTextLoginEmail"
android:layout_marginHorizontal="@dimen/loginMarginHorizontal"
android:layout_marginTop="@dimen/loginPasswordMarginTop"
android:hint="@string/passwordEditTextHint"
Expand All @@ -33,7 +33,7 @@
android:id="@+id/forgotpassText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/passwordEditText"
android:layout_below="@+id/editTextLoginPassword"
android:layout_marginStart="@dimen/loginMarginHorizontal"
android:layout_marginTop="@dimen/loginPasswordTextMarginTop"
android:fontFamily="@font/inter"
Expand Down
12 changes: 6 additions & 6 deletions app/src/main/res/layout/fragment_sign_up.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,25 @@
tools:layout_editor_absoluteY="-134dp">

<EditText
android:id="@+id/loginEditText"
android:id="@+id/editTextRegisterEmail"
android:layout_marginHorizontal="@dimen/loginMarginHorizontal"
android:layout_marginTop="@dimen/loginEmailMarginTop"
android:hint = "@string/loginEditTextHint"
android:inputType="text"
android:inputType="textEmailAddress"
style="@style/loginEditText"/>

<EditText
android:id="@+id/passwordEditText"
android:layout_below="@id/loginEditText"
android:id="@+id/editTextRegisterPassword"
android:layout_below="@id/editTextRegisterEmail"
android:layout_marginHorizontal="@dimen/loginMarginHorizontal"
android:layout_marginTop="@dimen/loginPasswordMarginTop"
android:hint="@string/passwordEditTextHint"
android:inputType="textPassword"
style="@style/loginEditText"/>

<EditText
android:id="@+id/repeatPassword"
android:layout_below="@id/passwordEditText"
android:id="@+id/editTextRegisterRepeatPassword"
android:layout_below="@id/editTextRegisterPassword"
android:layout_marginHorizontal="@dimen/loginMarginHorizontal"
android:layout_marginTop="@dimen/loginPasswordMarginTop"
android:hint="@string/repeatPassEditTextHint"
Expand Down