refactoring idea Parameters

This commit is contained in:
Adrian 2020-09-06 01:23:00 +02:00
parent 4a266f9956
commit a3440166c4
2 changed files with 34 additions and 35 deletions

View file

@ -243,18 +243,18 @@ class DefaultEditTextValidator : EditTextValidator {
!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
}
data class Parameters(
val testErrorString: String? = null,
val emptyAllowed: Boolean = false,
val testType: Int = EditTextValidator.TEST_NOCHECK,
val classType: String? = null,
val customRegexp: String? = null,
val customFormat: String? = null,
val emptyErrorStringDef: String? = null,
var minLength: Int = 0,
var minNumber: Int = 0,
var maxNumber: Int = 0,
var floatminNumber: Float = 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)
: EditTextPreference(ctx, attrs, defStyleAttr, defStyleRes) {
private lateinit var validatorParameters: DefaultEditTextValidator.Parameters
private val validatorParameters: DefaultEditTextValidator.Parameters = obtainValidatorParameters(attrs)
private var validator: DefaultEditTextValidator? = null
init {
obtainValidatorParameters(attrs)
setOnBindEditTextListener { editText ->
validator = DefaultEditTextValidator(editText, validatorParameters, context)
}
@ -35,25 +33,26 @@ class ValidatingEditTextPreference(ctx: Context, attrs: AttributeSet, defStyleAt
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)
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)
return DefaultEditTextValidator.Parameters(
emptyAllowed = typedArray.getBoolean(R.styleable.FormEditText_emptyAllowed, false),
testType = typedArray.getInt(R.styleable.FormEditText_testType, EditTextValidator.TEST_NOCHECK),
testErrorString = typedArray.getString(R.styleable.FormEditText_testErrorString),
classType = typedArray.getString(R.styleable.FormEditText_classType),
customRegexp = typedArray.getString(R.styleable.FormEditText_customRegexp),
emptyErrorStringDef = typedArray.getString(R.styleable.FormEditText_emptyErrorString),
customFormat = typedArray.getString(R.styleable.FormEditText_customFormat)).also { params ->
if (params.testType == EditTextValidator.TEST_MIN_LENGTH)
params.minLength = typedArray.getInt(R.styleable.FormEditText_minLength, 0)
if (params.testType == EditTextValidator.TEST_NUMERIC_RANGE) {
params.minNumber = typedArray.getInt(R.styleable.FormEditText_minNumber, Int.MIN_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)
}
}
typedArray.recycle()
}