diff --git a/app/build.gradle b/app/build.gradle index 0a897fd74f..b187abddaf 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -58,7 +58,6 @@ android { dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) - testCompile 'junit:junit:4.12' compile('com.crashlytics.sdk.android:crashlytics:2.5.7@aar') { transitive = true; } @@ -76,4 +75,5 @@ 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' } diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 9adfbc8bc6..b53035b7bb 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -27,12 +27,18 @@ - + + + + + + + - \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/PreferencesActivity.java b/app/src/main/java/info/nightscout/androidaps/PreferencesActivity.java index 521ced16d3..e11b858387 100644 --- a/app/src/main/java/info/nightscout/androidaps/PreferencesActivity.java +++ b/app/src/main/java/info/nightscout/androidaps/PreferencesActivity.java @@ -77,6 +77,7 @@ public class PreferencesActivity extends PreferenceActivity implements SharedPre @Override public void onCreate(final Bundle savedInstanceState) { super.onCreate(savedInstanceState); + addPreferencesFromResource(R.xml.pref_quickwizard); addPreferencesFromResource(R.xml.pref_language); if (Config.CAREPORTALENABLED) addPreferencesFromResource(R.xml.pref_careportal); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/DanaRFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/DanaRFragment.java index ac3faf6a6a..6e4dbcf982 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/DanaRFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/DanaRFragment.java @@ -71,10 +71,6 @@ public class DanaRFragment extends Fragment implements FragmentBase { } } - public static DanaRFragment newInstance() { - return new DanaRFragment(); - } - @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/comm/MsgBolusStart.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/comm/MsgBolusStart.java index ee9af703cb..2e2980cb70 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/comm/MsgBolusStart.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/comm/MsgBolusStart.java @@ -19,7 +19,7 @@ public class MsgBolusStart extends MessageBase { // HARDCODED LIMIT amount = MainApp.getConfigBuilder().applyBolusConstraints(amount); if (amount < 0) amount = 0d; - if (amount > 17) amount = 17d; + if (amount > 17) amount = 17d; // TODO: add to constraints too AddParamInt((int) (amount * 100)); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Loop/DeviceStatus.java b/app/src/main/java/info/nightscout/androidaps/plugins/Loop/DeviceStatus.java index fa4ba2a8d1..85a9f39b09 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Loop/DeviceStatus.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Loop/DeviceStatus.java @@ -384,8 +384,6 @@ public class DeviceStatus { public JSONObject iob = null; public String created_at = null; - public JSONObject lowsuspend = null; - public JSONObject mongoRecord () { JSONObject record = new JSONObject(); @@ -399,7 +397,6 @@ public class DeviceStatus { if (iob != null) openaps.put("iob", iob); record.put("openaps", openaps); } - if (lowsuspend != null) record.put("lowsuspend" , lowsuspend); if (created_at != null) record.put("created_at" , created_at); } catch (JSONException e) { e.printStackTrace(); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Objectives/ObjectivesPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/Objectives/ObjectivesPlugin.java index c4cd87d86f..89c319d2c1 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Objectives/ObjectivesPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Objectives/ObjectivesPlugin.java @@ -132,6 +132,10 @@ public class ObjectivesPlugin implements PluginBase, ConstraintsInterface { public void initializeData() { + bgIsAvailableInNS = false; + pumpStatusIsAvailableInNS = false; + manualEnacts = 0; + objectives = new ArrayList<>(); objectives.add(new Objective(0, MainApp.sResources.getString(R.string.objectives_0_objective), diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/EditQuickWizardDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/EditQuickWizardDialog.java new file mode 100644 index 0000000000..4cc94432d5 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/EditQuickWizardDialog.java @@ -0,0 +1,118 @@ +package info.nightscout.androidaps.plugins.Overview.Dialogs; + + +import android.os.Bundle; +import android.support.v4.app.DialogFragment; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.view.Window; +import android.view.WindowManager; +import android.widget.ArrayAdapter; +import android.widget.Button; +import android.widget.EditText; +import android.widget.Spinner; + +import org.json.JSONException; + +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.ArrayList; + +import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.R; +import info.nightscout.androidaps.plugins.Overview.OverviewPlugin; +import info.nightscout.androidaps.plugins.Overview.QuickWizard; +import info.nightscout.androidaps.plugins.Overview.events.EventQuickWizardChange; +import info.nightscout.utils.DateUtil; +import info.nightscout.utils.SafeParse; + +public class EditQuickWizardDialog extends DialogFragment implements View.OnClickListener { + + QuickWizard.QuickWizardEntry entry = new QuickWizard().newEmptyItem(); + QuickWizard quickWizard = ((OverviewPlugin) MainApp.getSpecificPlugin(OverviewPlugin.class)).quickWizard; + + EditText buttonEdit; + EditText carbsEdit; + Spinner fromSpinner; + Spinner toSpinner; + Button okButton; + + public EditQuickWizardDialog() { + } + + public void setData(QuickWizard.QuickWizardEntry data) { + entry = data; + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE); + getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN); + + View view = inflater.inflate(R.layout.overview_editquickwizard_dialog, container, false); + buttonEdit = (EditText) view.findViewById(R.id.overview_editquickwizard_button_edit); + carbsEdit = (EditText) view.findViewById(R.id.overview_editquickwizard_carbs_edit); + fromSpinner = (Spinner) view.findViewById(R.id.overview_editquickwizard_from_spinner); + toSpinner = (Spinner) view.findViewById(R.id.overview_editquickwizard_to_spinner); + okButton = (Button) view.findViewById(R.id.overview_editquickwizard_ok_button); + okButton.setOnClickListener(this); + + int posFrom = 0; + int posTo = 95; + ArrayList timeList = new ArrayList<>(); + DateFormat df = new SimpleDateFormat("HH:mm"); + int pos = 0; + for (int t = 0; t < 24 * 60 * 60; t += 15 * 60) { + timeList.add(df.format(DateUtil.toDate(t))); + if (entry.validFrom() == t) posFrom = pos; + if (entry.validTo() == t) posTo = pos; + pos++; + } + timeList.add(df.format(DateUtil.toDate(24 * 60 * 60 - 60))); + + ArrayAdapter adapter = new ArrayAdapter(getContext(), + android.R.layout.simple_spinner_item, timeList); + adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); + fromSpinner.setAdapter(adapter); + toSpinner.setAdapter(adapter); + + buttonEdit.setText(entry.buttonText()); + carbsEdit.setText(entry.carbs().toString()); + fromSpinner.setSelection(posFrom); + toSpinner.setSelection(posTo); + + return view; + } + + @Override + public void onResume() { + super.onResume(); + if (getDialog() != null) + getDialog().getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); + } + + @Override + public void onClick(View v) { + switch (v.getId()) { + case R.id.overview_editquickwizard_ok_button: + if (fromSpinner.getSelectedItem() == null) return; + if (toSpinner.getSelectedItem() == null) return; + try { + entry.storage.put("buttonText", buttonEdit.getText().toString()); + entry.storage.put("carbs", SafeParse.stringToInt(carbsEdit.getText().toString())); + int validFromInt = DateUtil.toSeconds(fromSpinner.getSelectedItem().toString()); + entry.storage.put("validFrom", validFromInt); + int validToInt = DateUtil.toSeconds(toSpinner.getSelectedItem().toString()); + entry.storage.put("validTo", validToInt); + } catch (JSONException e) { + e.printStackTrace(); + } + quickWizard.addOrUpdate(entry); + dismiss(); + MainApp.bus().post(new EventQuickWizardChange()); + break; + } + } +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java index 5112181537..dfa78351af 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java @@ -27,9 +27,12 @@ import com.jjoe64.graphview.series.LineGraphSeries; import com.jjoe64.graphview.series.PointsGraphSeries; import com.squareup.otto.Subscribe; +import org.json.JSONException; +import org.json.JSONObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.text.DecimalFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; @@ -51,6 +54,7 @@ import info.nightscout.androidaps.events.EventRefreshGui; import info.nightscout.androidaps.events.EventTempBasalChange; import info.nightscout.androidaps.events.EventTreatmentChange; import info.nightscout.androidaps.interfaces.PumpInterface; +import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.Loop.LoopPlugin; import info.nightscout.androidaps.plugins.Loop.events.EventNewOpenLoopNotification; import info.nightscout.androidaps.plugins.Objectives.ObjectivesPlugin; @@ -61,8 +65,11 @@ import info.nightscout.androidaps.plugins.Overview.Dialogs.NewTreatmentDialog; import info.nightscout.androidaps.plugins.Overview.Dialogs.WizardDialog; import info.nightscout.androidaps.plugins.Overview.GraphSeriesExtension.PointsWithLabelGraphSeries; import info.nightscout.client.data.NSProfile; +import info.nightscout.utils.BolusWizard; +import info.nightscout.utils.DateUtil; import info.nightscout.utils.DecimalFormatter; import info.nightscout.utils.Round; +import info.nightscout.utils.SafeParse; public class OverviewFragment extends Fragment { @@ -85,12 +92,14 @@ public class OverviewFragment extends Fragment { LinearLayout cancelTempLayout; LinearLayout setTempLayout; LinearLayout acceptTempLayout; + LinearLayout quickWizardLayout; Button cancelTempButton; Button treatmentButton; Button wizardButton; Button setTempButton; Button setExtenedButton; Button acceptTempButton; + Button quickWizardButton; Handler sLoopHandler = new Handler(); Runnable sRefreshLoop = null; @@ -107,10 +116,6 @@ public class OverviewFragment extends Fragment { } } - public static OverviewFragment newInstance() { - return new OverviewFragment(); - } - @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { @@ -132,6 +137,8 @@ public class OverviewFragment extends Fragment { cancelTempLayout = (LinearLayout) view.findViewById(R.id.overview_canceltemplayout); acceptTempButton = (Button) view.findViewById(R.id.overview_accepttempbutton); acceptTempLayout = (LinearLayout) view.findViewById(R.id.overview_accepttemplayout); + quickWizardButton = (Button) view.findViewById(R.id.overview_quickwizard); + quickWizardLayout = (LinearLayout) view.findViewById(R.id.overview_quickwizardlayout); treatmentButton.setOnClickListener(new View.OnClickListener() { @Override @@ -151,6 +158,13 @@ public class OverviewFragment extends Fragment { } }); + quickWizardButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + processQuickWizard(); + } + }); + cancelTempButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { @@ -228,6 +242,100 @@ public class OverviewFragment extends Fragment { return view; } + void processQuickWizard() { + final BgReading lastBG = MainApp.getDbHelper().lastBg(); + if (MainApp.getConfigBuilder() == null || ConfigBuilderPlugin.getActiveProfile() == null) // app not initialized yet + return; + final NSProfile profile = ConfigBuilderPlugin.getActiveProfile().getProfile(); + + QuickWizard.QuickWizardEntry quickWizardEntry = getPlugin().quickWizard.getActive(); + if (quickWizardEntry != null && lastBG != null) { + quickWizardLayout.setVisibility(View.VISIBLE); + String text = MainApp.sResources.getString(R.string.bolus) + ": " + quickWizardEntry.buttonText(); + BolusWizard wizard = new BolusWizard(); + wizard.doCalc(profile.getDefaultProfile(), quickWizardEntry.carbs(), lastBG.valueToUnits(profile.getUnits()), 0d, true, true); + + final JSONObject boluscalcJSON = new JSONObject(); + try { + boluscalcJSON.put("eventTime", DateUtil.toISOString(new Date())); + boluscalcJSON.put("targetBGLow", wizard.targetBGLow); + boluscalcJSON.put("targetBGHigh", wizard.targetBGHigh); + boluscalcJSON.put("isf", wizard.sens); + boluscalcJSON.put("ic", wizard.ic); + boluscalcJSON.put("iob", -(wizard.insulingFromBolusIOB + wizard.insulingFromBasalsIOB)); + boluscalcJSON.put("bolusiobused", true); + boluscalcJSON.put("basaliobused", true); + boluscalcJSON.put("bg", lastBG.valueToUnits(profile.getUnits())); + boluscalcJSON.put("insulinbg", wizard.insulinFromBG); + boluscalcJSON.put("insulinbgused", true); + boluscalcJSON.put("bgdiff", wizard.bgDiff); + boluscalcJSON.put("insulincarbs", wizard.insulinFromCarbs); + boluscalcJSON.put("carbs", quickWizardEntry.carbs()); + boluscalcJSON.put("othercorrection", 0d); + boluscalcJSON.put("insulin", wizard.calculatedTotalInsulin); + } catch (JSONException e) { + e.printStackTrace(); + } + if (wizard.calculatedTotalInsulin > 0d && quickWizardEntry.carbs() > 0d) { + DecimalFormat formatNumber2decimalplaces = new DecimalFormat("0.00"); + String confirmMessage = getString(R.string.entertreatmentquestion); + + Double insulinAfterConstraints = MainApp.getConfigBuilder().applyBolusConstraints(wizard.calculatedTotalInsulin); + Integer carbsAfterConstraints = MainApp.getConfigBuilder().applyCarbsConstraints(quickWizardEntry.carbs()); + + confirmMessage += "\n" + getString(R.string.bolus) + ": " + formatNumber2decimalplaces.format(insulinAfterConstraints) + "U"; + confirmMessage += "\n" + getString(R.string.carbs) + ": " + carbsAfterConstraints + "g"; + + if (insulinAfterConstraints - wizard.calculatedTotalInsulin != 0 || carbsAfterConstraints != quickWizardEntry.carbs()) { + AlertDialog.Builder builder = new AlertDialog.Builder(getContext()); + builder.setTitle(MainApp.sResources.getString(R.string.treatmentdeliveryerror)); + builder.setMessage(getString(R.string.constraints_violation) + "\n" + getString(R.string.changeyourinput)); + builder.setPositiveButton(MainApp.sResources.getString(R.string.ok), null); + builder.show(); + return; + } + + final Double finalInsulinAfterConstraints = insulinAfterConstraints; + final Integer finalCarbsAfterConstraints = carbsAfterConstraints; + + AlertDialog.Builder builder = new AlertDialog.Builder(getContext()); + builder.setTitle(MainApp.sResources.getString(R.string.confirmation)); + builder.setMessage(confirmMessage); + builder.setPositiveButton(getString(R.string.ok), new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + if (finalInsulinAfterConstraints > 0 || finalCarbsAfterConstraints > 0) { + final ConfigBuilderPlugin pump = MainApp.getConfigBuilder(); + sHandler.post(new Runnable() { + @Override + public void run() { + PumpEnactResult result = pump.deliverTreatmentFromBolusWizard( + getContext(), + finalInsulinAfterConstraints, + finalCarbsAfterConstraints, + lastBG.valueToUnits(profile.getUnits()), + "Manual", + 0, + boluscalcJSON + ); + if (!result.success) { + AlertDialog.Builder builder = new AlertDialog.Builder(getContext()); + builder.setTitle(MainApp.sResources.getString(R.string.treatmentdeliveryerror)); + builder.setMessage(result.comment); + builder.setPositiveButton(MainApp.sResources.getString(R.string.ok), null); + builder.show(); + } + } + }); + } + } + }); + builder.setNegativeButton(getString(R.string.cancel), null); + builder.show(); + } + } + + } + @Override public void onPause() { super.onPause(); @@ -369,6 +477,20 @@ public class OverviewFragment extends Fragment { String units = profile.getUnits(); + // QuickWizard button + QuickWizard.QuickWizardEntry quickWizardEntry = getPlugin().quickWizard.getActive(); + if (quickWizardEntry != null && lastBG != null) { + quickWizardLayout.setVisibility(View.VISIBLE); + String text = MainApp.sResources.getString(R.string.bolus) + ": " + quickWizardEntry.buttonText() + " " + DecimalFormatter.to0Decimal(quickWizardEntry.carbs()) + "g"; + BolusWizard wizard = new BolusWizard(); + wizard.doCalc(profile.getDefaultProfile(), quickWizardEntry.carbs(), lastBG.valueToUnits(profile.getUnits()), 0d, true, true); + text += " " + DecimalFormatter.to2Decimal(wizard.calculatedTotalInsulin) + "U"; + quickWizardButton.setText(text); + if (wizard.calculatedTotalInsulin <= 0) + quickWizardLayout.setVisibility(View.GONE); + } else + quickWizardLayout.setVisibility(View.GONE); + // **** BG value **** if (lastBG != null && bgView != null) { bgView.setText(lastBG.valueToUnitsToString(profile.getUnits())); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewPlugin.java index d8217241ee..86dfa1532f 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewPlugin.java @@ -1,5 +1,11 @@ package info.nightscout.androidaps.plugins.Overview; +import android.content.SharedPreferences; +import android.preference.PreferenceManager; + +import org.json.JSONArray; +import org.json.JSONException; + import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.interfaces.PluginBase; @@ -12,6 +18,18 @@ public class OverviewPlugin implements PluginBase { public static Double bgTargetLow = 80d; public static Double bgTargetHigh = 180d; + public QuickWizard quickWizard = new QuickWizard(); + + public OverviewPlugin() { + SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext()); + String storedData = preferences.getString("QuickWizard", "[]"); + try { + quickWizard.setData(new JSONArray(storedData)); + } catch (JSONException e) { + e.printStackTrace(); + } + } + @Override public String getFragmentClass() { return OverviewFragment.class.getName(); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/QuickWizard.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/QuickWizard.java new file mode 100644 index 0000000000..ac7dbaa101 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/QuickWizard.java @@ -0,0 +1,172 @@ +package info.nightscout.androidaps.plugins.Overview; + +import android.content.SharedPreferences; +import android.preference.PreferenceManager; + +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; + +import java.util.Calendar; +import java.util.Date; +import java.util.GregorianCalendar; + +import info.nightscout.androidaps.MainApp; +import info.nightscout.client.data.NSProfile; +import info.nightscout.utils.DateUtil; + +/** + * Created by mike on 12.10.2016. + */ + +public class QuickWizard { + + public class QuickWizardEntry { + public JSONObject storage; + public int position; + + /* + { + buttonText: "Meal", + carbs: 36, + validFrom: 8 * 60 * 60, // seconds from midnight + validTo: 9 * 60 * 60, // seconds from midnight + } + */ + public QuickWizardEntry() { + String emptyData = "{\"buttonText\":\"\",\"carbs\":0,\"validFrom\":0,\"validTo\":86340}"; + try { + storage = new JSONObject(emptyData); + } catch (JSONException e) { + e.printStackTrace(); + } + position = -1; + } + + public QuickWizardEntry(JSONObject entry, int position) { + storage = entry; + this.position = position; + } + + public Boolean isActive() { + return NSProfile.secondsFromMidnight() >= validFrom() && NSProfile.secondsFromMidnight() <= validTo(); + } + + public String buttonText() { + try { + return storage.getString("buttonText"); + } catch (JSONException e) { + e.printStackTrace(); + } + return ""; + } + + public Integer carbs() { + try { + return storage.getInt("carbs"); + } catch (JSONException e) { + e.printStackTrace(); + } + return 0; + } + + public Date validFromDate() { + return DateUtil.toDate(validFrom()); + } + + public Date validToDate() { + return DateUtil.toDate(validTo()); + } + + public Integer validFrom() { + try { + return storage.getInt("validFrom"); + } catch (JSONException e) { + e.printStackTrace(); + } + return 0; + } + + public Integer validTo() { + try { + return storage.getInt("validTo"); + } catch (JSONException e) { + e.printStackTrace(); + } + return 0; + } + + } + + JSONArray storage = new JSONArray(); + + public void setData(JSONArray newData) { + storage = newData; + } + + public void save() { + SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext()); + SharedPreferences.Editor editor = preferences.edit(); + editor.putString("QuickWizard", storage.toString()); + editor.apply(); + } + + public int size() { + return storage.length(); + } + + public QuickWizardEntry get(int position) { + try { + return new QuickWizardEntry((JSONObject) storage.get(position), position); + } catch (JSONException e) { + e.printStackTrace(); + } + return null; + } + + public Boolean isActive() { + for (int i = 0; i < storage.length(); i++) { + try { + if (new QuickWizardEntry((JSONObject) storage.get(i), i).isActive()) return true; + } catch (JSONException e) { + e.printStackTrace(); + } + } + return false; + } + + public QuickWizardEntry getActive() { + for (int i = 0; i < storage.length(); i++) { + QuickWizardEntry entry; + try { + entry = new QuickWizardEntry((JSONObject) storage.get(i), i); + } catch (JSONException e) { + continue; + } + if (entry.isActive()) return entry; + } + return null; + } + + public QuickWizardEntry newEmptyItem() { + return new QuickWizardEntry(); + } + + public void addOrUpdate(QuickWizardEntry newItem) { + if (newItem.position == -1) + storage.put(newItem.storage); + else { + try { + storage.put(newItem.position, newItem.storage); + } catch (JSONException e) { + e.printStackTrace(); + } + } + save(); + } + + public void remove(int position) { + storage.remove(position); + save(); + } +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/activities/QuickWizardListActivity.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/activities/QuickWizardListActivity.java new file mode 100644 index 0000000000..dafd0766d1 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/activities/QuickWizardListActivity.java @@ -0,0 +1,176 @@ +package info.nightscout.androidaps.plugins.Overview.activities; + +import android.app.Activity; +import android.support.v4.app.FragmentManager; +import android.support.v7.app.AppCompatActivity; +import android.os.Bundle; +import android.support.v7.widget.CardView; +import android.support.v7.widget.LinearLayoutManager; +import android.support.v7.widget.RecyclerView; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.Button; +import android.widget.TextView; + +import com.squareup.otto.Subscribe; + +import java.text.DateFormat; +import java.text.SimpleDateFormat; + +import info.nightscout.androidaps.AgreementActivity; +import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.R; +import info.nightscout.androidaps.plugins.Overview.Dialogs.EditQuickWizardDialog; +import info.nightscout.androidaps.plugins.Overview.OverviewPlugin; +import info.nightscout.androidaps.plugins.Overview.QuickWizard; +import info.nightscout.androidaps.plugins.Overview.events.EventQuickWizardChange; +import info.nightscout.androidaps.plugins.TempBasals.TempBasalsFragment; +import info.nightscout.utils.DecimalFormatter; + +public class QuickWizardListActivity extends AppCompatActivity implements View.OnClickListener { + + RecyclerView recyclerView; + LinearLayoutManager llm; + + Button adButton; + + public static class RecyclerViewAdapter extends RecyclerView.Adapter { + + QuickWizard qvData; + FragmentManager fragmentManager; + + RecyclerViewAdapter(QuickWizard data, FragmentManager fragmentManager) { + this.qvData = data; + this.fragmentManager = fragmentManager; + } + + @Override + public QuickWizardEntryViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) { + View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.overview_quickwizardlist_item, viewGroup, false); + QuickWizardEntryViewHolder quickWizardEntryViewHolder = new QuickWizardEntryViewHolder(v, fragmentManager, qvData); + return quickWizardEntryViewHolder; + } + + @Override + public void onBindViewHolder(QuickWizardEntryViewHolder holder, int position) { + DateFormat df = new SimpleDateFormat("HH:mm"); + holder.from.setText(df.format(qvData.get(position).validFromDate())); + holder.to.setText(df.format(qvData.get(position).validToDate())); + holder.buttonText.setText(qvData.get(position).buttonText()); + holder.carbs.setText(DecimalFormatter.to0Decimal(qvData.get(position).carbs()) + " g"); + } + + @Override + public int getItemCount() { + return qvData.size(); + } + + @Override + public void onAttachedToRecyclerView(RecyclerView recyclerView) { + super.onAttachedToRecyclerView(recyclerView); + } + + public static class QuickWizardEntryViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { + CardView cv; + TextView buttonText; + TextView carbs; + TextView from; + TextView to; + Button editButton; + Button removeButton; + FragmentManager fragmentManager; + QuickWizard qvData; + + QuickWizardEntryViewHolder(View itemView, FragmentManager fragmentManager, QuickWizard qvData) { + super(itemView); + cv = (CardView) itemView.findViewById(R.id.overview_quickwizard_cardview); + buttonText = (TextView) itemView.findViewById(R.id.overview_quickwizard_item_buttonText); + carbs = (TextView) itemView.findViewById(R.id.overview_quickwizard_item_carbs); + from = (TextView) itemView.findViewById(R.id.overview_quickwizard_item_from); + to = (TextView) itemView.findViewById(R.id.overview_quickwizard_item_to); + editButton = (Button) itemView.findViewById(R.id.overview_quickwizard_item_edit_button); + removeButton = (Button) itemView.findViewById(R.id.overview_quickwizard_item_remove_button); + editButton.setOnClickListener(this); + removeButton.setOnClickListener(this); + this.fragmentManager = fragmentManager; + this.qvData = qvData; + } + + @Override + public void onClick(View v) { + int position = getAdapterPosition(); + switch (v.getId()) { + case R.id.overview_quickwizard_item_edit_button: + FragmentManager manager = fragmentManager; + EditQuickWizardDialog editQuickWizardDialog = new EditQuickWizardDialog(); + editQuickWizardDialog.setData(qvData.get(position)); + editQuickWizardDialog.show(manager, "EditQuickWizardDialog"); + break; + case R.id.overview_quickwizard_item_remove_button: + qvData.remove(position); + MainApp.bus().post(new EventQuickWizardChange()); + break; + } + } + } + } + + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.overview_quickwizardlist_activity); + + recyclerView = (RecyclerView) findViewById(R.id.overview_quickwizardactivity_recyclerview); + recyclerView.setHasFixedSize(true); + llm = new LinearLayoutManager(this); + recyclerView.setLayoutManager(llm); + + RecyclerViewAdapter adapter = new RecyclerViewAdapter(((OverviewPlugin) MainApp.getSpecificPlugin(OverviewPlugin.class)).quickWizard, getSupportFragmentManager()); + recyclerView.setAdapter(adapter); + + adButton = (Button) findViewById(R.id.overview_quickwizardactivity_add_button); + adButton.setOnClickListener(this); + } + + @Override + protected void onResume() { + super.onResume(); + MainApp.bus().register(this); + } + + @Override + protected void onPause() { + super.onPause(); + MainApp.bus().unregister(this); + } + + @Override + public void onClick(View v) { + switch (v.getId()) { + case R.id.overview_quickwizardactivity_add_button: + FragmentManager manager = getSupportFragmentManager(); + EditQuickWizardDialog editQuickWizardDialog = new EditQuickWizardDialog(); + editQuickWizardDialog.show(manager, "EditQuickWizardDialog"); + break; + } + } + @Subscribe + public void onStatusEvent(final EventQuickWizardChange ev) { + updateGUI(); + } + + public void updateGUI() { + Activity activity = this; + if (activity != null && recyclerView != null) { + activity.runOnUiThread(new Runnable() { + @Override + public void run() { + RecyclerViewAdapter adapter = new RecyclerViewAdapter(((OverviewPlugin) MainApp.getSpecificPlugin(OverviewPlugin.class)).quickWizard, getSupportFragmentManager()); + recyclerView.swapAdapter(adapter, false); + } + }); + } + } +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/events/EventQuickWizardChange.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/events/EventQuickWizardChange.java new file mode 100644 index 0000000000..1a9923f606 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/events/EventQuickWizardChange.java @@ -0,0 +1,8 @@ +package info.nightscout.androidaps.plugins.Overview.events; + +/** + * Created by mike on 20.10.2016. + */ + +public class EventQuickWizardChange { +} 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 69023b969b..5b919466ee 100644 --- a/app/src/main/java/info/nightscout/client/data/NSProfile.java +++ b/app/src/main/java/info/nightscout/client/data/NSProfile.java @@ -29,7 +29,7 @@ public class NSProfile { this.activeProfile = activeProfile; } - JSONObject getDefaultProfile() { + public JSONObject getDefaultProfile() { String defaultProfileName = null; JSONObject store; JSONObject profile = null; diff --git a/app/src/main/java/info/nightscout/utils/DateUtil.java b/app/src/main/java/info/nightscout/utils/DateUtil.java index 2b127800d9..fb12986b7d 100644 --- a/app/src/main/java/info/nightscout/utils/DateUtil.java +++ b/app/src/main/java/info/nightscout/utils/DateUtil.java @@ -2,8 +2,12 @@ package info.nightscout.utils; import java.text.DateFormat; import java.text.SimpleDateFormat; +import java.util.Calendar; import java.util.Date; +import java.util.GregorianCalendar; import java.util.TimeZone; +import java.util.regex.Matcher; +import java.util.regex.Pattern; /** * The Class DateUtil. A simple wrapper around SimpleDateFormat to ease the handling of iso date string <-> date obj @@ -54,4 +58,27 @@ public class DateUtil { public static String toISOString(long date) { return toISOString(new Date(date), FORMAT_DATE_ISO, TimeZone.getTimeZone("UTC")); } + + public static Date toDate(Integer seconds) { + Calendar calendar = new GregorianCalendar(); + calendar.set(Calendar.HOUR_OF_DAY, seconds / 60 / 60); + String a = calendar.getTime().toString(); + calendar.set(Calendar.MINUTE, (seconds / 60) % 60); + String b = calendar.getTime().toString(); + calendar.set(Calendar.SECOND, 0); + String c = calendar.getTime().toString(); + return calendar.getTime(); + } + + public static int toSeconds(String hh_colon_mm) { + Pattern p = Pattern.compile("(\\d+):(\\d+)"); + Matcher m = p.matcher(hh_colon_mm); + int retval = 0; + + if (m.find()) { + retval = SafeParse.stringToInt(m.group(1)) * 60 * 60 + SafeParse.stringToInt(m.group(2)) * 60; + } + return retval; + } + } \ No newline at end of file diff --git a/app/src/main/res/drawable/bread.png b/app/src/main/res/drawable/bread.png new file mode 100644 index 0000000000..aa18f4f1d9 Binary files /dev/null and b/app/src/main/res/drawable/bread.png differ diff --git a/app/src/main/res/layout/overview_editquickwizard_dialog.xml b/app/src/main/res/layout/overview_editquickwizard_dialog.xml new file mode 100644 index 0000000000..c851d708c1 --- /dev/null +++ b/app/src/main/res/layout/overview_editquickwizard_dialog.xml @@ -0,0 +1,74 @@ + + + + + + + + + + + + + + + + + + + + + + + +