-
-
Save vargasgabriel/cd07e32c17d5517f856c2f536951b46c to your computer and use it in GitHub Desktop.
Revisions
-
vargasgabriel revised this gist
May 20, 2021 . 1 changed file with 11 additions and 12 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,17 +1,16 @@ val password = MutableLiveData<String>() val passwordValidator = LiveDataValidator<String>(password).apply{ addRule("password is required") { it.isNullOrBlank() } addRule("password length must be 7") { it?.length != 7 } } val isFormValid = MediatorLiveData<Boolean>().apply { value = false addSource(username) { value = validateForm() } } private fun validateForm() : Boolean { val validators = listOf(passwordValidator) val validatorResolver = LiveDataValidatorResolver(validators) return validatorResolver.isValid() } -
vargasgabriel revised this gist
May 20, 2021 . 1 changed file with 10 additions and 6 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,20 +1,24 @@ // https://oozou.com/blog/modern-android-form-validations-with-data-binding-147 typealias Predicate<T> = (value: T?) -> Boolean class LiveDataValidator<T>(private val liveData: LiveData<T>){ private val validationRules = mutableListOf<Predicate<T>>() private val errorMessages = mutableListOf<String>() var error = MutableLiveData<Message?>() fun addRule(errorMsg: String, predicate: Predicate<T>) { validationRules.add(predicate) errorMessages.add(errorMsg) } private fun emitErrorMessage(messageRes: String?) { error.value = messageRes } fun isValid(): Boolean { for(i in 0 until validationRules.size){ if(validationRules[i](liveData.value) ){ emitErrorMessage(errorMessages[i]) return false -
vargasgabriel revised this gist
May 19, 2021 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -15,7 +15,7 @@ class LiveDataValidator<T>(private val liveData: LiveData<T>){ fun isValid(): Boolean { for( i in 0 until validationRules.size){ if(validationRules[i](liveData.value) ){ emitErrorMessage(errorMessages[i]) return false } -
carlosezam created this gist
Mar 30, 2021 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,27 @@ class LiveDataValidator<T>(private val liveData: LiveData<T>){ private val validationRules = mutableListOf< (T?) -> Boolean >() private val errorMessages = mutableListOf< Message >() var error = MutableLiveData<Message?>() fun addRule(errorMsg: Message, predicate: (T?) -> Boolean ){ validationRules.add(predicate) errorMessages.add(errorMsg) } private fun emitErrorMessage(message: Message?){ error.value = message } fun isValid(): Boolean { for( i in 0 until validationRules.size){ if( ! validationRules[i](liveData.value) ){ emitErrorMessage(errorMessages[i]) return false } } emitErrorMessage(null) return true } } 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,3 @@ class LiveDataValidatorResolver(private val validators: List<LiveDataValidator<*>>){ fun isValid() : Boolean = validators.all { it.isValid() } } 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,17 @@ val biometric = MutableLiveData<Agreement>() val biometricValidator = LiveDataValidator<Agreement>( biometric ).apply{ addRule( messageOf("must acept or decline") ){ it == null } addRule( messageOf( "must accept or devline")){ it == Agreement.Agreed } } val isAgreementFormValid = MediatorLiveData<Boolean>() init { isAgreementFormValid.value = false isAgreementFormValid.addSource(biometric){ validateForm() } } fun validateForm(){ val validators = listOf( biometricValidator ) isAgreementFormValid.value = validators.all { it.isValid() } }