fix limit values in InputBg

This commit is contained in:
Milos Kozak 2019-08-27 19:07:05 +02:00
parent 52830f620f
commit e6ae4f03d5
3 changed files with 17 additions and 45 deletions

View file

@ -11,27 +11,10 @@ import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions;
import info.nightscout.androidaps.utils.NumberPicker;
public class InputBg extends Element {
final TextWatcher textWatcher = new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
if (units.equals(Constants.MMOL)) {
value = Math.max(value, 4d);
value = Math.min(value, 15d);
} else {
value = Math.max(value, 72d);
value = Math.min(value, 270d);
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
};
static final int MMOL_MIN = 3;
static final int MMOL_MAX = 20;
static final int MGDL_MIN = 54;
static final int MGDL_MAX = 360;
private String units = Constants.MGDL;
private double value;
@ -43,6 +26,10 @@ public class InputBg extends Element {
public InputBg() {
super();
setUnits(ProfileFunctions.getInstance().getProfileUnits());
if (getUnits().equals(Constants.MMOL))
value = MMOL_MIN;
else
value = MGDL_MIN;
}
public InputBg(InputBg another) {
@ -55,7 +42,7 @@ public class InputBg extends Element {
@Override
public void addToLayout(LinearLayout root) {
NumberPicker numberPicker = new NumberPicker(root.getContext(), null);
numberPicker.setParams(value, minValue, maxValue, step, decimalFormat, true, null, textWatcher);
numberPicker.setParams(value, minValue, maxValue, step, decimalFormat, true, null, null);
numberPicker.setOnValueChangedListener(value -> this.value = value);
root.addView(numberPicker);
}
@ -68,21 +55,18 @@ public class InputBg extends Element {
// set default initial value
if (units.equals(Constants.MMOL)) {
// mmol
minValue = 2;
maxValue = 30;
minValue = MMOL_MIN;
maxValue = MMOL_MAX;
step = 0.1;
decimalFormat = new DecimalFormat("0.0");
} else {
// mg/dL
minValue = 40;
maxValue = 540;
minValue = MGDL_MIN;
maxValue = MGDL_MAX;
step = 1;
decimalFormat = new DecimalFormat("0");
}
// make sure that value is in range
textWatcher.afterTextChanged(null);
this.units = units;
return this;
}

View file

@ -183,7 +183,8 @@ public class NumberPicker extends LinearLayout implements View.OnKeyListener,
}
setParams(initValue, minValue, maxValue, step, formater, allowZero, okButton);
this.textWatcher = textWatcher;
editText.addTextChangedListener(textWatcher);
if (textWatcher != null)
editText.addTextChangedListener(textWatcher);
}
public void setParams(Double initValue, Double minValue, Double maxValue, Double step, NumberFormat formater, boolean allowZero, Button okButton) {

View file

@ -16,27 +16,14 @@ import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions;
@PrepareForTest({MainApp.class, ProfileFunctions.class})
public class InputBgTest {
@Test
public void textWatcherTest() {
InputBg t = new InputBg().setUnits(Constants.MMOL).setValue(1d);
t.textWatcher.beforeTextChanged(null, 0, 0, 0);
t.textWatcher.onTextChanged(null, 0, 0, 0);
t.textWatcher.afterTextChanged(null);
Assert.assertEquals(4d, t.getValue(), 0.01d);
t = new InputBg().setValue(300d).setUnits(Constants.MGDL);
t.textWatcher.afterTextChanged(null);
Assert.assertEquals(270d, t.getValue(), 0.01d);
}
@Test
public void getSetValueTest() {
InputBg i = new InputBg().setUnits(Constants.MMOL).setValue(5d);
Assert.assertEquals(5d, i.getValue(), 0.01d);
Assert.assertEquals(2, i.minValue, 0.01d);
Assert.assertEquals(InputBg.MMOL_MIN, i.minValue, 0.01d);
i = new InputBg().setValue(100d).setUnits(Constants.MGDL);
Assert.assertEquals(100d, i.getValue(), 0.01d);
Assert.assertEquals(40, i.minValue, 0.01d);
Assert.assertEquals(InputBg.MGDL_MIN, i.minValue, 0.01d);
Assert.assertEquals(Constants.MGDL, i.getUnits());
}