Merge pull request #2106 from justmara/fix/safe-parse

SafeParse in ValidatingEditTextPreference
This commit is contained in:
Milos Kozak 2022-09-30 14:36:28 +02:00 committed by GitHub
commit 687b1c558b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 6 deletions

View file

@ -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)
}
}

View file

@ -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