Fix 2 bugs in ValidatingEditTextPreference

This commit is contained in:
Bart Sopers 2020-09-03 19:02:07 +02:00
parent 0a68e8111f
commit 808927ee55
2 changed files with 68 additions and 34 deletions

View file

@ -1,7 +1,6 @@
package info.nightscout.androidaps.utils.textValidator package info.nightscout.androidaps.utils.textValidator
import android.content.Context import android.content.Context
import android.content.res.TypedArray
import android.text.Editable import android.text.Editable
import android.text.TextUtils import android.text.TextUtils
import android.text.TextWatcher import android.text.TextWatcher
@ -37,25 +36,20 @@ class DefaultEditTextValidator : EditTextValidator {
resetValidators(context) resetValidators(context)
} }
constructor(editTextView: EditText, typedArray: TypedArray, context: Context) { constructor(editTextView: EditText, parameters: Parameters, context: Context) {
emptyAllowed = typedArray.getBoolean(R.styleable.FormEditText_emptyAllowed, false) emptyAllowed = parameters.emptyAllowed
testType = typedArray.getInt(R.styleable.FormEditText_testType, EditTextValidator.TEST_NOCHECK) testType = parameters.testType
testErrorString = typedArray.getString(R.styleable.FormEditText_testErrorString) testErrorString = parameters.testErrorString
classType = typedArray.getString(R.styleable.FormEditText_classType) classType = parameters.classType
customRegexp = typedArray.getString(R.styleable.FormEditText_customRegexp) customRegexp = parameters.customRegexp
emptyErrorStringDef = typedArray.getString(R.styleable.FormEditText_emptyErrorString) emptyErrorStringDef = parameters.emptyErrorStringDef
customFormat = typedArray.getString(R.styleable.FormEditText_customFormat) customFormat = parameters.customFormat
if (testType == EditTextValidator.TEST_MIN_LENGTH) minLength = parameters.minLength
minLength = typedArray.getInt(R.styleable.FormEditText_minLength, 0) minNumber = parameters.minNumber
if (testType == EditTextValidator.TEST_NUMERIC_RANGE) { maxNumber = parameters.maxNumber
minNumber = typedArray.getInt(R.styleable.FormEditText_minNumber, Int.MIN_VALUE) floatminNumber = parameters.floatminNumber
maxNumber = typedArray.getInt(R.styleable.FormEditText_maxNumber, Int.MAX_VALUE) floatmaxNumber = parameters.floatmaxNumber
}
if (testType == EditTextValidator.TEST_FLOAT_NUMERIC_RANGE) {
floatminNumber = typedArray.getFloat(R.styleable.FormEditText_floatminNumber, Float.MIN_VALUE)
floatmaxNumber = typedArray.getFloat(R.styleable.FormEditText_floatmaxNumber, Float.MAX_VALUE)
}
typedArray.recycle()
setEditText(editTextView) setEditText(editTextView)
resetValidators(context) resetValidators(context)
} }
@ -168,7 +162,7 @@ class DefaultEditTextValidator : EditTextValidator {
} }
@Suppress("unused") @Suppress("unused")
fun setClassType(classType: String?, testErrorString: String?, context: Context) : DefaultEditTextValidator{ fun setClassType(classType: String?, testErrorString: String?, context: Context): DefaultEditTextValidator {
testType = EditTextValidator.TEST_CUSTOM testType = EditTextValidator.TEST_CUSTOM
this.classType = classType this.classType = classType
this.testErrorString = testErrorString this.testErrorString = testErrorString
@ -177,7 +171,7 @@ class DefaultEditTextValidator : EditTextValidator {
} }
@Suppress("unused") @Suppress("unused")
fun setCustomRegexp(customRegexp: String?, context: Context) : DefaultEditTextValidator { fun setCustomRegexp(customRegexp: String?, context: Context): DefaultEditTextValidator {
testType = EditTextValidator.TEST_REGEXP testType = EditTextValidator.TEST_REGEXP
this.customRegexp = customRegexp this.customRegexp = customRegexp
resetValidators(context) resetValidators(context)
@ -185,13 +179,13 @@ class DefaultEditTextValidator : EditTextValidator {
} }
@Suppress("unused") @Suppress("unused")
fun setEmptyAllowed(emptyAllowed: Boolean, context: Context) : DefaultEditTextValidator { fun setEmptyAllowed(emptyAllowed: Boolean, context: Context): DefaultEditTextValidator {
this.emptyAllowed = emptyAllowed this.emptyAllowed = emptyAllowed
resetValidators(context) resetValidators(context)
return this return this
} }
fun setEmptyErrorString(emptyErrorString: String?) : DefaultEditTextValidator { fun setEmptyErrorString(emptyErrorString: String?): DefaultEditTextValidator {
emptyErrorStringActual = if (!TextUtils.isEmpty(emptyErrorString)) { emptyErrorStringActual = if (!TextUtils.isEmpty(emptyErrorString)) {
emptyErrorString emptyErrorString
} else { } else {
@ -201,14 +195,14 @@ class DefaultEditTextValidator : EditTextValidator {
} }
@Suppress("unused") @Suppress("unused")
fun setTestErrorString(testErrorString: String?, context: Context) : DefaultEditTextValidator { fun setTestErrorString(testErrorString: String?, context: Context): DefaultEditTextValidator {
this.testErrorString = testErrorString this.testErrorString = testErrorString
resetValidators(context) resetValidators(context)
return this return this
} }
@Suppress("unused") @Suppress("unused")
fun setTestType(testType: Int, context: Context) : DefaultEditTextValidator { fun setTestType(testType: Int, context: Context): DefaultEditTextValidator {
this.testType = testType this.testType = testType
resetValidators(context) resetValidators(context)
return this return this
@ -248,4 +242,19 @@ class DefaultEditTextValidator : EditTextValidator {
} catch (e: Throwable) { } catch (e: Throwable) {
!TextUtils.isEmpty(editTextView.error) !TextUtils.isEmpty(editTextView.error)
} }
class Parameters {
var testErrorString: String? = null
var emptyAllowed = false
var testType: Int = EditTextValidator.TEST_NOCHECK
var classType: String? = null
var customRegexp: String? = null
var customFormat: String? = null
var emptyErrorStringDef: String? = null
var minLength = 0
var minNumber = 0
var maxNumber = 0
var floatminNumber = 0f
var floatmaxNumber = 0f
}
} }

View file

@ -1,22 +1,26 @@
package info.nightscout.androidaps.utils.textValidator package info.nightscout.androidaps.utils.textValidator
import android.annotation.SuppressLint
import android.content.Context import android.content.Context
import android.util.AttributeSet import android.util.AttributeSet
import androidx.preference.EditTextPreference import androidx.preference.EditTextPreference
import androidx.preference.EditTextPreference.OnBindEditTextListener
import androidx.preference.PreferenceViewHolder import androidx.preference.PreferenceViewHolder
import info.nightscout.androidaps.core.R import info.nightscout.androidaps.core.R
class ValidatingEditTextPreference(ctx: Context, attrs: AttributeSet, defStyleAttr: Int, defStyleRes: Int) class ValidatingEditTextPreference(ctx: Context, attrs: AttributeSet, defStyleAttr: Int, defStyleRes: Int)
: EditTextPreference(ctx, attrs, defStyleAttr, defStyleRes) { : EditTextPreference(ctx, attrs, defStyleAttr, defStyleRes) {
private lateinit var validatorParameters: DefaultEditTextValidator.Parameters
private var validator: DefaultEditTextValidator? = null
init { init {
val typedArray = context.obtainStyledAttributes(attrs, R.styleable.FormEditText, 0, 0) obtainValidatorParameters(attrs)
val onBindEditTextListener = OnBindEditTextListener { editText ->
editTextValidator = DefaultEditTextValidator(editText, typedArray, context) setOnBindEditTextListener { editText ->
validator = DefaultEditTextValidator(editText, validatorParameters, context)
}
setOnPreferenceChangeListener { preference, newValue ->
validator?.testValidity(false) ?: true
} }
setOnBindEditTextListener(onBindEditTextListener)
} }
constructor(ctx: Context, attrs: AttributeSet, defStyle: Int) constructor(ctx: Context, attrs: AttributeSet, defStyle: Int)
@ -25,11 +29,32 @@ class ValidatingEditTextPreference(ctx: Context, attrs: AttributeSet, defStyleAt
constructor(ctx: Context, attrs: AttributeSet) constructor(ctx: Context, attrs: AttributeSet)
: this(ctx, attrs, R.attr.editTextPreferenceStyle) : this(ctx, attrs, R.attr.editTextPreferenceStyle)
private lateinit var editTextValidator: EditTextValidator
override fun onBindViewHolder(holder: PreferenceViewHolder?) { override fun onBindViewHolder(holder: PreferenceViewHolder?) {
super.onBindViewHolder(holder) super.onBindViewHolder(holder)
holder?.isDividerAllowedAbove = false holder?.isDividerAllowedAbove = false
holder?.isDividerAllowedBelow = false holder?.isDividerAllowedBelow = false
} }
private fun obtainValidatorParameters(attrs: AttributeSet) {
val typedArray = context.obtainStyledAttributes(attrs, R.styleable.FormEditText, 0, 0)
validatorParameters = DefaultEditTextValidator.Parameters()
validatorParameters.emptyAllowed = typedArray.getBoolean(R.styleable.FormEditText_emptyAllowed, false)
validatorParameters.testType = typedArray.getInt(R.styleable.FormEditText_testType, EditTextValidator.TEST_NOCHECK)
validatorParameters.testErrorString = typedArray.getString(R.styleable.FormEditText_testErrorString)
validatorParameters.classType = typedArray.getString(R.styleable.FormEditText_classType)
validatorParameters.customRegexp = typedArray.getString(R.styleable.FormEditText_customRegexp)
validatorParameters.emptyErrorStringDef = typedArray.getString(R.styleable.FormEditText_emptyErrorString)
validatorParameters.customFormat = typedArray.getString(R.styleable.FormEditText_customFormat)
if (validatorParameters.testType == EditTextValidator.TEST_MIN_LENGTH)
validatorParameters.minLength = typedArray.getInt(R.styleable.FormEditText_minLength, 0)
if (validatorParameters.testType == EditTextValidator.TEST_NUMERIC_RANGE) {
validatorParameters.minNumber = typedArray.getInt(R.styleable.FormEditText_minNumber, Int.MIN_VALUE)
validatorParameters.maxNumber = typedArray.getInt(R.styleable.FormEditText_maxNumber, Int.MAX_VALUE)
}
if (validatorParameters.testType == EditTextValidator.TEST_FLOAT_NUMERIC_RANGE) {
validatorParameters.floatminNumber = typedArray.getFloat(R.styleable.FormEditText_floatminNumber, Float.MIN_VALUE)
validatorParameters.floatmaxNumber = typedArray.getFloat(R.styleable.FormEditText_floatmaxNumber, Float.MAX_VALUE)
}
typedArray.recycle()
}
} }