From c5925c4f8fcfb475b3312dfede3b0c037b09fa6a Mon Sep 17 00:00:00 2001 From: Andries Smit Date: Wed, 23 Nov 2022 19:52:21 +0100 Subject: [PATCH 1/2] feat: cannula usage --- .../general/overview/OverviewFragment.kt | 1 + .../general/actions/ActionsFragment.kt | 6 +- .../nightscout/plugins/skins/SkinLowRes.kt | 2 +- .../plugins/ui/StatusLightHandler.kt | 59 ++++++++++++------- .../res/layout/actions_stats_fragment.xml | 18 +++--- plugins/main/src/main/res/values/strings.xml | 3 +- 6 files changed, 56 insertions(+), 33 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/OverviewFragment.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/OverviewFragment.kt index a96889c0f6..98043b9b8a 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/OverviewFragment.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/OverviewFragment.kt @@ -923,6 +923,7 @@ class OverviewFragment : DaggerFragment(), View.OnClickListener, OnLongClickList } statusLightHandler.updateStatusLights( binding.statusLightsLayout.cannulaAge, + null, binding.statusLightsLayout.insulinAge, binding.statusLightsLayout.reservoirLevel, binding.statusLightsLayout.sensorAge, diff --git a/plugins/main/src/main/java/info/nightscout/plugins/general/actions/ActionsFragment.kt b/plugins/main/src/main/java/info/nightscout/plugins/general/actions/ActionsFragment.kt index a10ba59a73..3b3daf4ce4 100644 --- a/plugins/main/src/main/java/info/nightscout/plugins/general/actions/ActionsFragment.kt +++ b/plugins/main/src/main/java/info/nightscout/plugins/general/actions/ActionsFragment.kt @@ -289,12 +289,14 @@ class ActionsFragment : DaggerFragment() { val imageResource = if (isPatchPump) R.drawable.ic_patch_pump_outline else R.drawable.ic_cp_age_cannula cannulaOrPatch.setCompoundDrawablesWithIntrinsicBounds(imageResource, 0, 0, 0) batteryLayout.visibility = (!isPatchPump || pump.pumpDescription.useHardwareLink).toVisibility() + cannulaUsageLabel.visibility = isPatchPump.not().toVisibility() + cannulaUsage.visibility = isPatchPump.not().toVisibility() if (!config.NSCLIENT) { - statusLightHandler.updateStatusLights(cannulaAge, insulinAge, reservoirLevel, sensorAge, sensorLevel, pbAge, batteryLevel) + statusLightHandler.updateStatusLights(cannulaAge, cannulaUsage, insulinAge, reservoirLevel, sensorAge, sensorLevel, pbAge, batteryLevel) sensorLevelLabel.text = if (activeBgSource.sensorBatteryLevel == -1) "" else rh.gs(R.string.level_label) } else { - statusLightHandler.updateStatusLights(cannulaAge, insulinAge, null, sensorAge, null, pbAge, null) + statusLightHandler.updateStatusLights(cannulaAge, cannulaUsage, insulinAge, null, sensorAge, null, pbAge, null) sensorLevelLabel.text = "" insulinLevelLabel.text = "" pbLevelLabel.text = "" diff --git a/plugins/main/src/main/java/info/nightscout/plugins/skins/SkinLowRes.kt b/plugins/main/src/main/java/info/nightscout/plugins/skins/SkinLowRes.kt index d9e3c51848..4af731facf 100644 --- a/plugins/main/src/main/java/info/nightscout/plugins/skins/SkinLowRes.kt +++ b/plugins/main/src/main/java/info/nightscout/plugins/skins/SkinLowRes.kt @@ -30,7 +30,7 @@ class SkinLowRes @Inject constructor(private val config: Config) : SkinInterface insulinAgeLabel.visibility = GONE insulinLevelLabel.visibility = GONE cannulaAgeLabel.visibility = GONE - cannulaPlaceholder.visibility = GONE + cannulaUsageLabel.visibility = GONE pbAgeLabel.visibility = GONE pbLevelLabel.visibility = GONE } diff --git a/plugins/main/src/main/java/info/nightscout/plugins/ui/StatusLightHandler.kt b/plugins/main/src/main/java/info/nightscout/plugins/ui/StatusLightHandler.kt index 95aa14d5ba..d164e08a0d 100644 --- a/plugins/main/src/main/java/info/nightscout/plugins/ui/StatusLightHandler.kt +++ b/plugins/main/src/main/java/info/nightscout/plugins/ui/StatusLightHandler.kt @@ -2,6 +2,7 @@ package info.nightscout.plugins.ui import android.widget.TextView import androidx.annotation.StringRes +import info.nightscout.database.entities.Bolus import info.nightscout.database.entities.TherapyEvent import info.nightscout.database.impl.AppRepository import info.nightscout.database.impl.ValueWrapper @@ -33,27 +34,28 @@ class StatusLightHandler @Inject constructor( * applies the extended statusLight subview on the overview fragment */ fun updateStatusLights( - careportal_cannula_age: TextView?, - careportal_insulin_age: TextView?, - careportal_reservoir_level: TextView?, - careportal_sensor_age: TextView?, - careportal_sensor_battery_level: TextView?, - careportal_pb_age: TextView?, - careportal_battery_level: TextView? + cannulaAge: TextView?, + cannulaUsage: TextView?, + insulinAge: TextView?, + reservoirLevel: TextView?, + sensorAge: TextView?, + sensorBatteryLevel: TextView?, + batteryAge: TextView?, + batteryLevel: TextView? ) { val pump = activePlugin.activePump val bgSource = activePlugin.activeBgSource - handleAge(careportal_cannula_age, TherapyEvent.Type.CANNULA_CHANGE, R.string.key_statuslights_cage_warning, 48.0, R.string.key_statuslights_cage_critical, 72.0) - handleAge(careportal_insulin_age, TherapyEvent.Type.INSULIN_CHANGE, R.string.key_statuslights_iage_warning, 72.0, R.string.key_statuslights_iage_critical, 144.0) - handleAge(careportal_sensor_age, TherapyEvent.Type.SENSOR_CHANGE, R.string.key_statuslights_sage_warning, 216.0, R.string.key_statuslights_sage_critical, 240.0) + handleAge(cannulaAge, TherapyEvent.Type.CANNULA_CHANGE, R.string.key_statuslights_cage_warning, 48.0, R.string.key_statuslights_cage_critical, 72.0) + handleAge(insulinAge, TherapyEvent.Type.INSULIN_CHANGE, R.string.key_statuslights_iage_warning, 72.0, R.string.key_statuslights_iage_critical, 144.0) + handleAge(sensorAge, TherapyEvent.Type.SENSOR_CHANGE, R.string.key_statuslights_sage_warning, 216.0, R.string.key_statuslights_sage_critical, 240.0) if (pump.pumpDescription.isBatteryReplaceable || pump.isBatteryChangeLoggingEnabled()) { - handleAge(careportal_pb_age, TherapyEvent.Type.PUMP_BATTERY_CHANGE, R.string.key_statuslights_bage_warning, 216.0, R.string.key_statuslights_bage_critical, 240.0) + handleAge(batteryAge, TherapyEvent.Type.PUMP_BATTERY_CHANGE, R.string.key_statuslights_bage_warning, 216.0, R.string.key_statuslights_bage_critical, 240.0) } val insulinUnit = rh.gs(R.string.insulin_unit_shortname) - if (pump.pumpDescription.isPatchPump) + if (pump.pumpDescription.isPatchPump) { handlePatchReservoirLevel( - careportal_reservoir_level, + reservoirLevel, R.string.key_statuslights_res_critical, 10.0, R.string.key_statuslights_res_warning, @@ -62,14 +64,15 @@ class StatusLightHandler @Inject constructor( insulinUnit, pump.pumpDescription.maxResorvoirReading.toDouble() ) - else - handleLevel(careportal_reservoir_level, R.string.key_statuslights_res_critical, 10.0, R.string.key_statuslights_res_warning, 80.0, pump.reservoirLevel, insulinUnit) - + } else { + handleUsage(cannulaUsage, TherapyEvent.Type.CANNULA_CHANGE, insulinUnit) + handleLevel(reservoirLevel, R.string.key_statuslights_res_critical, 10.0, R.string.key_statuslights_res_warning, 80.0, pump.reservoirLevel, insulinUnit) + } if (!config.NSCLIENT) { if (bgSource.sensorBatteryLevel != -1) - handleLevel(careportal_sensor_battery_level, R.string.key_statuslights_sbat_critical, 5.0, R.string.key_statuslights_sbat_warning, 20.0, bgSource.sensorBatteryLevel.toDouble(), "%") + handleLevel(sensorBatteryLevel, R.string.key_statuslights_sbat_critical, 5.0, R.string.key_statuslights_sbat_warning, 20.0, bgSource.sensorBatteryLevel.toDouble(), "%") else - careportal_sensor_battery_level?.text = "" + sensorBatteryLevel?.text = "" } if (!config.NSCLIENT) { @@ -79,10 +82,10 @@ class StatusLightHandler @Inject constructor( val erosBatteryLinkAvailable = pump.model() == PumpType.OMNIPOD_EROS && pump.isUseRileyLinkBatteryLevel() if (pump.model().supportBatteryLevel || erosBatteryLinkAvailable) { - handleLevel(careportal_battery_level, R.string.key_statuslights_bat_critical, 26.0, R.string.key_statuslights_bat_warning, 51.0, pump.batteryLevel.toDouble(), "%") + handleLevel(batteryLevel, R.string.key_statuslights_bat_critical, 26.0, R.string.key_statuslights_bat_warning, 51.0, pump.batteryLevel.toDouble(), "%") } else { - careportal_battery_level?.text = rh.gs(R.string.value_unavailable_short) - careportal_battery_level?.setTextColor(rh.gac(careportal_battery_level.context, R.attr.defaultTextColor)) + batteryLevel?.text = rh.gs(R.string.value_unavailable_short) + batteryLevel?.setTextColor(rh.gac(batteryLevel.context, R.attr.defaultTextColor)) } } } @@ -121,4 +124,16 @@ class StatusLightHandler @Inject constructor( handleLevel(view, criticalSetting, criticalDefaultValue, warnSetting, warnDefaultValue, level, units) } } -} \ No newline at end of file + + private fun handleUsage(view: TextView?, type: TherapyEvent.Type, units: String) { + val therapyEvent = repository.getLastTherapyRecordUpToNow(type).blockingGet() + var usage = 0.0 + if (therapyEvent is ValueWrapper.Existing) { + val startTime = therapyEvent.value.timestamp + usage = repository.getBolusesDataFromTime(startTime, true).blockingGet() + .filter { it.type != Bolus.Type.PRIMING } + .sumOf { t -> t.amount } + } + view?.text = " " + DecimalFormatter.to0Decimal(usage) + units + } +} diff --git a/plugins/main/src/main/res/layout/actions_stats_fragment.xml b/plugins/main/src/main/res/layout/actions_stats_fragment.xml index 80e98496ea..1dda739d7c 100644 --- a/plugins/main/src/main/res/layout/actions_stats_fragment.xml +++ b/plugins/main/src/main/res/layout/actions_stats_fragment.xml @@ -57,8 +57,8 @@ android:layout_marginEnd="20dp" android:gravity="center_vertical" android:paddingStart="2dp" - android:textSize="14sp" - tools:ignore="RtlSymmetry" /> + android:paddingEnd="0dp" + android:textSize="14sp" /> @@ -129,8 +129,9 @@ android:layout_marginEnd="20dp" android:gravity="center_vertical" android:paddingStart="2dp" + android:paddingEnd="0dp" android:textSize="14sp" - tools:ignore="RtlSymmetry" /> + tools:text="15U" /> @@ -186,23 +187,25 @@ android:textSize="14sp" /> + tools:text="22U" /> @@ -275,8 +278,9 @@ android:layout_marginEnd="20dp" android:gravity="center_vertical" android:paddingStart="2dp" + android:paddingEnd="0dp" android:textSize="14sp" - tools:ignore="RtlSymmetry" /> + tools:text="70%" /> diff --git a/plugins/main/src/main/res/values/strings.xml b/plugins/main/src/main/res/values/strings.xml index d0ca819e7e..f468ed8773 100644 --- a/plugins/main/src/main/res/values/strings.xml +++ b/plugins/main/src/main/res/values/strings.xml @@ -583,5 +583,6 @@ Data Broadcaster + usage: - \ No newline at end of file + From d7a7f6a53345193dcd67e3da17507a7d2e7cc47c Mon Sep 17 00:00:00 2001 From: Andries Smit Date: Thu, 24 Nov 2022 09:55:44 +0100 Subject: [PATCH 2/2] use tdd for cannula usage --- .../plugins/ui/StatusLightHandler.kt | 19 ++++++++----------- .../res/layout/actions_stats_fragment.xml | 8 ++++---- 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/plugins/main/src/main/java/info/nightscout/plugins/ui/StatusLightHandler.kt b/plugins/main/src/main/java/info/nightscout/plugins/ui/StatusLightHandler.kt index d164e08a0d..a709d2df29 100644 --- a/plugins/main/src/main/java/info/nightscout/plugins/ui/StatusLightHandler.kt +++ b/plugins/main/src/main/java/info/nightscout/plugins/ui/StatusLightHandler.kt @@ -2,7 +2,6 @@ package info.nightscout.plugins.ui import android.widget.TextView import androidx.annotation.StringRes -import info.nightscout.database.entities.Bolus import info.nightscout.database.entities.TherapyEvent import info.nightscout.database.impl.AppRepository import info.nightscout.database.impl.ValueWrapper @@ -10,6 +9,7 @@ import info.nightscout.interfaces.Config import info.nightscout.interfaces.plugin.ActivePlugin import info.nightscout.interfaces.pump.WarnColors import info.nightscout.interfaces.pump.defs.PumpType +import info.nightscout.interfaces.stats.TddCalculator import info.nightscout.interfaces.utils.DecimalFormatter import info.nightscout.plugins.R import info.nightscout.plugins.sync.nsclient.extensions.age @@ -27,7 +27,8 @@ class StatusLightHandler @Inject constructor( private val activePlugin: ActivePlugin, private val warnColors: WarnColors, private val config: Config, - private val repository: AppRepository + private val repository: AppRepository, + private val tddCalculator: TddCalculator ) { /** @@ -105,8 +106,7 @@ class StatusLightHandler @Inject constructor( private fun handleLevel(view: TextView?, criticalSetting: Int, criticalDefaultValue: Double, warnSetting: Int, warnDefaultValue: Double, level: Double, units: String) { val resUrgent = sp.getDouble(criticalSetting, criticalDefaultValue) val resWarn = sp.getDouble(warnSetting, warnDefaultValue) - @Suppress("SetTextI18n") - view?.text = " " + DecimalFormatter.to0Decimal(level) + units + view?.text = " " + DecimalFormatter.to0Decimal(level, units) warnColors.setColorInverse(view, level, resWarn, resUrgent) } @@ -117,8 +117,7 @@ class StatusLightHandler @Inject constructor( warnDefaultValue: Double, level: Double, units: String, maxReading: Double ) { if (level >= maxReading) { - @Suppress("SetTextI18n") - view?.text = " ${maxReading.toInt()}+$units" + view?.text = DecimalFormatter.to0Decimal(maxReading, units) view?.setTextColor(rh.gac(view.context, R.attr.defaultTextColor)) } else { handleLevel(view, criticalSetting, criticalDefaultValue, warnSetting, warnDefaultValue, level, units) @@ -129,11 +128,9 @@ class StatusLightHandler @Inject constructor( val therapyEvent = repository.getLastTherapyRecordUpToNow(type).blockingGet() var usage = 0.0 if (therapyEvent is ValueWrapper.Existing) { - val startTime = therapyEvent.value.timestamp - usage = repository.getBolusesDataFromTime(startTime, true).blockingGet() - .filter { it.type != Bolus.Type.PRIMING } - .sumOf { t -> t.amount } + val tdd = tddCalculator.calculate(therapyEvent.value.timestamp, dateUtil.now()) + usage = tdd.totalAmount } - view?.text = " " + DecimalFormatter.to0Decimal(usage) + units + view?.text = DecimalFormatter.to0Decimal(usage, units) } } diff --git a/plugins/main/src/main/res/layout/actions_stats_fragment.xml b/plugins/main/src/main/res/layout/actions_stats_fragment.xml index 1dda739d7c..f20d0c28bb 100644 --- a/plugins/main/src/main/res/layout/actions_stats_fragment.xml +++ b/plugins/main/src/main/res/layout/actions_stats_fragment.xml @@ -56,7 +56,7 @@ android:layout_height="wrap_content" android:layout_marginEnd="20dp" android:gravity="center_vertical" - android:paddingStart="2dp" + android:paddingStart="4dp" android:paddingEnd="0dp" android:textSize="14sp" /> @@ -128,7 +128,7 @@ android:layout_height="wrap_content" android:layout_marginEnd="20dp" android:gravity="center_vertical" - android:paddingStart="2dp" + android:paddingStart="4dp" android:paddingEnd="0dp" android:textSize="14sp" tools:text="15U" /> @@ -202,7 +202,7 @@ android:layout_height="wrap_content" android:layout_marginEnd="20dp" android:gravity="center_vertical" - android:paddingStart="2dp" + android:paddingStart="4dp" android:paddingEnd="0dp" android:textSize="14sp" tools:text="22U" /> @@ -277,7 +277,7 @@ android:layout_height="wrap_content" android:layout_marginEnd="20dp" android:gravity="center_vertical" - android:paddingStart="2dp" + android:paddingStart="4dp" android:paddingEnd="0dp" android:textSize="14sp" tools:text="70%" />