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"); 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

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

View file

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