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 3956e67fb6..86259bd729 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 @@ -109,6 +109,7 @@ class OverviewFragment : DaggerFragment(), View.OnClickListener, OnLongClickList @Inject lateinit var fabricPrivacy: FabricPrivacy @Inject lateinit var overviewMenus: OverviewMenus @Inject lateinit var skinProvider: SkinProvider + @Inject lateinit var trendCalculator: TrendCalculator @Inject lateinit var config: Config @Inject lateinit var dateUtil: DateUtil @Inject lateinit var databaseHelper: DatabaseHelperInterface @@ -584,7 +585,7 @@ class OverviewFragment : DaggerFragment(), View.OnClickListener, OnLongClickList binding.infoLayout.bg.text = lastBG.valueToUnitsString(units) binding.infoLayout.bg.setTextColor(color) - binding.infoLayout.arrow.setImageResource(lastBG.trendArrow.directionToIcon()) + binding.infoLayout.arrow.setImageResource(trendCalculator.getTrendArrow(lastBG).directionToIcon()) binding.infoLayout.arrow.setColorFilter(color) val glucoseStatus = GlucoseStatus(injector).glucoseStatusData diff --git a/app/src/main/java/info/nightscout/androidaps/utils/TrendCalculator.kt b/app/src/main/java/info/nightscout/androidaps/utils/TrendCalculator.kt new file mode 100644 index 0000000000..ac7738043d --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/utils/TrendCalculator.kt @@ -0,0 +1,45 @@ +package info.nightscout.androidaps.utils + +import info.nightscout.androidaps.database.AppRepository +import info.nightscout.androidaps.database.entities.GlucoseValue +import javax.inject.Inject +import javax.inject.Singleton + +@Singleton +class TrendCalculator @Inject constructor( + private val repository: AppRepository +) { + + fun getTrendArrow(glucoseValue: GlucoseValue): GlucoseValue.TrendArrow = + if (glucoseValue.trendArrow != GlucoseValue.TrendArrow.NONE) glucoseValue.trendArrow + else calculateDirection(glucoseValue) + + private fun calculateDirection(glucoseValue: GlucoseValue): GlucoseValue.TrendArrow { + + val toTime = glucoseValue.timestamp + val readings = repository.compatGetBgReadingsDataFromTime(toTime - T.mins(10).msecs(), toTime, false).blockingGet() + + if (readings.size < 2) + return GlucoseValue.TrendArrow.NONE + val current = readings[0] + val previous = readings[1] + + // Avoid division by 0 + val slope = + if (current.timestamp == previous.timestamp) 0.0 + else (previous.value - current.value) / (previous.timestamp - current.timestamp) + + val slopeByMinute = slope * 60000 + + return when { + slopeByMinute <= -3.5 -> GlucoseValue.TrendArrow.DOUBLE_DOWN + slopeByMinute <= -2 -> GlucoseValue.TrendArrow.SINGLE_DOWN + slopeByMinute <= -1 -> GlucoseValue.TrendArrow.FORTY_FIVE_DOWN + slopeByMinute <= 1 -> GlucoseValue.TrendArrow.FLAT + slopeByMinute <= 2 -> GlucoseValue.TrendArrow.FORTY_FIVE_UP + slopeByMinute <= 3.5 -> GlucoseValue.TrendArrow.SINGLE_UP + slopeByMinute <= 40 -> GlucoseValue.TrendArrow.DOUBLE_UP + else -> GlucoseValue.TrendArrow.NONE + } + } +} \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/utils/GlucoseValueUtils.kt b/app/src/main/java/info/nightscout/androidaps/utils/extensions/GlucoseValueUtils.kt similarity index 100% rename from app/src/main/java/info/nightscout/androidaps/utils/GlucoseValueUtils.kt rename to app/src/main/java/info/nightscout/androidaps/utils/extensions/GlucoseValueUtils.kt