From d69701a092202a6cf9c08f0936eee53de784830d Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Thu, 16 Jun 2016 19:22:58 +0200 Subject: [PATCH] SimpleProfile plugin, ConfigBuilder GUI improvements --- .../nightscout/androidaps/MainActivity.java | 4 - .../ConfigBuilder/ConfigBuilderFragment.java | 44 +++- .../SimpleProfile/SimpleProfileFragment.java | 205 +++++++++++++++++- .../res/layout/simpleprofile_fragment.xml | 148 +++++++++++++ 4 files changed, 390 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/MainActivity.java b/app/src/main/java/info/nightscout/androidaps/MainActivity.java index 98779179f6..4258eafa95 100644 --- a/app/src/main/java/info/nightscout/androidaps/MainActivity.java +++ b/app/src/main/java/info/nightscout/androidaps/MainActivity.java @@ -115,10 +115,6 @@ public class MainActivity extends AppCompatActivity { MainApp.bus().register(this); } - public static TabPageAdapter getPageAdapter() { - return pageAdapter; - } - public static ArrayList getPluginsList() { return pluginsList; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderFragment.java index add2491614..9c8ea0d4bb 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderFragment.java @@ -31,6 +31,7 @@ import info.nightscout.androidaps.R; import info.nightscout.androidaps.data.Result; import info.nightscout.androidaps.db.TempBasal; import info.nightscout.androidaps.events.EventRefreshGui; +import info.nightscout.androidaps.interfaces.ConstrainsInterface; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.ProfileInterface; import info.nightscout.androidaps.interfaces.PumpInterface; @@ -40,10 +41,7 @@ import info.nightscout.androidaps.plugins.TempBasals.TempBasalsFragment; import info.nightscout.androidaps.plugins.Treatments.TreatmentsFragment; import info.nightscout.client.data.NSProfile; -/** - * A simple {@link Fragment} subclass. - */ -public class ConfigBuilderFragment extends Fragment implements PluginBase, PumpInterface { +public class ConfigBuilderFragment extends Fragment implements PluginBase, PumpInterface, ConstrainsInterface { private static Logger log = LoggerFactory.getLogger(ConfigBuilderFragment.class); private static final String PREFS_NAME = "Settings"; @@ -310,7 +308,9 @@ public class ConfigBuilderFragment extends Fragment implements PluginBase, PumpI CheckBox cb = (CheckBox) v; PluginBase plugin = (PluginBase) cb.getTag(); plugin.setFragmentEnabled(cb.isChecked()); + if (cb.isChecked()) plugin.setFragmentVisible(true); onEnabledCategoryChanged(plugin); + storeSettings(); } }); @@ -319,8 +319,8 @@ public class ConfigBuilderFragment extends Fragment implements PluginBase, PumpI CheckBox cb = (CheckBox) v; PluginBase plugin = (PluginBase) cb.getTag(); plugin.setFragmentVisible(cb.isChecked()); - MainApp.bus().post(new EventRefreshGui()); storeSettings(); + MainApp.bus().post(new EventRefreshGui()); } }); } else { @@ -341,12 +341,25 @@ public class ConfigBuilderFragment extends Fragment implements PluginBase, PumpI } int type = plugin.getType(); + // Force enabled if there is only one plugin if (type == PluginBase.PUMP || type == PluginBase.TREATMENT || type == PluginBase.TEMPBASAL || type == PluginBase.PROFILE) if (pluginList.size() < 2) holder.checkboxEnabled.setEnabled(false); + + // Constrains cannot be disabled if (type == PluginBase.CONSTRAINS) holder.checkboxEnabled.setEnabled(false); + // Hide disabled profiles by default + if (type == PluginBase.PROFILE) { + if (!plugin.isEnabled()) { + holder.checkboxVisible.setEnabled(false); + holder.checkboxVisible.setChecked(false); + } else { + holder.checkboxVisible.setEnabled(true); + } + } + return convertView; } @@ -396,7 +409,6 @@ public class ConfigBuilderFragment extends Fragment implements PluginBase, PumpI } break; } - storeSettings(); } private void verifySelectionInCategories() { @@ -413,21 +425,41 @@ public class ConfigBuilderFragment extends Fragment implements PluginBase, PumpI activeProfile = (ProfileInterface) getTheOneEnabledInArray(pluginsInCategory); if (Config.logConfigBuilder) log.debug("Selected profile interface: " + ((PluginBase) activeProfile).getName()); + for (PluginBase p : pluginsInCategory) { + if (!p.getName().equals(((PluginBase) activeProfile).getName())) { + p.setFragmentVisible(false); + } + } break; case PluginBase.PUMP: activePump = (PumpInterface) getTheOneEnabledInArray(pluginsInCategory); if (Config.logConfigBuilder) log.debug("Selected pump interface: " + ((PluginBase) activePump).getName()); + for (PluginBase p : pluginsInCategory) { + if (!p.getName().equals(((PluginBase) activePump).getName())) { + p.setFragmentVisible(false); + } + } break; case PluginBase.TEMPBASAL: activeTempBasals = (TempBasalsInterface) getTheOneEnabledInArray(pluginsInCategory); if (Config.logConfigBuilder) log.debug("Selected tempbasal interface: " + ((PluginBase) activeTempBasals).getName()); + for (PluginBase p : pluginsInCategory) { + if (!p.getName().equals(((PluginBase) activeTempBasals).getName())) { + p.setFragmentVisible(false); + } + } break; case PluginBase.TREATMENT: activeTreatments = (TreatmentsInterface) getTheOneEnabledInArray(pluginsInCategory); if (Config.logConfigBuilder) log.debug("Selected treatment interface: " + ((PluginBase) activeTreatments).getName()); + for (PluginBase p : pluginsInCategory) { + if (!p.getName().equals(((PluginBase) activeTreatments).getName())) { + p.setFragmentVisible(false); + } + } break; } 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 e26a588d5d..20b852b904 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 @@ -1,15 +1,26 @@ package info.nightscout.androidaps.plugins.SimpleProfile; +import android.content.SharedPreferences; 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.EditText; +import android.widget.RadioButton; +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 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; @@ -19,12 +30,37 @@ import info.nightscout.client.data.NSProfile; public class SimpleProfileFragment extends Fragment implements PluginBase, ProfileInterface { private static Logger log = LoggerFactory.getLogger(SimpleProfileFragment.class); + private static final String PREFS_NAME = "SimpleProfile"; + boolean fragmentEnabled = true; boolean fragmentVisible = true; + EditText diaView; + RadioButton mgdlView; + RadioButton mmolView; + EditText icView; + EditText isfView; + EditText carView; + EditText basalView; + EditText targetlowView; + EditText targethighView; + + boolean mgdl; + boolean mmol; + Double dia; + Double ic; + Double isf; + Double car; + Double basal; + Double targetLow; + Double targetHigh; + + NSProfile convertedProfile = null; + public SimpleProfileFragment() { super(); registerBus(); + loadSettings(); } @Override @@ -71,7 +107,75 @@ public class SimpleProfileFragment extends Fragment implements PluginBase, Profi 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); + mgdlView.setChecked(mgdl); + mmolView.setChecked(mmol); + diaView.setText(dia.toString()); + icView.setText(ic.toString()); + isfView.setText(isf.toString()); + carView.setText(car.toString()); + basalView.setText(basal.toString()); + targetlowView.setText(targetLow.toString()); + targethighView.setText(targetHigh.toString()); + + mgdlView.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + mgdl = mgdlView.isChecked(); + mmol = !mgdl; + mmolView.setChecked(mmol); + storeSettings(); + } + }); + mmolView.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + mmol = mmolView.isChecked(); + mgdl = !mmol; + mgdlView.setChecked(mgdl); + storeSettings(); + } + }); + + 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) { + dia = Double.parseDouble(diaView.getText().toString().replace(",", ".")); + ic = Double.parseDouble(icView.getText().toString().replace(",", ".")); + isf = Double.parseDouble(isfView.getText().toString().replace(",", ".")); + car = Double.parseDouble(carView.getText().toString().replace(",", ".")); + basal = Double.parseDouble(basalView.getText().toString().replace(",", ".")); + targetLow = Double.parseDouble(targetlowView.getText().toString().replace(",", ".")); + targetHigh = Double.parseDouble(targethighView.getText().toString().replace(",", ".")); + } + }; + diaView.addTextChangedListener(textWatch); + icView.addTextChangedListener(textWatch); + isfView.addTextChangedListener(textWatch); + carView.addTextChangedListener(textWatch); + basalView.addTextChangedListener(textWatch); + targetlowView.addTextChangedListener(textWatch); + targethighView.addTextChangedListener(textWatch); return layout; } @@ -89,8 +193,107 @@ public class SimpleProfileFragment extends Fragment implements PluginBase, Profi MainApp.bus().register(this); } + private void storeSettings() { + if (Config.logPrefsChange) + log.debug("Storing settings"); + SharedPreferences settings = MainApp.instance().getApplicationContext().getSharedPreferences(PREFS_NAME, 0); + SharedPreferences.Editor editor = settings.edit(); + editor.putBoolean("mmol", mmol); + editor.putBoolean("mgdl", mgdl); + editor.putFloat("dia", new Float(dia)); + editor.putFloat("ic", new Float(ic)); + editor.putFloat("isf", new Float(isf)); + editor.putFloat("car", new Float(car)); + editor.putFloat("basal", new Float(basal)); + editor.putFloat("targetlow", new Float(targetLow)); + editor.putFloat("targethigh", new Float(targetHigh)); + + editor.commit(); + createConvertedProfile(); + } + + private void loadSettings() { + if (Config.logPrefsChange) + log.debug("Loading stored settings"); + SharedPreferences settings = MainApp.instance().getApplicationContext().getSharedPreferences(PREFS_NAME, 0); + + if (settings.contains("mgdl")) mgdl = settings.getBoolean("mgdl", true); else mgdl = true; + if (settings.contains("mmol")) mmol = settings.getBoolean("mmol", false); else mmol = false; + if (settings.contains("dia")) dia = (double) settings.getFloat("dia", 3); else dia = 3d; + if (settings.contains("ic")) ic = (double) settings.getFloat("ic", 20); else ic = 20d; + if (settings.contains("isf")) isf = (double) settings.getFloat("isf", 200); else isf = 200d; + if (settings.contains("car")) car = (double) settings.getFloat("car", 20); else car = 20d; + if (settings.contains("basal")) basal = (double) settings.getFloat("basal", 1); else basal = 1d; + if (settings.contains("targetlow")) targetLow = (double) settings.getFloat("targetlow", 80); else targetLow = 80d; + if (settings.contains("targethigh")) + targetHigh = (double) settings.getFloat("targethigh", 120); 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", "Profile"); + 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("Profile", profile); + } catch (JSONException e) { + e.printStackTrace(); + } + convertedProfile = new NSProfile(json, null); + } + @Override public NSProfile getProfile() { - return null; + return convertedProfile; } } diff --git a/app/src/main/res/layout/simpleprofile_fragment.xml b/app/src/main/res/layout/simpleprofile_fragment.xml index 5384f601fd..f31a5f02c1 100644 --- a/app/src/main/res/layout/simpleprofile_fragment.xml +++ b/app/src/main/res/layout/simpleprofile_fragment.xml @@ -4,4 +4,152 @@ android:layout_height="match_parent" tools:context=".plugins.SimpleProfile.SimpleProfileFragment"> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +