From 1eb3a07f34897a8eb5007a479ad3033e11d5cf99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mustafa=20Yi=C4=9Fit?= Date: Sun, 5 Jun 2022 11:42:31 +0300 Subject: [PATCH] add NotifyType.kt to rules and change example to form ui --- .idea/misc.xml | 2 +- app/build.gradle | 4 +- .../mustafayigit/validatable/CustomRule.kt | 19 +++ .../mustafayigit/validatable/MainActivity.kt | 150 ++++++++++++++---- app/src/main/res/layout/activity_main.xml | 53 +++++-- .../mustafayigit/validator/IValidatable.kt | 9 +- .../mustafayigit/validator/ValidatableRule.kt | 20 ++- .../com/mustafayigit/validator/Validator.kt | 60 ++++--- .../validator/base/BaseValidatableRule.kt | 1 + .../mustafayigit/validator/base/NotifyType.kt | 12 ++ .../mustafayigit/validator/util/Constants.kt | 3 - 11 files changed, 249 insertions(+), 84 deletions(-) create mode 100644 app/src/main/java/com/mustafayigit/validatable/CustomRule.kt create mode 100644 validator/src/main/java/com/mustafayigit/validator/base/NotifyType.kt diff --git a/.idea/misc.xml b/.idea/misc.xml index e572918..a280b31 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -3,7 +3,7 @@ diff --git a/app/build.gradle b/app/build.gradle index a06b7ab..3eec8bf 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -39,8 +39,8 @@ dependencies { implementation project(path: ':validator') implementation 'androidx.core:core-ktx:1.7.0' - implementation 'androidx.appcompat:appcompat:1.4.1' - implementation 'com.google.android.material:material:1.6.0' + implementation 'androidx.appcompat:appcompat:1.4.2' + implementation 'com.google.android.material:material:1.6.1' implementation 'androidx.constraintlayout:constraintlayout:2.1.4' testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.3' diff --git a/app/src/main/java/com/mustafayigit/validatable/CustomRule.kt b/app/src/main/java/com/mustafayigit/validatable/CustomRule.kt new file mode 100644 index 0000000..56507d7 --- /dev/null +++ b/app/src/main/java/com/mustafayigit/validatable/CustomRule.kt @@ -0,0 +1,19 @@ +package com.mustafayigit.validatable + +import com.mustafayigit.validator.base.BaseValidatableRule +import com.mustafayigit.validator.base.NotifyType + + +/** + * Created by Mustafa Yiğit on 05/06/2022 + * mustafa.yt65@gmail.com + */ +class PasswordConfirmRule( + override val errorMessage: String, + override val notifyType: NotifyType, + val originalInputGetter: () -> String +) : BaseValidatableRule( + errorMessage = errorMessage, + notifyType = notifyType, + rule = { it == originalInputGetter.invoke() } +) diff --git a/app/src/main/java/com/mustafayigit/validatable/MainActivity.kt b/app/src/main/java/com/mustafayigit/validatable/MainActivity.kt index 0f2413d..9130578 100644 --- a/app/src/main/java/com/mustafayigit/validatable/MainActivity.kt +++ b/app/src/main/java/com/mustafayigit/validatable/MainActivity.kt @@ -2,11 +2,13 @@ package com.mustafayigit.validatable import android.os.Bundle import android.util.Log +import android.widget.Toast import androidx.appcompat.app.AppCompatActivity import androidx.core.widget.doOnTextChanged import com.mustafayigit.validatable.databinding.ActivityMainBinding import com.mustafayigit.validator.ValidatableRule import com.mustafayigit.validator.Validator +import com.mustafayigit.validator.base.NotifyType class MainActivity : AppCompatActivity() { private val mBinding: ActivityMainBinding by lazy { @@ -17,52 +19,130 @@ class MainActivity : AppCompatActivity() { super.onCreate(savedInstanceState) setContentView(mBinding.root) - /* Init Validator with Default Constructor */ - val validator = Validator( - onValidation = { isValid, errorMessage -> - Log.d("Validator", "isValid: $isValid, errorMessage: $errorMessage") - mBinding.btnAction.isEnabled = isValid + val emailValidator = Validator.Builder() + .addRules( + ValidatableRule.Email("Enter valid mail address"), + ValidatableRule.Required( + "Email is required", NotifyType.ON_FOCUS_CHANGE, + ), + ValidatableRule.Email( + "Enter valid mail address", NotifyType.ON_FOCUS_CHANGE, + ), + ValidatableRule.Required( + "Email is required", NotifyType.ON_FORM_SUBMIT, + ), + ValidatableRule.Email( + "Enter valid mail address", NotifyType.ON_FORM_SUBMIT, + ), + ) + .addCollector { + mBinding.txtInputEmail.editText?.doOnTextChanged { text, _, _, _ -> + it.input = text.toString() + } + mBinding.txtInputEmail.editText?.setOnFocusChangeListener { _, isFocused -> + it.isFocused = isFocused + } } - ) - validator.addRules( - ValidatableRule.Email("Enter valid mail address"), - ValidatableRule.Required("Input is required"), - ) - mBinding.editText.doOnTextChanged { text, _, _, _ -> - validator.input = text.toString() - } - /* ------------------------------------------------- */ + .onValidate { isValid, errorMessage, notifyType -> + Log.d( + "Validator", + "notifyType: $notifyType, isValid: $isValid, errorMessage: $errorMessage" + ) + mBinding.txtInputEmail.error = errorMessage + } + .build() - /* Init Validator with Constructor */ - val validatorWithConstructor = Validator( - ValidatableRule.Email("Enter valid mail address"), - ValidatableRule.Required("Input is required"), - ) { isValid, errorMessage -> - Log.d("Validator", "isValid: $isValid, errorMessage: $errorMessage") - mBinding.btnAction.isEnabled = isValid - } - mBinding.editText.doOnTextChanged { text, _, _, _ -> - validatorWithConstructor.input = text.toString() - } - /* ------------------------------------------------- */ + val passwordValidator = Validator.Builder() + .addRules( + ValidatableRule.Password("Enter valid password"), + PasswordConfirmRule( + errorMessage = "Passwords does not match", + notifyType = NotifyType.ON_VALUE_CHANGE, + originalInputGetter = { mBinding.txtInputPasswordConfirm.editText?.text.toString() }, + ), + ValidatableRule.Required( + "Password is required", NotifyType.ON_FOCUS_CHANGE, + ), + ValidatableRule.Password( + "Enter valid password", NotifyType.ON_FOCUS_CHANGE, + ), + ValidatableRule.Required( + "Password is required", NotifyType.ON_FORM_SUBMIT, + ), + ValidatableRule.Password( + "Enter valid password", NotifyType.ON_FORM_SUBMIT, + ), + ) + .addCollector { + mBinding.txtInputPassword.editText?.doOnTextChanged { text, _, _, _ -> + it.input = text.toString() + } + mBinding.txtInputPassword.editText?.setOnFocusChangeListener { _, isFocused -> + it.isFocused = isFocused + } + } + .onValidate { isValid, errorMessage, notifyType -> + Log.d( + "Validator", + "notifyType: $notifyType, isValid: $isValid, errorMessage: $errorMessage" + ) + mBinding.txtInputPassword.error = errorMessage + } + .build() - /* Init Validator with Builder */ - val validatorWithBuilder = Validator.Builder() + val passwordConfirmValidator = Validator.Builder() .addRules( - ValidatableRule.Email("Enter valid mail address"), - ValidatableRule.Required("Input is required"), + PasswordConfirmRule( + errorMessage = "Passwords does not match", + notifyType = NotifyType.ON_VALUE_CHANGE, + originalInputGetter = { mBinding.txtInputPassword.editText?.text.toString() }, + ), + PasswordConfirmRule( + errorMessage = "Passwords does not match", + notifyType = NotifyType.ON_FORM_SUBMIT, + originalInputGetter = { mBinding.txtInputPassword.editText?.text.toString() }, + ), + ValidatableRule.Required( + "Password Confirm is required", NotifyType.ON_FOCUS_CHANGE, + ), + ValidatableRule.Required( + "Password Confirm is required", NotifyType.ON_FORM_SUBMIT, + ), ) .addCollector { - mBinding.editText.doOnTextChanged { text, _, _, _ -> + mBinding.txtInputPasswordConfirm.editText?.doOnTextChanged { text, _, _, _ -> it.input = text.toString() } + mBinding.txtInputPasswordConfirm.editText?.setOnFocusChangeListener { _, isFocused -> + it.isFocused = isFocused + } } - .onValidate { isValid, errorMessage -> - Log.d("Validator", "isValid: $isValid, errorMessage: $errorMessage") - mBinding.btnAction.isEnabled = isValid + .onValidate { isValid, errorMessage, notifyType -> + Log.d( + "Validator", + "notifyType: $notifyType, isValid: $isValid, errorMessage: $errorMessage" + ) + mBinding.txtInputPasswordConfirm.error = errorMessage } .build() - /* ------------------------------------------------- */ + + // set isFormSubmitted to true to validate on form submit + mBinding.btnRegister.setOnClickListener { + emailValidator.submitForm() + passwordValidator.submitForm() + passwordConfirmValidator.submitForm() + + if (emailValidator.submitForm().not() || passwordValidator.submitForm().not()) { + Toast.makeText(this, "Form is not valid", Toast.LENGTH_SHORT).show() + return@setOnClickListener + } + Toast.makeText( + this, + "Success", + Toast.LENGTH_SHORT + ).show() + + } } diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index ac18f80..acb93f9 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -9,22 +9,57 @@ android:paddingHorizontal="24dp" tools:context=".MainActivity"> - + android:hint="Email"> + + + + + + + + + + + + + + + +