From 2ace719e2a4e9f570cffac6270e16c808b22b50e Mon Sep 17 00:00:00 2001 From: Philoul Date: Fri, 22 Oct 2021 16:15:26 +0200 Subject: [PATCH] Add Bolus Reminder to CarbsDialog --- .../androidaps/dialogs/CarbsDialog.kt | 15 ++++++- .../nightscout/androidaps/utils/BolusTimer.kt | 44 +++++++++++++++++++ app/src/main/res/layout/dialog_carbs.xml | 34 ++++++++++++++ app/src/main/res/values/strings.xml | 7 +++ app/src/main/res/xml/pref_overview.xml | 6 +++ 5 files changed, 104 insertions(+), 2 deletions(-) create mode 100644 app/src/main/java/info/nightscout/androidaps/utils/BolusTimer.kt diff --git a/app/src/main/java/info/nightscout/androidaps/dialogs/CarbsDialog.kt b/app/src/main/java/info/nightscout/androidaps/dialogs/CarbsDialog.kt index 3bfa076da9..46643760b0 100644 --- a/app/src/main/java/info/nightscout/androidaps/dialogs/CarbsDialog.kt +++ b/app/src/main/java/info/nightscout/androidaps/dialogs/CarbsDialog.kt @@ -27,6 +27,8 @@ import info.nightscout.androidaps.interfaces.ProfileFunction import info.nightscout.androidaps.logging.LTag import info.nightscout.androidaps.logging.UserEntryLogger import info.nightscout.androidaps.plugins.configBuilder.ConstraintChecker +import info.nightscout.androidaps.plugins.iob.iobCobCalculator.GlucoseStatus +import info.nightscout.androidaps.plugins.iob.iobCobCalculator.GlucoseStatusProvider import info.nightscout.androidaps.queue.Callback import info.nightscout.androidaps.queue.CommandQueue import info.nightscout.androidaps.utils.* @@ -48,8 +50,10 @@ class CarbsDialog : DialogFragmentWithDate() { @Inject lateinit var defaultValueHelper: DefaultValueHelper @Inject lateinit var profileFunction: ProfileFunction @Inject lateinit var iobCobCalculator: IobCobCalculator + @Inject lateinit var glucoseStatusProvider: GlucoseStatusProvider @Inject lateinit var uel: UserEntryLogger @Inject lateinit var carbTimer: CarbTimer + @Inject lateinit var bolusTimer: BolusTimer @Inject lateinit var commandQueue: CommandQueue @Inject lateinit var repository: AppRepository @@ -110,7 +114,12 @@ class CarbsDialog : DialogFragmentWithDate() { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) - + if (sp.getBoolean(R.string.key_usebolusreminder, false)) { + glucoseStatusProvider.glucoseStatusData?.let { glucoseStatus -> + if (glucoseStatus.glucose + 3 * glucoseStatus.delta < 70.0) + binding.bolusReminder.visibility = View.VISIBLE + } + } val maxCarbs = constraintChecker.getMaxCarbsAllowed().value().toDouble() binding.time.setParams(savedInstanceState?.getDouble("time") ?: 0.0, -12 * 60.0, 12 * 60.0, 5.0, DecimalFormat("0"), false, binding.okcancel.ok, textWatcher) @@ -184,6 +193,7 @@ class CarbsDialog : DialogFragmentWithDate() { val actions: LinkedList = LinkedList() val unitLabel = if (units == GlucoseUnit.MMOL) resourceHelper.gs(R.string.mmol) else resourceHelper.gs(R.string.mgdl) val useAlarm = binding.alarmCheckBox.isChecked + val remindBolus = binding.bolusReminderCheckBox.isChecked val activitySelected = binding.activityTt.isChecked if (activitySelected) @@ -296,7 +306,8 @@ class CarbsDialog : DialogFragmentWithDate() { override fun run() { if (!result.success) { ErrorHelperActivity.runAlarm(ctx, result.comment, resourceHelper.gs(R.string.treatmentdeliveryerror), R.raw.boluserror) - } + } else if (sp.getBoolean(R.string.key_usebolusreminder, false) && remindBolus) + bolusTimer.scheduleBolusReminder() } }) } diff --git a/app/src/main/java/info/nightscout/androidaps/utils/BolusTimer.kt b/app/src/main/java/info/nightscout/androidaps/utils/BolusTimer.kt new file mode 100644 index 0000000000..aa248e1cb6 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/utils/BolusTimer.kt @@ -0,0 +1,44 @@ +package info.nightscout.androidaps.utils + +import dagger.android.HasAndroidInjector +import info.nightscout.androidaps.R +import info.nightscout.androidaps.interfaces.GlucoseUnit +import info.nightscout.androidaps.plugins.general.automation.AutomationEvent +import info.nightscout.androidaps.plugins.general.automation.AutomationPlugin +import info.nightscout.androidaps.plugins.general.automation.actions.ActionAlarm +import info.nightscout.androidaps.plugins.general.automation.elements.Comparator +import info.nightscout.androidaps.plugins.general.automation.elements.InputDelta +import info.nightscout.androidaps.plugins.general.automation.triggers.TriggerBg +import info.nightscout.androidaps.plugins.general.automation.triggers.TriggerConnector +import info.nightscout.androidaps.plugins.general.automation.triggers.TriggerDelta +import info.nightscout.androidaps.utils.resources.ResourceHelper +import java.text.DecimalFormat +import javax.inject.Inject +import javax.inject.Singleton + +@Singleton +class BolusTimer @Inject constructor( + private val injector: HasAndroidInjector, + private val resourceHelper: ResourceHelper, + private val automationPlugin: AutomationPlugin, +) { + + fun scheduleBolusReminder() { + val event = AutomationEvent(injector).apply { + title = resourceHelper.gs(R.string.bolusreminder) + readOnly = true + systemAction = true + autoRemove = true + trigger = TriggerConnector(injector, TriggerConnector.Type.AND).apply { + + // Bg above 70 mgdl and delta positive mgdl + list.add(TriggerBg(injector, 70.0, GlucoseUnit.MGDL, Comparator.Compare.IS_EQUAL_OR_GREATER)) + list.add(TriggerDelta(injector, InputDelta(resourceHelper, -15.0, -360.0, 360.0, 1.0, DecimalFormat("0"), InputDelta.DeltaType.DELTA), GlucoseUnit.MGDL, Comparator.Compare.IS_EQUAL_OR_GREATER)) + } + actions.add(ActionAlarm(injector, resourceHelper.gs(R.string.time_to_bolus))) + } + + automationPlugin.addIfNotExists(event) + } + +} \ No newline at end of file diff --git a/app/src/main/res/layout/dialog_carbs.xml b/app/src/main/res/layout/dialog_carbs.xml index 6e485a0edb..3d2663e23b 100644 --- a/app/src/main/res/layout/dialog_carbs.xml +++ b/app/src/main/res/layout/dialog_carbs.xml @@ -234,6 +234,40 @@ + + + + + + + + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index cc680cb04b..9ff07309ff 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -844,6 +844,7 @@ Reset to defaults NSClient malfunction. Consider NS and NSClient restart. Time offset + Remind to bolus later aps_mode Preferred APS mode Total @@ -1066,6 +1067,12 @@ Use reminder to start eating later instead of wizard result during high glycemia ("pre-bolus") Time to eat!\nRun Bolus wizard and do calculation again. Time to eat + Bolus reminder + use_bolus_reminder + Enable bolus reminder + Use reminder to bolus later with wizard + ("post-bolus") + Time to bolus!\nRun Bolus wizard and do calculation again. Crash logs upload disabled! Graph Chart menu diff --git a/app/src/main/res/xml/pref_overview.xml b/app/src/main/res/xml/pref_overview.xml index ad2367c9ee..18c27d2d44 100644 --- a/app/src/main/res/xml/pref_overview.xml +++ b/app/src/main/res/xml/pref_overview.xml @@ -502,6 +502,12 @@ android:summary="@string/enablesuperbolus_summary" android:title="@string/enablesuperbolus" /> + +