From f0a96866af8fcd4acc6a59f43177f46359210222 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Wed, 23 Dec 2020 22:27:27 +0100 Subject: [PATCH] NumberPicker better check for allowed range --- .../androidaps/utils/ui/NumberPicker.java | 23 +++++++++++-------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/core/src/main/java/info/nightscout/androidaps/utils/ui/NumberPicker.java b/core/src/main/java/info/nightscout/androidaps/utils/ui/NumberPicker.java index 0c77607cb8..02dfe36f81 100644 --- a/core/src/main/java/info/nightscout/androidaps/utils/ui/NumberPicker.java +++ b/core/src/main/java/info/nightscout/androidaps/utils/ui/NumberPicker.java @@ -132,12 +132,10 @@ public class NumberPicker extends LinearLayout implements View.OnKeyListener, setTextWatcher(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { - } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { - } @Override @@ -153,11 +151,10 @@ public class NumberPicker extends LinearLayout implements View.OnKeyListener, } }); - editText.setOnFocusChangeListener(new OnFocusChangeListener() { - @Override public void onFocusChange(View v, boolean hasFocus) { - focused = hasFocus; - updateEditText(); - } + editText.setOnFocusChangeListener((v, hasFocus) -> { + focused = hasFocus; + if (!focused) getValue(); // check min/max + updateEditText(); }); } @@ -204,12 +201,12 @@ public class NumberPicker extends LinearLayout implements View.OnKeyListener, editText.addTextChangedListener(textWatcher); } - public void setParams(Double initValue, Double minValue, Double maxValue, Double step, NumberFormat formater, boolean allowZero, Button okButton) { + public void setParams(Double initValue, Double minValue, Double maxValue, Double step, NumberFormat formatter, boolean allowZero, Button okButton) { this.value = initValue; this.minValue = minValue; this.maxValue = maxValue; this.step = step; - this.formatter = formater; + this.formatter = formatter; this.allowZero = allowZero; callValueChangedListener(); this.okButton = okButton; @@ -234,6 +231,14 @@ public class NumberPicker extends LinearLayout implements View.OnKeyListener, } public Double getValue() { + if (value > maxValue) { + value = maxValue; + ToastUtils.showToastInUiThread(getContext(), getContext().getString(R.string.youareonallowedlimit)); + } + if (value < minValue) { + value = minValue; + ToastUtils.showToastInUiThread(getContext(), getContext().getString(R.string.youareonallowedlimit)); + } return value; }