From 606a7d6c27098055f301c246344ceeae0d713f78 Mon Sep 17 00:00:00 2001 From: Andries Smit Date: Tue, 25 Jan 2022 10:44:22 +0100 Subject: [PATCH] Add percentage in quickwizard --- .../overview/dialogs/EditQuickWizardDialog.kt | 35 +++++++++++++--- .../androidaps/utils/wizard/BolusWizard.kt | 4 +- .../utils/wizard/QuickWizardEntry.kt | 21 +++++++++- .../overview_editquickwizard_dialog.xml | 42 ++++++++++++++++++- app/src/main/res/values/strings.xml | 5 ++- .../androidaps/data/ListenerService.java | 2 +- 6 files changed, 96 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/dialogs/EditQuickWizardDialog.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/dialogs/EditQuickWizardDialog.kt index cb4c3ee652..e7abb4690d 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/dialogs/EditQuickWizardDialog.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/dialogs/EditQuickWizardDialog.kt @@ -35,7 +35,6 @@ class EditQuickWizardDialog : DaggerDialogFragment(), View.OnClickListener { @Inject lateinit var sp: SP var position = -1 - var fromSeconds: Int = 0 var toSeconds: Int = 0 @@ -45,8 +44,10 @@ class EditQuickWizardDialog : DaggerDialogFragment(), View.OnClickListener { // onDestroyView. private val binding get() = _binding!! - override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, - savedInstanceState: Bundle?): View { + override fun onCreateView( + inflater: LayoutInflater, container: ViewGroup?, + savedInstanceState: Bundle? + ): View { dialog?.window?.requestFeature(Window.FEATURE_NO_TITLE) dialog?.window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN) isCancelable = true @@ -82,6 +83,9 @@ class EditQuickWizardDialog : DaggerDialogFragment(), View.OnClickListener { entry.storage.put("useTrend", binding.useTrend.selectedItemPosition) entry.storage.put("useSuperBolus", binding.useSuperBolus.selectedItemPosition) entry.storage.put("useTempTarget", binding.useTempTarget.selectedItemPosition) + entry.storage.put("usePercentage", binding.usePercentage.selectedItemPosition) + val percentage = SafeParse.stringToInt(binding.percentage.text.toString()) + entry.storage.put("percentage", percentage) } catch (e: JSONException) { aapsLogger.error("Unhandled exception", e) } @@ -100,7 +104,8 @@ class EditQuickWizardDialog : DaggerDialogFragment(), View.OnClickListener { binding.from.setOnClickListener { context?.let { - TimePickerDialog(it, fromTimeSetListener, + TimePickerDialog( + it, fromTimeSetListener, T.secs(fromSeconds.toLong()).hours().toInt(), T.secs((fromSeconds % 3600).toLong()).mins().toInt(), DateFormat.is24HourFormat(context) @@ -117,13 +122,29 @@ class EditQuickWizardDialog : DaggerDialogFragment(), View.OnClickListener { binding.to.setOnClickListener { context?.let { - TimePickerDialog(it, toTimeSetListener, + TimePickerDialog( + it, toTimeSetListener, T.secs(toSeconds.toLong()).hours().toInt(), T.secs((toSeconds % 3600).toLong()).mins().toInt(), DateFormat.is24HourFormat(context) ).show() } } + + fun usePercentage(custom: Boolean) { + if (custom) { + binding.percentageLabel.visibility = View.VISIBLE + binding.percentage.visibility = View.VISIBLE + } else { + binding.percentageLabel.visibility = View.GONE + binding.percentage.visibility = View.GONE + } + } + + binding.usePercentage.setOnCheckedChangeListener { _, checkedId -> + usePercentage(checkedId == R.id.use_percentage_custom) + } + toSeconds = entry.validTo() binding.to.text = dateUtil.timeString(dateUtil.secondsOfTheDayToMilliseconds(toSeconds)) @@ -138,7 +159,9 @@ class EditQuickWizardDialog : DaggerDialogFragment(), View.OnClickListener { binding.useTrend.setSelection(entry.useTrend()) binding.useSuperBolus.setSelection(entry.useSuperBolus()) binding.useTempTarget.setSelection(entry.useTempTarget()) - + binding.usePercentage.setSelection(entry.usePercentage()) + usePercentage(entry.usePercentage() == QuickWizardEntry.CUSTOM) + binding.percentage.setText(entry.percentage().toString()) binding.useCobYes.setOnClickListener(this) binding.useCobNo.setOnClickListener(this) processCob() diff --git a/app/src/main/java/info/nightscout/androidaps/utils/wizard/BolusWizard.kt b/app/src/main/java/info/nightscout/androidaps/utils/wizard/BolusWizard.kt index dbc0ddf651..de30b2f565 100644 --- a/app/src/main/java/info/nightscout/androidaps/utils/wizard/BolusWizard.kt +++ b/app/src/main/java/info/nightscout/androidaps/utils/wizard/BolusWizard.kt @@ -405,9 +405,9 @@ class BolusWizard @Inject constructor( if (useBg) message += "\n" + rh.gs(R.string.wizard_explain_bg, insulinFromBG) if (includeBolusIOB) message += "\n" + rh.gs(R.string.wizard_explain_bolus_iob, insulinFromBolusIOB) if (includeBasalIOB) message += "\n" + rh.gs(R.string.wizard_explain_basal_iob, insulinFromBasalIOB) - if (usePercentage) message += "\n" + rh.gs(R.string.wizard_explain_trend, insulinFromTrend) + if (useTrend) message += "\n" + rh.gs(R.string.wizard_explain_trend, insulinFromTrend) if (useSuperBolus) message += "\n" + rh.gs(R.string.wizard_explain_superbolus, insulinFromSuperBolus) - if (usePercentage) { + if (percentageCorrection != 100) { message += "\n" + rh.gs(R.string.wizard_explain_percent, totalBeforePercentageAdjustment, percentageCorrection, calculatedTotalInsulin) } return message diff --git a/app/src/main/java/info/nightscout/androidaps/utils/wizard/QuickWizardEntry.kt b/app/src/main/java/info/nightscout/androidaps/utils/wizard/QuickWizardEntry.kt index 1f3d48fc7a..c6dee8a660 100644 --- a/app/src/main/java/info/nightscout/androidaps/utils/wizard/QuickWizardEntry.kt +++ b/app/src/main/java/info/nightscout/androidaps/utils/wizard/QuickWizardEntry.kt @@ -45,12 +45,23 @@ class QuickWizardEntry @Inject constructor(private val injector: HasAndroidInjec const val DEVICE_ALL = 0 const val DEVICE_PHONE = 1 const val DEVICE_WATCH = 2 + const val DEFAULT = 0 + const val CUSTOM = 1 } init { injector.androidInjector().inject(this) val guid = UUID.randomUUID().toString() - val emptyData = "{\"guid\": \"$guid\",\"buttonText\":\"\",\"carbs\":0,\"validFrom\":0,\"validTo\":86340, \"device\": \"all\"}" + val emptyData = """{ + "guid": "$guid", + "buttonText": "", + "carbs": 0, + "validFrom": 0, + "validTo": 86340, + "device": "all", + "usePercentage": "default", + "percentage": 100 + }""".trimMargin() try { storage = JSONObject(emptyData) } catch (e: JSONException) { @@ -73,6 +84,8 @@ class QuickWizardEntry @Inject constructor(private val injector: HasAndroidInjec useTrend: 0, useSuperBolus: 0, useTemptarget: 0 + usePercentage: string, // default, custom + percentage: int, } */ fun from(entry: JSONObject, position: Int): QuickWizardEntry { @@ -127,7 +140,7 @@ class QuickWizardEntry @Inject constructor(private val injector: HasAndroidInjec } else if (useTrend() == NEGATIVE_ONLY && glucoseStatus != null && glucoseStatus.shortAvgDelta < 0) { trend = true } - val percentage = sp.getInt(R.string.key_boluswizard_percentage, 100) + val percentage = if (usePercentage() == DEFAULT) sp.getInt(R.string.key_boluswizard_percentage, 100) else percentage() return BolusWizard(injector).doCalc(profile, profileName, tempTarget, carbs(), cob, bg, 0.0, percentage, true, useCOB() == YES, bolusIOB, basalIOB, superBolus, useTempTarget() == YES, trend, false, buttonText(), quickWizard = true) //tbc, ok if only quickwizard, but if other sources elsewhere use Sources.QuickWizard } @@ -162,4 +175,8 @@ class QuickWizardEntry @Inject constructor(private val injector: HasAndroidInjec fun useSuperBolus(): Int = safeGetInt(storage, "useSuperBolus", NO) fun useTempTarget(): Int = safeGetInt(storage, "useTempTarget", NO) + + fun usePercentage(): Int = safeGetInt(storage, "usePercentage", DEFAULT) + + fun percentage(): Int = safeGetInt(storage, "percentage", 100) } diff --git a/app/src/main/res/layout/overview_editquickwizard_dialog.xml b/app/src/main/res/layout/overview_editquickwizard_dialog.xml index af1eee4f84..b032ddebb9 100644 --- a/app/src/main/res/layout/overview_editquickwizard_dialog.xml +++ b/app/src/main/res/layout/overview_editquickwizard_dialog.xml @@ -305,7 +305,6 @@ - + + + + + + + + + + + + + Negative only COB calculation Temporary target calculation + Percentage calculation Loop enabled APS selected NSClient has write permission @@ -1191,10 +1192,12 @@ Bolus IOB: %1$.2fU Superbolus: %1$.2fU 15\' trend: %1$.2fU - Perctage: %1$.2fU x %2$d% = %3$.2fU + Percentage: %1$.2fU x %2$d%% ≈ %3$.2fU Insulin constraint violation!\nCannot deliver %1$.2fU TempT: %1$s %1$s to %2$s No pump available! Unknown action command: + Percentage + Application default diff --git a/wear/src/main/java/info/nightscout/androidaps/data/ListenerService.java b/wear/src/main/java/info/nightscout/androidaps/data/ListenerService.java index 8104cef81c..eebd92eaae 100644 --- a/wear/src/main/java/info/nightscout/androidaps/data/ListenerService.java +++ b/wear/src/main/java/info/nightscout/androidaps/data/ListenerService.java @@ -613,7 +613,7 @@ public class ListenerService extends WearableListenerService implements GoogleAp // Log.d(TAG, logPrefix + "onConnected call requestData"); Wearable.ChannelApi.addListener(googleApiClient, this); - requestData(); + // requestData(); } @Override