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

View file

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

View file

@ -16,27 +16,14 @@ import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions;
@PrepareForTest({MainApp.class, ProfileFunctions.class}) @PrepareForTest({MainApp.class, ProfileFunctions.class})
public class InputBgTest { 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 @Test
public void getSetValueTest() { public void getSetValueTest() {
InputBg i = new InputBg().setUnits(Constants.MMOL).setValue(5d); InputBg i = new InputBg().setUnits(Constants.MMOL).setValue(5d);
Assert.assertEquals(5d, i.getValue(), 0.01d); 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); i = new InputBg().setValue(100d).setUnits(Constants.MGDL);
Assert.assertEquals(100d, i.getValue(), 0.01d); 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()); Assert.assertEquals(Constants.MGDL, i.getUnits());
} }