refactoring idea Parameters

This commit is contained in:
Adrian 2020-09-06 01:23:00 +02:00 committed by Bart Sopers
parent 503fc9dbf9
commit cd1d0b3a58
2 changed files with 34 additions and 35 deletions

View file

@ -243,18 +243,18 @@ class DefaultEditTextValidator : EditTextValidator {
!TextUtils.isEmpty(editTextView.error) !TextUtils.isEmpty(editTextView.error)
} }
class Parameters { data class Parameters(
var testErrorString: String? = null val testErrorString: String? = null,
var emptyAllowed = false val emptyAllowed: Boolean = false,
var testType: Int = EditTextValidator.TEST_NOCHECK val testType: Int = EditTextValidator.TEST_NOCHECK,
var classType: String? = null val classType: String? = null,
var customRegexp: String? = null val customRegexp: String? = null,
var customFormat: String? = null val customFormat: String? = null,
var emptyErrorStringDef: String? = null val emptyErrorStringDef: String? = null,
var minLength = 0 var minLength: Int = 0,
var minNumber = 0 var minNumber: Int = 0,
var maxNumber = 0 var maxNumber: Int = 0,
var floatminNumber = 0f var floatminNumber: Float = 0f,
var floatmaxNumber = 0f var floatmaxNumber: Float = 0f
} )
} }

View file

@ -9,12 +9,10 @@ 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 val validatorParameters: DefaultEditTextValidator.Parameters = obtainValidatorParameters(attrs)
private var validator: DefaultEditTextValidator? = null private var validator: DefaultEditTextValidator? = null
init { init {
obtainValidatorParameters(attrs)
setOnBindEditTextListener { editText -> setOnBindEditTextListener { editText ->
validator = DefaultEditTextValidator(editText, validatorParameters, context) validator = DefaultEditTextValidator(editText, validatorParameters, context)
} }
@ -35,25 +33,26 @@ class ValidatingEditTextPreference(ctx: Context, attrs: AttributeSet, defStyleAt
holder?.isDividerAllowedBelow = false holder?.isDividerAllowedBelow = false
} }
private fun obtainValidatorParameters(attrs: AttributeSet) { private fun obtainValidatorParameters(attrs: AttributeSet): DefaultEditTextValidator.Parameters {
val typedArray = context.obtainStyledAttributes(attrs, R.styleable.FormEditText, 0, 0) val typedArray = context.obtainStyledAttributes(attrs, R.styleable.FormEditText, 0, 0)
validatorParameters = DefaultEditTextValidator.Parameters() return DefaultEditTextValidator.Parameters(
validatorParameters.emptyAllowed = typedArray.getBoolean(R.styleable.FormEditText_emptyAllowed, false) emptyAllowed = typedArray.getBoolean(R.styleable.FormEditText_emptyAllowed, false),
validatorParameters.testType = typedArray.getInt(R.styleable.FormEditText_testType, EditTextValidator.TEST_NOCHECK) testType = typedArray.getInt(R.styleable.FormEditText_testType, EditTextValidator.TEST_NOCHECK),
validatorParameters.testErrorString = typedArray.getString(R.styleable.FormEditText_testErrorString) testErrorString = typedArray.getString(R.styleable.FormEditText_testErrorString),
validatorParameters.classType = typedArray.getString(R.styleable.FormEditText_classType) classType = typedArray.getString(R.styleable.FormEditText_classType),
validatorParameters.customRegexp = typedArray.getString(R.styleable.FormEditText_customRegexp) customRegexp = typedArray.getString(R.styleable.FormEditText_customRegexp),
validatorParameters.emptyErrorStringDef = typedArray.getString(R.styleable.FormEditText_emptyErrorString) emptyErrorStringDef = typedArray.getString(R.styleable.FormEditText_emptyErrorString),
validatorParameters.customFormat = typedArray.getString(R.styleable.FormEditText_customFormat) customFormat = typedArray.getString(R.styleable.FormEditText_customFormat)).also { params ->
if (validatorParameters.testType == EditTextValidator.TEST_MIN_LENGTH) if (params.testType == EditTextValidator.TEST_MIN_LENGTH)
validatorParameters.minLength = typedArray.getInt(R.styleable.FormEditText_minLength, 0) params.minLength = typedArray.getInt(R.styleable.FormEditText_minLength, 0)
if (validatorParameters.testType == EditTextValidator.TEST_NUMERIC_RANGE) { if (params.testType == EditTextValidator.TEST_NUMERIC_RANGE) {
validatorParameters.minNumber = typedArray.getInt(R.styleable.FormEditText_minNumber, Int.MIN_VALUE) params.minNumber = typedArray.getInt(R.styleable.FormEditText_minNumber, Int.MIN_VALUE)
validatorParameters.maxNumber = typedArray.getInt(R.styleable.FormEditText_maxNumber, Int.MAX_VALUE) params.maxNumber = typedArray.getInt(R.styleable.FormEditText_maxNumber, Int.MAX_VALUE)
}
if (params.testType == EditTextValidator.TEST_FLOAT_NUMERIC_RANGE) {
params.floatminNumber = typedArray.getFloat(R.styleable.FormEditText_floatminNumber, Float.MIN_VALUE)
params.floatmaxNumber = typedArray.getFloat(R.styleable.FormEditText_floatmaxNumber, Float.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() typedArray.recycle()
} }