Merge pull request #2106 from justmara/fix/safe-parse
SafeParse in ValidatingEditTextPreference
This commit is contained in:
commit
687b1c558b
2 changed files with 9 additions and 6 deletions
|
@ -8,6 +8,7 @@ import dagger.android.HasAndroidInjector
|
||||||
import info.nightscout.androidaps.core.R
|
import info.nightscout.androidaps.core.R
|
||||||
import info.nightscout.androidaps.interfaces.Profile
|
import info.nightscout.androidaps.interfaces.Profile
|
||||||
import info.nightscout.androidaps.interfaces.ProfileFunction
|
import info.nightscout.androidaps.interfaces.ProfileFunction
|
||||||
|
import info.nightscout.shared.SafeParse
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
class ValidatingEditTextPreference(ctx: Context, attrs: AttributeSet, defStyleAttr: Int, defStyleRes: Int) : EditTextPreference(ctx, attrs, defStyleAttr, defStyleRes) {
|
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?) {
|
override fun onSetInitialValue(defaultValue: Any?) {
|
||||||
text =
|
text =
|
||||||
if (validatorParameters.testType == EditTextValidator.TEST_BG_RANGE)
|
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
|
else
|
||||||
getPersistedString(defaultValue as String?)
|
getPersistedString(defaultValue as String?)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun persistString(value: String?): Boolean =
|
override fun persistString(value: String?): Boolean =
|
||||||
if (validatorParameters.testType == EditTextValidator.TEST_BG_RANGE)
|
when (validatorParameters.testType) {
|
||||||
super.persistString(Profile.toMgdl(value?.toDouble() ?: 0.0, profileFunction.getUnits()).toString())
|
EditTextValidator.TEST_BG_RANGE -> super.persistString(Profile.toMgdl(SafeParse.stringToDouble(value, 0.0), profileFunction.getUnits()).toString())
|
||||||
else
|
EditTextValidator.TEST_FLOAT_NUMERIC_RANGE -> super.persistString(SafeParse.stringToDouble(value, 0.0).toString())
|
||||||
super.persistString(value)
|
else -> super.persistString(value)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package info.nightscout.androidaps.utils.textValidator.validators
|
package info.nightscout.androidaps.utils.textValidator.validators
|
||||||
|
|
||||||
import android.widget.EditText
|
import android.widget.EditText
|
||||||
|
import info.nightscout.shared.SafeParse
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A validator that returns true only if the input field contains only numbers
|
* 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 {
|
override fun isValid(editText: EditText): Boolean {
|
||||||
return try {
|
return try {
|
||||||
val value = editText.text.toString().toFloat()
|
val value = SafeParse.stringToFloat(editText.text.toString())
|
||||||
value in floatMin..floatMax
|
value in floatMin..floatMax
|
||||||
} catch (e: NumberFormatException) {
|
} catch (e: NumberFormatException) {
|
||||||
false
|
false
|
||||||
|
|
Loading…
Reference in a new issue