From 2bb37a4bfc539da99e529ac3f9eda64b99b98767 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Fri, 22 Dec 2017 21:03:36 +0100 Subject: [PATCH] fix doubled percentage when comming from NS --- .../androidaps/db/DatabaseHelper.java | 3 +++ .../nightscout/utils/PercentageSplitter.java | 21 +++++++++++++++++++ .../utils/PercentageSplitterTest.java | 20 ++++++++++++++++++ 3 files changed, 44 insertions(+) create mode 100644 app/src/main/java/info/nightscout/utils/PercentageSplitter.java create mode 100644 app/src/test/java/info/nightscout/utils/PercentageSplitterTest.java diff --git a/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java b/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java index 566df6995b..dc8e921e88 100644 --- a/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java +++ b/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java @@ -46,6 +46,7 @@ import info.nightscout.androidaps.events.EventTreatmentChange; import info.nightscout.androidaps.plugins.IobCobCalculator.events.EventNewHistoryData; import info.nightscout.androidaps.plugins.PumpDanaR.activities.DanaRNSHistorySync; import info.nightscout.androidaps.plugins.PumpVirtual.VirtualPumpPlugin; +import info.nightscout.utils.PercentageSplitter; public class DatabaseHelper extends OrmLiteSqliteOpenHelper { private static Logger log = LoggerFactory.getLogger(DatabaseHelper.class); @@ -1598,6 +1599,8 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper { } } } + // look for already added percentage from NS + profileSwitch.profileName = PercentageSplitter.pureName(profileSwitch.profileName); getDaoProfileSwitch().create(profileSwitch); log.debug("PROFILESWITCH: New record from: " + Source.getString(profileSwitch.source) + " " + profileSwitch.toString()); scheduleProfileSwitchChange(); diff --git a/app/src/main/java/info/nightscout/utils/PercentageSplitter.java b/app/src/main/java/info/nightscout/utils/PercentageSplitter.java new file mode 100644 index 0000000000..53e50466b5 --- /dev/null +++ b/app/src/main/java/info/nightscout/utils/PercentageSplitter.java @@ -0,0 +1,21 @@ +package info.nightscout.utils; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * Created by mike on 22.12.2017. + */ + +public class PercentageSplitter { + public static String pureName(String name) { + String newName = name; + String s = "(.*)\\((\\d+)\\%\\)"; + Pattern r = Pattern.compile(s); + Matcher m = r.matcher(name); + if (m.find()) { + newName = m.group(1); + } + return newName; + } +} diff --git a/app/src/test/java/info/nightscout/utils/PercentageSplitterTest.java b/app/src/test/java/info/nightscout/utils/PercentageSplitterTest.java new file mode 100644 index 0000000000..d70edd3a0d --- /dev/null +++ b/app/src/test/java/info/nightscout/utils/PercentageSplitterTest.java @@ -0,0 +1,20 @@ +package info.nightscout.utils; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +/** + * Created by mike on 22.12.2017. + */ + +public class PercentageSplitterTest { + public PercentageSplitterTest() { + super(); + } + + @Test + public void pureNameTest() throws Exception { + assertEquals("Fiasp", PercentageSplitter.pureName("Fiasp(101%)")); + } +}