From 38a9c942584535c2289ce6a22986c2c3466e13b7 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Sat, 31 Dec 2016 20:10:36 +0100 Subject: [PATCH] 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 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +