SafeParse in ValidatingEditTextPreference and FloatNumericRangeValidator
This commit is contained in:
parent
d0ea721e4e
commit
ca25801c62
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue