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 75e830e654..f45e57cfbe 100644 --- a/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java +++ b/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java @@ -666,8 +666,8 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper { TempTarget tempTarget = new TempTarget() .date(trJson.getLong("mills")) .duration(trJson.getInt("duration")) - .low(Profile.toMgdl(trJson.getDouble("targetBottom"), units)) - .high(Profile.toMgdl(trJson.getDouble("targetTop"), units)) + .low(trJson.getDouble("targetBottom")) + .high(trJson.getDouble("targetTop")) .reason(trJson.getString("reason")) ._id(trJson.getString("_id")) .source(Source.NIGHTSCOUT); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Actions/dialogs/FillDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/Actions/dialogs/FillDialog.java index b3b15bf7fb..5e99a6ca17 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Actions/dialogs/FillDialog.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Actions/dialogs/FillDialog.java @@ -5,7 +5,9 @@ import android.content.Intent; import android.os.Bundle; import android.support.v4.app.DialogFragment; import android.support.v7.app.AlertDialog; +import android.text.Editable; import android.text.Html; +import android.text.TextWatcher; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; @@ -14,6 +16,8 @@ import android.view.Window; import android.view.WindowManager; import android.widget.Button; import android.widget.CheckBox; +import android.widget.EditText; +import android.widget.LinearLayout; import com.crashlytics.android.answers.CustomEvent; import com.google.common.base.Joiner; @@ -21,8 +25,6 @@ import com.google.common.base.Joiner; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import info.nightscout.androidaps.Constants; -import java.text.DecimalFormat; import java.util.LinkedList; import java.util.List; @@ -41,19 +43,45 @@ import info.nightscout.utils.NSUpload; import info.nightscout.utils.NumberPicker; import info.nightscout.utils.SP; import info.nightscout.utils.SafeParse; +import info.nightscout.utils.ToastUtils; + +import static info.nightscout.utils.DateUtil.now; public class FillDialog extends DialogFragment implements OnClickListener { private static Logger log = LoggerFactory.getLogger(FillDialog.class); + private CheckBox pumpSiteChangeCheckbox; + private CheckBox insulinCartridgeChangeCheckbox; + + private NumberPicker editInsulin; + double amount1 = 0d; double amount2 = 0d; double amount3 = 0d; - NumberPicker editInsulin; - CheckBox pumpSiteChangeCheckbox; - CheckBox insulinCartridgeChangeCheckbox; + private EditText notesEdit; - public FillDialog() { + final private TextWatcher textWatcher = new TextWatcher() { + @Override + public void afterTextChanged(Editable s) { + validateInputs(); + } + + @Override + public void beforeTextChanged(CharSequence s, int start, int count, int after) { + } + + @Override + public void onTextChanged(CharSequence s, int start, int before, int count) { + } + }; + + private void validateInputs() { + int time = editInsulin.getValue().intValue(); + if (Math.abs(time) > 12 * 60) { + editInsulin.setValue(0d); + ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(), MainApp.gs(R.string.constraintapllied)); + } } @Override @@ -67,45 +95,47 @@ public class FillDialog extends DialogFragment implements OnClickListener { getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE); getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN); - pumpSiteChangeCheckbox = view.findViewById(R.id.catheter_change); - insulinCartridgeChangeCheckbox = view.findViewById(R.id.cartridge_change); + pumpSiteChangeCheckbox = view.findViewById(R.id.fill_catheter_change); + insulinCartridgeChangeCheckbox = view.findViewById(R.id.fill_cartridge_change); Double maxInsulin = MainApp.getConstraintChecker().getMaxBolusAllowed().value(); double bolusstep = ConfigBuilderPlugin.getActivePump().getPumpDescription().bolusStep; - editInsulin = view.findViewById(R.id.treatments_newtreatment_insulinamount); - editInsulin.setParams(0d, 0d, maxInsulin, bolusstep, DecimalFormatter.pumpSupportedBolusFormat(), false); + editInsulin = view.findViewById(R.id.fill_insulinamount); + editInsulin.setParams(0d, 0d, maxInsulin, bolusstep, DecimalFormatter.pumpSupportedBolusFormat(), false, textWatcher); - //setup preset buttons - Button button1 = (Button) view.findViewById(R.id.fill_preset_button1); - Button button2 = (Button) view.findViewById(R.id.fill_preset_button2); - Button button3 = (Button) view.findViewById(R.id.fill_preset_button3); + Button preset1Button = view.findViewById(R.id.fill_preset_button1); amount1 = SP.getDouble("fill_button1", 0.3); - amount2 = SP.getDouble("fill_button2", 0d); - amount3 = SP.getDouble("fill_button3", 0d); - if (amount1 > 0) { - button1.setVisibility(View.VISIBLE); - button1.setText(DecimalFormatter.toPumpSupportedBolus(amount1)); // + "U"); - button1.setOnClickListener(this); + preset1Button.setVisibility(View.VISIBLE); + preset1Button.setText(DecimalFormatter.toPumpSupportedBolus(amount1)); // + "U"); + preset1Button.setOnClickListener(this); } else { - button1.setVisibility(View.GONE); + preset1Button.setVisibility(View.GONE); } + Button preset2Button = view.findViewById(R.id.fill_preset_button2); + amount2 = SP.getDouble("fill_button2", 0d); if (amount2 > 0) { - button2.setVisibility(View.VISIBLE); - button2.setText(DecimalFormatter.toPumpSupportedBolus(amount2)); // + "U"); - button2.setOnClickListener(this); + preset2Button.setVisibility(View.VISIBLE); + preset2Button.setText(DecimalFormatter.toPumpSupportedBolus(amount2)); // + "U"); + preset2Button.setOnClickListener(this); } else { - button2.setVisibility(View.GONE); + preset2Button.setVisibility(View.GONE); } + Button preset3Button = view.findViewById(R.id.fill_preset_button3); + amount3 = SP.getDouble("fill_button3", 0d); if (amount3 > 0) { - button3.setVisibility(View.VISIBLE); - button3.setText(DecimalFormatter.toPumpSupportedBolus(amount3)); // + "U"); - button3.setOnClickListener(this); + preset3Button.setVisibility(View.VISIBLE); + preset3Button.setText(DecimalFormatter.toPumpSupportedBolus(amount3)); // + "U"); + preset3Button.setOnClickListener(this); } else { - button3.setVisibility(View.GONE); + preset3Button.setVisibility(View.GONE); } + LinearLayout notesLayout = view.findViewById(R.id.fill_notes_layout); + notesLayout.setVisibility(SP.getBoolean(R.string.key_show_notes_entry_dialogs, false) ? View.VISIBLE : View.GONE); + notesEdit = view.findViewById(R.id.fill_notes); + setCancelable(true); getDialog().setCanceledOnTouchOutside(false); return view; @@ -154,42 +184,50 @@ public class FillDialog extends DialogFragment implements OnClickListener { if (insulinCartridgeChangeCheckbox.isChecked()) confirmMessage.add("" + "" + getString(R.string.record_insulin_cartridge_change) + ""); + final String notes = notesEdit.getText().toString(); + if (!notes.isEmpty()) { + confirmMessage.add(MainApp.gs(R.string.careportal_newnstreatment_notes_label) + ": " + notes); + } + final Double finalInsulinAfterConstraints = insulinAfterConstraints; final Context context = getContext(); AlertDialog.Builder builder = new AlertDialog.Builder(context); - if (confirmMessage.isEmpty()) - confirmMessage.add(MainApp.gs(R.string.no_action_selected)); - builder.setTitle(MainApp.gs(R.string.confirmation)); - builder.setMessage(Html.fromHtml(Joiner.on("
").join(confirmMessage))); - builder.setPositiveButton(getString(R.string.primefill), (dialog, id) -> { - if (finalInsulinAfterConstraints > 0) { - DetailedBolusInfo detailedBolusInfo = new DetailedBolusInfo(); - detailedBolusInfo.insulin = finalInsulinAfterConstraints; - detailedBolusInfo.context = context; - detailedBolusInfo.source = Source.USER; - detailedBolusInfo.isValid = false; // do not count it in IOB (for pump history) - ConfigBuilderPlugin.getCommandQueue().bolus(detailedBolusInfo, new Callback() { - @Override - public void run() { - if (!result.success) { - Intent i = new Intent(MainApp.instance(), ErrorHelperActivity.class); - i.putExtra("soundid", R.raw.boluserror); - i.putExtra("status", result.comment); - i.putExtra("title", MainApp.sResources.getString(R.string.treatmentdeliveryerror)); - i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - MainApp.instance().startActivity(i); + if (insulinAfterConstraints > 0 || pumpSiteChangeCheckbox.isChecked() || insulinCartridgeChangeCheckbox.isChecked()) { + builder.setMessage(Html.fromHtml(Joiner.on("
").join(confirmMessage))); + builder.setPositiveButton(getString(R.string.primefill), (dialog, id) -> { + if (finalInsulinAfterConstraints > 0) { + DetailedBolusInfo detailedBolusInfo = new DetailedBolusInfo(); + detailedBolusInfo.insulin = finalInsulinAfterConstraints; + detailedBolusInfo.context = context; + detailedBolusInfo.source = Source.USER; + detailedBolusInfo.isValid = false; // do not count it in IOB (for pump history) + detailedBolusInfo.notes = notes; + ConfigBuilderPlugin.getCommandQueue().bolus(detailedBolusInfo, new Callback() { + @Override + public void run() { + if (!result.success) { + Intent i = new Intent(MainApp.instance(), ErrorHelperActivity.class); + i.putExtra("soundid", R.raw.boluserror); + i.putExtra("status", result.comment); + i.putExtra("title", MainApp.sResources.getString(R.string.treatmentdeliveryerror)); + i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + MainApp.instance().startActivity(i); + } } - } - }); - FabricPrivacy.getInstance().logCustom(new CustomEvent("Fill")); - } - long now = System.currentTimeMillis(); - if (pumpSiteChangeCheckbox.isChecked()) NSUpload.uploadEvent(CareportalEvent.SITECHANGE, now); - if (insulinCartridgeChangeCheckbox.isChecked()) NSUpload.uploadEvent(CareportalEvent.INSULINCHANGE, now + 1000); - }); + }); + FabricPrivacy.getInstance().logCustom(new CustomEvent("Fill")); + } + if (pumpSiteChangeCheckbox.isChecked()) + NSUpload.uploadEvent(CareportalEvent.SITECHANGE, now(), notes); + if (insulinCartridgeChangeCheckbox.isChecked()) + NSUpload.uploadEvent(CareportalEvent.INSULINCHANGE, now() + 1000, notes); + }); + } else { + builder.setMessage(MainApp.gs(R.string.no_action_selected)); + } builder.setNegativeButton(getString(R.string.cancel), null); builder.show(); dismiss(); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewCarbsDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewCarbsDialog.java index d8bdb6648d..a0da25c251 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewCarbsDialog.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewCarbsDialog.java @@ -1,15 +1,14 @@ package info.nightscout.androidaps.plugins.Overview.Dialogs; -import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.os.HandlerThread; +import android.support.annotation.Nullable; import android.support.v4.app.DialogFragment; import android.support.v7.app.AlertDialog; import android.text.Editable; import android.text.Html; import android.text.TextWatcher; -import android.text.format.DateFormat; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; @@ -19,20 +18,15 @@ import android.view.WindowManager; import android.widget.Button; import android.widget.CompoundButton; import android.widget.EditText; +import android.widget.LinearLayout; import android.widget.RadioButton; -import android.widget.TextView; import com.google.common.base.Joiner; -import com.wdullaer.materialdatetimepicker.date.DatePickerDialog; -import com.wdullaer.materialdatetimepicker.time.RadialPickerLayout; -import com.wdullaer.materialdatetimepicker.time.TimePickerDialog; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.text.DecimalFormat; -import java.util.Calendar; -import java.util.Date; import java.util.LinkedList; import java.util.List; @@ -52,36 +46,29 @@ import info.nightscout.utils.DateUtil; import info.nightscout.utils.DecimalFormatter; import info.nightscout.utils.NumberPicker; import info.nightscout.utils.SP; -import info.nightscout.utils.SafeParse; import info.nightscout.utils.ToastUtils; -public class NewCarbsDialog extends DialogFragment implements OnClickListener, DatePickerDialog.OnDateSetListener, TimePickerDialog.OnTimeSetListener, CompoundButton.OnCheckedChangeListener { +import static info.nightscout.utils.DateUtil.now; + +public class NewCarbsDialog extends DialogFragment implements OnClickListener, CompoundButton.OnCheckedChangeListener { private static Logger log = LoggerFactory.getLogger(NewCarbsDialog.class); - private NumberPicker editCarbs; - - private TextView dateButton; - private TextView timeButton; - - private Date initialEventTime; - private Date eventTime; - - private Button fav1Button; - private Button fav2Button; - private Button fav3Button; - - private EditText notesEdit; - private static final int FAV1_DEFAULT = 5; private static final int FAV2_DEFAULT = 10; private static final int FAV3_DEFAULT = 20; + private RadioButton startActivityTTCheckbox; private RadioButton startEatingSoonTTCheckbox; private RadioButton startHypoTTCheckbox; private boolean togglingTT; + private NumberPicker editTime; + private NumberPicker editDuration; + private NumberPicker editCarbs; private Integer maxCarbs; + private EditText notesEdit; + //one shot guards private boolean accepted; private boolean okClicked; @@ -94,6 +81,7 @@ public class NewCarbsDialog extends DialogFragment implements OnClickListener, D final private TextWatcher textWatcher = new TextWatcher() { @Override public void afterTextChanged(Editable s) { + validateInputs(); } @Override @@ -102,12 +90,21 @@ public class NewCarbsDialog extends DialogFragment implements OnClickListener, D @Override public void onTextChanged(CharSequence s, int start, int before, int count) { - validateInputs(); } }; private void validateInputs() { - Integer carbs = SafeParse.stringToInt(editCarbs.getText()); + int time = editTime.getValue().intValue(); + if (time > 12 * 60 || time < -12 * 60) { + editTime.setValue(0d); + ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(), MainApp.gs(R.string.constraintapllied)); + } + Double duration = editDuration.getValue(); + if (duration > 10) { + editDuration.setValue(0d); + ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(), MainApp.gs(R.string.constraintapllied)); + } + int carbs = editCarbs.getValue().intValue(); if (carbs > maxCarbs) { editCarbs.setValue(0d); ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(), MainApp.gs(R.string.carbsconstraintapplied)); @@ -125,12 +122,6 @@ public class NewCarbsDialog extends DialogFragment implements OnClickListener, D getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE); getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN); - maxCarbs = MainApp.getConstraintChecker().getMaxCarbsAllowed().value(); - - editCarbs = view.findViewById(R.id.newcarb_carbsamount); - - editCarbs.setParams(0d, 0d, (double) maxCarbs, 1d, new DecimalFormat("0"), false, textWatcher); - startActivityTTCheckbox = view.findViewById(R.id.newcarbs_activity_tt); startActivityTTCheckbox.setOnCheckedChangeListener(this); startEatingSoonTTCheckbox = view.findViewById(R.id.newcarbs_eating_soon_tt); @@ -138,29 +129,35 @@ public class NewCarbsDialog extends DialogFragment implements OnClickListener, D startHypoTTCheckbox = view.findViewById(R.id.newcarbs_hypo_tt); startHypoTTCheckbox.setOnCheckedChangeListener(this); - dateButton = view.findViewById(R.id.newcarbs_eventdate); - timeButton = view.findViewById(R.id.newcarb_eventtime); + editTime = view.findViewById(R.id.newcarbs_time); + editTime.setParams(0d, -12 * 60d, 12 * 60d, 5d, new DecimalFormat("0"), false, textWatcher); - initialEventTime = new Date(); - eventTime = new Date(initialEventTime.getTime()); - dateButton.setText(DateUtil.dateString(eventTime)); - timeButton.setText(DateUtil.timeString(eventTime)); - dateButton.setOnClickListener(this); - timeButton.setOnClickListener(this); + LinearLayout durationLayout = view.findViewById(R.id.newcarbs_duration_layout); + durationLayout.setVisibility(MainApp.engineeringMode ? View.VISIBLE : View.GONE); - fav1Button = view.findViewById(R.id.newcarbs_plus1); + editDuration = view.findViewById(R.id.new_carbs_duration); + editDuration.setParams(0d, 0d, 10d, 1d, new DecimalFormat("0"), false, textWatcher); + + maxCarbs = MainApp.getConstraintChecker().getMaxCarbsAllowed().value(); + + editCarbs = view.findViewById(R.id.newcarb_carbsamount); + editCarbs.setParams(0d, 0d, (double) maxCarbs, 1d, new DecimalFormat("0"), false, textWatcher); + + Button fav1Button = view.findViewById(R.id.newcarbs_plus1); fav1Button.setOnClickListener(this); fav1Button.setText(toSignedString(SP.getInt(R.string.key_carbs_button_increment_1, FAV1_DEFAULT))); - fav2Button = view.findViewById(R.id.newcarbs_plus2); + Button fav2Button = view.findViewById(R.id.newcarbs_plus2); fav2Button.setOnClickListener(this); fav2Button.setText(toSignedString(SP.getInt(R.string.key_carbs_button_increment_2, FAV2_DEFAULT))); - fav3Button = view.findViewById(R.id.newcarbs_plus3); + Button fav3Button = view.findViewById(R.id.newcarbs_plus3); fav3Button.setOnClickListener(this); fav3Button.setText(toSignedString(SP.getInt(R.string.key_carbs_button_increment_3, FAV3_DEFAULT))); - notesEdit = (EditText) view.findViewById(R.id.newcarbs_notes); + LinearLayout notesLayout = view.findViewById(R.id.newcarbs_notes_layout); + notesLayout.setVisibility(SP.getBoolean(R.string.key_show_notes_entry_dialogs, false) ? View.VISIBLE : View.GONE); + notesEdit = view.findViewById(R.id.newcarbs_notes); setCancelable(true); getDialog().setCanceledOnTouchOutside(false); @@ -173,8 +170,6 @@ public class NewCarbsDialog extends DialogFragment implements OnClickListener, D @Override public synchronized void onClick(View view) { - Calendar calendar = Calendar.getInstance(); - calendar.setTime(eventTime); switch (view.getId()) { case R.id.ok: submit(); @@ -182,28 +177,6 @@ public class NewCarbsDialog extends DialogFragment implements OnClickListener, D case R.id.cancel: dismiss(); break; - case R.id.newcarbs_eventdate: - DatePickerDialog dpd = DatePickerDialog.newInstance( - this, - calendar.get(Calendar.YEAR), - calendar.get(Calendar.MONTH), - calendar.get(Calendar.DAY_OF_MONTH) - ); - dpd.setThemeDark(true); - dpd.dismissOnPause(true); - dpd.show(getActivity().getFragmentManager(), "Datepickerdialog"); - break; - case R.id.newcarb_eventtime: - TimePickerDialog tpd = TimePickerDialog.newInstance( - this, - calendar.get(Calendar.HOUR_OF_DAY), - calendar.get(Calendar.MINUTE), - DateFormat.is24HourFormat(getActivity()) - ); - tpd.setThemeDark(true); - tpd.dismissOnPause(true); - tpd.show(getActivity().getFragmentManager(), "Timepickerdialog"); - break; case R.id.newcarbs_plus1: editCarbs.setValue(Math.max(0, editCarbs.getValue() + SP.getInt(R.string.key_carbs_button_increment_1, FAV1_DEFAULT))); @@ -309,18 +282,13 @@ public class NewCarbsDialog extends DialogFragment implements OnClickListener, D } okClicked = true; try { - final Integer carbs = SafeParse.stringToInt(editCarbs.getText()); - Integer carbsAfterConstraints = MainApp.getConstraintChecker().applyCarbsConstraints(new Constraint<>(carbs)).value(); - - List actions = new LinkedList<>(); - if (carbs > 0) - actions.add(MainApp.gs(R.string.carbs) + ": " + "" + carbsAfterConstraints + "g" + ""); - if (!carbsAfterConstraints.equals(carbs)) - actions.add("" + MainApp.gs(R.string.carbsconstraintapplied) + ""); - final Profile currentProfile = MainApp.getConfigBuilder().getProfile(); - if (currentProfile == null) + if (currentProfile == null) { return; + } + + int carbs = editCarbs.getValue().intValue(); + Integer carbsAfterConstraints = MainApp.getConstraintChecker().applyCarbsConstraints(new Constraint<>(carbs)).value(); int activityTTDuration = SP.getInt(R.string.key_activity_duration, Constants.defaultActivityTTDuration); activityTTDuration = activityTTDuration > 0 ? activityTTDuration : Constants.defaultActivityTTDuration; @@ -337,6 +305,8 @@ public class NewCarbsDialog extends DialogFragment implements OnClickListener, D double hypoTT = SP.getDouble(R.string.key_hypo_target, currentProfile.getUnits().equals(Constants.MMOL) ? Constants.defaultHypoTTmmol : Constants.defaultHypoTTmgdl); hypoTT = hypoTT > 0 ? hypoTT : currentProfile.getUnits().equals(Constants.MMOL) ? Constants.defaultHypoTTmmol : Constants.defaultHypoTTmgdl; + List actions = new LinkedList<>(); + if (startActivityTTCheckbox.isChecked()) { if (currentProfile.getUnits().equals(Constants.MMOL)) { actions.add(MainApp.gs(R.string.temptargetshort) + ": " + "" + DecimalFormatter.to1Decimal(activityTT) + " mmol/l (" + activityTTDuration + " min)"); @@ -357,34 +327,45 @@ public class NewCarbsDialog extends DialogFragment implements OnClickListener, D actions.add(MainApp.gs(R.string.temptargetshort) + ": " + "" + DecimalFormatter.to0Decimal(hypoTT) + " mg/dl (" + hypoTTDuration + " min)"); } + int timeOffset = editTime.getValue().intValue(); + final long time = now() + timeOffset * 1000 * 60; + if (timeOffset != 0) { + actions.add(MainApp.gs(R.string.time) + ": " + DateUtil.dateAndTimeString(time)); + } + int duration = editDuration.getValue().intValue(); + if (duration > 0) { + actions.add(MainApp.gs(R.string.duration) + ": " + duration + MainApp.gs(R.string.shorthour)); + } + if (carbs > 0) { + actions.add(MainApp.gs(R.string.carbs) + ": " + "" + carbsAfterConstraints + "g" + ""); + } + if (!carbsAfterConstraints.equals(carbs)) { + actions.add("" + MainApp.gs(R.string.carbsconstraintapplied) + ""); + } + final String notes = notesEdit.getText().toString(); + if (!notes.isEmpty()) { + actions.add(MainApp.gs(R.string.careportal_newnstreatment_notes_label) + ": " + notes); + } + final double finalActivityTT = activityTT; final int finalActivityTTDuration = activityTTDuration; final double finalEatigSoonTT = eatingSoonTT; final int finalEatingSoonTTDuration = eatingSoonTTDuration; final double finalHypoTT = hypoTT; final int finalHypoTTDuration = hypoTTDuration; - final String finalNotes = notesEdit.getText().toString(); - - if (!initialEventTime.equals(eventTime)) { - actions.add("Time: " + DateUtil.dateAndTimeString(eventTime)); - } - - final int finalCarbsAfterConstraints = carbsAfterConstraints; - - final Context context = getContext(); - final AlertDialog.Builder builder = new AlertDialog.Builder(context); + final AlertDialog.Builder builder = new AlertDialog.Builder(getContext()); builder.setTitle(MainApp.gs(R.string.confirmation)); - builder.setMessage(actions.isEmpty() - ? MainApp.gs(R.string.no_action_selected) - : Html.fromHtml(Joiner.on("
").join(actions))); - builder.setPositiveButton(MainApp.gs(R.string.ok), actions.isEmpty() ? null : (dialog, id) -> { - synchronized (builder) { - if (accepted) { - log.debug("guarding: already accepted"); - return; - } - accepted = true; + if (carbsAfterConstraints > 0 || startActivityTTCheckbox.isChecked() + || startEatingSoonTTCheckbox.isChecked() || startHypoTTCheckbox.isChecked()) { + builder.setMessage(Html.fromHtml(Joiner.on("
").join(actions))); + builder.setPositiveButton(MainApp.gs(R.string.ok), (dialog, id) -> { + synchronized (builder) { + if (accepted) { + log.debug("guarding: already accepted"); + return; + } + accepted = true; if (startActivityTTCheckbox.isChecked()) { TempTarget tempTarget = new TempTarget() @@ -415,34 +396,25 @@ public class NewCarbsDialog extends DialogFragment implements OnClickListener, D TreatmentsPlugin.getPlugin().addToHistoryTempTarget(tempTarget); } - if (finalCarbsAfterConstraints > 0) { - DetailedBolusInfo detailedBolusInfo = new DetailedBolusInfo(); - detailedBolusInfo.date = eventTime.getTime(); - detailedBolusInfo.eventType = CareportalEvent.CARBCORRECTION; - detailedBolusInfo.carbs = finalCarbsAfterConstraints; - detailedBolusInfo.context = context; - detailedBolusInfo.source = Source.USER; - detailedBolusInfo.notes = finalNotes; - if (ConfigBuilderPlugin.getActivePump().getPumpDescription().storesCarbInfo) { - ConfigBuilderPlugin.getCommandQueue().bolus(detailedBolusInfo, new Callback() { - @Override - public void run() { - if (!result.success) { - Intent i = new Intent(MainApp.instance(), ErrorHelperActivity.class); - i.putExtra("soundid", R.raw.boluserror); - i.putExtra("status", result.comment); - i.putExtra("title", MainApp.gs(R.string.treatmentdeliveryerror)); - i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - MainApp.instance().startActivity(i); - } + if (carbsAfterConstraints > 0) { + if (duration == 0) { + createCarb(carbsAfterConstraints, time, notes); + } else { + long remainingCarbs = carbsAfterConstraints; + int ticks = (duration * 4); //duration guaranteed to be integer greater zero + for (int i = 0; i < ticks; i++){ + long carbTime = time + i * 15 * 60 * 1000; + long smallCarbAmount = Math.round((1d * remainingCarbs) / (ticks-i)); //on last iteration (ticks-i) is 1 -> smallCarbAmount == remainingCarbs + remainingCarbs -= smallCarbAmount; + createCarb(smallCarbAmount, carbTime, notes); } - }); - } else { - TreatmentsPlugin.getPlugin().addToHistoryTreatment(detailedBolusInfo); + } } } - } - }); + }); + } else { + builder.setMessage(MainApp.gs(R.string.no_action_selected)); + } builder.setNegativeButton(MainApp.gs(R.string.cancel), null); builder.show(); dismiss(); @@ -451,19 +423,30 @@ public class NewCarbsDialog extends DialogFragment implements OnClickListener, D } } - @Override - public void onDateSet(DatePickerDialog view, int year, int monthOfYear, int dayOfMonth) { - eventTime.setYear(year - 1900); - eventTime.setMonth(monthOfYear); - eventTime.setDate(dayOfMonth); - dateButton.setText(DateUtil.dateString(eventTime)); - } - - @Override - public void onTimeSet(RadialPickerLayout view, int hourOfDay, int minute, int second) { - eventTime.setHours(hourOfDay); - eventTime.setMinutes(minute); - eventTime.setSeconds(second); - timeButton.setText(DateUtil.timeString(eventTime)); + private void createCarb(long carbs, long time, @Nullable String notes) { + DetailedBolusInfo carbInfo = new DetailedBolusInfo(); + carbInfo.date = time; + carbInfo.eventType = CareportalEvent.CARBCORRECTION; + carbInfo.carbs = carbs; + carbInfo.context = getContext(); + carbInfo.source = Source.USER; + carbInfo.notes = notes; + if (ConfigBuilderPlugin.getActivePump().getPumpDescription().storesCarbInfo) { + ConfigBuilderPlugin.getCommandQueue().bolus(carbInfo, new Callback() { + @Override + public void run() { + if (!result.success) { + Intent i = new Intent(MainApp.instance(), ErrorHelperActivity.class); + i.putExtra("soundid", R.raw.boluserror); + i.putExtra("status", result.comment); + i.putExtra("title", MainApp.gs(R.string.treatmentdeliveryerror)); + i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + MainApp.instance().startActivity(i); + } + } + }); + } else { + TreatmentsPlugin.getPlugin().addToHistoryTreatment(carbInfo); + } } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewInsulinDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewInsulinDialog.java index 58971e3d80..3539bee093 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewInsulinDialog.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewInsulinDialog.java @@ -9,7 +9,6 @@ import android.support.v7.app.AlertDialog; import android.text.Editable; import android.text.Html; import android.text.TextWatcher; -import android.text.format.DateFormat; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; @@ -18,19 +17,16 @@ import android.view.Window; import android.view.WindowManager; import android.widget.Button; import android.widget.CheckBox; -import android.widget.TextView; +import android.widget.EditText; +import android.widget.LinearLayout; import com.crashlytics.android.answers.CustomEvent; import com.google.common.base.Joiner; -import com.wdullaer.materialdatetimepicker.date.DatePickerDialog; -import com.wdullaer.materialdatetimepicker.time.RadialPickerLayout; -import com.wdullaer.materialdatetimepicker.time.TimePickerDialog; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.Calendar; -import java.util.Date; +import java.text.DecimalFormat; import java.util.LinkedList; import java.util.List; @@ -54,30 +50,25 @@ import info.nightscout.utils.SP; import info.nightscout.utils.SafeParse; import info.nightscout.utils.ToastUtils; -public class NewInsulinDialog extends DialogFragment implements OnClickListener, DatePickerDialog.OnDateSetListener, TimePickerDialog.OnTimeSetListener { +import static info.nightscout.utils.DateUtil.now; + +public class NewInsulinDialog extends DialogFragment implements OnClickListener { private static Logger log = LoggerFactory.getLogger(NewInsulinDialog.class); - private NumberPicker editInsulin; - - private TextView dateButton; - private TextView timeButton; - - private Date initialEventTime; - private Date eventTime; - - private Button plus1Button; - private Button plus2Button; - private Button plus3Button; - public static final double PLUS1_DEFAULT = 0.5d; public static final double PLUS2_DEFAULT = 1d; public static final double PLUS3_DEFAULT = 2d; - private CheckBox startESMCheckbox; + private CheckBox startEatingSoonTTCheckbox; private CheckBox recordOnlyCheckbox; + private LinearLayout editLayout; + private NumberPicker editTime; + private NumberPicker editInsulin; private Double maxInsulin; + private EditText notesEdit; + //one shot guards private boolean accepted; private boolean okClicked; @@ -90,6 +81,7 @@ public class NewInsulinDialog extends DialogFragment implements OnClickListener, final private TextWatcher textWatcher = new TextWatcher() { @Override public void afterTextChanged(Editable s) { + validateInputs(); } @Override @@ -98,12 +90,16 @@ public class NewInsulinDialog extends DialogFragment implements OnClickListener, @Override public void onTextChanged(CharSequence s, int start, int before, int count) { - validateInputs(); } }; private void validateInputs() { - Double insulin = SafeParse.stringToDouble(editInsulin.getText()); + int time = editTime.getValue().intValue(); + if (Math.abs(time) > 12 * 60) { + editTime.setValue(0d); + ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(), MainApp.gs(R.string.constraintapllied)); + } + Double insulin = editInsulin.getValue(); if (insulin > maxInsulin) { editInsulin.setValue(0d); ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(), MainApp.gs(R.string.bolusconstraintapplied)); @@ -121,39 +117,34 @@ public class NewInsulinDialog extends DialogFragment implements OnClickListener, getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE); getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN); + startEatingSoonTTCheckbox = view.findViewById(R.id.newinsulin_start_eating_soon_tt); + + recordOnlyCheckbox = view.findViewById(R.id.newinsulin_record_only); + recordOnlyCheckbox.setOnCheckedChangeListener((buttonView, isChecked) -> editLayout.setVisibility(isChecked ? View.VISIBLE : View.GONE)); + + editLayout = view.findViewById(R.id.newinsulin_time_layout); + editLayout.setVisibility(View.GONE); + editTime = view.findViewById(R.id.newinsulin_time); + editTime.setParams(0d, -12 * 60d, 12 * 60d, 5d, new DecimalFormat("0"), false, textWatcher); + maxInsulin = MainApp.getConstraintChecker().getMaxBolusAllowed().value(); - editInsulin = (NumberPicker) view.findViewById(R.id.treatments_newinsulin_amount); - + editInsulin = view.findViewById(R.id.newinsulin_amount); editInsulin.setParams(0d, 0d, maxInsulin, ConfigBuilderPlugin.getActivePump().getPumpDescription().bolusStep, DecimalFormatter.pumpSupportedBolusFormat(), false, textWatcher); - dateButton = (TextView) view.findViewById(R.id.newinsulin_eventdate); - timeButton = (TextView) view.findViewById(R.id.newinsulin_eventtime); - - initialEventTime = new Date(); - eventTime = new Date(initialEventTime.getTime()); - dateButton.setText(DateUtil.dateString(eventTime)); - timeButton.setText(DateUtil.timeString(eventTime)); - dateButton.setOnClickListener(this); - timeButton.setOnClickListener(this); - - plus1Button = (Button) view.findViewById(R.id.newinsulin_plus05); + Button plus1Button = view.findViewById(R.id.newinsulin_plus05); plus1Button.setOnClickListener(this); plus1Button.setText(toSignedString(SP.getDouble(MainApp.gs(R.string.key_insulin_button_increment_1), PLUS1_DEFAULT))); - plus2Button = (Button) view.findViewById(R.id.newinsulin_plus10); + Button plus2Button = view.findViewById(R.id.newinsulin_plus10); plus2Button.setOnClickListener(this); plus2Button.setText(toSignedString(SP.getDouble(MainApp.gs(R.string.key_insulin_button_increment_2), PLUS2_DEFAULT))); - plus3Button = (Button) view.findViewById(R.id.newinsulin_plus20); + Button plus3Button = view.findViewById(R.id.newinsulin_plus20); plus3Button.setOnClickListener(this); plus3Button.setText(toSignedString(SP.getDouble(MainApp.gs(R.string.key_insulin_button_increment_3), PLUS3_DEFAULT))); - startESMCheckbox = (CheckBox) view.findViewById(R.id.newinsulin_start_eating_soon_tt); - - recordOnlyCheckbox = (CheckBox) view.findViewById(R.id.newinsulin_record_only); - recordOnlyCheckbox.setOnCheckedChangeListener((buttonView, isChecked) -> { - if (dateButton != null) dateButton.setEnabled(isChecked); - if (timeButton != null) timeButton.setEnabled(isChecked); - }); + LinearLayout notesLayout = view.findViewById(R.id.newinsulin_notes_layout); + notesLayout.setVisibility(SP.getBoolean(R.string.key_show_notes_entry_dialogs, false) ? View.VISIBLE : View.GONE); + notesEdit = view.findViewById(R.id.newinsulin_notes); setCancelable(true); getDialog().setCanceledOnTouchOutside(false); @@ -167,8 +158,6 @@ public class NewInsulinDialog extends DialogFragment implements OnClickListener, @Override public synchronized void onClick(View view) { - Calendar calendar = Calendar.getInstance(); - calendar.setTime(eventTime); switch (view.getId()) { case R.id.ok: submit(); @@ -176,28 +165,6 @@ public class NewInsulinDialog extends DialogFragment implements OnClickListener, case R.id.cancel: dismiss(); break; - case R.id.newinsulin_eventdate: - DatePickerDialog dpd = DatePickerDialog.newInstance( - this, - calendar.get(Calendar.YEAR), - calendar.get(Calendar.MONTH), - calendar.get(Calendar.DAY_OF_MONTH) - ); - dpd.setThemeDark(true); - dpd.dismissOnPause(true); - dpd.show(getActivity().getFragmentManager(), "Datepickerdialog"); - break; - case R.id.newinsulin_eventtime: - TimePickerDialog tpd = TimePickerDialog.newInstance( - this, - calendar.get(Calendar.HOUR_OF_DAY), - calendar.get(Calendar.MINUTE), - DateFormat.is24HourFormat(getActivity()) - ); - tpd.setThemeDark(true); - tpd.dismissOnPause(true); - tpd.show(getActivity().getFragmentManager(), "Timepickerdialog"); - break; case R.id.newinsulin_plus05: editInsulin.setValue(Math.max(0, editInsulin.getValue() + SP.getDouble(MainApp.gs(R.string.key_insulin_button_increment_1), PLUS1_DEFAULT))); @@ -225,6 +192,10 @@ public class NewInsulinDialog extends DialogFragment implements OnClickListener, okClicked = true; try { + Profile currentProfile = MainApp.getConfigBuilder().getProfile(); + if (currentProfile == null) + return; + Double insulin = SafeParse.stringToDouble(editInsulin.getText()); Double insulinAfterConstraints = MainApp.getConstraintChecker().applyBolusConstraints(new Constraint<>(insulin)).value(); @@ -239,92 +210,90 @@ public class NewInsulinDialog extends DialogFragment implements OnClickListener, if (!insulinAfterConstraints.equals(insulin)) actions.add("" + MainApp.gs(R.string.bolusconstraintapplied) + ""); - double prefTTDuration = SP.getDouble(R.string.key_eatingsoon_duration, 45d); - double ttDuration = prefTTDuration > 0 ? prefTTDuration : 45d; - double prefTT = SP.getDouble(R.string.key_eatingsoon_target, 80d); - Profile currentProfile = MainApp.getConfigBuilder().getProfile(); - if (currentProfile == null) - return; - double tt; - if (currentProfile.getUnits().equals(Constants.MMOL)) - tt = prefTT > 0 ? Profile.toMgdl(prefTT, Constants.MMOL) : 80d; - else - tt = prefTT > 0 ? prefTT : 80d; - final double finalTT = tt; + int eatingSoonTTDuration = SP.getInt(R.string.key_eatingsoon_duration, Constants.defaultEatingSoonTTDuration); + eatingSoonTTDuration = eatingSoonTTDuration > 0 ? eatingSoonTTDuration : Constants.defaultEatingSoonTTDuration; + double eatingSoonTT = SP.getDouble(R.string.key_eatingsoon_target, currentProfile.getUnits().equals(Constants.MMOL) ? Constants.defaultEatingSoonTTmmol : Constants.defaultEatingSoonTTmgdl); + eatingSoonTT = eatingSoonTT > 0 ? eatingSoonTT : currentProfile.getUnits().equals(Constants.MMOL) ? Constants.defaultEatingSoonTTmmol : Constants.defaultEatingSoonTTmgdl; - if (startESMCheckbox.isChecked()) { - if (currentProfile.getUnits().equals("mmol")) { - actions.add("TT: " + "" + Profile.toMmol(tt, Constants.MGDL) + " mmol for " + ((int) ttDuration) + " min "); + if (startEatingSoonTTCheckbox.isChecked()) { + if (currentProfile.getUnits().equals(Constants.MMOL)) { + actions.add(MainApp.gs(R.string.temptargetshort) + ": " + "" + DecimalFormatter.to1Decimal(eatingSoonTT) + " mmol/l (" + eatingSoonTTDuration + " min)"); } else - actions.add("TT: " + "" + ((int) tt) + "mg/dl for " + ((int) ttDuration) + " min "); + actions.add(MainApp.gs(R.string.temptargetshort) + ": " + "" + DecimalFormatter.to0Decimal(eatingSoonTT) + " mg/dl (" + eatingSoonTTDuration + " min)"); } - if (!initialEventTime.equals(eventTime)) { - actions.add("Time: " + DateUtil.dateAndTimeString(eventTime)); + int timeOffset = editTime.getValue().intValue(); + final long time = now() + timeOffset * 1000 * 60; + if (timeOffset != 0) { + actions.add(MainApp.gs(R.string.time) + ": " + DateUtil.dateAndTimeString(time)); + } + final String notes = notesEdit.getText().toString(); + if (!notes.isEmpty()) { + actions.add(MainApp.gs(R.string.careportal_newnstreatment_notes_label) + ": " + notes); } final double finalInsulinAfterConstraints = insulinAfterConstraints; + final double finalEatigSoonTT = eatingSoonTT; + final int finalEatingSoonTTDuration = eatingSoonTTDuration; final Context context = getContext(); final AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setTitle(MainApp.gs(R.string.confirmation)); - builder.setMessage(actions.isEmpty() - ? MainApp.gs(R.string.no_action_selected) - : Html.fromHtml(Joiner.on("
").join(actions))); - builder.setPositiveButton(MainApp.gs(R.string.ok), actions.isEmpty() ? null : (dialog, id) -> { - synchronized (builder) { - if (accepted) { - log.debug("guarding: already accepted"); - return; - } - accepted = true; + if (finalInsulinAfterConstraints > 0 || startEatingSoonTTCheckbox.isChecked()) { + builder.setMessage(Html.fromHtml(Joiner.on("
").join(actions))); + builder.setPositiveButton(MainApp.gs(R.string.ok), (dialog, id) -> { + synchronized (builder) { + if (accepted) { + log.debug("guarding: already accepted"); + return; + } + accepted = true; - if (startESMCheckbox.isChecked()) { - TempTarget tempTarget = new TempTarget() - .date(System.currentTimeMillis()) - .duration((int) ttDuration) - .reason("Eating soon") - .source(Source.USER) - .low((int) finalTT) - .high((int) finalTT); - TreatmentsPlugin.getPlugin().addToHistoryTempTarget(tempTarget); - } + if (startEatingSoonTTCheckbox.isChecked()) { + TempTarget tempTarget = new TempTarget() + .date(System.currentTimeMillis()) + .duration(finalEatingSoonTTDuration) + .reason(MainApp.gs(R.string.eatingsoon)) + .source(Source.USER) + .low(Profile.toMgdl(finalEatigSoonTT, currentProfile.getUnits())) + .high(Profile.toMgdl(finalEatigSoonTT, currentProfile.getUnits())); + TreatmentsPlugin.getPlugin().addToHistoryTempTarget(tempTarget); + } - if (finalInsulinAfterConstraints <= 0.01) { - return; - } - - if (recordOnlyCheckbox.isChecked()) { - DetailedBolusInfo detailedBolusInfo = new DetailedBolusInfo(); - detailedBolusInfo.source = Source.USER; - detailedBolusInfo.date = eventTime.getTime(); - detailedBolusInfo.eventType = CareportalEvent.CORRECTIONBOLUS; - detailedBolusInfo.insulin = finalInsulinAfterConstraints; - TreatmentsPlugin.getPlugin().addToHistoryTreatment(detailedBolusInfo); - } else { - DetailedBolusInfo detailedBolusInfo = new DetailedBolusInfo(); - detailedBolusInfo.eventType = CareportalEvent.CORRECTIONBOLUS; - detailedBolusInfo.insulin = finalInsulinAfterConstraints; - detailedBolusInfo.context = context; - detailedBolusInfo.source = Source.USER; - ConfigBuilderPlugin.getCommandQueue().bolus(detailedBolusInfo, new Callback() { - @Override - public void run() { - if (!result.success) { - Intent i = new Intent(MainApp.instance(), ErrorHelperActivity.class); - i.putExtra("soundid", R.raw.boluserror); - i.putExtra("status", result.comment); - i.putExtra("title", MainApp.gs(R.string.treatmentdeliveryerror)); - i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - MainApp.instance().startActivity(i); - } + if (finalInsulinAfterConstraints > 0) { + DetailedBolusInfo detailedBolusInfo = new DetailedBolusInfo(); + detailedBolusInfo.eventType = CareportalEvent.CORRECTIONBOLUS; + detailedBolusInfo.insulin = finalInsulinAfterConstraints; + detailedBolusInfo.context = context; + detailedBolusInfo.source = Source.USER; + detailedBolusInfo.notes = notes; + if (recordOnlyCheckbox.isChecked()) { + detailedBolusInfo.date = time; + TreatmentsPlugin.getPlugin().addToHistoryTreatment(detailedBolusInfo); + } else { + detailedBolusInfo.date = now(); + ConfigBuilderPlugin.getCommandQueue().bolus(detailedBolusInfo, new Callback() { + @Override + public void run() { + if (!result.success) { + Intent i = new Intent(MainApp.instance(), ErrorHelperActivity.class); + i.putExtra("soundid", R.raw.boluserror); + i.putExtra("status", result.comment); + i.putExtra("title", MainApp.gs(R.string.treatmentdeliveryerror)); + i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + MainApp.instance().startActivity(i); + } + } + }); + FabricPrivacy.getInstance().logCustom(new CustomEvent("Bolus")); } - }); - FabricPrivacy.getInstance().logCustom(new CustomEvent("Bolus")); + } } - } - }); + }); + } else { + builder.setMessage(MainApp.gs(R.string.no_action_selected)); + } builder.setNegativeButton(MainApp.gs(R.string.cancel), null); builder.show(); dismiss(); @@ -332,20 +301,4 @@ public class NewInsulinDialog extends DialogFragment implements OnClickListener, log.error("Unhandled exception", e); } } - - @Override - public void onDateSet(DatePickerDialog view, int year, int monthOfYear, int dayOfMonth) { - eventTime.setYear(year - 1900); - eventTime.setMonth(monthOfYear); - eventTime.setDate(dayOfMonth); - dateButton.setText(DateUtil.dateString(eventTime)); - } - - @Override - public void onTimeSet(RadialPickerLayout view, int hourOfDay, int minute, int second) { - eventTime.setHours(hourOfDay); - eventTime.setMinutes(minute); - eventTime.setSeconds(second); - timeButton.setText(DateUtil.timeString(eventTime)); - } } 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 263bc7e090..edee0c838d 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 @@ -105,6 +105,7 @@ public class WizardDialog extends DialogFragment implements OnClickListener, Com NumberPicker editCorr; NumberPicker editCarbTime; + LinearLayout notesLayout; EditText notesEdit; Integer calculatedCarbs = 0; @@ -209,7 +210,9 @@ public class WizardDialog extends DialogFragment implements OnClickListener, Com superbolus = (TextView) view.findViewById(R.id.treatments_wizard_sb); superbolusInsulin = (TextView) view.findViewById(R.id.treatments_wizard_sbinsulin); - notesEdit = (EditText) view.findViewById(R.id.newcarbs_notes); + notesLayout = view.findViewById(R.id.treatments_wizard_notes_layout); + notesLayout.setVisibility(SP.getBoolean(R.string.key_show_notes_entry_dialogs, false) ? View.VISIBLE : View.GONE); + notesEdit = (EditText) view.findViewById(R.id.treatment_wizard_notes); bgTrend = (TextView) view.findViewById(R.id.treatments_wizard_bgtrend); bgTrendInsulin = (TextView) view.findViewById(R.id.treatments_wizard_bgtrendinsulin); 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 2f5135c0cb..8f82f71c35 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 @@ -452,7 +452,7 @@ public class TreatmentsPlugin extends PluginBase implements TreatmentsInterface //log.debug("Adding new Treatment record" + carbsTreatment); } if (newRecordCreated && detailedBolusInfo.isValid) - NSUpload.uploadBolusWizardRecord(detailedBolusInfo); + NSUpload.uploadTreatmentRecord(detailedBolusInfo); return newRecordCreated; } 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 87626ed4cd..ae977d4024 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 @@ -89,6 +89,10 @@ public class TreatmentsBolusFragment extends SubscriberFragment implements View. holder.iob.setTextColor(ContextCompat.getColor(MainApp.instance(), R.color.colorActive)); else holder.iob.setTextColor(holder.carbs.getCurrentTextColor()); + if (t.date > DateUtil.now()) + holder.date.setTextColor(ContextCompat.getColor(MainApp.instance(), R.color.colorScheduled)); + else + holder.date.setTextColor(holder.carbs.getCurrentTextColor()); holder.remove.setTag(t); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/fragments/TreatmentsTempTargetFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/fragments/TreatmentsTempTargetFragment.java index d018efd97b..5ce79636b5 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/fragments/TreatmentsTempTargetFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/fragments/TreatmentsTempTargetFragment.java @@ -84,14 +84,10 @@ public class TreatmentsTempTargetFragment extends SubscriberFragment implements holder.reasonLabel.setText(""); holder.reasonColon.setText(""); } - if (tempTarget.isInProgress()) { - if (tempTarget == currentlyActiveTarget) { - // active as newest - holder.date.setTextColor(ContextCompat.getColor(MainApp.instance(), R.color.colorInProgress)); - } else { - // other's that might become active again after the latest (overlapping) is over - holder.date.setTextColor(ContextCompat.getColor(MainApp.instance(), R.color.colorActive)); - } + if (tempTarget.isInProgress() && tempTarget == currentlyActiveTarget) { + holder.date.setTextColor(ContextCompat.getColor(MainApp.instance(), R.color.colorActive)); + } else if (tempTarget.date > DateUtil.now()) { + holder.date.setTextColor(ContextCompat.getColor(MainApp.instance(), R.color.colorScheduled)); } else { holder.date.setTextColor(holder.reasonColon.getCurrentTextColor()); } diff --git a/app/src/main/java/info/nightscout/androidaps/queue/CommandQueue.java b/app/src/main/java/info/nightscout/androidaps/queue/CommandQueue.java index fa15ee46d2..a514a85815 100644 --- a/app/src/main/java/info/nightscout/androidaps/queue/CommandQueue.java +++ b/app/src/main/java/info/nightscout/androidaps/queue/CommandQueue.java @@ -166,14 +166,19 @@ public class CommandQueue { public boolean bolus(DetailedBolusInfo detailedBolusInfo, Callback callback) { Command.CommandType type = detailedBolusInfo.isSMB ? Command.CommandType.SMB_BOLUS : Command.CommandType.BOLUS; - if (isRunning(type)) { - if (callback != null) - callback.result(executingNowError()).run(); - return false; - } + if(type.equals(Command.CommandType.BOLUS) && detailedBolusInfo.carbs > 0 && detailedBolusInfo.insulin == 0){ + type = Command.CommandType.CARBS_ONLY_TREATMENT; + //Carbs only can be added in parallel as they can be "in the future". + } else { + if (isRunning(type)) { + if (callback != null) + callback.result(executingNowError()).run(); + return false; + } - // remove all unfinished boluses - removeAll(type); + // remove all unfinished boluses + removeAll(type); + } // apply constraints detailedBolusInfo.insulin = MainApp.getConstraintChecker().applyBolusConstraints(new Constraint<>(detailedBolusInfo.insulin)).value(); @@ -183,12 +188,14 @@ public class CommandQueue { if (detailedBolusInfo.isSMB) { add(new CommandSMBBolus(detailedBolusInfo, callback)); } else { - add(new CommandBolus(detailedBolusInfo, callback)); - // Bring up bolus progress dialog (start here, so the dialog is shown when the bolus is requested, - // not when the Bolus command is starting. The command closes the dialog upon completion). - showBolusProgressDialog(detailedBolusInfo.insulin, detailedBolusInfo.context); - // Notify Wear about upcoming bolus - MainApp.bus().post(new EventBolusRequested(detailedBolusInfo.insulin)); + add(new CommandBolus(detailedBolusInfo, callback, type)); + if(type.equals(Command.CommandType.BOLUS)) { + // Bring up bolus progress dialog (start here, so the dialog is shown when the bolus is requested, + // not when the Bolus command is starting. The command closes the dialog upon completion). + showBolusProgressDialog(detailedBolusInfo.insulin, detailedBolusInfo.context); + // Notify Wear about upcoming bolus + MainApp.bus().post(new EventBolusRequested(detailedBolusInfo.insulin)); + } } notifyAboutNewCommand(); diff --git a/app/src/main/java/info/nightscout/androidaps/queue/commands/Command.java b/app/src/main/java/info/nightscout/androidaps/queue/commands/Command.java index c5b3ada417..b865fac86e 100644 --- a/app/src/main/java/info/nightscout/androidaps/queue/commands/Command.java +++ b/app/src/main/java/info/nightscout/androidaps/queue/commands/Command.java @@ -12,6 +12,7 @@ public abstract class Command { public enum CommandType { BOLUS, SMB_BOLUS, + CARBS_ONLY_TREATMENT, TEMPBASAL, EXTENDEDBOLUS, BASALPROFILE, diff --git a/app/src/main/java/info/nightscout/androidaps/queue/commands/CommandBolus.java b/app/src/main/java/info/nightscout/androidaps/queue/commands/CommandBolus.java index 278fd7681b..cab7d23311 100644 --- a/app/src/main/java/info/nightscout/androidaps/queue/commands/CommandBolus.java +++ b/app/src/main/java/info/nightscout/androidaps/queue/commands/CommandBolus.java @@ -16,8 +16,8 @@ import info.nightscout.utils.DecimalFormatter; public class CommandBolus extends Command { DetailedBolusInfo detailedBolusInfo; - public CommandBolus(DetailedBolusInfo detailedBolusInfo, Callback callback) { - commandType = CommandType.BOLUS; + public CommandBolus(DetailedBolusInfo detailedBolusInfo, Callback callback, CommandType type) { + commandType = type; this.detailedBolusInfo = detailedBolusInfo; this.callback = callback; } diff --git a/app/src/main/java/info/nightscout/utils/NSUpload.java b/app/src/main/java/info/nightscout/utils/NSUpload.java index c56c4f1a51..e617191732 100644 --- a/app/src/main/java/info/nightscout/utils/NSUpload.java +++ b/app/src/main/java/info/nightscout/utils/NSUpload.java @@ -7,6 +7,7 @@ import android.content.pm.ResolveInfo; import android.os.Build; import android.os.Bundle; import android.preference.PreferenceManager; +import android.support.annotation.Nullable; import org.apache.commons.lang3.StringUtils; import org.json.JSONArray; @@ -258,7 +259,7 @@ public class NSUpload { } } - public static void uploadBolusWizardRecord(DetailedBolusInfo detailedBolusInfo) { + public static void uploadTreatmentRecord(DetailedBolusInfo detailedBolusInfo) { JSONObject data = new JSONObject(); try { data.put("eventType", detailedBolusInfo.eventType); @@ -500,7 +501,7 @@ public class NSUpload { } } - public static void uploadEvent(String careportalEvent, long time) { + public static void uploadEvent(String careportalEvent, long time, @Nullable String notes) { Context context = MainApp.instance().getApplicationContext(); Bundle bundle = new Bundle(); bundle.putString("action", "dbAdd"); @@ -510,6 +511,9 @@ public class NSUpload { data.put("eventType", careportalEvent); data.put("created_at", DateUtil.toISOString(time)); data.put("enteredBy", SP.getString("careportal_enteredby", MainApp.gs(R.string.app_name))); + if (notes != null) { + data.put("notes", notes); + } } catch (JSONException e) { log.error("Unhandled exception", e); } diff --git a/app/src/main/java/info/nightscout/utils/NumberPicker.java b/app/src/main/java/info/nightscout/utils/NumberPicker.java index 01c7dcb336..64239cbaee 100644 --- a/app/src/main/java/info/nightscout/utils/NumberPicker.java +++ b/app/src/main/java/info/nightscout/utils/NumberPicker.java @@ -5,6 +5,7 @@ import android.os.Handler; import android.os.Message; import android.text.Editable; import android.text.TextWatcher; +import android.text.method.DigitsKeyListener; import android.util.AttributeSet; import android.view.KeyEvent; import android.view.LayoutInflater; @@ -148,7 +149,7 @@ public class NumberPicker extends LinearLayout implements View.OnKeyListener, } public void setParams(Double initValue, Double minValue, Double maxValue, Double step, NumberFormat formater, boolean allowZero, TextWatcher textWatcher) { - if(this.textWatcher != null) { + if (this.textWatcher != null) { editText.removeTextChangedListener(this.textWatcher); } setParams(initValue, minValue, maxValue, step, formater, allowZero); @@ -164,6 +165,8 @@ public class NumberPicker extends LinearLayout implements View.OnKeyListener, this.formater = formater; this.allowZero = allowZero; + editText.setKeyListener(DigitsKeyListener.getInstance(minValue < 0, step != Math.rint(step))); + if (textWatcher != null) editText.removeTextChangedListener(textWatcher); updateEditText(); @@ -202,7 +205,7 @@ public class NumberPicker extends LinearLayout implements View.OnKeyListener, updateEditText(); } - private void dec( int multiplier) { + private void dec(int multiplier) { value -= step * multiplier; if (value < minValue) { value = minValue; diff --git a/app/src/main/res/layout/actions_fill_dialog.xml b/app/src/main/res/layout/actions_fill_dialog.xml index 18a5886246..2883485bf7 100644 --- a/app/src/main/res/layout/actions_fill_dialog.xml +++ b/app/src/main/res/layout/actions_fill_dialog.xml @@ -1,97 +1,153 @@ - + android:layout_height="wrap_content"> + + + android:layout_gravity="center" + android:orientation="horizontal"> + + - + + + + + android:text="@string/careportal_pumpsitechange" /> - - - - - - - - - - - - -