NewInsulinDialog: add notes field, use relative time, align style.

This commit is contained in:
Johannes Mockenhaupt 2018-04-13 21:22:29 +02:00
parent 719d3d459b
commit bf7e9e4bf3
No known key found for this signature in database
GPG key ID: 9E1EA6AF7BBBB0D1
3 changed files with 190 additions and 164 deletions

View file

@ -9,7 +9,6 @@ import android.support.v7.app.AlertDialog;
import android.text.Editable; import android.text.Editable;
import android.text.Html; import android.text.Html;
import android.text.TextWatcher; import android.text.TextWatcher;
import android.text.format.DateFormat;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.View.OnClickListener; import android.view.View.OnClickListener;
@ -18,19 +17,16 @@ import android.view.Window;
import android.view.WindowManager; import android.view.WindowManager;
import android.widget.Button; import android.widget.Button;
import android.widget.CheckBox; import android.widget.CheckBox;
import android.widget.TextView; import android.widget.EditText;
import android.widget.LinearLayout;
import com.crashlytics.android.answers.CustomEvent; import com.crashlytics.android.answers.CustomEvent;
import com.google.common.base.Joiner; 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.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.util.Calendar; import java.text.DecimalFormat;
import java.util.Date;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
@ -54,16 +50,16 @@ import info.nightscout.utils.SP;
import info.nightscout.utils.SafeParse; import info.nightscout.utils.SafeParse;
import info.nightscout.utils.ToastUtils; 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 static Logger log = LoggerFactory.getLogger(NewInsulinDialog.class);
private LinearLayout editLayout;
private NumberPicker editTime;
private NumberPicker editInsulin; private NumberPicker editInsulin;
private LinearLayout notesLayout;
private TextView dateButton; private EditText notesEdit;
private TextView timeButton;
private Date initialEventTime;
private Date eventTime;
private Button plus1Button; private Button plus1Button;
private Button plus2Button; private Button plus2Button;
@ -90,6 +86,7 @@ public class NewInsulinDialog extends DialogFragment implements OnClickListener,
final private TextWatcher textWatcher = new TextWatcher() { final private TextWatcher textWatcher = new TextWatcher() {
@Override @Override
public void afterTextChanged(Editable s) { public void afterTextChanged(Editable s) {
validateInputs();
} }
@Override @Override
@ -98,12 +95,16 @@ public class NewInsulinDialog extends DialogFragment implements OnClickListener,
@Override @Override
public void onTextChanged(CharSequence s, int start, int before, int count) { public void onTextChanged(CharSequence s, int start, int before, int count) {
validateInputs();
} }
}; };
private void 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) { if (insulin > maxInsulin) {
editInsulin.setValue(0d); editInsulin.setValue(0d);
ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(), MainApp.gs(R.string.bolusconstraintapplied)); ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(), MainApp.gs(R.string.bolusconstraintapplied));
@ -121,39 +122,34 @@ public class NewInsulinDialog extends DialogFragment implements OnClickListener,
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE); getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN); getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
startESMCheckbox = 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(); 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); editInsulin.setParams(0d, 0d, maxInsulin, ConfigBuilderPlugin.getActivePump().getPumpDescription().bolusStep, DecimalFormatter.pumpSupportedBolusFormat(), false, textWatcher);
dateButton = (TextView) view.findViewById(R.id.newinsulin_eventdate); plus1Button = view.findViewById(R.id.newinsulin_plus05);
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);
plus1Button.setOnClickListener(this); plus1Button.setOnClickListener(this);
plus1Button.setText(toSignedString(SP.getDouble(MainApp.gs(R.string.key_insulin_button_increment_1), PLUS1_DEFAULT))); plus1Button.setText(toSignedString(SP.getDouble(MainApp.gs(R.string.key_insulin_button_increment_1), PLUS1_DEFAULT)));
plus2Button = (Button) view.findViewById(R.id.newinsulin_plus10); plus2Button = view.findViewById(R.id.newinsulin_plus10);
plus2Button.setOnClickListener(this); plus2Button.setOnClickListener(this);
plus2Button.setText(toSignedString(SP.getDouble(MainApp.gs(R.string.key_insulin_button_increment_2), PLUS2_DEFAULT))); plus2Button.setText(toSignedString(SP.getDouble(MainApp.gs(R.string.key_insulin_button_increment_2), PLUS2_DEFAULT)));
plus3Button = (Button) view.findViewById(R.id.newinsulin_plus20); plus3Button = view.findViewById(R.id.newinsulin_plus20);
plus3Button.setOnClickListener(this); plus3Button.setOnClickListener(this);
plus3Button.setText(toSignedString(SP.getDouble(MainApp.gs(R.string.key_insulin_button_increment_3), PLUS3_DEFAULT))); 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); notesLayout = view.findViewById(R.id.newcarbs_notes_layout);
notesLayout.setVisibility(SP.getBoolean(R.string.key_show_notes_entry_dialogs, false) ? View.VISIBLE : View.GONE);
recordOnlyCheckbox = (CheckBox) view.findViewById(R.id.newinsulin_record_only); notesEdit = view.findViewById(R.id.newcarbs_notes);
recordOnlyCheckbox.setOnCheckedChangeListener((buttonView, isChecked) -> {
if (dateButton != null) dateButton.setEnabled(isChecked);
if (timeButton != null) timeButton.setEnabled(isChecked);
});
setCancelable(true); setCancelable(true);
getDialog().setCanceledOnTouchOutside(false); getDialog().setCanceledOnTouchOutside(false);
@ -167,8 +163,6 @@ public class NewInsulinDialog extends DialogFragment implements OnClickListener,
@Override @Override
public synchronized void onClick(View view) { public synchronized void onClick(View view) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(eventTime);
switch (view.getId()) { switch (view.getId()) {
case R.id.ok: case R.id.ok:
submit(); submit();
@ -176,28 +170,6 @@ public class NewInsulinDialog extends DialogFragment implements OnClickListener,
case R.id.cancel: case R.id.cancel:
dismiss(); dismiss();
break; 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: case R.id.newinsulin_plus05:
editInsulin.setValue(Math.max(0, editInsulin.getValue() editInsulin.setValue(Math.max(0, editInsulin.getValue()
+ SP.getDouble(MainApp.gs(R.string.key_insulin_button_increment_1), PLUS1_DEFAULT))); + SP.getDouble(MainApp.gs(R.string.key_insulin_button_increment_1), PLUS1_DEFAULT)));
@ -253,14 +225,20 @@ public class NewInsulinDialog extends DialogFragment implements OnClickListener,
final double finalTT = tt; final double finalTT = tt;
if (startESMCheckbox.isChecked()) { if (startESMCheckbox.isChecked()) {
if (currentProfile.getUnits().equals("mmol")) { if (currentProfile.getUnits().equals(Constants.MMOL)) {
actions.add("TT: " + "<font color='" + MainApp.sResources.getColor(R.color.high) + "'>" + Profile.toMmol(tt, Constants.MGDL) + " mmol for " + ((int) ttDuration) + " min </font>"); actions.add("TT: " + "<font color='" + MainApp.sResources.getColor(R.color.high) + "'>" + Profile.toMmol(tt, Constants.MGDL) + " mmol for " + ((int) ttDuration) + " min </font>");
} else } else
actions.add("TT: " + "<font color='" + MainApp.sResources.getColor(R.color.high) + "'>" + ((int) tt) + "mg/dl for " + ((int) ttDuration) + " min </font>"); actions.add("TT: " + "<font color='" + MainApp.sResources.getColor(R.color.high) + "'>" + ((int) tt) + "mg/dl for " + ((int) ttDuration) + " min </font>");
} }
if (!initialEventTime.equals(eventTime)) { int timeOffset = editTime.getValue().intValue();
actions.add("Time: " + DateUtil.dateAndTimeString(eventTime)); 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 finalInsulinAfterConstraints = insulinAfterConstraints;
@ -269,10 +247,9 @@ public class NewInsulinDialog extends DialogFragment implements OnClickListener,
final AlertDialog.Builder builder = new AlertDialog.Builder(context); final AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(MainApp.gs(R.string.confirmation)); builder.setTitle(MainApp.gs(R.string.confirmation));
builder.setMessage(actions.isEmpty() if (finalInsulinAfterConstraints > 0 || startESMCheckbox.isChecked()) {
? MainApp.gs(R.string.no_action_selected) builder.setMessage(Html.fromHtml(Joiner.on("<br/>").join(actions)));
: Html.fromHtml(Joiner.on("<br/>").join(actions))); builder.setPositiveButton(MainApp.gs(R.string.ok), (dialog, id) -> {
builder.setPositiveButton(MainApp.gs(R.string.ok), actions.isEmpty() ? null : (dialog, id) -> {
synchronized (builder) { synchronized (builder) {
if (accepted) { if (accepted) {
log.debug("guarding: already accepted"); log.debug("guarding: already accepted");
@ -291,23 +268,21 @@ public class NewInsulinDialog extends DialogFragment implements OnClickListener,
TreatmentsPlugin.getPlugin().addToHistoryTempTarget(tempTarget); TreatmentsPlugin.getPlugin().addToHistoryTempTarget(tempTarget);
} }
if (finalInsulinAfterConstraints <= 0.01) { if (finalInsulinAfterConstraints == 0) {
return; 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 detailedBolusInfo = new DetailedBolusInfo();
detailedBolusInfo.eventType = CareportalEvent.CORRECTIONBOLUS; detailedBolusInfo.eventType = CareportalEvent.CORRECTIONBOLUS;
detailedBolusInfo.insulin = finalInsulinAfterConstraints; detailedBolusInfo.insulin = finalInsulinAfterConstraints;
detailedBolusInfo.context = context; detailedBolusInfo.context = context;
detailedBolusInfo.source = Source.USER; 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() { ConfigBuilderPlugin.getCommandQueue().bolus(detailedBolusInfo, new Callback() {
@Override @Override
public void run() { public void run() {
@ -325,6 +300,9 @@ public class NewInsulinDialog extends DialogFragment implements OnClickListener,
} }
} }
}); });
} else {
builder.setMessage(MainApp.gs(R.string.no_action_selected));
}
builder.setNegativeButton(MainApp.gs(R.string.cancel), null); builder.setNegativeButton(MainApp.gs(R.string.cancel), null);
builder.show(); builder.show();
dismiss(); dismiss();
@ -332,20 +310,4 @@ public class NewInsulinDialog extends DialogFragment implements OnClickListener,
log.error("Unhandled exception", e); 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));
}
} }

View file

@ -16,20 +16,31 @@
android:orientation="vertical" android:orientation="vertical"
android:padding="10dp"> android:padding="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:padding="5dp"
android:layout_gravity="center"
android:src="@drawable/icon_bolus" />
<TextView <TextView
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" android:paddingBottom="10dp"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:paddingTop="10dp"
android:textAlignment="center"
android:text="@string/overview_insulin_label" android:text="@string/overview_insulin_label"
android:textAppearance="?android:attr/textAppearanceLarge" android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@color/colorInsulinButton" /> android:textColor="@color/colorInsulinButton" />
</LinearLayout>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="5dp"
android:padding="5dp"
android:src="@drawable/icon_bolus" />
<LinearLayout <LinearLayout
android:layout_width="wrap_content" android:layout_width="wrap_content"
@ -52,40 +63,69 @@
</LinearLayout> </LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="2dip"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:background="@color/listdelimiter" />
<LinearLayout
android:id="@+id/newinsulin_time_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="5dp"
android:paddingTop="5dp">
<TextView
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:textStyle="bold"
android:text="@string/time" />
<info.nightscout.utils.NumberPicker
android:id="@+id/newinsulin_time"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_gravity="end"
android:paddingLeft="5dp"
android:paddingRight="5dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="@string/shortminute" />
</LinearLayout>
<LinearLayout <LinearLayout
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" android:paddingBottom="5dp"
android:padding="5dp"> android:paddingTop="5dp">
<TextView <TextView
android:id="@+id/newinsulin_eventdate" android:layout_width="60dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" android:layout_gravity="center_vertical"
android:enabled="false" android:textStyle="bold"
android:padding="5dp" android:text="@string/overview_insulin_label" />
android:text="2017/05/05" />
<TextView
android:id="@+id/newinsulin_eventtime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:enabled="false"
android:padding="5dp"
android:text="08:20pm" />
</LinearLayout>
<info.nightscout.utils.NumberPicker <info.nightscout.utils.NumberPicker
android:id="@+id/treatments_newinsulin_amount" android:id="@+id/newinsulin_amount"
android:layout_width="130dp" android:layout_width="wrap_content"
android:layout_height="40dp" android:layout_height="40dp"
android:layout_gravity="end" android:layout_gravity="end"
android:layout_marginBottom="10dp" android:paddingLeft="5dp"
android:layout_marginLeft="30dp" android:paddingRight="5dp" />
android:layout_marginRight="30dp"
android:layout_marginTop="10dp" /> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="@string/insulin_unit_shortname" />
</LinearLayout>
<LinearLayout <LinearLayout
android:layout_width="match_parent" android:layout_width="match_parent"
@ -117,6 +157,30 @@
</LinearLayout> </LinearLayout>
<LinearLayout
android:id="@+id/newcarbs_notes_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="5dp"
android:orientation="horizontal">
<TextView
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:width="60dp"
android:padding="2dp"
android:text="@string/careportal_newnstreatment_notes_label"
android:textAppearance="@android:style/TextAppearance.Material.Small"
android:textStyle="bold" />
<EditText
android:id="@+id/newcarbs_notes"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<include layout="@layout/mdtp_done_button" /> <include layout="@layout/mdtp_done_button" />
</LinearLayout> </LinearLayout>

View file

@ -999,5 +999,5 @@
<string name="openapssmb_maxiob_summary">This value is called Max IOB in OpenAPS context\nOpenAPS will not add more insulin if current IOB is greater than this value</string> <string name="openapssmb_maxiob_summary">This value is called Max IOB in OpenAPS context\nOpenAPS will not add more insulin if current IOB is greater than this value</string>
<string name="time">Time</string> <string name="time">Time</string>
<string name="key_show_notes_entry_dialogs">show_notes_entry_dialogs</string> <string name="key_show_notes_entry_dialogs">show_notes_entry_dialogs</string>
<string name="overview_show_notes_field_in_dialogs_title">Show notes field in dialogs</string> <string name="overview_show_notes_field_in_dialogs_title">Show notes field in treatment dialogs</string>
</resources> </resources>