diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Actions/ActionsFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/Actions/ActionsFragment.java index 245d189660..f263adcc99 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Actions/ActionsFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Actions/ActionsFragment.java @@ -10,6 +10,7 @@ import android.view.ViewGroup; import info.nightscout.androidaps.R; import info.nightscout.androidaps.interfaces.FragmentBase; +import info.nightscout.androidaps.plugins.Actions.dialogs.FillDialog; import info.nightscout.androidaps.plugins.Careportal.Dialogs.NewNSTreatmentDialog; import info.nightscout.androidaps.plugins.Careportal.OptionsToShow; import info.nightscout.androidaps.plugins.Actions.dialogs.NewExtendedBolusDialog; @@ -37,6 +38,7 @@ public class ActionsFragment extends Fragment implements FragmentBase, View.OnCl view.findViewById(R.id.actions_profileswitch).setOnClickListener(this); view.findViewById(R.id.actions_extendedbolus).setOnClickListener(this); view.findViewById(R.id.actions_settempbasal).setOnClickListener(this); + view.findViewById(R.id.actions_fill).setOnClickListener(this); return view; } @@ -60,6 +62,10 @@ public class ActionsFragment extends Fragment implements FragmentBase, View.OnCl NewTempBasalDialog newTempDialog = new NewTempBasalDialog(); newTempDialog.show(manager, "NewTempDialog"); break; + case R.id.actions_fill: + FillDialog fillDialog = new FillDialog(); + fillDialog.show(manager, "FillDialog"); + break; } } } 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 new file mode 100644 index 0000000000..2317c59983 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Actions/dialogs/FillDialog.java @@ -0,0 +1,125 @@ +package info.nightscout.androidaps.plugins.Actions.dialogs; + +import android.content.Context; +import android.content.DialogInterface; +import android.os.Bundle; +import android.os.Handler; +import android.os.HandlerThread; +import android.support.v4.app.DialogFragment; +import android.support.v7.app.AlertDialog; +import android.view.LayoutInflater; +import android.view.View; +import android.view.View.OnClickListener; +import android.view.ViewGroup; +import android.view.Window; +import android.view.WindowManager; +import android.widget.Button; +import android.widget.TextView; + +import java.text.DecimalFormat; + +import info.nightscout.androidaps.Constants; +import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.R; +import info.nightscout.androidaps.data.PumpEnactResult; +import info.nightscout.androidaps.interfaces.PumpInterface; +import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; +import info.nightscout.utils.PlusMinusEditText; +import info.nightscout.utils.SafeParse; + +public class FillDialog extends DialogFragment implements OnClickListener { + + Button deliverButton; + TextView insulin; + + PlusMinusEditText editInsulin; + + Handler mHandler; + public static HandlerThread mHandlerThread; + + public FillDialog() { + mHandlerThread = new HandlerThread(FillDialog.class.getSimpleName()); + mHandlerThread.start(); + this.mHandler = new Handler(mHandlerThread.getLooper()); + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + View view = inflater.inflate(R.layout.actions_fill_dialog, null, false); + + deliverButton = (Button) view.findViewById(R.id.treatments_newtreatment_deliverbutton); + + deliverButton.setOnClickListener(this); + getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE); + getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN); + + insulin = (TextView) view.findViewById(R.id.treatments_newtreatment_insulinamount); + Double maxInsulin = MainApp.getConfigBuilder().applyBolusConstraints(Constants.bolusOnlyForCheckLimit); + + editInsulin = new PlusMinusEditText(view, R.id.treatments_newtreatment_insulinamount, R.id.treatments_newtreatment_insulinamount_plus, R.id.treatments_newtreatment_insulinamount_minus, 0d, 0d, maxInsulin, 0.05d, new DecimalFormat("0.00"), false); + + return view; + } + + @Override + public void onResume() { + super.onResume(); + if (getDialog() != null) + getDialog().getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); + } + + @Override + public void onClick(View view) { + switch (view.getId()) { + case R.id.treatments_newtreatment_deliverbutton: + + try { + Double insulin = SafeParse.stringToDouble(this.insulin.getText().toString()); + + String confirmMessage = getString(R.string.fillwarning) + "\n"; + + Double insulinAfterConstraints = MainApp.getConfigBuilder().applyBolusConstraints(insulin); + confirmMessage += getString(R.string.bolus) + ": " + insulinAfterConstraints + "U"; + if (insulinAfterConstraints - insulin != 0) + confirmMessage += "\n" + getString(R.string.constraintapllied); + + final Double finalInsulinAfterConstraints = insulinAfterConstraints; + + final Context context = getContext(); + AlertDialog.Builder builder = new AlertDialog.Builder(context); + + builder.setTitle(this.getContext().getString(R.string.confirmation)); + builder.setMessage(confirmMessage); + builder.setPositiveButton(getString(R.string.primefill), new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + if (finalInsulinAfterConstraints > 0) { + final ConfigBuilderPlugin pump = MainApp.getConfigBuilder(); + mHandler.post(new Runnable() { + @Override + public void run() { + PumpEnactResult result = pump.deliverTreatment(finalInsulinAfterConstraints, 0, context, false); + if (!result.success) { + AlertDialog.Builder builder = new AlertDialog.Builder(context); + builder.setTitle(MainApp.sResources.getString(R.string.treatmentdeliveryerror)); + builder.setMessage(result.comment); + builder.setPositiveButton(MainApp.sResources.getString(R.string.ok), null); + builder.show(); + } + } + }); + } + } + }); + builder.setNegativeButton(getString(R.string.cancel), null); + builder.show(); + dismiss(); + } catch (Exception e) { + e.printStackTrace(); + } + break; + } + + } + +} \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderPlugin.java index 6aea91ca05..2e0696c228 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderPlugin.java @@ -390,6 +390,10 @@ public class ConfigBuilderPlugin implements PluginBase, PumpInterface, Constrain @Override public PumpEnactResult deliverTreatment(Double insulin, Integer carbs, Context context) { + return deliverTreatment(insulin, carbs, context, true); + } + + public PumpEnactResult deliverTreatment(Double insulin, Integer carbs, Context context, boolean createTreatment) { mWakeLock.acquire(); insulin = applyBolusConstraints(insulin); carbs = applyCarbsConstraints(carbs); @@ -416,7 +420,7 @@ public class ConfigBuilderPlugin implements PluginBase, PumpInterface, Constrain if (Config.logCongigBuilderActions) log.debug("deliverTreatment insulin: " + insulin + " carbs: " + carbs + " success: " + result.success + " enacted: " + result.enacted + " bolusDelivered: " + result.bolusDelivered); - if (result.success) { + if (result.success && createTreatment) { Treatment t = new Treatment(); t.insulin = result.bolusDelivered; t.carbs = (double) result.carbsDelivered; @@ -435,6 +439,8 @@ public class ConfigBuilderPlugin implements PluginBase, PumpInterface, Constrain return result; } + + @Override public void stopBolusDelivering() { activePump.stopBolusDelivering(); diff --git a/app/src/main/res/layout/actions_fill_dialog.xml b/app/src/main/res/layout/actions_fill_dialog.xml new file mode 100644 index 0000000000..6394ccf304 --- /dev/null +++ b/app/src/main/res/layout/actions_fill_dialog.xml @@ -0,0 +1,77 @@ + + + + + + + + + + + + + + + + +