From a3440166c4db7772d55c5d3db088f691e58f6334 Mon Sep 17 00:00:00 2001 From: Adrian Date: Sun, 6 Sep 2020 01:23:00 +0200 Subject: [PATCH] refactoring idea Parameters --- .../textValidator/DefaultEditTextValidator.kt | 28 ++++++------- .../ValidatingEditTextPreference.kt | 41 +++++++++---------- 2 files changed, 34 insertions(+), 35 deletions(-) diff --git a/core/src/main/java/info/nightscout/androidaps/utils/textValidator/DefaultEditTextValidator.kt b/core/src/main/java/info/nightscout/androidaps/utils/textValidator/DefaultEditTextValidator.kt index 94f3cfd3b6..4306ae20fc 100644 --- a/core/src/main/java/info/nightscout/androidaps/utils/textValidator/DefaultEditTextValidator.kt +++ b/core/src/main/java/info/nightscout/androidaps/utils/textValidator/DefaultEditTextValidator.kt @@ -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 + ) } \ No newline at end of file diff --git a/core/src/main/java/info/nightscout/androidaps/utils/textValidator/ValidatingEditTextPreference.kt b/core/src/main/java/info/nightscout/androidaps/utils/textValidator/ValidatingEditTextPreference.kt index 5bf97f6dee..98209f29b2 100644 --- a/core/src/main/java/info/nightscout/androidaps/utils/textValidator/ValidatingEditTextPreference.kt +++ b/core/src/main/java/info/nightscout/androidaps/utils/textValidator/ValidatingEditTextPreference.kt @@ -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() }