From 3ac9811d9a31a9d0472c64f323c8f826863b4adb Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Sat, 5 Nov 2016 15:46:11 +0100 Subject: [PATCH 1/3] Actions page & offline profile switch --- .idea/misc.xml | 2 +- .../info/nightscout/androidaps/MainApp.java | 2 + .../plugins/Actions/ActionsFragment.java | 57 +++++++++++++++++++ .../plugins/Actions/ActionsPlugin.java | 56 ++++++++++++++++++ .../Careportal/CareportalFragment.java | 38 ------------- .../Dialogs/NewNSTreatmentDialog.java | 28 ++++++++- .../plugins/Careportal/OptionsToShow.java | 46 +++++++++++++++ .../nightscout/client/data/NSProfile.java | 13 +++++ app/src/main/res/layout/actions_fragment.xml | 27 +++++++++ app/src/main/res/values-cs/strings.xml | 1 + app/src/main/res/values/colors.xml | 1 + app/src/main/res/values/strings.xml | 1 + 12 files changed, 231 insertions(+), 41 deletions(-) create mode 100644 app/src/main/java/info/nightscout/androidaps/plugins/Actions/ActionsFragment.java create mode 100644 app/src/main/java/info/nightscout/androidaps/plugins/Actions/ActionsPlugin.java create mode 100644 app/src/main/java/info/nightscout/androidaps/plugins/Careportal/OptionsToShow.java create mode 100644 app/src/main/res/layout/actions_fragment.xml diff --git a/.idea/misc.xml b/.idea/misc.xml index fbb68289f4..5d19981032 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -37,7 +37,7 @@ - + diff --git a/app/src/main/java/info/nightscout/androidaps/MainApp.java b/app/src/main/java/info/nightscout/androidaps/MainApp.java index 665ca1f253..6c92f385da 100644 --- a/app/src/main/java/info/nightscout/androidaps/MainApp.java +++ b/app/src/main/java/info/nightscout/androidaps/MainApp.java @@ -16,6 +16,7 @@ import java.util.ArrayList; import info.nightscout.androidaps.db.DatabaseHelper; import info.nightscout.androidaps.interfaces.PluginBase; +import info.nightscout.androidaps.plugins.Actions.ActionsFragment; import info.nightscout.androidaps.plugins.Careportal.CareportalFragment; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderFragment; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; @@ -62,6 +63,7 @@ public class MainApp extends Application { pluginsList = new ArrayList<>(); // Register all tabs in app here pluginsList.add(OverviewFragment.getPlugin()); + pluginsList.add(ActionsFragment.getPlugin()); if (Config.DANAR) pluginsList.add(DanaRFragment.getPlugin()); if (Config.MM640G) pluginsList.add(MM640gFragment.getPlugin()); if (Config.CAREPORTALENABLED) pluginsList.add(CareportalFragment.getPlugin()); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Actions/ActionsFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/Actions/ActionsFragment.java new file mode 100644 index 0000000000..add18ff4fd --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Actions/ActionsFragment.java @@ -0,0 +1,57 @@ +package info.nightscout.androidaps.plugins.Actions; + + +import android.os.Bundle; +import android.support.v4.app.Fragment; +import android.support.v4.app.FragmentManager; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; + +import info.nightscout.androidaps.R; +import info.nightscout.androidaps.interfaces.FragmentBase; +import info.nightscout.androidaps.plugins.Careportal.CareportalFragment; +import info.nightscout.androidaps.plugins.Careportal.Dialogs.NewNSTreatmentDialog; +import info.nightscout.androidaps.plugins.Careportal.OptionsToShow; + +/** + * A simple {@link Fragment} subclass. + */ +public class ActionsFragment extends Fragment implements FragmentBase, View.OnClickListener { + + static ActionsPlugin actionsPlugin = new ActionsPlugin(); + static public ActionsPlugin getPlugin() { + return actionsPlugin; + } + + public ActionsFragment() { + } + + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + View view = inflater.inflate(R.layout.actions_fragment, container, false); + + view.findViewById(R.id.actions_profileswitch).setOnClickListener(this); + + return view; + } + + @Override + public void onClick(View view) { + FragmentManager manager = getFragmentManager(); + NewNSTreatmentDialog newDialog = new NewNSTreatmentDialog(); + switch (view.getId()) { + case R.id.actions_profileswitch: + 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); + break; + default: + newDialog = null; + } + if (newDialog != null) + newDialog.show(manager, "NewNSTreatmentDialog"); + } +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Actions/ActionsPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/Actions/ActionsPlugin.java new file mode 100644 index 0000000000..d591f67556 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Actions/ActionsPlugin.java @@ -0,0 +1,56 @@ +package info.nightscout.androidaps.plugins.Actions; + +import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.R; +import info.nightscout.androidaps.interfaces.PluginBase; + +/** + * Created by mike on 05.11.2016. + */ + +public class ActionsPlugin implements PluginBase { + + boolean fragmentEnabled = true; + boolean fragmentVisible = true; + + @Override + public int getType() { + return PluginBase.GENERAL; + } + + @Override + public String getFragmentClass() { + return ActionsFragment.class.getName(); + } + + @Override + public String getName() { + return MainApp.sResources.getString(R.string.actions); + } + + @Override + public boolean isEnabled(int type) { + return fragmentEnabled; + } + + @Override + public boolean isVisibleInTabs(int type) { + return fragmentVisible; + } + + @Override + public boolean canBeHidden(int type) { + return true; + } + + @Override + public void setFragmentEnabled(int type, boolean fragmentEnabled) { + this.fragmentEnabled = fragmentEnabled; + } + + @Override + public void setFragmentVisible(int type, boolean fragmentVisible) { + this.fragmentVisible = fragmentVisible; + } + +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Careportal/CareportalFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/Careportal/CareportalFragment.java index d308363b03..047dd3e7c5 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Careportal/CareportalFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Careportal/CareportalFragment.java @@ -22,44 +22,6 @@ public class CareportalFragment extends Fragment implements FragmentBase, View.O return careportalPlugin; } - public class OptionsToShow { - public int eventType; - public int eventName; - public boolean bg; - public boolean insulin; - public boolean carbs; - public boolean prebolus; - public boolean duration; - public boolean percent; - public boolean absolute; - public boolean profile; - public boolean split; - - public OptionsToShow(int eventType, - int eventName, - boolean bg, - boolean insulin, - boolean carbs, - boolean prebolus, - boolean duration, - boolean percent, - boolean absolute, - boolean profile, - boolean split) { - this.eventType = eventType; - this.eventName = eventName; - this.bg = bg; - this.insulin = insulin; - this.carbs = carbs; - this.prebolus = prebolus; - this.duration = duration; - this.percent = percent; - this.absolute = absolute; - this.profile = profile; - this.split = split; - } - } - // bg,insulin,carbs,prebolus,duration,percent,absolute,profile,split final OptionsToShow bgcheck = new OptionsToShow(R.id.careportal_bgcheck, R.string.careportal_bgcheck, true, true, true, false, false, false, false, false, false); final OptionsToShow snackbolus = new OptionsToShow(R.id.careportal_snackbolus, R.string.careportal_snackbolus, true, true, true, true, false, false, false, false, false); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Careportal/Dialogs/NewNSTreatmentDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/Careportal/Dialogs/NewNSTreatmentDialog.java index dac8968b64..aefcdd646d 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Careportal/Dialogs/NewNSTreatmentDialog.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Careportal/Dialogs/NewNSTreatmentDialog.java @@ -40,12 +40,16 @@ import java.util.Calendar; import java.util.Date; +import info.nightscout.androidaps.Config; import info.nightscout.androidaps.Constants; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.Services.Intents; import info.nightscout.androidaps.db.BgReading; +import info.nightscout.androidaps.events.EventNewBasalProfile; +import info.nightscout.androidaps.interfaces.PumpInterface; import info.nightscout.androidaps.plugins.Careportal.CareportalFragment; +import info.nightscout.androidaps.plugins.Careportal.OptionsToShow; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderFragment; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.Overview.Dialogs.NewExtendedBolusDialog; @@ -62,7 +66,7 @@ public class NewNSTreatmentDialog extends DialogFragment implements View.OnClick private FragmentActivity context; - private static CareportalFragment.OptionsToShow options; + private static OptionsToShow options; NSProfile profile; String units; @@ -107,7 +111,7 @@ public class NewNSTreatmentDialog extends DialogFragment implements View.OnClick Date eventTime; - public void setOptions(CareportalFragment.OptionsToShow options) { + public void setOptions(OptionsToShow options) { this.options = options; } @@ -532,6 +536,26 @@ public class NewNSTreatmentDialog extends DialogFragment implements View.OnClick builder.setPositiveButton(getContext().getString(R.string.ok), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { ConfigBuilderPlugin.uploadCareportalEntryToNS(data); + if (options.executeProfileSwitch) { + if (data.has("profile")) { + try { + String profile = data.getString("profile"); + NSProfile nsProfile = MainApp.getConfigBuilder().getActiveProfile().getProfile(); + nsProfile.setActiveProfile(profile); + PumpInterface pump = MainApp.getConfigBuilder(); + if (pump != null) { + pump.setNewBasalProfile(nsProfile); + log.debug("Setting new profile: " + profile); + MainApp.bus().post(new EventNewBasalProfile(nsProfile)); + } else { + log.error("No active pump selected"); + } + + } catch (JSONException e) { + e.printStackTrace(); + } + } + } } }); builder.setNegativeButton(getContext().getString(R.string.cancel), null); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Careportal/OptionsToShow.java b/app/src/main/java/info/nightscout/androidaps/plugins/Careportal/OptionsToShow.java new file mode 100644 index 0000000000..e5a086c07e --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Careportal/OptionsToShow.java @@ -0,0 +1,46 @@ +package info.nightscout.androidaps.plugins.Careportal; + +/** + * Created by mike on 05.11.2016. + */ + +public class OptionsToShow { + public int eventType; + public int eventName; + public boolean bg; + public boolean insulin; + public boolean carbs; + public boolean prebolus; + public boolean duration; + public boolean percent; + public boolean absolute; + public boolean profile; + public boolean split; + + // perform direct actions + public boolean executeProfileSwitch = false; + + public OptionsToShow(int eventType, + int eventName, + boolean bg, + boolean insulin, + boolean carbs, + boolean prebolus, + boolean duration, + boolean percent, + boolean absolute, + boolean profile, + boolean split) { + this.eventType = eventType; + this.eventName = eventName; + this.bg = bg; + this.insulin = insulin; + this.carbs = carbs; + this.prebolus = prebolus; + this.duration = duration; + this.percent = percent; + this.absolute = absolute; + this.profile = profile; + this.split = split; + } +} diff --git a/app/src/main/java/info/nightscout/client/data/NSProfile.java b/app/src/main/java/info/nightscout/client/data/NSProfile.java index 5b919466ee..c9a0c0723e 100644 --- a/app/src/main/java/info/nightscout/client/data/NSProfile.java +++ b/app/src/main/java/info/nightscout/client/data/NSProfile.java @@ -349,6 +349,19 @@ public class NSProfile { return activeProfile; } + public void setActiveProfile(String newProfile) { + try { + JSONObject store = json.getJSONObject("store"); + if (newProfile != null && store.has(newProfile)) { + activeProfile = newProfile; + } else { + log.error("Attempt to set wrong active profile"); + } + } catch (JSONException e) { + e.printStackTrace(); + } + } + public Double getMaxDailyBasal() { Double max = 0d; for (Integer hour = 0; hour < 24; hour++) { diff --git a/app/src/main/res/layout/actions_fragment.xml b/app/src/main/res/layout/actions_fragment.xml new file mode 100644 index 0000000000..5557a64e6e --- /dev/null +++ b/app/src/main/res/layout/actions_fragment.xml @@ -0,0 +1,27 @@ + + + + +