From ca25801c629ec074ce5eec38f89af304035b328b Mon Sep 17 00:00:00 2001 From: Sergey Zorchenko Date: Fri, 30 Sep 2022 14:47:07 +0300 Subject: [PATCH] SafeParse in ValidatingEditTextPreference and FloatNumericRangeValidator --- .../textValidator/ValidatingEditTextPreference.kt | 12 +++++++----- .../validators/FloatNumericRangeValidator.kt | 3 ++- 2 files changed, 9 insertions(+), 6 deletions(-) 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 64a7e7d09e..5ebdd6dbfa 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 @@ -8,6 +8,7 @@ import dagger.android.HasAndroidInjector import info.nightscout.androidaps.core.R import info.nightscout.androidaps.interfaces.Profile import info.nightscout.androidaps.interfaces.ProfileFunction +import info.nightscout.shared.SafeParse import javax.inject.Inject class ValidatingEditTextPreference(ctx: Context, attrs: AttributeSet, defStyleAttr: Int, defStyleRes: Int) : EditTextPreference(ctx, attrs, defStyleAttr, defStyleRes) { @@ -67,14 +68,15 @@ class ValidatingEditTextPreference(ctx: Context, attrs: AttributeSet, defStyleAt override fun onSetInitialValue(defaultValue: Any?) { text = if (validatorParameters.testType == EditTextValidator.TEST_BG_RANGE) - Profile.fromMgdlToUnits(getPersistedString(defaultValue as String?).toDouble(), profileFunction.getUnits()).toString() + Profile.fromMgdlToUnits(SafeParse.stringToDouble(getPersistedString(defaultValue as String?)), profileFunction.getUnits()).toString() else getPersistedString(defaultValue as String?) } override fun persistString(value: String?): Boolean = - if (validatorParameters.testType == EditTextValidator.TEST_BG_RANGE) - super.persistString(Profile.toMgdl(value?.toDouble() ?: 0.0, profileFunction.getUnits()).toString()) - else - super.persistString(value) + when (validatorParameters.testType) { + EditTextValidator.TEST_BG_RANGE -> super.persistString(Profile.toMgdl(SafeParse.stringToDouble(value, 0.0), profileFunction.getUnits()).toString()) + EditTextValidator.TEST_FLOAT_NUMERIC_RANGE -> super.persistString(SafeParse.stringToDouble(value, 0.0).toString()) + else -> super.persistString(value) + } } diff --git a/core/src/main/java/info/nightscout/androidaps/utils/textValidator/validators/FloatNumericRangeValidator.kt b/core/src/main/java/info/nightscout/androidaps/utils/textValidator/validators/FloatNumericRangeValidator.kt index d574233f43..889914dbdc 100644 --- a/core/src/main/java/info/nightscout/androidaps/utils/textValidator/validators/FloatNumericRangeValidator.kt +++ b/core/src/main/java/info/nightscout/androidaps/utils/textValidator/validators/FloatNumericRangeValidator.kt @@ -1,6 +1,7 @@ package info.nightscout.androidaps.utils.textValidator.validators import android.widget.EditText +import info.nightscout.shared.SafeParse /** * A validator that returns true only if the input field contains only numbers @@ -12,7 +13,7 @@ class FloatNumericRangeValidator(_customErrorMessage: String?, private val float override fun isValid(editText: EditText): Boolean { return try { - val value = editText.text.toString().toFloat() + val value = SafeParse.stringToFloat(editText.text.toString()) value in floatMin..floatMax } catch (e: NumberFormatException) { false