From e1c2e4c492b5b3f6afb239731380b6b010d7be99 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Fri, 30 Dec 2016 16:43:43 +0100 Subject: [PATCH 1/5] TimeListEdit initial work --- app/build.gradle | 5 +- .../info/nightscout/utils/TimeListEdit.java | 154 ++++++++++++++++++ app/src/main/res/drawable/add.png | Bin 0 -> 420 bytes app/src/main/res/drawable/clone.png | Bin 0 -> 643 bytes app/src/main/res/drawable/remove.png | Bin 0 -> 741 bytes .../main/res/layout/timelistedit_element.xml | 46 ++++++ app/src/main/res/values-bg/strings.xml | 7 +- .../info/nightscout/utils/RoundTest.java | 10 +- .../nightscout/utils/TimeListEditTest.java | 44 +++++ 9 files changed, 258 insertions(+), 8 deletions(-) create mode 100644 app/src/main/java/info/nightscout/utils/TimeListEdit.java create mode 100644 app/src/main/res/drawable/add.png create mode 100644 app/src/main/res/drawable/clone.png create mode 100644 app/src/main/res/drawable/remove.png create mode 100644 app/src/main/res/layout/timelistedit_element.xml rename app/src/test/{ => java}/info/nightscout/utils/RoundTest.java (75%) create mode 100644 app/src/test/java/info/nightscout/utils/TimeListEditTest.java diff --git a/app/build.gradle b/app/build.gradle index aee236a796..628d41b7ab 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -107,7 +107,7 @@ android { dependencies { wearWearApp project(path: ':wear', configuration: 'fullRelease') - compile fileTree(dir: 'libs', include: ['*.jar']) + compile fileTree(include: ['*.jar'], dir: 'libs') compile('com.crashlytics.sdk.android:crashlytics:2.5.7@aar') { transitive = true; } @@ -127,6 +127,7 @@ dependencies { compile 'com.jjoe64:graphview:4.0.1' compile 'com.eclipsesource.j2v8:j2v8:3.1.6@aar' compile 'com.joanzapata.iconify:android-iconify-fontawesome:2.1.1' - testCompile 'junit:junit:4.12' compile 'com.google.android.gms:play-services-wearable:7.5.0' + compile 'junit:junit:4.12' + testCompile 'org.json:json:20140107' } diff --git a/app/src/main/java/info/nightscout/utils/TimeListEdit.java b/app/src/main/java/info/nightscout/utils/TimeListEdit.java new file mode 100644 index 0000000000..b77099cb50 --- /dev/null +++ b/app/src/main/java/info/nightscout/utils/TimeListEdit.java @@ -0,0 +1,154 @@ +package info.nightscout.utils; + +import android.content.Context; +import android.view.LayoutInflater; +import android.view.View; +import android.widget.EditText; +import android.widget.ImageView; +import android.widget.LinearLayout; +import android.widget.TextView; + +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; + +import info.nightscout.androidaps.R; + +/** + * Created by mike on 29.12.2016. + */ + +public class TimeListEdit { + + LinearLayout layout; + + Context context; + View view; + int resLayoutId; + JSONArray data; + boolean per30min = false; + String array1; + String array2; + + public TimeListEdit(Context context, View view, int resLayoutId, JSONArray data, String array1, String array2, boolean per30min) { + this.context = context; + this.view = view; + this.resLayoutId = resLayoutId; + this.data = data; + this.array1 = array1; + this.array2 = array2; + this.per30min = per30min; + buildView(); + } + + private void buildView() { + /* + LayoutInflater inflater = LayoutInflater.from(context); + layout = (LinearLayout) view.findViewById(resLayoutId); + + final EditText[] editTexts = new EditText[24]; + + for (int i = 0; i < 24; i++) { + View childview = inflater.inflate(R.layout.timelistedit_element, layout, false); + ((TextView) childview.findViewById(R.id.basal_time_elem)).setText((i < 10 ? "0" : "") + i + ":00: "); + + ImageView copyprevbutton = (ImageView) childview.findViewById(R.id.basal_copyprev_elem); + + if (i == 0) { + copyprevbutton.setVisibility(View.INVISIBLE); + ; + } else { + final int j = i; //needs to be final to be passed to inner class. + copyprevbutton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + editTexts[j].setText(editTexts[j - 1].getText()); + } + }); + } + + editTexts[i] = ((EditText) childview.findViewById(R.id.basal_edittext_elem)); + //editTexts[i].setText(DecimalFormatter.to2Decimal(values[i])); + layout.addView(childview); + + } + */ + } + + public int itemsCount() { + return data.length(); + } + + public int secondFromMidnight(int index) { + try { + JSONObject item = (JSONObject) data.get(index); + if (item.has("timeAsSeconds")) { + return item.getInt("timeAsSeconds"); + } + } catch (JSONException e) { + e.printStackTrace(); + } + return 0; + } + + public double value1(int index) { + try { + JSONObject item = (JSONObject) data.get(index); + if (item.has(array1)) { + return item.getDouble(array1); + } + } catch (JSONException e) { + e.printStackTrace(); + } + return 0d; + } + + public double value2(int index) { + try { + JSONObject item = (JSONObject) data.get(index); + if (item.has(array2)) { + return item.getDouble(array2); + } + } catch (JSONException e) { + e.printStackTrace(); + } + return 0d; + } + + public void editItem(int index, int timeAsSeconds, double value1, double value2) { + try { + JSONObject newObject = new JSONObject(); + newObject.put("timeAsSeconds", timeAsSeconds); + newObject.put(array1, value1); + if (array2 != null) + newObject.put(array2, value2); + data.put(index, newObject); + } catch (JSONException e) { + e.printStackTrace(); + } + + } + + public void addItem(int index, int timeAsSeconds, double value1, double value2) { + try { + // shift data + for (int i = data.length(); i > index; i--) { + data.put(i, data.get(i - 1)); + } + // add new object + JSONObject newObject = new JSONObject(); + newObject.put("timeAsSeconds", timeAsSeconds); + newObject.put(array1, value1); + if (array2 != null) + newObject.put(array2, value2); + data.put(index, newObject); + } catch (JSONException e) { + e.printStackTrace(); + } + + } + + public void removeItem(int index) { + data.remove(index); + } +} diff --git a/app/src/main/res/drawable/add.png b/app/src/main/res/drawable/add.png new file mode 100644 index 0000000000000000000000000000000000000000..b25da646726b217b89e70ae4b7f3e5ec33f65154 GIT binary patch literal 420 zcmV;V0bBlwP)F%&z^R9OUk(lubGK4@l%kqDVM6DMxa3b5|$_7o~`D;mn^PhgBSi-Ig(H zZSTzN?CiYSnW8j%n7sA$*0=Zhy!CmXR}iss+a2m}e7pH2qRvK!O~A(T7LIRW`xXaB zJH!U8Ev{4k@Sx+9qvON)fR%++j6wsg%jEpzG(KQ{ZV98{p?8(JI=gHQh!WP6H90$t zQS{I|%UoaFMEMJ#VsyV>%}mT=`!bZ)kU>>tjY`GEPgi($?=UR`>H=y491Zo93IQW~ zeQJDs3e%_ujiGsobQ~Ndd6aposssWwKnT!_8oVV#z|d~58Xg-7ngT$>!uBkx(jou; zfF(*Kl>d9Pt$}Qo9s;5qq?1{)gFdfjm29GpdPspUz$e@rfD$2~)g+1miS4$LaXr+1 zCjK*uoJCDbkSePmVX;P8^w_xVI>{IDBP<@EyN3EBEEeEjzcF&`T7=(w?AQ@%mGgN3 O0000IVdm&T_+S( zEn8BxC-La$d~L7s_Q?vTYO;5HQSNZr!sokPT!jgTCuQg$Fd`fu6>zYBm{bd;>5YZJ zLIoUBcoEJ_Wq@}yDqp$mjyT;e?B!H2#-No6g;E*$6BQ&7n#3j?WpgN={G5|4+AP7M z6kzd@LEGKpNc0bM^P;LFoysAd%EwBYo-ONw*T!4DqSXpbS7DJPl#U809Otv`9d5q8 zn*&`Oe;&GVy(ipcwc^vqt!(;7ej@qw_M57-uDh@9+wArSPPY?e(qJ}85Ojj2S-@ue z0OXroi3|;2khixo*odbm;?Y|bjenb~_^fum=JYrKQG;X>A&P{r@eF8q>aEg$wa34@ znux_7`Un37S3Hsx&dkkg?VWC~$~1rgQDDd!60Y5QpSaZ5>0OP*XV)K(%+%S=MT<>w zdCvKmnV~F{Aqd8G1nhk~01ZvOQ`$S){9lr(iO-LIpQtwaLLc;ctg1O0m{cOIPKUg{ z`8}||^2fZhK(#gM9e){;tgYd6Iy1fhdTj20fu{_(Kf>)TZHZ#3IGkXv1LDFVsPhKpdlHj1B|U*cNE{w dVj$KCgI|s%*C@&V;gSFV002ovPDHLkV1nCUHEaL? literal 0 HcmV?d00001 diff --git a/app/src/main/res/drawable/remove.png b/app/src/main/res/drawable/remove.png new file mode 100644 index 0000000000000000000000000000000000000000..835575cea2ac5e90ae0b489a43af43a8a637274d GIT binary patch literal 741 zcmV!M?HxbQTzi$^rU#u zQV?HYw5ukHaVCiJ`lWhDmA)Li&&j@Dp@bp)dcY!&? zHOhFGl^fs&fis!j+j=n~ijOaij6h=+w0II$W&?^{#>-3=%(5o#t!19n$J_7+p#U$P z85$%+2t&_M=&@BaaUNKlfhAVNM_H!kR(#u%+8H?T*zhok2w|8d26cG}x~;;``5~YQ zn#aO|TEv%Zwn5^i5E6XS*s&^XYZjE1uSgZr_>iL*XxW7>K8R*`Q1;mPq?E9@nXXB= z8{Mt)$s+xvk^B|ITxOH9N~JI0_b&{zwUHh^fF0mKIbdEbV=0}YcjK7| zceAHcJ~cQnpneJB3Xm$BM$JK6z%LH&N~NYy4`q@1TU)HxV7@Xy3vv7wuSm6YBBx|{O|k^ XZwHDXkz%aC00000NkvXXu0mjf7S&Q1 literal 0 HcmV?d00001 diff --git a/app/src/main/res/layout/timelistedit_element.xml b/app/src/main/res/layout/timelistedit_element.xml new file mode 100644 index 0000000000..1cb652308e --- /dev/null +++ b/app/src/main/res/layout/timelistedit_element.xml @@ -0,0 +1,46 @@ + + + + + + + + + + + + diff --git a/app/src/main/res/values-bg/strings.xml b/app/src/main/res/values-bg/strings.xml index 3557e5d27b..3ca9fc4008 100644 --- a/app/src/main/res/values-bg/strings.xml +++ b/app/src/main/res/values-bg/strings.xml @@ -16,12 +16,12 @@ Известие Проверка на КЗ Корекция с въглехидр. - Замяна сензор + Смяна на сензор Рестарт на сензор Комбиниран болус Болус за корекция Физ. активност - Замяна резервоар + Смяна на резервоар Болус за осн. хранене Абсолютна ВХ @@ -41,7 +41,7 @@ Бележка OpenAPS оффлайн Смяна на профил - Замяна сет + Смяна на сет Въпрос Болус за закуска Край на временен базал @@ -375,4 +375,5 @@ Изпрати всички данни отново Wear Избран е грешен тип помпа + преди %d м. diff --git a/app/src/test/info/nightscout/utils/RoundTest.java b/app/src/test/java/info/nightscout/utils/RoundTest.java similarity index 75% rename from app/src/test/info/nightscout/utils/RoundTest.java rename to app/src/test/java/info/nightscout/utils/RoundTest.java index 91bce62014..d95ab2b391 100644 --- a/app/src/test/info/nightscout/utils/RoundTest.java +++ b/app/src/test/java/info/nightscout/utils/RoundTest.java @@ -6,20 +6,24 @@ import static org.junit.Assert.*; public class RoundTest { + public RoundTest(){ + super(); + } + @Test - public void roundTo() { + public void roundToTest() throws Exception { assertEquals( 0.55d, Round.roundTo(0.54d, 0.05d), 0.00000001d ); assertEquals( 1d, Round.roundTo(1.49d, 1d), 0.00000001d ); } @Test - public void floorTo() { + public void floorToTest() throws Exception { assertEquals( 0.5d, Round.floorTo(0.54d, 0.05d), 0.00000001d ); assertEquals( 1d, Round.floorTo(1.59d, 1d), 0.00000001d ); } @Test - public void ceilTo() { + public void ceilToTest() throws Exception { assertEquals( 0.6d, Round.ceilTo(0.54d, 0.1d), 0.00000001d ); assertEquals( 2d, Round.ceilTo(1.49999d, 1d), 0.00000001d ); } diff --git a/app/src/test/java/info/nightscout/utils/TimeListEditTest.java b/app/src/test/java/info/nightscout/utils/TimeListEditTest.java new file mode 100644 index 0000000000..f7c3767a65 --- /dev/null +++ b/app/src/test/java/info/nightscout/utils/TimeListEditTest.java @@ -0,0 +1,44 @@ +package info.nightscout.utils; + +import org.json.JSONArray; +import org.junit.Test; + +import java.lang.reflect.Method; + +import static org.junit.Assert.*; + +/** + * Created by mike on 30.12.2016. + */ +public class TimeListEditTest { + + JSONArray data = new JSONArray(); + JSONArray data2 = new JSONArray(); + TimeListEdit tle = new TimeListEdit(null, null, 0, data, "ic", null, false); + TimeListEdit tle2 = new TimeListEdit(null, null, 0, data2, "ic", "ic2", false); + + @Test + public void doArrayTest() throws Exception { + tle.addItem(0, 0, 0.1, 0); + tle.addItem(1, 60 * 60, 0.2, 0); + assertEquals(2, tle.itemsCount()); + + tle.editItem(0, 2 * 60 * 60, 1, 0); + assertEquals(2, tle.itemsCount()); + assertEquals(1d, tle.value1(0), 0.00001d); + assertEquals( 2 * 60 * 60, tle.secondFromMidnight(0)); + tle.removeItem(0); + assertEquals(0.2d, tle.value1(0), 0.00001d); + assertEquals(0, tle.value2(0), 0.00001d); + assertEquals(60 * 60, tle.secondFromMidnight(0)); + + //System.out.print(tle2.toString()); + assertEquals(0, tle2.itemsCount()); + tle2.addItem(0, 0, 1, 2); + assertEquals(1, tle2.itemsCount()); + assertEquals(0, tle2.secondFromMidnight(0)); + assertEquals(1d, tle2.value1(0), 0.00001d); + assertEquals(2d, tle2.value2(0), 0.00001d); + } + +} \ No newline at end of file From 2e6c168adc83376c8d4d4ba3493fff6376fe312d Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Sat, 31 Dec 2016 13:11:32 +0100 Subject: [PATCH 2/5] TimeListEdit most work done --- app/build.gradle | 4 + .../SimpleProfile/SimpleProfileFragment.java | 9 + .../info/nightscout/utils/TimeListEdit.java | 191 +++++++++++++++--- .../res/layout/simpleprofile_fragment.xml | 21 +- .../main/res/layout/timelistedit_element.xml | 5 +- .../nightscout/utils/TimeListEditTest.java | 13 +- 6 files changed, 200 insertions(+), 43 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 628d41b7ab..da35bd40a7 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -130,4 +130,8 @@ dependencies { compile 'com.google.android.gms:play-services-wearable:7.5.0' compile 'junit:junit:4.12' testCompile 'org.json:json:20140107' + testCompile 'org.mockito:mockito-core:2.+' + androidTestCompile 'org.mockito:mockito-core:2.+' + androidTestCompile "com.google.dexmaker:dexmaker:1.2" + androidTestCompile "com.google.dexmaker:dexmaker-mockito:1.2" } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/SimpleProfile/SimpleProfileFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/SimpleProfile/SimpleProfileFragment.java index 6c23f035a0..0fb799c7cf 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/SimpleProfile/SimpleProfileFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/SimpleProfile/SimpleProfileFragment.java @@ -11,13 +11,17 @@ import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.EditText; +import android.widget.LinearLayout; import android.widget.RadioButton; import com.squareup.otto.Subscribe; +import org.json.JSONArray; 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.events.EventInitializationChanged; @@ -25,7 +29,9 @@ import info.nightscout.androidaps.interfaces.FragmentBase; import info.nightscout.androidaps.plugins.Careportal.Dialogs.NewNSTreatmentDialog; import info.nightscout.androidaps.plugins.Careportal.OptionsToShow; import info.nightscout.androidaps.plugins.Overview.events.EventNewNotification; +import info.nightscout.utils.DecimalFormatter; import info.nightscout.utils.SafeParse; +import info.nightscout.utils.TimeListEdit; public class SimpleProfileFragment extends Fragment implements FragmentBase { private static Logger log = LoggerFactory.getLogger(SimpleProfileFragment.class); @@ -46,6 +52,8 @@ public class SimpleProfileFragment extends Fragment implements FragmentBase { EditText targetlowView; EditText targethighView; Button profileswitchButton; + TimeListEdit test; + JSONArray data = new JSONArray(); @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, @@ -61,6 +69,7 @@ public class SimpleProfileFragment extends Fragment implements FragmentBase { targetlowView = (EditText) layout.findViewById(R.id.simpleprofile_targetlow); targethighView = (EditText) layout.findViewById(R.id.simpleprofile_targethigh); profileswitchButton = (Button) layout.findViewById(R.id.simpleprofile_profileswitch); + test = new TimeListEdit(getContext(), layout, R.id.simpleprofile_test, "Test", data, "ic1", null, new DecimalFormat("0.00")); onStatusEvent(null); diff --git a/app/src/main/java/info/nightscout/utils/TimeListEdit.java b/app/src/main/java/info/nightscout/utils/TimeListEdit.java index b77099cb50..ae828b4ecf 100644 --- a/app/src/main/java/info/nightscout/utils/TimeListEdit.java +++ b/app/src/main/java/info/nightscout/utils/TimeListEdit.java @@ -1,17 +1,31 @@ package info.nightscout.utils; import android.content.Context; +import android.text.Editable; +import android.text.TextWatcher; +import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; +import android.widget.AdapterView; +import android.widget.ArrayAdapter; import android.widget.EditText; import android.widget.ImageView; import android.widget.LinearLayout; +import android.widget.Spinner; import android.widget.TextView; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import java.text.DateFormat; +import java.text.NumberFormat; +import java.text.SimpleDateFormat; +import java.util.ArrayList; + +import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; /** @@ -19,60 +33,181 @@ import info.nightscout.androidaps.R; */ public class TimeListEdit { + private static Logger log = LoggerFactory.getLogger(TimeListEdit.class); LinearLayout layout; Context context; View view; int resLayoutId; + String label; JSONArray data; - boolean per30min = false; + NumberFormat formatter; String array1; String array2; - public TimeListEdit(Context context, View view, int resLayoutId, JSONArray data, String array1, String array2, boolean per30min) { + public TimeListEdit(Context context, View view, int resLayoutId, String label, JSONArray data, String array1, String array2, NumberFormat formatter) { this.context = context; this.view = view; this.resLayoutId = resLayoutId; + this.label = label; this.data = data; this.array1 = array1; this.array2 = array2; - this.per30min = per30min; + this.formatter = formatter; buildView(); } private void buildView() { - /* LayoutInflater inflater = LayoutInflater.from(context); layout = (LinearLayout) view.findViewById(resLayoutId); - final EditText[] editTexts = new EditText[24]; + layout.removeAllViews(); - for (int i = 0; i < 24; i++) { + TextView textlabel = new TextView(context); + textlabel.setText(label); + textlabel.setGravity(Gravity.LEFT); + LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); + llp.setMargins(10, 0, 0, 0); // llp.setMargins(left, top, right, bottom); + textlabel.setLayoutParams(llp); + layout.addView(textlabel); + + for (int i = 0; i < itemsCount(); i++) { View childview = inflater.inflate(R.layout.timelistedit_element, layout, false); - ((TextView) childview.findViewById(R.id.basal_time_elem)).setText((i < 10 ? "0" : "") + i + ":00: "); + final Spinner timeSpinner = (Spinner) childview.findViewById(R.id.timelistedit_time); + int previous = i == 0 ? -1 * 60 * 60 : secondFromMidnight(i - 1); + int next = i == itemsCount() - 1 ? 24 * 60 * 60 : secondFromMidnight(i + 1); + if (i == 0) next = 60 * 60; + fillSpinner(timeSpinner, secondFromMidnight(i), previous, next); - ImageView copyprevbutton = (ImageView) childview.findViewById(R.id.basal_copyprev_elem); - - if (i == 0) { - copyprevbutton.setVisibility(View.INVISIBLE); - ; - } else { - final int j = i; //needs to be final to be passed to inner class. - copyprevbutton.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View view) { - editTexts[j].setText(editTexts[j - 1].getText()); - } - }); + final EditText editText1 = (EditText) childview.findViewById(R.id.timelistedit_edit1); + fillNumber(editText1, value1(i)); + final EditText editText2 = ((EditText) childview.findViewById(R.id.timelistedit_edit2)); + fillNumber(editText2, value2(i)); + if (array2 == null) { + editText2.setVisibility(View.GONE); } - editTexts[i] = ((EditText) childview.findViewById(R.id.basal_edittext_elem)); - //editTexts[i].setText(DecimalFormatter.to2Decimal(values[i])); - layout.addView(childview); + ImageView addbutton = (ImageView) childview.findViewById(R.id.timelistedit_add); + ImageView removebutton = (ImageView) childview.findViewById(R.id.timelistedit_remove); + + + final int fixedPos = i; + addbutton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + addItem(fixedPos, 0, 0, 0); + log(); + buildView(); + } + }); + + removebutton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + removeItem(fixedPos); + log(); + buildView(); + } + }); + + timeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { + @Override + public void onItemSelected(AdapterView parent, View view, int position, long id) { + int seconds = DateUtil.toSeconds(timeSpinner.getSelectedItem().toString()); + editItem(fixedPos, seconds, value1(fixedPos), value2(fixedPos)); + log(); + buildView(); + } + + @Override + public void onNothingSelected(AdapterView parent) { + editItem(fixedPos, 0, value1(fixedPos), value2(fixedPos)); + log(); + buildView(); + } + } + ); + + editText1.addTextChangedListener(new TextWatcher() { + @Override + public void afterTextChanged(Editable s) { + editItem(fixedPos, secondFromMidnight(fixedPos), SafeParse.stringToDouble(editText1.getText().toString()), value2(fixedPos)); + log(); + } + + @Override + public void beforeTextChanged(CharSequence s, int start, + int count, int after) { + } + + @Override + public void onTextChanged(CharSequence s, int start, + int before, int count) { + } + }); + + editText2.addTextChangedListener(new TextWatcher() { + @Override + public void afterTextChanged(Editable s) { + editItem(fixedPos, secondFromMidnight(fixedPos), value1(fixedPos), SafeParse.stringToDouble(editText2.getText().toString())); + log(); + } + + @Override + public void beforeTextChanged(CharSequence s, int start, + int count, int after) { + } + + @Override + public void onTextChanged(CharSequence s, int start, + int before, int count) { + } + }); + + layout.addView(childview); } - */ + + if (!(itemsCount() > 0 && secondFromMidnight(itemsCount() - 1) == 23 * 60 * 60)) { + ImageView imageView = new ImageView(context); + imageView.setImageResource(R.drawable.add); + layout.addView(imageView); + imageView.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + addItem(itemsCount(), itemsCount() > 0 ? secondFromMidnight(itemsCount() - 1) + 60 * 60 : 0, 0, 0); + log(); + buildView(); + } + }); + } + + } + + public void fillSpinner(Spinner spinner, int secondsFromMidnight, int previous, int next) { + int posInList = 0; + ArrayList timeList = new ArrayList<>(); + DateFormat df = new SimpleDateFormat("HH:mm"); + int pos = 0; + for (int t = previous + 60 * 60; t < next; t += 60 * 60) { + timeList.add(df.format(DateUtil.toDate(t))); + if (secondsFromMidnight == t) posInList = pos; + pos++; + } + + ArrayAdapter adapter = new ArrayAdapter(context, + android.R.layout.simple_spinner_item, timeList); + adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); + spinner.setAdapter(adapter); + spinner.setSelection(posInList, false); + } + + public void fillNumber(EditText edit, Double value) { + if (value.equals(0d)) + edit.setText(""); + else + edit.setText(formatter.format(value)); } public int itemsCount() { @@ -151,4 +286,12 @@ public class TimeListEdit { public void removeItem(int index) { data.remove(index); } + + void log() { + DateFormat df = new SimpleDateFormat("HH:mm"); + for (int i = 0; i < data.length(); i++) { + int pos = 0; + log.debug(i + ": " + df.format(DateUtil.toDate(secondFromMidnight(i))) + " " + array1 + ": " + value1(i) + (array2 != null ? " " + array2 + ": " + value2(i) : "")); + } + } } diff --git a/app/src/main/res/layout/simpleprofile_fragment.xml b/app/src/main/res/layout/simpleprofile_fragment.xml index ad1022b3f0..f006a646d3 100644 --- a/app/src/main/res/layout/simpleprofile_fragment.xml +++ b/app/src/main/res/layout/simpleprofile_fragment.xml @@ -28,16 +28,12 @@ android:id="@+id/simpleprofile_mgdl" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:layout_column="2" - android:layout_weight="1" android:text="@string/mgdl" /> @@ -45,7 +41,6 @@ @@ -53,7 +48,6 @@ android:id="@+id/simpleprofile_dia" android:layout_width="match_parent" android:layout_height="wrap_content" - android:layout_column="2" android:inputType="numberDecimal" /> @@ -152,6 +138,13 @@ android:paddingRight="10dp" android:text="@string/send_to_pump" android:textColor="@color/colorProfileSwitchButton" /> + + + diff --git a/app/src/main/res/layout/timelistedit_element.xml b/app/src/main/res/layout/timelistedit_element.xml index 1cb652308e..e6b9415476 100644 --- a/app/src/main/res/layout/timelistedit_element.xml +++ b/app/src/main/res/layout/timelistedit_element.xml @@ -2,7 +2,6 @@ xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content" - android:background="@color/cardColorBackground" android:gravity="center_horizontal" android:orientation="horizontal" android:paddingTop="5dp" @@ -34,7 +33,7 @@ android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginLeft="15dp" - tools:src="@drawable/add" /> + android:src="@drawable/add" /> + android:src="@drawable/remove" /> diff --git a/app/src/test/java/info/nightscout/utils/TimeListEditTest.java b/app/src/test/java/info/nightscout/utils/TimeListEditTest.java index f7c3767a65..a7d2359f1d 100644 --- a/app/src/test/java/info/nightscout/utils/TimeListEditTest.java +++ b/app/src/test/java/info/nightscout/utils/TimeListEditTest.java @@ -1,21 +1,29 @@ package info.nightscout.utils; +import android.content.Context; +import android.view.LayoutInflater; + import org.json.JSONArray; import org.junit.Test; +import org.mockito.Mock; import java.lang.reflect.Method; +import java.text.DecimalFormat; import static org.junit.Assert.*; +import static org.mockito.Mockito.when; /** * Created by mike on 30.12.2016. */ public class TimeListEditTest { +/* JSONArray data = new JSONArray(); JSONArray data2 = new JSONArray(); - TimeListEdit tle = new TimeListEdit(null, null, 0, data, "ic", null, false); - TimeListEdit tle2 = new TimeListEdit(null, null, 0, data2, "ic", "ic2", false); + TimeListEdit tle = new TimeListEdit(null, null, 0, "Test1", data, "ic", null, new DecimalFormat("0.00")); + TimeListEdit tle2 = new TimeListEdit(null, null, 0, "Test2", data2, "ic", "ic2", new DecimalFormat("0.00")); + @Test public void doArrayTest() throws Exception { @@ -41,4 +49,5 @@ public class TimeListEditTest { assertEquals(2d, tle2.value2(0), 0.00001d); } +*/ } \ No newline at end of file From 0458ad65ce125ab0af05ddfdac3e66ad54a2e843 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Sat, 31 Dec 2016 14:52:23 +0100 Subject: [PATCH 3/5] handle arrays separately --- .../SimpleProfile/SimpleProfileFragment.java | 5 +- .../info/nightscout/utils/TimeListEdit.java | 99 +++++++++++-------- .../nightscout/utils/TimeListEditTest.java | 58 ++++++----- 3 files changed, 93 insertions(+), 69 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/SimpleProfile/SimpleProfileFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/SimpleProfile/SimpleProfileFragment.java index 0fb799c7cf..090c2542e2 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/SimpleProfile/SimpleProfileFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/SimpleProfile/SimpleProfileFragment.java @@ -53,7 +53,8 @@ public class SimpleProfileFragment extends Fragment implements FragmentBase { EditText targethighView; Button profileswitchButton; TimeListEdit test; - JSONArray data = new JSONArray(); + JSONArray data1 = new JSONArray(); + JSONArray data2 = new JSONArray(); @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, @@ -69,7 +70,7 @@ public class SimpleProfileFragment extends Fragment implements FragmentBase { targetlowView = (EditText) layout.findViewById(R.id.simpleprofile_targetlow); targethighView = (EditText) layout.findViewById(R.id.simpleprofile_targethigh); profileswitchButton = (Button) layout.findViewById(R.id.simpleprofile_profileswitch); - test = new TimeListEdit(getContext(), layout, R.id.simpleprofile_test, "Test", data, "ic1", null, new DecimalFormat("0.00")); + test = new TimeListEdit(getContext(), layout, R.id.simpleprofile_test, "Test", data1, data2, new DecimalFormat("0.00")); onStatusEvent(null); diff --git a/app/src/main/java/info/nightscout/utils/TimeListEdit.java b/app/src/main/java/info/nightscout/utils/TimeListEdit.java index ae828b4ecf..9342dfcba9 100644 --- a/app/src/main/java/info/nightscout/utils/TimeListEdit.java +++ b/app/src/main/java/info/nightscout/utils/TimeListEdit.java @@ -35,25 +35,25 @@ import info.nightscout.androidaps.R; public class TimeListEdit { private static Logger log = LoggerFactory.getLogger(TimeListEdit.class); + final int ONEHOURINSECONDS = 60 * 60; + LinearLayout layout; Context context; View view; int resLayoutId; String label; - JSONArray data; + JSONArray data1; + JSONArray data2; NumberFormat formatter; - String array1; - String array2; - public TimeListEdit(Context context, View view, int resLayoutId, String label, JSONArray data, String array1, String array2, NumberFormat formatter) { + public TimeListEdit(Context context, View view, int resLayoutId, String label, JSONArray data1, JSONArray data2, NumberFormat formatter) { this.context = context; this.view = view; this.resLayoutId = resLayoutId; this.label = label; - this.data = data; - this.array1 = array1; - this.array2 = array2; + this.data1 = data1; + this.data2 = data2; this.formatter = formatter; buildView(); } @@ -75,16 +75,16 @@ public class TimeListEdit { for (int i = 0; i < itemsCount(); i++) { View childview = inflater.inflate(R.layout.timelistedit_element, layout, false); final Spinner timeSpinner = (Spinner) childview.findViewById(R.id.timelistedit_time); - int previous = i == 0 ? -1 * 60 * 60 : secondFromMidnight(i - 1); - int next = i == itemsCount() - 1 ? 24 * 60 * 60 : secondFromMidnight(i + 1); - if (i == 0) next = 60 * 60; + int previous = i == 0 ? -1 * ONEHOURINSECONDS : secondFromMidnight(i - 1); + int next = i == itemsCount() - 1 ? 24 * ONEHOURINSECONDS : secondFromMidnight(i + 1); + if (i == 0) next = ONEHOURINSECONDS; fillSpinner(timeSpinner, secondFromMidnight(i), previous, next); final EditText editText1 = (EditText) childview.findViewById(R.id.timelistedit_edit1); fillNumber(editText1, value1(i)); final EditText editText2 = ((EditText) childview.findViewById(R.id.timelistedit_edit2)); fillNumber(editText2, value2(i)); - if (array2 == null) { + if (data2 == null) { editText2.setVisibility(View.GONE); } @@ -92,12 +92,28 @@ public class TimeListEdit { ImageView addbutton = (ImageView) childview.findViewById(R.id.timelistedit_add); ImageView removebutton = (ImageView) childview.findViewById(R.id.timelistedit_remove); + if (itemsCount() == 1 && i == 0) { + removebutton.setVisibility(View.GONE); + } + + if (itemsCount() >= 24) { + addbutton.setVisibility(View.GONE); + } final int fixedPos = i; addbutton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { - addItem(fixedPos, 0, 0, 0); + int seconds = secondFromMidnight(fixedPos); + addItem(fixedPos, seconds, 0, 0); + // for here for the rest of values + for (int i = fixedPos + 1; i < itemsCount(); i++) { + if (secondFromMidnight(i - 1) >= secondFromMidnight(i)) { + editItem(i, secondFromMidnight(i - 1) + ONEHOURINSECONDS, value1(i), value2(i)); + } + } + while (itemsCount() > 24 || secondFromMidnight(itemsCount() - 1) > 23 * ONEHOURINSECONDS) + removeItem(itemsCount() - 1); log(); buildView(); } @@ -169,14 +185,14 @@ public class TimeListEdit { layout.addView(childview); } - if (!(itemsCount() > 0 && secondFromMidnight(itemsCount() - 1) == 23 * 60 * 60)) { + if (!(itemsCount() > 0 && secondFromMidnight(itemsCount() - 1) == 23 * ONEHOURINSECONDS)) { ImageView imageView = new ImageView(context); imageView.setImageResource(R.drawable.add); layout.addView(imageView); imageView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { - addItem(itemsCount(), itemsCount() > 0 ? secondFromMidnight(itemsCount() - 1) + 60 * 60 : 0, 0, 0); + addItem(itemsCount(), itemsCount() > 0 ? secondFromMidnight(itemsCount() - 1) + ONEHOURINSECONDS : 0, 0, 0); log(); buildView(); } @@ -190,7 +206,7 @@ public class TimeListEdit { ArrayList timeList = new ArrayList<>(); DateFormat df = new SimpleDateFormat("HH:mm"); int pos = 0; - for (int t = previous + 60 * 60; t < next; t += 60 * 60) { + for (int t = previous + ONEHOURINSECONDS; t < next; t += ONEHOURINSECONDS) { timeList.add(df.format(DateUtil.toDate(t))); if (secondsFromMidnight == t) posInList = pos; pos++; @@ -211,12 +227,12 @@ public class TimeListEdit { } public int itemsCount() { - return data.length(); + return data1.length(); } public int secondFromMidnight(int index) { try { - JSONObject item = (JSONObject) data.get(index); + JSONObject item = (JSONObject) data1.get(index); if (item.has("timeAsSeconds")) { return item.getInt("timeAsSeconds"); } @@ -228,9 +244,9 @@ public class TimeListEdit { public double value1(int index) { try { - JSONObject item = (JSONObject) data.get(index); - if (item.has(array1)) { - return item.getDouble(array1); + JSONObject item = (JSONObject) data1.get(index); + if (item.has("value")) { + return item.getDouble("value"); } } catch (JSONException e) { e.printStackTrace(); @@ -240,9 +256,9 @@ public class TimeListEdit { public double value2(int index) { try { - JSONObject item = (JSONObject) data.get(index); - if (item.has(array2)) { - return item.getDouble(array2); + JSONObject item = (JSONObject) data2.get(index); + if (item.has("value")) { + return item.getDouble("value"); } } catch (JSONException e) { e.printStackTrace(); @@ -252,12 +268,16 @@ public class TimeListEdit { public void editItem(int index, int timeAsSeconds, double value1, double value2) { try { - JSONObject newObject = new JSONObject(); - newObject.put("timeAsSeconds", timeAsSeconds); - newObject.put(array1, value1); - if (array2 != null) - newObject.put(array2, value2); - data.put(index, newObject); + JSONObject newObject1 = new JSONObject(); + newObject1.put("timeAsSeconds", timeAsSeconds); + newObject1.put("value", value1); + data1.put(index, newObject1); + if (data2 != null) { + JSONObject newObject2 = new JSONObject(); + newObject2.put("timeAsSeconds", timeAsSeconds); + newObject2.put("value", value2); + data2.put(index, newObject2); + } } catch (JSONException e) { e.printStackTrace(); } @@ -267,16 +287,13 @@ public class TimeListEdit { public void addItem(int index, int timeAsSeconds, double value1, double value2) { try { // shift data - for (int i = data.length(); i > index; i--) { - data.put(i, data.get(i - 1)); + for (int i = data1.length(); i > index; i--) { + data1.put(i, data1.get(i - 1)); + if (data2 != null) + data2.put(i, data2.get(i - 1)); } // add new object - JSONObject newObject = new JSONObject(); - newObject.put("timeAsSeconds", timeAsSeconds); - newObject.put(array1, value1); - if (array2 != null) - newObject.put(array2, value2); - data.put(index, newObject); + editItem(index, timeAsSeconds, value1, value2); } catch (JSONException e) { e.printStackTrace(); } @@ -284,14 +301,16 @@ public class TimeListEdit { } public void removeItem(int index) { - data.remove(index); + data1.remove(index); + if (data2 != null) + data2.remove(index); } void log() { DateFormat df = new SimpleDateFormat("HH:mm"); - for (int i = 0; i < data.length(); i++) { + for (int i = 0; i < data1.length(); i++) { int pos = 0; - log.debug(i + ": " + df.format(DateUtil.toDate(secondFromMidnight(i))) + " " + array1 + ": " + value1(i) + (array2 != null ? " " + array2 + ": " + value2(i) : "")); + log.debug(i + ": @" + df.format(DateUtil.toDate(secondFromMidnight(i))) + " " + value1(i) + (data2 != null ? " " + value2(i) : "")); } } } diff --git a/app/src/test/java/info/nightscout/utils/TimeListEditTest.java b/app/src/test/java/info/nightscout/utils/TimeListEditTest.java index a7d2359f1d..8fd387eebd 100644 --- a/app/src/test/java/info/nightscout/utils/TimeListEditTest.java +++ b/app/src/test/java/info/nightscout/utils/TimeListEditTest.java @@ -18,36 +18,40 @@ import static org.mockito.Mockito.when; */ public class TimeListEditTest { -/* - JSONArray data = new JSONArray(); - JSONArray data2 = new JSONArray(); - TimeListEdit tle = new TimeListEdit(null, null, 0, "Test1", data, "ic", null, new DecimalFormat("0.00")); - TimeListEdit tle2 = new TimeListEdit(null, null, 0, "Test2", data2, "ic", "ic2", new DecimalFormat("0.00")); + /* + JSONArray data = new JSONArray(); + JSONArray data2 = new JSONArray(); + TimeListEdit tle = new TimeListEdit(null, null, 0, "Test1", data, "ic", null, new DecimalFormat("0.00")); + TimeListEdit tle2 = new TimeListEdit(null, null, 0, "Test2", data2, "ic", "ic2", new DecimalFormat("0.00")); + @Test + public void doArrayTest() throws Exception { + tle.addItem(0, 0, 0.1, 0); + tle.addItem(1, 60 * 60, 0.2, 0); + assertEquals(2, tle.itemsCount()); + + tle.editItem(0, 2 * 60 * 60, 1, 0); + assertEquals(2, tle.itemsCount()); + assertEquals(1d, tle.value1(0), 0.00001d); + assertEquals( 2 * 60 * 60, tle.secondFromMidnight(0)); + tle.removeItem(0); + assertEquals(0.2d, tle.value1(0), 0.00001d); + assertEquals(0, tle.value2(0), 0.00001d); + assertEquals(60 * 60, tle.secondFromMidnight(0)); + + //System.out.print(tle2.toString()); + assertEquals(0, tle2.itemsCount()); + tle2.addItem(0, 0, 1, 2); + assertEquals(1, tle2.itemsCount()); + assertEquals(0, tle2.secondFromMidnight(0)); + assertEquals(1d, tle2.value1(0), 0.00001d); + assertEquals(2d, tle2.value2(0), 0.00001d); + } + + */ @Test - public void doArrayTest() throws Exception { - tle.addItem(0, 0, 0.1, 0); - tle.addItem(1, 60 * 60, 0.2, 0); - assertEquals(2, tle.itemsCount()); - - tle.editItem(0, 2 * 60 * 60, 1, 0); - assertEquals(2, tle.itemsCount()); - assertEquals(1d, tle.value1(0), 0.00001d); - assertEquals( 2 * 60 * 60, tle.secondFromMidnight(0)); - tle.removeItem(0); - assertEquals(0.2d, tle.value1(0), 0.00001d); - assertEquals(0, tle.value2(0), 0.00001d); - assertEquals(60 * 60, tle.secondFromMidnight(0)); - - //System.out.print(tle2.toString()); - assertEquals(0, tle2.itemsCount()); - tle2.addItem(0, 0, 1, 2); - assertEquals(1, tle2.itemsCount()); - assertEquals(0, tle2.secondFromMidnight(0)); - assertEquals(1d, tle2.value1(0), 0.00001d); - assertEquals(2d, tle2.value2(0), 0.00001d); + public void fakeTest() throws Exception { } -*/ } \ No newline at end of file From 6b18336fa273e418af7795b9fb3d6a23a1c1167c Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Sat, 31 Dec 2016 15:06:45 +0100 Subject: [PATCH 4/5] LocalProfile plugin --- .../info/nightscout/androidaps/MainApp.java | 2 + .../LocalProfile/LocalProfileFragment.java | 174 +++++++++++++ .../LocalProfile/LocalProfilePlugin.java | 243 ++++++++++++++++++ .../SimpleProfile/SimpleProfileFragment.java | 4 - app/src/main/res/values/strings.xml | 1 + 5 files changed, 420 insertions(+), 4 deletions(-) create mode 100644 app/src/main/java/info/nightscout/androidaps/plugins/LocalProfile/LocalProfileFragment.java create mode 100644 app/src/main/java/info/nightscout/androidaps/plugins/LocalProfile/LocalProfilePlugin.java diff --git a/app/src/main/java/info/nightscout/androidaps/MainApp.java b/app/src/main/java/info/nightscout/androidaps/MainApp.java index b647e9b6d1..8f9d7606e8 100644 --- a/app/src/main/java/info/nightscout/androidaps/MainApp.java +++ b/app/src/main/java/info/nightscout/androidaps/MainApp.java @@ -24,6 +24,7 @@ import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderFragment; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.DanaR.DanaRFragment; import info.nightscout.androidaps.plugins.DanaRKorean.DanaRKoreanFragment; +import info.nightscout.androidaps.plugins.LocalProfile.LocalProfileFragment; import info.nightscout.androidaps.plugins.Loop.LoopFragment; import info.nightscout.androidaps.plugins.MDI.MDIFragment; import info.nightscout.androidaps.plugins.NSProfile.NSProfileFragment; @@ -83,6 +84,7 @@ public class MainApp extends Application { if (Config.OPENAPSMAENABLED) pluginsList.add(OpenAPSMAFragment.getPlugin()); pluginsList.add(NSProfileFragment.getPlugin()); pluginsList.add(SimpleProfileFragment.getPlugin()); + pluginsList.add(LocalProfileFragment.getPlugin()); pluginsList.add(CircadianPercentageProfileFragment.getPlugin()); pluginsList.add(TreatmentsFragment.getPlugin()); pluginsList.add(TempBasalsFragment.getPlugin()); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/LocalProfile/LocalProfileFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/LocalProfile/LocalProfileFragment.java new file mode 100644 index 0000000000..8c9a156d33 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/LocalProfile/LocalProfileFragment.java @@ -0,0 +1,174 @@ +package info.nightscout.androidaps.plugins.LocalProfile; + + +import android.app.Activity; +import android.os.Bundle; +import android.support.v4.app.Fragment; +import android.text.Editable; +import android.text.TextWatcher; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.Button; +import android.widget.EditText; +import android.widget.RadioButton; + +import com.squareup.otto.Subscribe; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.R; +import info.nightscout.androidaps.events.EventInitializationChanged; +import info.nightscout.androidaps.interfaces.FragmentBase; +import info.nightscout.androidaps.plugins.Careportal.Dialogs.NewNSTreatmentDialog; +import info.nightscout.androidaps.plugins.Careportal.OptionsToShow; +import info.nightscout.androidaps.plugins.SimpleProfile.SimpleProfilePlugin; +import info.nightscout.utils.SafeParse; + +public class LocalProfileFragment extends Fragment implements FragmentBase { + private static Logger log = LoggerFactory.getLogger(LocalProfileFragment.class); + + private static LocalProfilePlugin localProfilePlugin = new LocalProfilePlugin(); + + public static LocalProfilePlugin getPlugin() { + return localProfilePlugin; + } + + EditText diaView; + RadioButton mgdlView; + RadioButton mmolView; + EditText icView; + EditText isfView; + EditText carView; + EditText basalView; + EditText targetlowView; + EditText targethighView; + Button profileswitchButton; + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + View layout = inflater.inflate(R.layout.simpleprofile_fragment, container, false); + diaView = (EditText) layout.findViewById(R.id.simpleprofile_dia); + mgdlView = (RadioButton) layout.findViewById(R.id.simpleprofile_mgdl); + mmolView = (RadioButton) layout.findViewById(R.id.simpleprofile_mmol); + icView = (EditText) layout.findViewById(R.id.simpleprofile_ic); + isfView = (EditText) layout.findViewById(R.id.simpleprofile_isf); + carView = (EditText) layout.findViewById(R.id.simpleprofile_car); + basalView = (EditText) layout.findViewById(R.id.simpleprofile_basalrate); + targetlowView = (EditText) layout.findViewById(R.id.simpleprofile_targetlow); + targethighView = (EditText) layout.findViewById(R.id.simpleprofile_targethigh); + profileswitchButton = (Button) layout.findViewById(R.id.simpleprofile_profileswitch); + + onStatusEvent(null); + + mgdlView.setChecked(localProfilePlugin.mgdl); + mmolView.setChecked(localProfilePlugin.mmol); + diaView.setText(localProfilePlugin.dia.toString()); + icView.setText(localProfilePlugin.ic.toString()); + isfView.setText(localProfilePlugin.isf.toString()); + carView.setText(localProfilePlugin.car.toString()); + basalView.setText(localProfilePlugin.basal.toString()); + targetlowView.setText(localProfilePlugin.targetLow.toString()); + targethighView.setText(localProfilePlugin.targetHigh.toString()); + + mgdlView.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + localProfilePlugin.mgdl = mgdlView.isChecked(); + localProfilePlugin.mmol = !localProfilePlugin.mgdl; + mmolView.setChecked(localProfilePlugin.mmol); + localProfilePlugin.storeSettings(); + } + }); + mmolView.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + localProfilePlugin.mmol = mmolView.isChecked(); + localProfilePlugin.mgdl = !localProfilePlugin.mmol; + mgdlView.setChecked(localProfilePlugin.mgdl); + localProfilePlugin.storeSettings(); + } + }); + + profileswitchButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + NewNSTreatmentDialog newDialog = new NewNSTreatmentDialog(); + final OptionsToShow profileswitch = new OptionsToShow(R.id.careportal_profileswitch, R.string.careportal_profileswitch, true, false, false, false, false, false, false, true, false); + profileswitch.executeProfileSwitch = true; + newDialog.setOptions(profileswitch); + newDialog.show(getFragmentManager(), "NewNSTreatmentDialog"); + } + }); + + TextWatcher textWatch = new TextWatcher() { + + @Override + public void afterTextChanged(Editable s) { + } + + @Override + public void beforeTextChanged(CharSequence s, int start, + int count, int after) { + } + + @Override + public void onTextChanged(CharSequence s, int start, + int before, int count) { + localProfilePlugin.dia = SafeParse.stringToDouble(diaView.getText().toString()); + localProfilePlugin.ic = SafeParse.stringToDouble(icView.getText().toString()); + localProfilePlugin.isf = SafeParse.stringToDouble(isfView.getText().toString()); + localProfilePlugin.car = SafeParse.stringToDouble(carView.getText().toString()); + localProfilePlugin.basal = SafeParse.stringToDouble(basalView.getText().toString()); + localProfilePlugin.targetLow = SafeParse.stringToDouble(targetlowView.getText().toString()); + localProfilePlugin.targetHigh = SafeParse.stringToDouble(targethighView.getText().toString()); + localProfilePlugin.storeSettings(); + } + }; + + diaView.addTextChangedListener(textWatch); + icView.addTextChangedListener(textWatch); + isfView.addTextChangedListener(textWatch); + carView.addTextChangedListener(textWatch); + basalView.addTextChangedListener(textWatch); + targetlowView.addTextChangedListener(textWatch); + targethighView.addTextChangedListener(textWatch); + + onStatusEvent(null); + + return layout; + } + + @Override + public void onPause() { + super.onPause(); + MainApp.bus().unregister(this); + } + + @Override + public void onResume() { + super.onResume(); + MainApp.bus().register(this); + onStatusEvent(null); + } + + @Subscribe + public void onStatusEvent(final EventInitializationChanged e) { + Activity activity = getActivity(); + if (activity != null) + activity.runOnUiThread(new Runnable() { + @Override + public void run() { + if (!MainApp.getConfigBuilder().isInitialized() || !MainApp.getConfigBuilder().getPumpDescription().isSetBasalProfileCapable) { + profileswitchButton.setVisibility(View.GONE); + } else { + profileswitchButton.setVisibility(View.VISIBLE); + } + } + }); + } + +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/LocalProfile/LocalProfilePlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/LocalProfile/LocalProfilePlugin.java new file mode 100644 index 0000000000..37daeac883 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/LocalProfile/LocalProfilePlugin.java @@ -0,0 +1,243 @@ +package info.nightscout.androidaps.plugins.LocalProfile; + +import android.content.SharedPreferences; +import android.preference.PreferenceManager; + +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import info.nightscout.androidaps.Config; +import info.nightscout.androidaps.Constants; +import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.R; +import info.nightscout.androidaps.interfaces.PluginBase; +import info.nightscout.androidaps.interfaces.ProfileInterface; +import info.nightscout.androidaps.plugins.SimpleProfile.SimpleProfileFragment; +import info.nightscout.client.data.NSProfile; +import info.nightscout.utils.SafeParse; + +/** + * Created by mike on 05.08.2016. + */ +public class LocalProfilePlugin implements PluginBase, ProfileInterface { + private static Logger log = LoggerFactory.getLogger(LocalProfilePlugin.class); + + private static boolean fragmentEnabled = true; + private static boolean fragmentVisible = true; + + private static NSProfile convertedProfile = null; + + boolean mgdl; + boolean mmol; + Double dia; + Double ic; + Double isf; + Double car; + Double basal; + Double targetLow; + Double targetHigh; + + public LocalProfilePlugin() { + loadSettings(); + } + + @Override + public String getFragmentClass() { + return LocalProfileFragment.class.getName(); + } + + @Override + public int getType() { + return PluginBase.PROFILE; + } + + @Override + public String getName() { + return MainApp.instance().getString(R.string.localprofile); + } + + @Override + public boolean isEnabled(int type) { + return type == PROFILE && fragmentEnabled; + } + + @Override + public boolean isVisibleInTabs(int type) { + return type == PROFILE && fragmentVisible; + } + + @Override + public boolean canBeHidden(int type) { + return true; + } + + @Override + public void setFragmentEnabled(int type, boolean fragmentEnabled) { + if (type == PROFILE) this.fragmentEnabled = fragmentEnabled; + } + + @Override + public void setFragmentVisible(int type, boolean fragmentVisible) { + if (type == PROFILE) this.fragmentVisible = fragmentVisible; + } + + public void storeSettings() { + if (Config.logPrefsChange) + log.debug("Storing settings"); + SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext()); + SharedPreferences.Editor editor = settings.edit(); + editor.putBoolean("SimpleProfile" + "mmol", mmol); + editor.putBoolean("SimpleProfile" + "mgdl", mgdl); + editor.putString("SimpleProfile" + "dia", dia.toString()); + editor.putString("SimpleProfile" + "ic", ic.toString()); + editor.putString("SimpleProfile" + "isf", isf.toString()); + editor.putString("SimpleProfile" + "car", car.toString()); + editor.putString("SimpleProfile" + "basal", basal.toString()); + editor.putString("SimpleProfile" + "targetlow", targetLow.toString()); + editor.putString("SimpleProfile" + "targethigh", targetHigh.toString()); + + editor.commit(); + createConvertedProfile(); + } + + private void loadSettings() { + if (Config.logPrefsChange) + log.debug("Loading stored settings"); + SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext()); + + if (settings.contains("SimpleProfile" + "mgdl")) + try { + mgdl = settings.getBoolean("SimpleProfile" + "mgdl", true); + } catch (Exception e) { + log.debug(e.getMessage()); + } + else mgdl = true; + if (settings.contains("SimpleProfile" + "mmol")) + try { + mmol = settings.getBoolean("SimpleProfile" + "mmol", false); + } catch (Exception e) { + log.debug(e.getMessage()); + } + else mmol = false; + if (settings.contains("SimpleProfile" + "dia")) + try { + dia = SafeParse.stringToDouble(settings.getString("SimpleProfile" + "dia", "3")); + } catch (Exception e) { + log.debug(e.getMessage()); + } + else dia = 3d; + if (settings.contains("SimpleProfile" + "ic")) + try { + ic = SafeParse.stringToDouble(settings.getString("SimpleProfile" + "ic", "20")); + } catch (Exception e) { + log.debug(e.getMessage()); + } + else ic = 20d; + if (settings.contains("SimpleProfile" + "isf")) + try { + isf = SafeParse.stringToDouble(settings.getString("SimpleProfile" + "isf", "200")); + } catch (Exception e) { + log.debug(e.getMessage()); + } + else isf = 200d; + if (settings.contains("SimpleProfile" + "car")) + try { + car = SafeParse.stringToDouble(settings.getString("SimpleProfile" + "car", "20")); + } catch (Exception e) { + log.debug(e.getMessage()); + } + else car = 20d; + if (settings.contains("SimpleProfile" + "basal")) + try { + basal = SafeParse.stringToDouble(settings.getString("SimpleProfile" + "basal", "1")); + } catch (Exception e) { + log.debug(e.getMessage()); + } + else basal = 1d; + if (settings.contains("SimpleProfile" + "targetlow")) + try { + targetLow = SafeParse.stringToDouble(settings.getString("SimpleProfile" + "targetlow", "80")); + } catch (Exception e) { + log.debug(e.getMessage()); + } + else targetLow = 80d; + if (settings.contains("SimpleProfile" + "targethigh")) + try { + targetHigh = SafeParse.stringToDouble(settings.getString("SimpleProfile" + "targethigh", "120")); + } catch (Exception e) { + log.debug(e.getMessage()); + } + else targetHigh = 120d; + createConvertedProfile(); + } + + /* + { + "_id": "576264a12771b7500d7ad184", + "startDate": "2016-06-16T08:35:00.000Z", + "defaultProfile": "Default", + "store": { + "Default": { + "dia": "3", + "carbratio": [{ + "time": "00:00", + "value": "30" + }], + "carbs_hr": "20", + "delay": "20", + "sens": [{ + "time": "00:00", + "value": "100" + }], + "timezone": "UTC", + "basal": [{ + "time": "00:00", + "value": "0.1" + }], + "target_low": [{ + "time": "00:00", + "value": "0" + }], + "target_high": [{ + "time": "00:00", + "value": "0" + }], + "startDate": "1970-01-01T00:00:00.000Z", + "units": "mmol" + } + }, + "created_at": "2016-06-16T08:34:41.256Z" + } + */ + void createConvertedProfile() { + JSONObject json = new JSONObject(); + JSONObject store = new JSONObject(); + JSONObject profile = new JSONObject(); + + try { + json.put("defaultProfile", "SimpleProfile"); + json.put("store", store); + profile.put("dia", dia); + profile.put("carbratio", new JSONArray().put(new JSONObject().put("timeAsSeconds", 0).put("value", ic))); + profile.put("carbs_hr", car); + profile.put("sens", new JSONArray().put(new JSONObject().put("timeAsSeconds", 0).put("value", isf))); + profile.put("basal", new JSONArray().put(new JSONObject().put("timeAsSeconds", 0).put("value", basal))); + profile.put("target_low", new JSONArray().put(new JSONObject().put("timeAsSeconds", 0).put("value", targetLow))); + profile.put("target_high", new JSONArray().put(new JSONObject().put("timeAsSeconds", 0).put("value", targetHigh))); + profile.put("units", mgdl ? Constants.MGDL : Constants.MMOL); + store.put("SimpleProfile", profile); + } catch (JSONException e) { + e.printStackTrace(); + } + convertedProfile = new NSProfile(json, "SimpleProfile"); + } + + @Override + public NSProfile getProfile() { + return convertedProfile; + } + +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/SimpleProfile/SimpleProfileFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/SimpleProfile/SimpleProfileFragment.java index 090c2542e2..1d0381ce3b 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/SimpleProfile/SimpleProfileFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/SimpleProfile/SimpleProfileFragment.java @@ -52,9 +52,6 @@ public class SimpleProfileFragment extends Fragment implements FragmentBase { EditText targetlowView; EditText targethighView; Button profileswitchButton; - TimeListEdit test; - JSONArray data1 = new JSONArray(); - JSONArray data2 = new JSONArray(); @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, @@ -70,7 +67,6 @@ public class SimpleProfileFragment extends Fragment implements FragmentBase { targetlowView = (EditText) layout.findViewById(R.id.simpleprofile_targetlow); targethighView = (EditText) layout.findViewById(R.id.simpleprofile_targethigh); profileswitchButton = (Button) layout.findViewById(R.id.simpleprofile_profileswitch); - test = new TimeListEdit(getContext(), layout, R.id.simpleprofile_test, "Test", data1, data2, new DecimalFormat("0.00")); onStatusEvent(null); diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index b147ca2fbc..9359821c78 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -395,4 +395,5 @@ OLD DATA %dmin ago %dmin ago + Local Profile From 38a9c942584535c2289ce6a22986c2c3466e13b7 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Sat, 31 Dec 2016 20:10:36 +0100 Subject: [PATCH 5/5] Local Profile --- .../LocalProfile/LocalProfileFragment.java | 57 +++---- .../LocalProfile/LocalProfilePlugin.java | 151 ++++++++++++------ .../info/nightscout/utils/TimeListEdit.java | 35 +++- .../main/res/layout/localprofile_fragment.xml | 118 ++++++++++++++ .../res/layout/simpleprofile_fragment.xml | 8 +- .../main/res/layout/timelistedit_element.xml | 10 +- app/src/main/res/values-cs/strings.xml | 1 + 7 files changed, 283 insertions(+), 97 deletions(-) create mode 100644 app/src/main/res/layout/localprofile_fragment.xml diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/LocalProfile/LocalProfileFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/LocalProfile/LocalProfileFragment.java index 8c9a156d33..401344d4df 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/LocalProfile/LocalProfileFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/LocalProfile/LocalProfileFragment.java @@ -11,6 +11,7 @@ import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.EditText; +import android.widget.LinearLayout; import android.widget.RadioButton; import com.squareup.otto.Subscribe; @@ -18,14 +19,16 @@ import com.squareup.otto.Subscribe; 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.events.EventInitializationChanged; import info.nightscout.androidaps.interfaces.FragmentBase; import info.nightscout.androidaps.plugins.Careportal.Dialogs.NewNSTreatmentDialog; import info.nightscout.androidaps.plugins.Careportal.OptionsToShow; -import info.nightscout.androidaps.plugins.SimpleProfile.SimpleProfilePlugin; import info.nightscout.utils.SafeParse; +import info.nightscout.utils.TimeListEdit; public class LocalProfileFragment extends Fragment implements FragmentBase { private static Logger log = LoggerFactory.getLogger(LocalProfileFragment.class); @@ -39,40 +42,40 @@ public class LocalProfileFragment extends Fragment implements FragmentBase { EditText diaView; RadioButton mgdlView; RadioButton mmolView; - EditText icView; - EditText isfView; + TimeListEdit icView; + TimeListEdit isfView; EditText carView; - EditText basalView; - EditText targetlowView; - EditText targethighView; + TimeListEdit basalView; + TimeListEdit targetView; Button profileswitchButton; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - View layout = inflater.inflate(R.layout.simpleprofile_fragment, container, false); - diaView = (EditText) layout.findViewById(R.id.simpleprofile_dia); - mgdlView = (RadioButton) layout.findViewById(R.id.simpleprofile_mgdl); - mmolView = (RadioButton) layout.findViewById(R.id.simpleprofile_mmol); - icView = (EditText) layout.findViewById(R.id.simpleprofile_ic); - isfView = (EditText) layout.findViewById(R.id.simpleprofile_isf); - carView = (EditText) layout.findViewById(R.id.simpleprofile_car); - basalView = (EditText) layout.findViewById(R.id.simpleprofile_basalrate); - targetlowView = (EditText) layout.findViewById(R.id.simpleprofile_targetlow); - targethighView = (EditText) layout.findViewById(R.id.simpleprofile_targethigh); - profileswitchButton = (Button) layout.findViewById(R.id.simpleprofile_profileswitch); + Runnable save = new Runnable() { + @Override + public void run() { + localProfilePlugin.storeSettings(); + } + }; + + View layout = inflater.inflate(R.layout.localprofile_fragment, container, false); + diaView = (EditText) layout.findViewById(R.id.localprofile_dia); + mgdlView = (RadioButton) layout.findViewById(R.id.localprofile_mgdl); + mmolView = (RadioButton) layout.findViewById(R.id.localprofile_mmol); + icView = new TimeListEdit(getContext(), layout, R.id.localprofile_ic, MainApp.sResources.getString(R.string.nsprofileview_ic_label), getPlugin().ic, null, new DecimalFormat("0.0"), save); + isfView = new TimeListEdit(getContext(), layout, R.id.localprofile_isf, MainApp.sResources.getString(R.string.nsprofileview_isf_label), getPlugin().isf, null, new DecimalFormat("0.0"), save); + carView = (EditText) layout.findViewById(R.id.localprofile_car); + basalView = new TimeListEdit(getContext(), layout, R.id.localprofile_basal, MainApp.sResources.getString(R.string.nsprofileview_basal_label), getPlugin().basal, null, new DecimalFormat("0.00"), save); + targetView = new TimeListEdit(getContext(), layout, R.id.localprofile_target, MainApp.sResources.getString(R.string.nsprofileview_target_label), getPlugin().targetLow, getPlugin().targetHigh, new DecimalFormat("0.0"), save); + profileswitchButton = (Button) layout.findViewById(R.id.localprofile_profileswitch); onStatusEvent(null); mgdlView.setChecked(localProfilePlugin.mgdl); mmolView.setChecked(localProfilePlugin.mmol); diaView.setText(localProfilePlugin.dia.toString()); - icView.setText(localProfilePlugin.ic.toString()); - isfView.setText(localProfilePlugin.isf.toString()); carView.setText(localProfilePlugin.car.toString()); - basalView.setText(localProfilePlugin.basal.toString()); - targetlowView.setText(localProfilePlugin.targetLow.toString()); - targethighView.setText(localProfilePlugin.targetHigh.toString()); mgdlView.setOnClickListener(new View.OnClickListener() { @Override @@ -119,23 +122,13 @@ public class LocalProfileFragment extends Fragment implements FragmentBase { public void onTextChanged(CharSequence s, int start, int before, int count) { localProfilePlugin.dia = SafeParse.stringToDouble(diaView.getText().toString()); - localProfilePlugin.ic = SafeParse.stringToDouble(icView.getText().toString()); - localProfilePlugin.isf = SafeParse.stringToDouble(isfView.getText().toString()); localProfilePlugin.car = SafeParse.stringToDouble(carView.getText().toString()); - localProfilePlugin.basal = SafeParse.stringToDouble(basalView.getText().toString()); - localProfilePlugin.targetLow = SafeParse.stringToDouble(targetlowView.getText().toString()); - localProfilePlugin.targetHigh = SafeParse.stringToDouble(targethighView.getText().toString()); localProfilePlugin.storeSettings(); } }; diaView.addTextChangedListener(textWatch); - icView.addTextChangedListener(textWatch); - isfView.addTextChangedListener(textWatch); carView.addTextChangedListener(textWatch); - basalView.addTextChangedListener(textWatch); - targetlowView.addTextChangedListener(textWatch); - targethighView.addTextChangedListener(textWatch); onStatusEvent(null); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/LocalProfile/LocalProfilePlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/LocalProfile/LocalProfilePlugin.java index 37daeac883..0ead686e5e 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/LocalProfile/LocalProfilePlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/LocalProfile/LocalProfilePlugin.java @@ -30,15 +30,17 @@ public class LocalProfilePlugin implements PluginBase, ProfileInterface { private static NSProfile convertedProfile = null; + final private String DEFAULTARRAY = "[{\"timeAsSeconds\":0,\"value\":0}]"; + boolean mgdl; boolean mmol; Double dia; - Double ic; - Double isf; + JSONArray ic; + JSONArray isf; Double car; - Double basal; - Double targetLow; - Double targetHigh; + JSONArray basal; + JSONArray targetLow; + JSONArray targetHigh; public LocalProfilePlugin() { loadSettings(); @@ -89,15 +91,15 @@ public class LocalProfilePlugin implements PluginBase, ProfileInterface { log.debug("Storing settings"); SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext()); SharedPreferences.Editor editor = settings.edit(); - editor.putBoolean("SimpleProfile" + "mmol", mmol); - editor.putBoolean("SimpleProfile" + "mgdl", mgdl); - editor.putString("SimpleProfile" + "dia", dia.toString()); - editor.putString("SimpleProfile" + "ic", ic.toString()); - editor.putString("SimpleProfile" + "isf", isf.toString()); - editor.putString("SimpleProfile" + "car", car.toString()); - editor.putString("SimpleProfile" + "basal", basal.toString()); - editor.putString("SimpleProfile" + "targetlow", targetLow.toString()); - editor.putString("SimpleProfile" + "targethigh", targetHigh.toString()); + editor.putBoolean("LocalProfile" + "mmol", mmol); + editor.putBoolean("LocalProfile" + "mgdl", mgdl); + editor.putString("LocalProfile" + "dia", dia.toString()); + editor.putString("LocalProfile" + "ic", ic.toString()); + editor.putString("LocalProfile" + "isf", isf.toString()); + editor.putString("LocalProfile" + "car", car.toString()); + editor.putString("LocalProfile" + "basal", basal.toString()); + editor.putString("LocalProfile" + "targetlow", targetLow.toString()); + editor.putString("LocalProfile" + "targethigh", targetHigh.toString()); editor.commit(); createConvertedProfile(); @@ -108,69 +110,124 @@ public class LocalProfilePlugin implements PluginBase, ProfileInterface { log.debug("Loading stored settings"); SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext()); - if (settings.contains("SimpleProfile" + "mgdl")) + if (settings.contains("LocalProfile" + "mgdl")) try { - mgdl = settings.getBoolean("SimpleProfile" + "mgdl", true); + mgdl = settings.getBoolean("LocalProfile" + "mgdl", false); } catch (Exception e) { log.debug(e.getMessage()); } - else mgdl = true; - if (settings.contains("SimpleProfile" + "mmol")) + else mgdl = false; + if (settings.contains("LocalProfile" + "mmol")) try { - mmol = settings.getBoolean("SimpleProfile" + "mmol", false); + mmol = settings.getBoolean("LocalProfile" + "mmol", true); } catch (Exception e) { log.debug(e.getMessage()); } - else mmol = false; - if (settings.contains("SimpleProfile" + "dia")) + else mmol = true; + if (settings.contains("LocalProfile" + "dia")) try { - dia = SafeParse.stringToDouble(settings.getString("SimpleProfile" + "dia", "3")); + dia = SafeParse.stringToDouble(settings.getString("LocalProfile" + "dia", "3")); } catch (Exception e) { log.debug(e.getMessage()); } else dia = 3d; - if (settings.contains("SimpleProfile" + "ic")) + if (settings.contains("LocalProfile" + "ic")) try { - ic = SafeParse.stringToDouble(settings.getString("SimpleProfile" + "ic", "20")); + ic = new JSONArray(settings.getString("LocalProfile" + "ic", DEFAULTARRAY)); } catch (Exception e) { log.debug(e.getMessage()); + try { + ic = new JSONArray(DEFAULTARRAY); + } catch (JSONException e1) { + e1.printStackTrace(); + } } - else ic = 20d; - if (settings.contains("SimpleProfile" + "isf")) + else { try { - isf = SafeParse.stringToDouble(settings.getString("SimpleProfile" + "isf", "200")); + ic = new JSONArray(DEFAULTARRAY); + } catch (JSONException e) { + e.printStackTrace(); + } + } + if (settings.contains("LocalProfile" + "isf")) + try { + isf = new JSONArray(settings.getString("LocalProfile" + "isf", DEFAULTARRAY)); } catch (Exception e) { log.debug(e.getMessage()); + try { + isf = new JSONArray(DEFAULTARRAY); + } catch (JSONException e1) { + e1.printStackTrace(); + } } - else isf = 200d; - if (settings.contains("SimpleProfile" + "car")) + else { try { - car = SafeParse.stringToDouble(settings.getString("SimpleProfile" + "car", "20")); + isf = new JSONArray(DEFAULTARRAY); + } catch (JSONException e) { + e.printStackTrace(); + } + } + if (settings.contains("LocalProfile" + "car")) + try { + car = SafeParse.stringToDouble(settings.getString("LocalProfile" + "car", "20")); } catch (Exception e) { log.debug(e.getMessage()); } else car = 20d; - if (settings.contains("SimpleProfile" + "basal")) + if (settings.contains("LocalProfile" + "basal")) try { - basal = SafeParse.stringToDouble(settings.getString("SimpleProfile" + "basal", "1")); + basal = new JSONArray(settings.getString("LocalProfile" + "basal", DEFAULTARRAY)); } catch (Exception e) { log.debug(e.getMessage()); + try { + basal = new JSONArray(DEFAULTARRAY); + } catch (JSONException e1) { + e1.printStackTrace(); + } } - else basal = 1d; - if (settings.contains("SimpleProfile" + "targetlow")) + else { try { - targetLow = SafeParse.stringToDouble(settings.getString("SimpleProfile" + "targetlow", "80")); + basal = new JSONArray(DEFAULTARRAY); + } catch (JSONException e) { + e.printStackTrace(); + } + } + if (settings.contains("LocalProfile" + "targetlow")) + try { + targetLow = new JSONArray(settings.getString("LocalProfile" + "targetlow", DEFAULTARRAY)); } catch (Exception e) { log.debug(e.getMessage()); + try { + targetLow = new JSONArray(DEFAULTARRAY); + } catch (JSONException e1) { + e1.printStackTrace(); + } } - else targetLow = 80d; - if (settings.contains("SimpleProfile" + "targethigh")) + else { try { - targetHigh = SafeParse.stringToDouble(settings.getString("SimpleProfile" + "targethigh", "120")); + targetLow = new JSONArray(DEFAULTARRAY); + } catch (JSONException e) { + e.printStackTrace(); + } + } + if (settings.contains("LocalProfile" + "targethigh")) + try { + targetHigh = new JSONArray(settings.getString("LocalProfile" + "targethigh", DEFAULTARRAY)); } catch (Exception e) { log.debug(e.getMessage()); + try { + targetHigh = new JSONArray(DEFAULTARRAY); + } catch (JSONException e1) { + e1.printStackTrace(); + } } - else targetHigh = 120d; + else { + try { + targetHigh = new JSONArray(DEFAULTARRAY); + } catch (JSONException e) { + e.printStackTrace(); + } + } createConvertedProfile(); } @@ -218,21 +275,21 @@ public class LocalProfilePlugin implements PluginBase, ProfileInterface { JSONObject profile = new JSONObject(); try { - json.put("defaultProfile", "SimpleProfile"); + json.put("defaultProfile", "LocalProfile"); json.put("store", store); profile.put("dia", dia); - profile.put("carbratio", new JSONArray().put(new JSONObject().put("timeAsSeconds", 0).put("value", ic))); + profile.put("carbratio", ic); profile.put("carbs_hr", car); - profile.put("sens", new JSONArray().put(new JSONObject().put("timeAsSeconds", 0).put("value", isf))); - profile.put("basal", new JSONArray().put(new JSONObject().put("timeAsSeconds", 0).put("value", basal))); - profile.put("target_low", new JSONArray().put(new JSONObject().put("timeAsSeconds", 0).put("value", targetLow))); - profile.put("target_high", new JSONArray().put(new JSONObject().put("timeAsSeconds", 0).put("value", targetHigh))); + profile.put("sens", isf); + profile.put("basal", basal); + profile.put("target_low", targetLow); + profile.put("target_high", targetHigh); profile.put("units", mgdl ? Constants.MGDL : Constants.MMOL); - store.put("SimpleProfile", profile); + store.put("LocalProfile", profile); } catch (JSONException e) { e.printStackTrace(); } - convertedProfile = new NSProfile(json, "SimpleProfile"); + convertedProfile = new NSProfile(json, "LocalProfile"); } @Override diff --git a/app/src/main/java/info/nightscout/utils/TimeListEdit.java b/app/src/main/java/info/nightscout/utils/TimeListEdit.java index 9342dfcba9..b1abfb6849 100644 --- a/app/src/main/java/info/nightscout/utils/TimeListEdit.java +++ b/app/src/main/java/info/nightscout/utils/TimeListEdit.java @@ -1,11 +1,14 @@ package info.nightscout.utils; import android.content.Context; +import android.os.Build; import android.text.Editable; +import android.text.Layout; import android.text.TextWatcher; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; +import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.EditText; @@ -46,8 +49,9 @@ public class TimeListEdit { JSONArray data1; JSONArray data2; NumberFormat formatter; + Runnable save; - public TimeListEdit(Context context, View view, int resLayoutId, String label, JSONArray data1, JSONArray data2, NumberFormat formatter) { + public TimeListEdit(Context context, View view, int resLayoutId, String label, JSONArray data1, JSONArray data2, NumberFormat formatter, Runnable save) { this.context = context; this.view = view; this.resLayoutId = resLayoutId; @@ -55,6 +59,7 @@ public class TimeListEdit { this.data1 = data1; this.data2 = data2; this.formatter = formatter; + this.save = save; buildView(); } @@ -67,9 +72,14 @@ public class TimeListEdit { TextView textlabel = new TextView(context); textlabel.setText(label); textlabel.setGravity(Gravity.LEFT); - LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); + LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); llp.setMargins(10, 0, 0, 0); // llp.setMargins(left, top, right, bottom); textlabel.setLayoutParams(llp); + textlabel.setBackgroundColor(MainApp.sResources.getColor(R.color.linearBlockBackground)); + if (Build.VERSION.SDK_INT < 23) + textlabel.setTextAppearance(context, android.R.style.TextAppearance_Medium); + else + textlabel.setTextAppearance(android.R.style.TextAppearance_Medium); layout.addView(textlabel); for (int i = 0; i < itemsCount(); i++) { @@ -188,7 +198,11 @@ public class TimeListEdit { if (!(itemsCount() > 0 && secondFromMidnight(itemsCount() - 1) == 23 * ONEHOURINSECONDS)) { ImageView imageView = new ImageView(context); imageView.setImageResource(R.drawable.add); + LinearLayout.LayoutParams illp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); + illp.setMargins(0, 25, 0, 25); // llp.setMargins(left, top, right, bottom); + illp.gravity = Gravity.CENTER; layout.addView(imageView); + imageView.setLayoutParams(illp); imageView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { @@ -255,13 +269,15 @@ public class TimeListEdit { } public double value2(int index) { - try { - JSONObject item = (JSONObject) data2.get(index); - if (item.has("value")) { - return item.getDouble("value"); + if (data2 != null) { + try { + JSONObject item = (JSONObject) data2.get(index); + if (item.has("value")) { + return item.getDouble("value"); + } + } catch (JSONException e) { + e.printStackTrace(); } - } catch (JSONException e) { - e.printStackTrace(); } return 0d; } @@ -278,6 +294,7 @@ public class TimeListEdit { newObject2.put("value", value2); data2.put(index, newObject2); } + if (save != null) save.run(); } catch (JSONException e) { e.printStackTrace(); } @@ -294,6 +311,7 @@ public class TimeListEdit { } // add new object editItem(index, timeAsSeconds, value1, value2); + if (save != null) save.run(); } catch (JSONException e) { e.printStackTrace(); } @@ -304,6 +322,7 @@ public class TimeListEdit { data1.remove(index); if (data2 != null) data2.remove(index); + if (save != null) save.run(); } void log() { diff --git a/app/src/main/res/layout/localprofile_fragment.xml b/app/src/main/res/layout/localprofile_fragment.xml new file mode 100644 index 0000000000..03b459eb9b --- /dev/null +++ b/app/src/main/res/layout/localprofile_fragment.xml @@ -0,0 +1,118 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +