Using InputDouble

This commit is contained in:
Roumen Georgiev 2019-05-16 12:08:36 +03:00
parent 3280f220ad
commit 4aaca7acb1
4 changed files with 13 additions and 163 deletions

View file

@ -1,90 +0,0 @@
package info.nightscout.androidaps.plugins.general.automation.elements;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.LinearLayout;
import java.text.DecimalFormat;
import info.nightscout.androidaps.utils.NumberPicker;
import info.nightscout.androidaps.utils.SP;
public class InputAutosens extends Element {
private int value;
public int minValue = (int) (SP.getDouble("openapsama_autosens_min", 0.7d) * 100);
public int maxValue = (int) (SP.getDouble("openapsama_autosens_max", 1.2d) * 100);
private double step = 1;
private DecimalFormat decimalFormat = new DecimalFormat("1");;
NumberPicker numberPicker;
final TextWatcher textWatcher = new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
value = Math.max(value, 70);
value = Math.min(value, 200);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
};
public InputAutosens() {
super();
}
public InputAutosens(double value, double minValue, double maxValue, double step, DecimalFormat decimalFormat) {
super();
this.value = (int) value;
this.minValue = (int) ( minValue * 100 );
this.maxValue = (int) ( maxValue * 100 );
this.step = step;
this.decimalFormat = decimalFormat;
}
public InputAutosens(InputAutosens another) {
super();
value = another.getValue();
minValue = another.minValue;
maxValue = another.maxValue;
step = another.step;
decimalFormat = another.decimalFormat;
}
@Override
public void addToLayout(LinearLayout root) {
minValue = (int) (SP.getDouble("openapsama_autosens_min", 0.7d) * 100);
maxValue = (int) (SP.getDouble("openapsama_autosens_max", 1.2d) * 100);
value = minValue & maxValue;
numberPicker = new NumberPicker(root.getContext(), null);
numberPicker.setParams((double) value, (double) minValue, (double) maxValue, step, decimalFormat, true, textWatcher);
numberPicker.setOnValueChangedListener(value -> this.value = (int) value);
root.addView(numberPicker);
}
public InputAutosens setValue(int value) {
minValue = (int) (SP.getDouble("openapsama_autosens_min", 0.7d) * 100);
maxValue = (int) (SP.getDouble("openapsama_autosens_max", 1.2d) * 100);
if (value > maxValue)
value = maxValue;
if (value < minValue)
value = minValue;
this.value = value;
if (numberPicker != null)
numberPicker.setValue((double) value);
return this;
}
public int getValue() {
return value;
}
}

View file

@ -11,11 +11,13 @@ import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.text.DecimalFormat;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.logging.L;
import info.nightscout.androidaps.plugins.general.automation.elements.Comparator;
import info.nightscout.androidaps.plugins.general.automation.elements.InputAutosens;
import info.nightscout.androidaps.plugins.general.automation.elements.InputDouble;
import info.nightscout.androidaps.plugins.general.automation.elements.LabelWithElement;
import info.nightscout.androidaps.plugins.general.automation.elements.LayoutBuilder;
import info.nightscout.androidaps.plugins.general.automation.elements.StaticLabel;
@ -23,12 +25,16 @@ import info.nightscout.androidaps.plugins.iob.iobCobCalculator.AutosensData;
import info.nightscout.androidaps.plugins.iob.iobCobCalculator.IobCobCalculatorPlugin;
import info.nightscout.androidaps.utils.DateUtil;
import info.nightscout.androidaps.utils.JsonHelper;
import info.nightscout.androidaps.utils.SP;
import info.nightscout.androidaps.utils.T;
public class TriggerAutosensValue extends Trigger {
private static Logger log = LoggerFactory.getLogger(L.AUTOMATION);
private InputAutosens value = new InputAutosens();
public int minValue = (int) (SP.getDouble("openapsama_autosens_min", 0.7d) * 100);
public int maxValue = (int) (SP.getDouble("openapsama_autosens_max", 1.2d) * 100);
private double step = 1;
private DecimalFormat decimalFormat = new DecimalFormat("1");;
private InputDouble value = new InputDouble( (double) minValue,(double) minValue, (double) maxValue, step, decimalFormat);
private Comparator comparator = new Comparator();
public TriggerAutosensValue() {
@ -41,7 +47,7 @@ public class TriggerAutosensValue extends Trigger {
lastRun = triggerAutosensValue.lastRun;
}
public int getValue() {
public double getValue() {
return value.getValue();
}

View file

@ -1,60 +0,0 @@
package info.nightscout.androidaps.plugins.general.automation.elements;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import info.AAPSMocker;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions;
import info.nightscout.androidaps.utils.SP;
import static org.mockito.ArgumentMatchers.anyDouble;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.when;
@RunWith(PowerMockRunner.class)
@PrepareForTest({MainApp.class, ProfileFunctions.class, SP.class})
public class InputAutosensTest {
@Test
public void textWatcherTest() {
when(SP.getDouble(Mockito.eq("openapsama_autosens_max"), anyDouble())).thenReturn(1.2d);
when(SP.getDouble(Mockito.eq("openapsama_autosens_min"), anyDouble())).thenReturn(0.7d);
InputAutosens t = new InputAutosens().setValue(100);
t.textWatcher.beforeTextChanged(null, 0, 0, 0);
t.textWatcher.onTextChanged(null, 0, 0, 0);
t.textWatcher.afterTextChanged(null);
Assert.assertEquals(100, t.getValue(), 0.01d);
t = new InputAutosens().setValue(200);
t.textWatcher.afterTextChanged(null);
Assert.assertEquals(120, t.getValue(), 0.01d);
}
@Test
public void getSetValueTest() {
when(SP.getDouble(Mockito.eq("openapsama_autosens_max"), anyDouble())).thenReturn(1.2d);
when(SP.getDouble(anyInt(), anyDouble())).thenReturn(0.7d);
InputAutosens i = new InputAutosens().setValue(500);
Assert.assertEquals(120, i.getValue(), 0.01d);
Assert.assertEquals(0, i.minValue, 0.01d);
i = new InputAutosens().setValue(110);
Assert.assertEquals(110, i.getValue(), 0.01d);
Assert.assertEquals(0, i.minValue, 0.01d);
}
@Before
public void prepare() {
AAPSMocker.mockMainApp();
AAPSMocker.mockBus();
AAPSMocker.mockStrings();
AAPSMocker.mockProfileFunctions();
AAPSMocker.mockSP();
}
}

View file

@ -73,11 +73,9 @@ public class TriggerAutosensValueTest {
@Test
public void copyConstructorTest() {
Mockito.when(SP.getDouble(Mockito.eq("openapsama_autosens_max"), anyDouble())).thenReturn(1.2d);
Mockito.when(SP.getDouble(Mockito.eq("openapsama_autosens_min"), anyDouble())).thenReturn(0.7d);
TriggerAutosensValue t = new TriggerAutosensValue().setValue(213).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER);
TriggerAutosensValue t1 = (TriggerAutosensValue) t.duplicate();
Assert.assertEquals(120, t1.getValue(), 0.01d);
Assert.assertEquals(213, t1.getValue(), 0.01d);
Assert.assertEquals(Comparator.Compare.IS_EQUAL_OR_LESSER, t.getComparator().getValue());
}
@ -88,25 +86,21 @@ public class TriggerAutosensValueTest {
Assert.assertEquals(1l, t.getLastRun());
}
String ASJson = "{\"data\":{\"comparator\":\"IS_EQUAL\",\"lastRun\":0,\"value\":120},\"type\":\"info.nightscout.androidaps.plugins.general.automation.triggers.TriggerAutosensValue\"}";
String ASJson = "{\"data\":{\"comparator\":\"IS_EQUAL\",\"lastRun\":0,\"value\":410},\"type\":\"info.nightscout.androidaps.plugins.general.automation.triggers.TriggerAutosensValue\"}";
@Test
public void toJSONTest() {
Mockito.when(SP.getDouble(Mockito.eq("openapsama_autosens_max"), anyDouble())).thenReturn(1.2d);
Mockito.when(SP.getDouble(Mockito.eq("openapsama_autosens_min"), anyDouble())).thenReturn(0.7d);
TriggerAutosensValue t = new TriggerAutosensValue().setValue(410).comparator(Comparator.Compare.IS_EQUAL);
Assert.assertEquals(ASJson, t.toJSON());
}
@Test
public void fromJSONTest() throws JSONException {
Mockito.when(SP.getDouble(Mockito.eq("openapsama_autosens_max"), anyDouble())).thenReturn(1.2d);
Mockito.when(SP.getDouble(Mockito.eq("openapsama_autosens_min"), anyDouble())).thenReturn(0.7d);
TriggerAutosensValue t = new TriggerAutosensValue().setValue(410).comparator(Comparator.Compare.IS_EQUAL);
TriggerAutosensValue t2 = (TriggerAutosensValue) Trigger.instantiate(new JSONObject(t.toJSON()));
Assert.assertEquals(Comparator.Compare.IS_EQUAL, t2.getComparator().getValue());
Assert.assertEquals(120, t2.getValue(), 0.01d);
Assert.assertEquals(410, t2.getValue(), 0.01d);
}
@Test