diff --git a/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java b/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java index c12252fbc9..e73a8dae17 100644 --- a/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java +++ b/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java @@ -74,7 +74,7 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper { public static final String DATABASE_PROFILESWITCHES = "ProfileSwitches"; public static final String DATABASE_TDDS = "TDDs"; - private static final int DATABASE_VERSION = 8; + private static final int DATABASE_VERSION = 9; public static Long earliestDataChange = null; @@ -133,6 +133,8 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper { if (oldVersion == 7 && newVersion == 8) { log.debug("Upgrading database from v7 to v8"); + } else if (oldVersion == 8 && newVersion == 9) { + log.debug("Upgrading database from v8 to v9"); } else { log.info(DatabaseHelper.class.getName(), "onUpgrade"); TableUtils.dropTable(connectionSource, TempTarget.class, true); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Food/FoodService.java b/app/src/main/java/info/nightscout/androidaps/plugins/Food/FoodService.java index c864c623db..b2e23dfd69 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Food/FoodService.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Food/FoodService.java @@ -121,13 +121,9 @@ public class FoodService extends OrmLiteBaseService { } public void onUpgrade(ConnectionSource connectionSource, int oldVersion, int newVersion) { - if (oldVersion == 7 && newVersion == 8) { - log.debug("Upgrading database from v7 to v8"); - } else { - if (L.isEnabled(L.DATAFOOD)) - log.info("onUpgrade"); + if (L.isEnabled(L.DATAFOOD)) + log.info("onUpgrade"); // this.resetFood(); - } } public void onDowngrade(ConnectionSource connectionSource, int oldVersion, int newVersion) { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/WizardDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/WizardDialog.java index 7985418ada..458608edbf 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/WizardDialog.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/WizardDialog.java @@ -483,9 +483,10 @@ public class WizardDialog extends DialogFragment implements OnClickListener, Com return; // not initialized yet String selectedAlternativeProfile = profileSpinner.getSelectedItem().toString(); Profile specificProfile; - if (selectedAlternativeProfile.equals(MainApp.gs(R.string.active))) + if (selectedAlternativeProfile.equals(MainApp.gs(R.string.active))) { specificProfile = ProfileFunctions.getInstance().getProfile(); - else + selectedAlternativeProfile = MainApp.getConfigBuilder().getActiveProfileInterface().getProfileName(); + } else specificProfile = profileStore.getSpecificProfile(selectedAlternativeProfile); // Entered values @@ -579,12 +580,15 @@ public class WizardDialog extends DialogFragment implements OnClickListener, Com boluscalcJSON = new JSONObject(); try { boluscalcJSON.put("profile", selectedAlternativeProfile); + boluscalcJSON.put("notes", notesEdit.getText()); 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("bolusiob", wizard.insulingFromBolusIOB); + boluscalcJSON.put("basaliob", wizard.insulingFromBasalsIOB); boluscalcJSON.put("bolusiobused", bolusIobCheckbox.isChecked()); boluscalcJSON.put("basaliobused", basalIobCheckbox.isChecked()); boluscalcJSON.put("bg", c_bg); @@ -594,11 +598,18 @@ public class WizardDialog extends DialogFragment implements OnClickListener, Com boluscalcJSON.put("insulincarbs", wizard.insulinFromCarbs); boluscalcJSON.put("carbs", c_carbs); boluscalcJSON.put("cob", c_cob); + boluscalcJSON.put("cobused", cobCheckbox.isChecked()); boluscalcJSON.put("insulincob", wizard.insulinFromCOB); boluscalcJSON.put("othercorrection", corrAfterConstraint); boluscalcJSON.put("insulinsuperbolus", wizard.insulinFromSuperBolus); boluscalcJSON.put("insulintrend", wizard.insulinFromTrend); boluscalcJSON.put("insulin", calculatedTotalInsulin); + boluscalcJSON.put("superbolusused", superbolusCheckbox.isChecked()); + boluscalcJSON.put("insulinsuperbolus", wizard.insulinFromSuperBolus); + boluscalcJSON.put("trendused", bgtrendCheckbox.isChecked()); + boluscalcJSON.put("insulintrend", wizard.insulinFromTrend); + boluscalcJSON.put("trend", bgTrend.getText()); + boluscalcJSON.put("ttused", ttCheckbox.isChecked()); } catch (JSONException e) { log.error("Unhandled exception", e); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/Treatment.java b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/Treatment.java index 9d76097a7f..2e9dfdd5ea 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/Treatment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/Treatment.java @@ -1,6 +1,7 @@ package info.nightscout.androidaps.plugins.Treatments; import android.graphics.Color; +import android.support.annotation.Nullable; import com.j256.ormlite.field.DatabaseField; import com.j256.ormlite.table.DatabaseTable; @@ -59,6 +60,8 @@ public class Treatment implements DataPointWithLabelInterface { public int insulinInterfaceID = InsulinInterface.OREF_RAPID_ACTING; // currently unused, will be used in the future @DatabaseField public double dia = Constants.defaultDIA; // currently unused, will be used in the future + @DatabaseField + public String boluscalc; public Treatment() { } @@ -79,6 +82,7 @@ public class Treatment implements DataPointWithLabelInterface { double carbs = treatment.carbs; if (json.has("boluscalc")) { JSONObject boluscalc = json.getJSONObject("boluscalc"); + treatment.boluscalc = boluscalc.toString(); if (boluscalc.has("carbs")) { carbs = Math.max(boluscalc.getDouble("carbs"), carbs); } @@ -131,6 +135,15 @@ public class Treatment implements DataPointWithLabelInterface { return true; } + @Nullable + public JSONObject getBoluscalc() { + try { + if (boluscalc != null) + return new JSONObject(boluscalc); + } catch (JSONException ignored) { + } + return null; + } /* * mealBolus, _id and isSMB cannot be known coming from pump. Only compare rest diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentService.java b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentService.java index 9e09b661c1..7a316161b3 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentService.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentService.java @@ -122,6 +122,13 @@ public class TreatmentService extends OrmLiteBaseService { log.error("Can't create database", e); throw new RuntimeException(e); } + } else if (oldVersion == 8 && newVersion == 9) { + log.debug("Upgrading database from v8 to v9"); + try { + getDao().executeRaw("ALTER TABLE `" + Treatment.TABLE_TREATMENTS + "` ADD COLUMN boluscalc STRING;"); + } catch (SQLException e) { + e.printStackTrace(); + } } else { if (L.isEnabled(L.DATATREATMENTS)) log.info("onUpgrade"); @@ -130,7 +137,13 @@ public class TreatmentService extends OrmLiteBaseService { } public void onDowngrade(ConnectionSource connectionSource, int oldVersion, int newVersion) { - // this method is not supported right now + if (oldVersion == 9 && newVersion == 8) { + try { + getDao().executeRaw("ALTER TABLE `" + Treatment.TABLE_TREATMENTS + "` DROP COLUMN boluscalc STRING;"); + } catch (SQLException e) { + e.printStackTrace(); + } + } } public void resetTreatments() { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentsPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentsPlugin.java index bb9c7aeae0..c22db62ea9 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentsPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentsPlugin.java @@ -506,6 +506,7 @@ public class TreatmentsPlugin extends PluginBase implements TreatmentsInterface treatment.carbs = detailedBolusInfo.carbs; treatment.source = detailedBolusInfo.source; treatment.mealBolus = treatment.carbs > 0; + treatment.boluscalc = detailedBolusInfo.boluscalc.toString(); TreatmentService.UpdateReturn creatOrUpdateResult = getService().createOrUpdate(treatment); boolean newRecordCreated = creatOrUpdateResult.newRecord; //log.debug("Adding new Treatment record" + treatment.toString()); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/dialogs/WizardInfoDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/dialogs/WizardInfoDialog.java new file mode 100644 index 0000000000..3adc248f12 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/dialogs/WizardInfoDialog.java @@ -0,0 +1,88 @@ +package info.nightscout.androidaps.plugins.Treatments.dialogs; + +import android.os.Bundle; +import android.support.v4.app.DialogFragment; +import android.view.LayoutInflater; +import android.view.View; +import android.view.View.OnClickListener; +import android.view.ViewGroup; +import android.view.Window; +import android.view.WindowManager; +import android.widget.CheckBox; +import android.widget.TextView; + +import org.json.JSONObject; + +import info.nightscout.androidaps.R; +import info.nightscout.utils.DecimalFormatter; +import info.nightscout.utils.JsonHelper; + +public class WizardInfoDialog extends DialogFragment implements OnClickListener { + JSONObject json; + + public WizardInfoDialog() { + super(); + } + + public void setData(JSONObject json) { + this.json = json; + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + View view = inflater.inflate(R.layout.treatments_wizardinfo_dialog, null, false); + + getDialog().getWindow().requestFeature(Window.FEATURE_CUSTOM_TITLE); + getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN); + + view.findViewById(R.id.ok).setOnClickListener(this); + + // BG + ((TextView) view.findViewById(R.id.treatments_wizard_bg)).setText(DecimalFormatter.to1Decimal(JsonHelper.safeGetDouble(json, "bg")) + " ISF: " + DecimalFormatter.to1Decimal(JsonHelper.safeGetDouble(json, "isf"))); + ((TextView) view.findViewById(R.id.treatments_wizard_bginsulin)).setText(DecimalFormatter.to2Decimal(JsonHelper.safeGetDouble(json, "insulinbg")) + "U"); + ((CheckBox) view.findViewById(R.id.treatments_wizard_bgcheckbox)).setChecked(JsonHelper.safeGetBoolean(json, "insulinbgused")); + ((CheckBox) view.findViewById(R.id.treatments_wizard_ttcheckbox)).setChecked(JsonHelper.safeGetBoolean(json, "ttused")); + // Trend + ((TextView) view.findViewById(R.id.treatments_wizard_bgtrend)).setText(JsonHelper.safeGetString(json, "trend")); + ((TextView) view.findViewById(R.id.treatments_wizard_bgtrendinsulin)).setText(DecimalFormatter.to2Decimal(JsonHelper.safeGetDouble(json, "insulintrend")) + "U"); + ((CheckBox) view.findViewById(R.id.treatments_wizard_bgtrendcheckbox)).setChecked(JsonHelper.safeGetBoolean(json, "trendused")); + // COB + ((TextView) view.findViewById(R.id.treatments_wizard_cob)).setText(DecimalFormatter.to0Decimal(JsonHelper.safeGetDouble(json, "cob")) + "g IC: " + DecimalFormatter.to1Decimal(JsonHelper.safeGetDouble(json, "ic"))); + ((TextView) view.findViewById(R.id.treatments_wizard_cobinsulin)).setText(DecimalFormatter.to2Decimal(JsonHelper.safeGetDouble(json, "insulincob")) + "U"); + ((CheckBox) view.findViewById(R.id.treatments_wizard_cobcheckbox)).setChecked(JsonHelper.safeGetBoolean(json, "cobused")); + // Bolus IOB + ((TextView) view.findViewById(R.id.treatments_wizard_bolusiobinsulin)).setText(DecimalFormatter.to2Decimal(JsonHelper.safeGetDouble(json, "bolusiob")) + "U"); + ((CheckBox) view.findViewById(R.id.treatments_wizard_bolusiobcheckbox)).setChecked(JsonHelper.safeGetBoolean(json, "bolusiobused")); + // Basal IOB + ((TextView) view.findViewById(R.id.treatments_wizard_basaliobinsulin)).setText(DecimalFormatter.to2Decimal(JsonHelper.safeGetDouble(json, "basaliob")) + "U"); + ((CheckBox) view.findViewById(R.id.treatments_wizard_basaliobcheckbox)).setChecked(JsonHelper.safeGetBoolean(json, "basaliobused")); + // Superbolus + ((TextView) view.findViewById(R.id.treatments_wizard_sbinsulin)).setText(DecimalFormatter.to2Decimal(JsonHelper.safeGetDouble(json, "insulinsuperbolus")) + "U"); + ((CheckBox) view.findViewById(R.id.treatments_wizard_sbcheckbox)).setChecked(JsonHelper.safeGetBoolean(json, "superbolusused")); + // Carbs + ((TextView) view.findViewById(R.id.treatments_wizard_carbs)).setText(DecimalFormatter.to0Decimal(JsonHelper.safeGetDouble(json, "carbs")) + "g IC: " + DecimalFormatter.to1Decimal(JsonHelper.safeGetDouble(json, "ic"))); + ((TextView) view.findViewById(R.id.treatments_wizard_carbsinsulin)).setText(DecimalFormatter.to2Decimal(JsonHelper.safeGetDouble(json, "insulincarbs")) + "U"); + // Correction + ((TextView) view.findViewById(R.id.treatments_wizard_correctioninsulin)).setText(DecimalFormatter.to2Decimal(JsonHelper.safeGetDouble(json, "othercorrection")) + "U"); + // Profile + ((TextView) view.findViewById(R.id.treatments_wizard_profile)).setText(JsonHelper.safeGetString(json, "profile")); + // Notes + ((TextView) view.findViewById(R.id.treatments_wizard_notes)).setText(JsonHelper.safeGetString(json, "notes")); + // Total + ((TextView) view.findViewById(R.id.treatments_wizard_totalinsulin)).setText(DecimalFormatter.to2Decimal(JsonHelper.safeGetDouble(json, "insulin")) + "U"); + + setCancelable(true); + return view; + } + + @Override + public synchronized void onClick(View view) { + switch (view.getId()) { + case R.id.ok: + dismiss(); + break; + } + } + +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/fragments/TreatmentsBolusFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/fragments/TreatmentsBolusFragment.java index c9b744e86d..b825cc3fb8 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/fragments/TreatmentsBolusFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/fragments/TreatmentsBolusFragment.java @@ -6,6 +6,7 @@ import android.content.DialogInterface; import android.content.Intent; import android.graphics.Paint; import android.os.Bundle; +import android.support.v4.app.FragmentManager; import android.support.v4.content.ContextCompat; import android.support.v7.app.AlertDialog; import android.support.v7.widget.CardView; @@ -20,25 +21,28 @@ import android.widget.TextView; import com.crashlytics.android.answers.CustomEvent; import com.squareup.otto.Subscribe; +import org.json.JSONObject; + import java.util.List; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; -import info.nightscout.androidaps.plugins.ConfigBuilder.ProfileFunctions; -import info.nightscout.androidaps.services.Intents; import info.nightscout.androidaps.data.Iob; import info.nightscout.androidaps.data.Profile; import info.nightscout.androidaps.db.Source; -import info.nightscout.androidaps.plugins.Treatments.Treatment; import info.nightscout.androidaps.events.EventNewBG; import info.nightscout.androidaps.events.EventTreatmentChange; import info.nightscout.androidaps.plugins.Common.SubscriberFragment; +import info.nightscout.androidaps.plugins.ConfigBuilder.ProfileFunctions; +import info.nightscout.androidaps.plugins.NSClientInternal.NSUpload; import info.nightscout.androidaps.plugins.NSClientInternal.UploadQueue; +import info.nightscout.androidaps.plugins.Treatments.Treatment; import info.nightscout.androidaps.plugins.Treatments.TreatmentsPlugin; -import info.nightscout.utils.FabricPrivacy; +import info.nightscout.androidaps.plugins.Treatments.dialogs.WizardInfoDialog; +import info.nightscout.androidaps.services.Intents; import info.nightscout.utils.DateUtil; import info.nightscout.utils.DecimalFormatter; -import info.nightscout.androidaps.plugins.NSClientInternal.NSUpload; +import info.nightscout.utils.FabricPrivacy; import info.nightscout.utils.SP; import static info.nightscout.utils.DateUtil.now; @@ -79,7 +83,6 @@ public class TreatmentsBolusFragment extends SubscriberFragment implements View. holder.carbs.setText(DecimalFormatter.to0Decimal(t.carbs) + " g"); Iob iob = t.iobCalc(System.currentTimeMillis(), profile.getDia()); holder.iob.setText(DecimalFormatter.to2Decimal(iob.iobContrib) + " U"); - holder.activity.setText(DecimalFormatter.to3Decimal(iob.activityContrib) + " U"); holder.mealOrCorrection.setText(t.isSMB ? "SMB" : t.mealBolus ? MainApp.gs(R.string.mealbolus) : MainApp.gs(R.string.correctionbous)); holder.ph.setVisibility(t.source == Source.PUMP ? View.VISIBLE : View.GONE); holder.ns.setVisibility(NSUpload.isIdValid(t._id) ? View.VISIBLE : View.GONE); @@ -93,6 +96,8 @@ public class TreatmentsBolusFragment extends SubscriberFragment implements View. else holder.date.setTextColor(holder.carbs.getCurrentTextColor()); holder.remove.setTag(t); + holder.calculation.setTag(t); + holder.calculation.setVisibility(t.getBoluscalc() == null ? View.INVISIBLE : View.VISIBLE); } @Override @@ -111,9 +116,9 @@ public class TreatmentsBolusFragment extends SubscriberFragment implements View. TextView insulin; TextView carbs; TextView iob; - TextView activity; TextView mealOrCorrection; TextView remove; + TextView calculation; TextView ph; TextView ns; TextView invalid; @@ -125,11 +130,13 @@ public class TreatmentsBolusFragment extends SubscriberFragment implements View. insulin = (TextView) itemView.findViewById(R.id.treatments_insulin); carbs = (TextView) itemView.findViewById(R.id.treatments_carbs); iob = (TextView) itemView.findViewById(R.id.treatments_iob); - activity = (TextView) itemView.findViewById(R.id.treatments_activity); mealOrCorrection = (TextView) itemView.findViewById(R.id.treatments_mealorcorrection); ph = (TextView) itemView.findViewById(R.id.pump_sign); ns = (TextView) itemView.findViewById(R.id.ns_sign); invalid = (TextView) itemView.findViewById(R.id.invalid_sign); + calculation = (TextView) itemView.findViewById(R.id.treatments_calculation); + calculation.setOnClickListener(this); + calculation.setPaintFlags(calculation.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG); remove = (TextView) itemView.findViewById(R.id.treatments_remove); remove.setOnClickListener(this); remove.setPaintFlags(remove.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG); @@ -164,6 +171,18 @@ public class TreatmentsBolusFragment extends SubscriberFragment implements View. builder.setNegativeButton(MainApp.gs(R.string.cancel), null); builder.show(); break; + case R.id.treatments_calculation: + FragmentManager manager = getFragmentManager(); + // try to fix https://fabric.io/nightscout3/android/apps/info.nightscout.androidaps/issues/5aca7a1536c7b23527eb4be7?time=last-seven-days + // https://stackoverflow.com/questions/14860239/checking-if-state-is-saved-before-committing-a-fragmenttransaction + if (manager.isStateSaved()) + return; + if (treatment.getBoluscalc() != null) { + WizardInfoDialog wizardDialog = new WizardInfoDialog(); + wizardDialog.setData(treatment.getBoluscalc()); + wizardDialog.show(manager, "WizardInfoDialog"); + } + break; } } } diff --git a/app/src/main/res/layout/treatments_bolus_item.xml b/app/src/main/res/layout/treatments_bolus_item.xml index e8a06151e8..29cbb6fe1d 100644 --- a/app/src/main/res/layout/treatments_bolus_item.xml +++ b/app/src/main/res/layout/treatments_bolus_item.xml @@ -137,15 +137,6 @@ android:layout_marginRight="30dp" android:textStyle="bold" /> - - + + diff --git a/app/src/main/res/layout/treatments_wizardinfo_dialog.xml b/app/src/main/res/layout/treatments_wizardinfo_dialog.xml new file mode 100644 index 0000000000..12e0e4e73e --- /dev/null +++ b/app/src/main/res/layout/treatments_wizardinfo_dialog.xml @@ -0,0 +1,444 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +