Check value after setting unit

This makes sure that the value is in the range of minValue and maxValue. Also, it sets an inital value != 0 as it's called in the constructor.
This commit is contained in:
Nico Schmitz 2019-04-21 20:01:29 +02:00
parent e679f5eaff
commit 3fec2ce16c
3 changed files with 6 additions and 3 deletions

View file

@ -80,6 +80,9 @@ public class InputBg extends Element {
decimalFormat = new DecimalFormat("0");
}
// make sure that value is in range
textWatcher.afterTextChanged(null);
this.units = units;
return this;
}

View file

@ -102,10 +102,10 @@ public class TriggerBg extends Trigger {
Trigger fromJSON(String data) {
try {
JSONObject d = new JSONObject(data);
bg.setUnits(JsonHelper.safeGetString(d, "units"));
bg.setValue(JsonHelper.safeGetDouble(d, "bg"));
lastRun = JsonHelper.safeGetLong(d, "lastRun");
comparator.setValue(Comparator.Compare.valueOf(JsonHelper.safeGetString(d, "comparator")));
bg.setUnits(JsonHelper.safeGetString(d, "units"));
} catch (Exception e) {
e.printStackTrace();
}

View file

@ -18,7 +18,7 @@ public class InputBgTest {
@Test
public void textWatcherTest() {
InputBg t = new InputBg().setValue(1d).setUnits(Constants.MMOL);
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);
@ -31,7 +31,7 @@ public class InputBgTest {
@Test
public void getSetValueTest() {
InputBg i = new InputBg().setValue(5d).setUnits(Constants.MMOL);
InputBg i = new InputBg().setUnits(Constants.MMOL).setValue(5d);
Assert.assertEquals(5d, i.getValue(), 0.01d);
Assert.assertEquals(2, i.minValue, 0.01d);
i = new InputBg().setValue(100d).setUnits(Constants.MGDL);