From 3040be317cbfd9e719fa6fdbf06bb910fe742af1 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Fri, 14 May 2021 21:45:48 +0200 Subject: [PATCH] TBR as live --- .../plugins/general/overview/OverviewData.kt | 65 +++++++++++++++++++ .../general/overview/OverviewFragment.kt | 55 +++++++--------- .../general/overview/OverviewPlugin.kt | 47 ++++++++++++-- .../overview/events/EventUpdateOverview.kt | 6 ++ 4 files changed, 136 insertions(+), 37 deletions(-) create mode 100644 app/src/main/java/info/nightscout/androidaps/plugins/general/overview/OverviewData.kt create mode 100644 app/src/main/java/info/nightscout/androidaps/plugins/general/overview/events/EventUpdateOverview.kt diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/OverviewData.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/OverviewData.kt new file mode 100644 index 0000000000..3ba7f417d4 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/OverviewData.kt @@ -0,0 +1,65 @@ +package info.nightscout.androidaps.plugins.general.overview + +import info.nightscout.androidaps.R +import info.nightscout.androidaps.database.entities.TemporaryBasal +import info.nightscout.androidaps.extensions.convertedToPercent +import info.nightscout.androidaps.extensions.toStringFull +import info.nightscout.androidaps.extensions.toStringShort +import info.nightscout.androidaps.interfaces.Profile +import info.nightscout.androidaps.utils.DateUtil +import info.nightscout.androidaps.utils.resources.ResourceHelper +import javax.inject.Inject +import javax.inject.Singleton + +@Singleton +class OverviewData @Inject constructor( + private val resourceHelper: ResourceHelper, + private val dateUtil: DateUtil +) { + + enum class Property { + PROFILE, + TEMPORARY_BASAL + } + + @get:Synchronized @set:Synchronized + var profile: Profile? = null + + @get:Synchronized @set:Synchronized + var profileName: String? = null + + var temporaryBasal: TemporaryBasal? = null + + val temporaryBasalText: String + get() = + profile?.let { profile -> + temporaryBasal?.let { "T:" + it.toStringShort() } + ?: resourceHelper.gs(R.string.pump_basebasalrate, profile.getBasal()) + } ?: resourceHelper.gs(R.string.notavailable) + + val temporaryBasalDialogText: String + get() = profile?.let { profile -> + temporaryBasal?.let { temporaryBasal -> + "${resourceHelper.gs(R.string.basebasalrate_label)}: ${resourceHelper.gs(R.string.pump_basebasalrate, profile.getBasal())}" + + "\n" + resourceHelper.gs(R.string.tempbasal_label) + ": " + temporaryBasal.toStringFull(profile, dateUtil) + } + ?: "${resourceHelper.gs(R.string.basebasalrate_label)}: ${resourceHelper.gs(R.string.pump_basebasalrate, profile.getBasal())}" + } ?: resourceHelper.gs(R.string.notavailable) + + val temporaryBasalIcon: Int + get() = + profile?.let { profile -> + temporaryBasal?.let { temporaryBasal -> + val percentRate = temporaryBasal.convertedToPercent(dateUtil.now(), profile) + when { + percentRate > 100 -> R.drawable.ic_cp_basal_tbr_high + percentRate < 100 -> R.drawable.ic_cp_basal_tbr_low + else -> R.drawable.ic_cp_basal_no_tbr + } + } + } ?: R.drawable.ic_cp_basal_no_tbr + + val temporaryBasalColor: Int + get() = temporaryBasal?.let { resourceHelper.gc(R.color.basal) } + ?: resourceHelper.gc(R.color.defaulttextcolor) +} \ No newline at end of file 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 9f34c84065..ea423a352f 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 @@ -46,6 +46,7 @@ import info.nightscout.androidaps.plugins.bus.RxBusWrapper import info.nightscout.androidaps.plugins.configBuilder.ConstraintChecker import info.nightscout.androidaps.plugins.general.nsclient.data.NSDeviceStatus import info.nightscout.androidaps.plugins.general.overview.activities.QuickWizardListActivity +import info.nightscout.androidaps.plugins.general.overview.events.EventUpdateOverview import info.nightscout.androidaps.plugins.general.overview.graphData.GraphData import info.nightscout.androidaps.plugins.general.overview.graphExtensions.GlucoseValueDataPoint import info.nightscout.androidaps.plugins.general.overview.notifications.NotificationStore @@ -115,6 +116,7 @@ class OverviewFragment : DaggerFragment(), View.OnClickListener, OnLongClickList @Inject lateinit var uel: UserEntryLogger @Inject lateinit var repository: AppRepository @Inject lateinit var glucoseStatusProvider: GlucoseStatusProvider + @Inject lateinit var overviewData: OverviewData private val disposable = CompositeDisposable() @@ -223,14 +225,14 @@ class OverviewFragment : DaggerFragment(), View.OnClickListener, OnLongClickList if (it.now) updateGUI(it.from) else scheduleUpdateGUI(it.from) }, fabricPrivacy::logException)) + disposable.add(rxBus + .toObservable(EventUpdateOverview::class.java) + .observeOn(aapsSchedulers.main) + .subscribe({ updateGUI(it.what) }, fabricPrivacy::logException)) disposable.add(rxBus .toObservable(EventExtendedBolusChange::class.java) .observeOn(aapsSchedulers.io) .subscribe({ scheduleUpdateGUI("EventExtendedBolusChange") }, fabricPrivacy::logException)) - disposable.add(rxBus - .toObservable(EventTempBasalChange::class.java) - .observeOn(aapsSchedulers.io) - .subscribe({ scheduleUpdateGUI("EventTempBasalChange") }, fabricPrivacy::logException)) disposable.add(rxBus .toObservable(EventTreatmentChange::class.java) .observeOn(aapsSchedulers.io) @@ -255,10 +257,6 @@ class OverviewFragment : DaggerFragment(), View.OnClickListener, OnLongClickList .toObservable(EventAutosensCalculationFinished::class.java) .observeOn(aapsSchedulers.io) .subscribe({ scheduleUpdateGUI("EventAutosensCalculationFinished") }, fabricPrivacy::logException)) - disposable.add(rxBus - .toObservable(EventProfileSwitchChanged::class.java) - .observeOn(aapsSchedulers.io) - .subscribe({ scheduleUpdateGUI("EventProfileNeedsUpdate") }, fabricPrivacy::logException)) disposable.add(rxBus .toObservable(EventPreferenceChange::class.java) .observeOn(aapsSchedulers.io) @@ -544,6 +542,22 @@ class OverviewFragment : DaggerFragment(), View.OnClickListener, OnLongClickList view?.postDelayed(task, 500) } + fun updateGUI(what: OverviewData.Property) { + when (what) { + OverviewData.Property.PROFILE -> TODO() + + OverviewData.Property.TEMPORARY_BASAL -> { + // Basal, TBR + binding.infoLayout.baseBasal.text = overviewData.temporaryBasalText + binding.infoLayout.baseBasal.setTextColor(overviewData.temporaryBasalColor) + binding.infoLayout.baseBasalIcon.setImageResource(overviewData.temporaryBasalIcon) + binding.infoLayout.basalLayout.setOnClickListener { + activity?.let { OKDialog.show(it, resourceHelper.gs(R.string.basal), overviewData.temporaryBasalDialogText) } + } + } + } + } + @SuppressLint("SetTextI18n") fun updateGUI(from: String) { if (_binding == null) return @@ -551,7 +565,7 @@ class OverviewFragment : DaggerFragment(), View.OnClickListener, OnLongClickList binding.infoLayout.time.text = dateUtil.timeString(dateUtil.now()) - if (!profileFunction.isProfileValid("Overview")) { + if (overviewData.profile == null) { binding.loopPumpStatusLayout.pumpStatus.setText(R.string.noprofileset) binding.loopPumpStatusLayout.pumpStatusLayout.visibility = View.VISIBLE binding.loopPumpStatusLayout.loopLayout.visibility = View.GONE @@ -561,7 +575,7 @@ class OverviewFragment : DaggerFragment(), View.OnClickListener, OnLongClickList binding.loopPumpStatusLayout.pumpStatusLayout.visibility = View.GONE binding.loopPumpStatusLayout.loopLayout.visibility = View.VISIBLE - val profile = profileFunction.getProfile() ?: return + val profile = overviewData.profile ?: return val actualBG = iobCobCalculator.ads.actualBg() val lastBG = iobCobCalculator.ads.lastBg() val pump = activePlugin.activePump @@ -701,27 +715,6 @@ class OverviewFragment : DaggerFragment(), View.OnClickListener, OnLongClickList } } - // Basal, TBR - val activeTemp = iobCobCalculator.getTempBasalIncludingConvertedExtended(System.currentTimeMillis()) - binding.infoLayout.baseBasal.text = activeTemp?.let { "T:" + activeTemp.toStringShort() } - ?: resourceHelper.gs(R.string.pump_basebasalrate, profile.getBasal()) - binding.infoLayout.basalLayout.setOnClickListener { - var fullText = "${resourceHelper.gs(R.string.basebasalrate_label)}: ${resourceHelper.gs(R.string.pump_basebasalrate, profile.getBasal())}" - if (activeTemp != null) - fullText += "\n" + resourceHelper.gs(R.string.tempbasal_label) + ": " + activeTemp.toStringFull(profile, dateUtil) - activity?.let { - OKDialog.show(it, resourceHelper.gs(R.string.basal), fullText) - } - } - binding.infoLayout.baseBasal.setTextColor(activeTemp?.let { resourceHelper.gc(R.color.basal) } - ?: resourceHelper.gc(R.color.defaulttextcolor)) - - binding.infoLayout.baseBasalIcon.setImageResource(R.drawable.ic_cp_basal_no_tbr) - val percentRate = activeTemp?.convertedToPercent(System.currentTimeMillis(), profile) - ?: 100 - if (percentRate > 100) binding.infoLayout.baseBasalIcon.setImageResource(R.drawable.ic_cp_basal_tbr_high) - if (percentRate < 100) binding.infoLayout.baseBasalIcon.setImageResource(R.drawable.ic_cp_basal_tbr_low) - // Extended bolus val extendedBolus = repository.getExtendedBolusActiveAt(dateUtil.now()).blockingGet() binding.infoLayout.extendedBolus.text = diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/OverviewPlugin.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/OverviewPlugin.kt index 8aadd2ea34..673cc9598f 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/OverviewPlugin.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/OverviewPlugin.kt @@ -3,19 +3,20 @@ package info.nightscout.androidaps.plugins.general.overview import androidx.preference.PreferenceFragmentCompat import androidx.preference.SwitchPreference import dagger.android.HasAndroidInjector -import info.nightscout.androidaps.interfaces.Config import info.nightscout.androidaps.R +import info.nightscout.androidaps.events.EventAppInitialized +import info.nightscout.androidaps.events.EventProfileSwitchChanged import info.nightscout.androidaps.events.EventRefreshOverview +import info.nightscout.androidaps.events.EventTempBasalChange import info.nightscout.androidaps.extensions.* -import info.nightscout.androidaps.interfaces.Overview -import info.nightscout.androidaps.interfaces.PluginBase -import info.nightscout.androidaps.interfaces.PluginDescription -import info.nightscout.androidaps.interfaces.PluginType +import info.nightscout.androidaps.interfaces.* import info.nightscout.androidaps.logging.AAPSLogger import info.nightscout.androidaps.plugins.bus.RxBusWrapper import info.nightscout.androidaps.plugins.general.overview.events.EventDismissNotification import info.nightscout.androidaps.plugins.general.overview.events.EventNewNotification +import info.nightscout.androidaps.plugins.general.overview.events.EventUpdateOverview import info.nightscout.androidaps.plugins.general.overview.notifications.NotificationStore +import info.nightscout.androidaps.utils.DateUtil import info.nightscout.androidaps.utils.FabricPrivacy import info.nightscout.androidaps.utils.resources.ResourceHelper import info.nightscout.androidaps.utils.rx.AapsSchedulers @@ -36,7 +37,11 @@ class OverviewPlugin @Inject constructor( aapsLogger: AAPSLogger, private val aapsSchedulers: AapsSchedulers, resourceHelper: ResourceHelper, - private val config: Config + private val config: Config, + private val dateUtil: DateUtil, + private val profileFunction: ProfileFunction, + private val iobCobCalculator: IobCobCalculator, + private val overviewData: OverviewData ) : PluginBase(PluginDescription() .mainType(PluginType.GENERAL) .fragmentClass(OverviewFragment::class.qualifiedName) @@ -69,6 +74,18 @@ class OverviewPlugin @Inject constructor( if (notificationStore.remove(n.id)) rxBus.send(EventRefreshOverview("EventDismissNotification")) }, fabricPrivacy::logException) + disposable += rxBus + .toObservable(EventAppInitialized::class.java) + .observeOn(aapsSchedulers.io) + .subscribe( { loadAll() }, fabricPrivacy::logException) + disposable += rxBus + .toObservable(EventTempBasalChange::class.java) + .observeOn(aapsSchedulers.io) + .subscribe( { loadTemporaryBasal() }, fabricPrivacy::logException) + disposable.add(rxBus + .toObservable(EventProfileSwitchChanged::class.java) + .observeOn(aapsSchedulers.io) + .subscribe({ loadProfile() }, fabricPrivacy::logException)) } override fun onStop() { @@ -142,4 +159,22 @@ class OverviewPlugin @Inject constructor( .storeDouble(R.string.key_statuslights_bat_warning, sp, resourceHelper) .storeDouble(R.string.key_statuslights_bat_critical, sp, resourceHelper) } + + private fun loadAll() { + loadProfile() + loadTemporaryBasal() + } + + @Synchronized + private fun loadProfile() { + overviewData.profile = profileFunction.getProfile() + overviewData.profileName = profileFunction.getProfileName() + rxBus.send(EventUpdateOverview(OverviewData.Property.PROFILE)) + } + + @Synchronized + private fun loadTemporaryBasal() { + overviewData.temporaryBasal = iobCobCalculator.getTempBasalIncludingConvertedExtended(dateUtil.now()) + rxBus.send(EventUpdateOverview(OverviewData.Property.TEMPORARY_BASAL)) + } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/events/EventUpdateOverview.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/events/EventUpdateOverview.kt new file mode 100644 index 0000000000..4bf8cc82e8 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/events/EventUpdateOverview.kt @@ -0,0 +1,6 @@ +package info.nightscout.androidaps.plugins.general.overview.events + +import info.nightscout.androidaps.events.Event +import info.nightscout.androidaps.plugins.general.overview.OverviewData + +class EventUpdateOverview(val what: OverviewData.Property) : Event() \ No newline at end of file