Created
November 22, 2018 13:12
-
-
Save bloderxd/078141bdccce0ddcfc6b94aa05185f7d to your computer and use it in GitHub Desktop.
This file contains hidden or 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 ContextValidation(private val validator: FormValidator) : DefaultValidation(validator) { | |
fun whenBrazil(conditions: BrazilValidations.() -> Unit) { | |
if (isBrazilApp()) BrazilValidations(validator).conditions() | |
} | |
fun whenColombia(conditions: ColombiaValidations.() -> Unit) { | |
if (isColombiaApp()) ColombiaValidations(validator).conditions() | |
} | |
fun whenMexico(conditions: MexicoValidations.() -> Unit) { | |
if (isMexicoApp()) MexicoValidations(validator).conditions() | |
} | |
} |
This file contains hidden or 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
private typealias Validated = Boolean | |
private typealias Condition = (String) -> Validated | |
class FormValidator(private val view: View) { | |
private val conditions: MutableMap<EditText, Validated> = mutableMapOf() | |
private val textWatchers: MutableMap<EditText, TextWatcher> = mutableMapOf() | |
fun buildValidation(editText: EditText, condition: Condition) : EditText { | |
conditions[editText] = condition(editText.text.toString()) | |
editText.removeTextChangedListener(textWatchers[editText]) | |
textWatchers[editText] = TextChangedWatcher { | |
conditions[editText] = condition(it) | |
checkIfViewCanBeEnable() | |
} | |
editText.addTextChangedListener(textWatchers[editText]) | |
return editText | |
} | |
fun checkIfViewCanBeEnable() { | |
view.isEnabled = conditions.filter { !it.value }.isEmpty() | |
} | |
} | |
fun View.enableWhen(conditions: ContextValidation.() -> Unit) = FormValidator(this).also { | |
ContextValidation(it).conditions() | |
it.checkIfViewCanBeEnable() | |
} |
This file contains hidden or 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
private object MaskDsl { | |
private val textWatchers: MutableMap<EditText, TextWatcher> = mutableMapOf() | |
fun injectMask(editText: EditText, textWatcher: TextWatcher) { | |
editText.removeTextChangedListener(textWatchers[editText]) | |
textWatcher.let { | |
textWatchers[editText] = it | |
editText.addTextChangedListener(it) | |
} | |
} | |
} | |
infix fun EditText.withMask(mask: String) = if (mask.isNotEmpty() && mask.isNotBlank()) { | |
MaskDsl.injectMask(this, NumberEditTextMask(this, mask)) | |
} else {} | |
infix fun EditText.withMaskSequence(masks: MutableList<String>) = MaskDsl.injectMask(this, NumberEditTextMask(this, masks.flatMap { listOf( | |
NumberEditTextMask.Mask(it, it.filter { char -> char == '#' }.length)) | |
})) | |
infix fun String.then(mask: String) : MutableList<String> = mutableListOf(this, mask) | |
infix fun MutableList<String>.then(mask: String) : MutableList<String> = this.also { it.add(mask) } |
This file contains hidden or 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
open class DefaultValidation(private val validator: FormValidator) { | |
fun EditText.isFollowingRegex(pattern: String) : EditText = validator.buildValidation(this) { if (pattern.isNotEmpty() && pattern.isNotBlank()) { | |
it.matches(Regex(pattern)) | |
} else { | |
it.isNotEmpty() && it.isNotBlank() | |
}} | |
fun EditText.isEmail() : EditText = validator.buildValidation(this) { | |
it.matches(Regex("^([_a-zA-Z0-9-]+(\\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\\.[a-zA-Z0-9-]+)*(\\.[a-zA-Z]{1,6}))?$")) | |
} | |
fun EditText.isFilled() : EditText = validator.buildValidation(this) { it.isNotEmpty() } | |
} | |
class BrazilValidations(private val validator: FormValidator) : DefaultValidation(validator) { | |
fun EditText.isPhone() : EditText = validator.buildValidation(this) { | |
it.matches(Regex(".((10)|([1-9][1-9]).)\\s9?[6-9][0-9]{3}-[0-9]{4}")) || | |
it.matches(Regex(".((10)|([1-9][1-9]).)\\s[2-5][0-9]{3}-[0-9]{4}")) | |
} | |
fun EditText.isCpfOrCnpj() : EditText = validator.buildValidation(this) { | |
it.matches(Regex("(^\\d{3}\\x2E\\d{3}\\x2E\\d{3}\\x2D\\d{2}\$)")) || | |
it.matches(Regex("(^\\d{2}.\\d{3}.\\d{3}/\\d{4}-\\d{2}\$)")) | |
}.also { it withMaskSequence("###.###.###-##" then "##.###.###/####-##") } | |
} | |
class ColombiaValidations(validator: FormValidator) : DefaultValidation(validator) | |
class MexicoValidations(validator: FormValidator) : DefaultValidation(validator) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment