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/MainActivity.java b/app/src/main/java/info/nightscout/androidaps/MainActivity.java index 5d1eaaf274..9a7366d1f2 100644 --- a/app/src/main/java/info/nightscout/androidaps/MainActivity.java +++ b/app/src/main/java/info/nightscout/androidaps/MainActivity.java @@ -1,6 +1,7 @@ package info.nightscout.androidaps; import android.content.Intent; +import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; @@ -184,12 +185,17 @@ public class MainActivity extends AppCompatActivity { return newList; } + @Nullable public static PluginBase getSpecificPlugin(Class pluginClass) { - Iterator it = pluginsList.iterator(); - while (it.hasNext()) { - PluginBase p = it.next(); - if (p.getClass() == pluginClass) - return p; + if (pluginsList != null) { + Iterator it = pluginsList.iterator(); + while (it.hasNext()) { + PluginBase p = it.next(); + if (p.getClass() == pluginClass) + return p; + } + } else { + log.debug("pluginsList=null"); } return null; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/NSProfileViewer/NSProfileViewerFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/NSProfileViewer/NSProfileViewerFragment.java index 45a303a028..290c79e24d 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/NSProfileViewer/NSProfileViewerFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/NSProfileViewer/NSProfileViewerFragment.java @@ -1,6 +1,7 @@ package info.nightscout.androidaps.plugins.NSProfileViewer; import android.app.Activity; +import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.support.annotation.Nullable; @@ -22,6 +23,7 @@ import java.text.DecimalFormat; import info.nightscout.androidaps.Config; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; +import info.nightscout.androidaps.Services.Intents; import info.nightscout.androidaps.events.EventNewBasalProfile; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.ProfileInterface; @@ -119,7 +121,7 @@ public class NSProfileViewerFragment extends Fragment implements PluginBase, Pro private void setContent() { if (profile == null) { noProfile.setVisibility(View.VISIBLE); - return; + return; } else { noProfile.setVisibility(View.GONE); } @@ -187,6 +189,9 @@ public class NSProfileViewerFragment extends Fragment implements PluginBase, Pro } else { if (Config.logPrefsChange) { log.debug("Stored profile not found"); + // force restart of nsclient to fetch profile + Intent restartNSClient = new Intent(Intents.ACTION_RESTART); + MainApp.instance().getApplicationContext().sendBroadcast(restartNSClient); } } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewTreatmentDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewTreatmentDialog.java index 89e76822f0..f6cb977e43 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewTreatmentDialog.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewTreatmentDialog.java @@ -46,7 +46,7 @@ public class NewTreatmentDialog extends DialogFragment implements OnClickListene Double insulin = SafeParse.stringToDouble(this.insulin.getText().toString()); Integer carbs = SafeParse.stringToInt(this.carbs.getText().toString()); - String confirmMessage = getString(R.string.entertreatmentquestion); + String confirmMessage = getString(R.string.entertreatmentquestion) + "\n"; Double insulinAfterConstraints = MainApp.getConfigBuilder().applyBolusConstraints(insulin); Integer carbsAfterConstraints = MainApp.getConfigBuilder().applyCarbsConstraints(carbs); 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 a51124029b..b7595ae719 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 @@ -35,8 +35,10 @@ public class WizardDialog extends DialogFragment implements OnClickListener { TextView bg, bgInsulin, bgUnits; CheckBox bgCheckbox; TextView carbs, carbsInsulin; - TextView iob, iobInsulin; - CheckBox iobCheckbox; + TextView bolusIobInsulin; + TextView basalIobInsulin; + CheckBox bolusIobCheckbox; + CheckBox basalIobCheckbox; TextView correctionInsulin; TextView total, totalInsulin; @@ -48,20 +50,22 @@ public class WizardDialog extends DialogFragment implements OnClickListener { final private TextWatcher textWatcher = new TextWatcher() { @Override - public void afterTextChanged(Editable s) {} + public void afterTextChanged(Editable s) { + } + @Override - public void beforeTextChanged(CharSequence s, int start, int count, int after) {} + public void beforeTextChanged(CharSequence s, int start, int count, int after) { + } + @Override - public void onTextChanged(CharSequence s, int start,int before, int count) { + public void onTextChanged(CharSequence s, int start, int before, int count) { calculateInsulin(); } }; - final CompoundButton.OnCheckedChangeListener onCheckedChangeListener = new CompoundButton.OnCheckedChangeListener() - { + final CompoundButton.OnCheckedChangeListener onCheckedChangeListener = new CompoundButton.OnCheckedChangeListener() { @Override - public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) - { + public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { calculateInsulin(); } }; @@ -76,29 +80,31 @@ public class WizardDialog extends DialogFragment implements OnClickListener { getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE); getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE); - correctionInput = (TextView)view.findViewById(R.id.treatments_wizard_correctioninput); - carbsInput = (TextView)view.findViewById(R.id.treatments_wizard_carbsinput); - bgInput = (TextView)view.findViewById(R.id.treatments_wizard_bginput); + correctionInput = (TextView) view.findViewById(R.id.treatments_wizard_correctioninput); + carbsInput = (TextView) view.findViewById(R.id.treatments_wizard_carbsinput); + bgInput = (TextView) view.findViewById(R.id.treatments_wizard_bginput); correctionInput.addTextChangedListener(textWatcher); carbsInput.addTextChangedListener(textWatcher); bgInput.addTextChangedListener(textWatcher); - bg = (TextView)view.findViewById(R.id.treatments_wizard_bg); - bgInsulin = (TextView)view.findViewById(R.id.treatments_wizard_bginsulin); - bgUnits = (TextView)view.findViewById(R.id.treatments_wizard_bgunits); + bg = (TextView) view.findViewById(R.id.treatments_wizard_bg); + bgInsulin = (TextView) view.findViewById(R.id.treatments_wizard_bginsulin); + bgUnits = (TextView) view.findViewById(R.id.treatments_wizard_bgunits); bgCheckbox = (CheckBox) view.findViewById(R.id.treatments_wizard_bgcheckbox); - carbs = (TextView)view.findViewById(R.id.treatments_wizard_carbs); - carbsInsulin = (TextView)view.findViewById(R.id.treatments_wizard_carbsinsulin); - iob = (TextView)view.findViewById(R.id.treatments_wizard_iob); - iobInsulin = (TextView)view.findViewById(R.id.treatments_wizard_iobinsulin); - iobCheckbox = (CheckBox) view.findViewById(R.id.treatments_wizard_iobcheckbox); - correctionInsulin = (TextView)view.findViewById(R.id.treatments_wizard_correctioninsulin); - total = (TextView)view.findViewById(R.id.treatments_wizard_total); - totalInsulin = (TextView)view.findViewById(R.id.treatments_wizard_totalinsulin); + carbs = (TextView) view.findViewById(R.id.treatments_wizard_carbs); + carbsInsulin = (TextView) view.findViewById(R.id.treatments_wizard_carbsinsulin); + bolusIobInsulin = (TextView) view.findViewById(R.id.treatments_wizard_bolusiobinsulin); + basalIobInsulin = (TextView) view.findViewById(R.id.treatments_wizard_basaliobinsulin); + bolusIobCheckbox = (CheckBox) view.findViewById(R.id.treatments_wizard_bolusiobcheckbox); + basalIobCheckbox = (CheckBox) view.findViewById(R.id.treatments_wizard_basaliobcheckbox); + correctionInsulin = (TextView) view.findViewById(R.id.treatments_wizard_correctioninsulin); + total = (TextView) view.findViewById(R.id.treatments_wizard_total); + totalInsulin = (TextView) view.findViewById(R.id.treatments_wizard_totalinsulin); bgCheckbox.setOnCheckedChangeListener(onCheckedChangeListener); - iobCheckbox.setOnCheckedChangeListener(onCheckedChangeListener); + basalIobCheckbox.setOnCheckedChangeListener(onCheckedChangeListener); + bolusIobCheckbox.setOnCheckedChangeListener(onCheckedChangeListener); initDialog(); return view; @@ -108,7 +114,7 @@ public class WizardDialog extends DialogFragment implements OnClickListener { public void onClick(View view) { switch (view.getId()) { case R.id.treatments_wizard_deliverButton: - if (calculatedTotalInsulin > 0d || calculatedCarbs > 0d){ + if (calculatedTotalInsulin > 0d || calculatedCarbs > 0d) { DecimalFormat formatNumber2decimalplaces = new DecimalFormat("0.00"); String confirmMessage = getString(R.string.entertreatmentquestion); @@ -118,7 +124,7 @@ public class WizardDialog extends DialogFragment implements OnClickListener { confirmMessage += "\n" + getString(R.string.bolus) + ": " + formatNumber2decimalplaces.format(insulinAfterConstraints) + "U"; confirmMessage += "\n" + getString(R.string.carbs) + ": " + carbsAfterConstraints + "g"; - if (insulinAfterConstraints != calculatedTotalInsulin || carbsAfterConstraints != calculatedCarbs) { + if (insulinAfterConstraints - calculatedTotalInsulin != 0 || carbsAfterConstraints != calculatedCarbs) { AlertDialog.Builder builder = new AlertDialog.Builder(getContext()); builder.setTitle(getContext().getString(R.string.treatmentdeliveryerror)); builder.setMessage(getString(R.string.constraints_violation) + "\n" + getString(R.string.changeyourinput)); @@ -162,7 +168,6 @@ public class WizardDialog extends DialogFragment implements OnClickListener { if (profile == null) { ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(), "No profile loaded from NS yet"); - dismiss(); return; } @@ -175,8 +180,8 @@ public class WizardDialog extends DialogFragment implements OnClickListener { if (lastBg != null) { Double lastBgValue = lastBg.valueToUnits(units); Double sens = profile.getIsf(NSProfile.secondsFromMidnight()); - Double targetBGLow = profile.getTargetLow(NSProfile.secondsFromMidnight()); - Double targetBGHigh = profile.getTargetHigh(NSProfile.secondsFromMidnight()); + Double targetBGLow = profile.getTargetLow(NSProfile.secondsFromMidnight()); + Double targetBGHigh = profile.getTargetHigh(NSProfile.secondsFromMidnight()); Double bgDiff; if (lastBgValue <= targetBGLow) { bgDiff = lastBgValue - targetBGLow; @@ -186,11 +191,15 @@ public class WizardDialog extends DialogFragment implements OnClickListener { bg.setText(lastBg.valueToUnitsToString(units) + " ISF: " + intFormat.format(sens)); bgInsulin.setText(numberFormat.format(bgDiff / sens) + "U"); + bgInput.removeTextChangedListener(textWatcher); bgInput.setText(lastBg.valueToUnitsToString(units)); + bgInput.addTextChangedListener(textWatcher); } else { bg.setText(""); bgInsulin.setText(""); + bgInput.removeTextChangedListener(textWatcher); bgInput.setText(""); + bgInput.addTextChangedListener(textWatcher); } // IOB calculation @@ -201,8 +210,8 @@ public class WizardDialog extends DialogFragment implements OnClickListener { IobTotal bolusIob = treatments.getLastCalculation(); IobTotal basalIob = tempBasals.getLastCalculation(); - Double iobTotal = bolusIob.iob + basalIob.iob; - iobInsulin.setText("-" + numberFormat.format(iobTotal) + "U"); + bolusIobInsulin.setText("-" + numberFormat.format(bolusIob.iob) + "U"); + basalIobInsulin.setText("-" + numberFormat.format(basalIob.basaliob) + "U"); totalInsulin.setText(""); wizardDialogDeliverButton.setVisibility(Button.GONE); @@ -216,29 +225,37 @@ public class WizardDialog extends DialogFragment implements OnClickListener { Double c_bg = SafeParse.stringToDouble(this.bgInput.getText().toString()); Integer c_carbs = SafeParse.stringToInt(this.carbsInput.getText().toString()); Double c_correction = SafeParse.stringToDouble(this.correctionInput.getText().toString()); - if(c_correction != MainApp.getConfigBuilder().applyBolusConstraints(c_correction)) { - this.correctionInput.setText(""); - wizardDialogDeliverButton.setVisibility(Button.GONE); + Double corrAfterConstraint = MainApp.getConfigBuilder().applyBolusConstraints(c_correction); + if (c_correction - corrAfterConstraint != 0) { // c_correction != corrAfterConstraint doesn't work + correctionInput.removeTextChangedListener(textWatcher); + correctionInput.setText(""); + correctionInput.addTextChangedListener(textWatcher); + //wizardDialogDeliverButton.setVisibility(Button.GONE); + ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(), getString(R.string.bolusconstraintapplied)); return; } - if(c_carbs != MainApp.getConfigBuilder().applyCarbsConstraints(c_carbs)) { - this.carbsInput.setText(""); - wizardDialogDeliverButton.setVisibility(Button.GONE); + Integer carbsAfterConstraint = MainApp.getConfigBuilder().applyCarbsConstraints(c_carbs); + if (c_carbs != carbsAfterConstraint) { + carbsInput.removeTextChangedListener(textWatcher); + carbsInput.setText(""); + carbsInput.addTextChangedListener(textWatcher); + //wizardDialogDeliverButton.setVisibility(Button.GONE); + ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(), getString(R.string.carbsconstraintapplied)); return; } // Insulin from BG Double sens = profile.getIsf(NSProfile.secondsFromMidnight()); - Double targetBGLow = profile.getTargetLow(NSProfile.secondsFromMidnight()); - Double targetBGHigh = profile.getTargetHigh(NSProfile.secondsFromMidnight()); + Double targetBGLow = profile.getTargetLow(NSProfile.secondsFromMidnight()); + Double targetBGHigh = profile.getTargetHigh(NSProfile.secondsFromMidnight()); Double bgDiff; if (c_bg <= targetBGLow) { bgDiff = c_bg - targetBGLow; } else { bgDiff = c_bg - targetBGHigh; } - Double insulinFromBG = (bgCheckbox.isChecked() && c_bg != 0d) ? bgDiff /sens : 0d; + Double insulinFromBG = (bgCheckbox.isChecked() && c_bg != 0d) ? bgDiff / sens : 0d; bg.setText(c_bg + " ISF: " + intFormat.format(sens)); bgInsulin.setText(numberFormat.format(insulinFromBG) + "U"); @@ -256,16 +273,17 @@ public class WizardDialog extends DialogFragment implements OnClickListener { IobTotal bolusIob = treatments.getLastCalculation(); IobTotal basalIob = tempBasals.getLastCalculation(); - Double iobTotal = bolusIob.iob + basalIob.iob; - Double insulingFromIOB = iobCheckbox.isChecked() ? iobTotal : 0d; - iobInsulin.setText("-" + numberFormat.format(insulingFromIOB) + "U"); + Double insulingFromBolusIOB = bolusIobCheckbox.isChecked() ? -bolusIob.iob : 0d; + Double insulingFromBasalsIOB = basalIobCheckbox.isChecked() ? -basalIob.basaliob : 0d; + bolusIobInsulin.setText(numberFormat.format(insulingFromBolusIOB) + "U"); + basalIobInsulin.setText(numberFormat.format(insulingFromBasalsIOB) + "U"); // Insulin from correction - Double insulinFromCorrection = c_correction; + Double insulinFromCorrection = corrAfterConstraint; correctionInsulin.setText(numberFormat.format(insulinFromCorrection) + "U"); // Total - calculatedTotalInsulin = insulinFromBG + insulinFromCarbs - insulingFromIOB + insulinFromCorrection; + calculatedTotalInsulin = insulinFromBG + insulinFromCarbs + insulingFromBolusIOB + insulingFromBasalsIOB + insulinFromCorrection; if (calculatedTotalInsulin < 0) { Double carbsEquivalent = -calculatedTotalInsulin * ic; 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 e5335cd47a..d4ebfd6de8 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 @@ -377,11 +377,6 @@ public class OverviewFragment extends Fragment implements PluginBase { BgReading lastBG = MainApp.getDbHelper().lastBg(); if (MainApp.getConfigBuilder() == null || MainApp.getConfigBuilder().getActiveProfile() == null) // app not initialized yet return; - NSProfile profile = MainApp.getConfigBuilder().getActiveProfile().getProfile(); - if (profile == null) - return; - - String units = profile.getUnits(); // Skip if not initialized yet if (bgGraph == null) @@ -412,6 +407,7 @@ public class OverviewFragment extends Fragment implements PluginBase { } // **** Temp button **** + NSProfile profile = MainApp.getConfigBuilder().getActiveProfile().getProfile(); PumpInterface pump = MainApp.getConfigBuilder().getActivePump(); if (pump.isTempBasalInProgress()) { @@ -427,6 +423,22 @@ public class OverviewFragment extends Fragment implements PluginBase { runningTempView.setText(formatNumber2decimalplaces.format(currentBasal) + " U/h"); } + if (profile == null) { + // disable all treatment buttons because we are not able to check constraints without profile + setExtenedButton.setVisibility(View.INVISIBLE); + setTempLayout.setVisibility(View.INVISIBLE); + wizardButton.setVisibility(View.INVISIBLE); + treatmentButton.setVisibility(View.INVISIBLE); + return; + } else { + setExtenedButton.setVisibility(View.VISIBLE); + setTempLayout.setVisibility(View.VISIBLE); + wizardButton.setVisibility(View.VISIBLE); + treatmentButton.setVisibility(View.VISIBLE); + } + + String units = profile.getUnits(); + // **** BG value **** if (profile != null && lastBG != null && bgView != null) { bgView.setText(lastBG.valueToUnitsToString(profile.getUnits())); diff --git a/app/src/main/res/layout/overview_wizard_fragment.xml b/app/src/main/res/layout/overview_wizard_fragment.xml index efea953295..b15b2c10ca 100644 --- a/app/src/main/res/layout/overview_wizard_fragment.xml +++ b/app/src/main/res/layout/overview_wizard_fragment.xml @@ -204,28 +204,64 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" android:width="32dp" - android:id="@+id/treatments_wizard_iobcheckbox" + android:id="@+id/treatments_wizard_bolusiobcheckbox" android:checked="true" /> + android:text="@string/treatments_wizard_bolusiob_label" + android:width="100dp" /> + android:width="50dp" + android:id="@+id/treatments_wizard_bolusiob" /> + + + + + + + + + + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 47f1ac7f19..dfa05227dc 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -47,7 +47,7 @@ Carbs Corr U - IOB + Bolus IOB TOTAL Run now VIRTUAL PUMP @@ -156,5 +156,8 @@ LOOP DISABLED BY CONSTRAINTS Czech language English language + Basal IOB + Bolus constraint applied + Carbs constraint applied