From 103e07648460bc528d3ff37fec840061eafdbb5e Mon Sep 17 00:00:00 2001 From: osodebailar Date: Mon, 28 Mar 2022 12:39:37 +0200 Subject: [PATCH] Theme switcher preparation: gac for overview and historybrowser --- .../activities/HistoryBrowseActivity.kt | 6 +-- .../plugins/general/overview/OverviewData.kt | 12 +++-- .../general/overview/OverviewFragment.kt | 50 +++++++++---------- .../utils/resources/ResourceHelper.kt | 2 +- .../resources/ResourceHelperImplementation.kt | 4 +- core/src/main/res/values-night/styles.xml | 15 ++++++ core/src/main/res/values/attrs.xml | 16 +++++- core/src/main/res/values/colors.xml | 2 +- core/src/main/res/values/styles.xml | 15 ++++++ 9 files changed, 84 insertions(+), 38 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/activities/HistoryBrowseActivity.kt b/app/src/main/java/info/nightscout/androidaps/activities/HistoryBrowseActivity.kt index a9bc13f1c3..006732d5aa 100644 --- a/app/src/main/java/info/nightscout/androidaps/activities/HistoryBrowseActivity.kt +++ b/app/src/main/java/info/nightscout/androidaps/activities/HistoryBrowseActivity.kt @@ -196,7 +196,7 @@ class HistoryBrowseActivity : NoSplashAppCompatActivity() { axisWidth = if (dm.densityDpi <= 120) 3 else if (dm.densityDpi <= 160) 10 else if (dm.densityDpi <= 320) 35 else if (dm.densityDpi <= 420) 50 else if (dm.densityDpi <= 560) 70 else 80 - binding.bgGraph.gridLabelRenderer?.gridColor = rh.gc(R.color.graphgrid) + binding.bgGraph.gridLabelRenderer?.gridColor = rh.gac(this, R.attr.graphgrid) binding.bgGraph.gridLabelRenderer?.reloadStyles() binding.bgGraph.gridLabelRenderer?.labelVerticalWidth = axisWidth @@ -280,12 +280,12 @@ class HistoryBrowseActivity : NoSplashAppCompatActivity() { val graph = GraphView(this) graph.layoutParams = LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, rh.dpToPx(100)).also { it.setMargins(0, rh.dpToPx(15), 0, rh.dpToPx(10)) } - graph.gridLabelRenderer?.gridColor = rh.gc(R.color.graphgrid) + graph.gridLabelRenderer?.gridColor = rh.gac( R.attr.graphgrid) graph.gridLabelRenderer?.reloadStyles() graph.gridLabelRenderer?.isHorizontalLabelsVisible = false graph.gridLabelRenderer?.labelVerticalWidth = axisWidth graph.gridLabelRenderer?.numVerticalLabels = 3 - graph.viewport.backgroundColor = Color.argb(20, 255, 255, 255) // 8% of gray + graph.viewport.backgroundColor =rh.gac(this , R.attr.viewPortbackgroundColor) relativeLayout.addView(graph) val label = TextView(this) 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 index 1f2867d5cc..b64d30970b 100644 --- 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 @@ -1,5 +1,6 @@ package info.nightscout.androidaps.plugins.general.overview +import android.content.Context import android.graphics.DashPathEffect import android.graphics.Paint import com.jjoe64.graphview.series.BarGraphSeries @@ -138,12 +139,13 @@ class OverviewData @Inject constructor( lastBg.valueToUnits(profileFunction.getUnits()) > defaultValueHelper.determineHighLine() } ?: false - val lastBgColor: Int - get() = when { - isLow -> rh.gc(R.color.low) - isHigh -> rh.gc(R.color.high) - else -> rh.gc(R.color.inrange) + fun lastBgColor(context: Context?): Int { + return when { + isLow -> rh.gac(context, R.attr.bgLow) + isHigh -> rh.gac(context, R.attr.bgHigh) + else -> rh.gac(context, R.attr.bgInRange) } + } val lastBgDescription: String get() = when { 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 07c402a0e7..eb2333c3eb 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 @@ -173,7 +173,7 @@ class OverviewFragment : DaggerFragment(), View.OnClickListener, OnLongClickList binding.notifications.setHasFixedSize(false) binding.notifications.layoutManager = LinearLayoutManager(view.context) axisWidth = if (dm.densityDpi <= 120) 3 else if (dm.densityDpi <= 160) 10 else if (dm.densityDpi <= 320) 35 else if (dm.densityDpi <= 420) 50 else if (dm.densityDpi <= 560) 70 else 80 - binding.graphsLayout.bgGraph.gridLabelRenderer?.gridColor = rh.gc(R.color.graphgrid) + binding.graphsLayout.bgGraph.gridLabelRenderer?.gridColor = rh.gac(context , R.attr.graphgrid) binding.graphsLayout.bgGraph.gridLabelRenderer?.reloadStyles() binding.graphsLayout.bgGraph.gridLabelRenderer?.labelVerticalWidth = axisWidth binding.graphsLayout.bgGraph.layoutParams?.height = rh.dpToPx(skinProvider.activeSkin().mainGraphHeight) @@ -559,10 +559,10 @@ class OverviewFragment : DaggerFragment(), View.OnClickListener, OnLongClickList binding.buttonsLayout.calibrationButton.visibility = (xDripIsBgSource && actualBG != null && sp.getBoolean(R.string.key_show_calibration_button, true)).toVisibility() if (dexcomIsSource) { binding.buttonsLayout.cgmButton.setCompoundDrawablesWithIntrinsicBounds(null, rh.gd(R.drawable.ic_byoda), null, null) - binding.buttonsLayout.cgmButton.setTextColor(rh.gc(R.color.colorLightGray)) + binding.buttonsLayout.cgmButton.setTextColor(rh.gac(context, R.attr.cgmdexColor)) } else if (xDripIsBgSource) { binding.buttonsLayout.cgmButton.setCompoundDrawablesWithIntrinsicBounds(null, rh.gd(R.drawable.ic_xdrip), null, null) - binding.buttonsLayout.cgmButton.setTextColor(rh.gc(R.color.colorCalibrationButton)) + binding.buttonsLayout.cgmButton.setTextColor(rh.gac(context, R.attr.cgmxdripColor)) } binding.buttonsLayout.cgmButton.visibility = (sp.getBoolean(R.string.key_show_cgm_button, false) && (xDripIsBgSource || dexcomIsSource)).toVisibility() @@ -574,7 +574,7 @@ class OverviewFragment : DaggerFragment(), View.OnClickListener, OnLongClickList if (event.isEnabled && event.trigger.shouldRun()) context?.let { context -> SingleClickButton(context).also { - it.setTextColor(rh.gc(R.color.colorTreatmentButton)) + it.setTextColor(rh.gac(context, R.attr.treatmentButton)) it.setTextSize(TypedValue.COMPLEX_UNIT_SP, 10f) it.layoutParams = LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT, 0.5f).also { l -> l.setMargins(0, 0, rh.dpToPx(-4), 0) @@ -722,12 +722,12 @@ class OverviewFragment : DaggerFragment(), View.OnClickListener, OnLongClickList val graph = GraphView(context) graph.layoutParams = LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, rh.dpToPx(skinProvider.activeSkin().secondaryGraphHeight)).also { it.setMargins(0, rh.dpToPx(15), 0, rh.dpToPx(10)) } - graph.gridLabelRenderer?.gridColor = rh.gc(R.color.graphgrid) + graph.gridLabelRenderer?.gridColor = rh.gac(context , R.attr.graphgrid) graph.gridLabelRenderer?.reloadStyles() graph.gridLabelRenderer?.isHorizontalLabelsVisible = false graph.gridLabelRenderer?.labelVerticalWidth = axisWidth graph.gridLabelRenderer?.numVerticalLabels = 3 - graph.viewport.backgroundColor = Color.argb(20, 255, 255, 255) // 8% of gray + graph.viewport.backgroundColor = rh.gac(context , R.attr.viewPortbackgroundColor) relativeLayout.addView(graph) val label = TextView(context) @@ -765,15 +765,15 @@ class OverviewFragment : DaggerFragment(), View.OnClickListener, OnLongClickList val units = profileFunction.getUnits() binding.infoLayout.bg.text = overviewData.lastBg?.valueToUnitsString(units) ?: rh.gs(R.string.notavailable) - binding.infoLayout.bg.setTextColor(overviewData.lastBgColor) + binding.infoLayout.bg.setTextColor(overviewData.lastBgColor(context)) binding.infoLayout.arrow.setImageResource(trendCalculator.getTrendArrow(overviewData.lastBg).directionToIcon()) - binding.infoLayout.arrow.setColorFilter(overviewData.lastBgColor) + binding.infoLayout.arrow.setColorFilter(overviewData.lastBgColor(context)) binding.infoLayout.arrow.contentDescription = overviewData.lastBgDescription + " " + rh.gs(R.string.and) + " " + trendCalculator.getTrendDescription(overviewData.lastBg) val glucoseStatus = glucoseStatusProvider.glucoseStatusData if (glucoseStatus != null) { binding.infoLayout.deltaLarge.text = Profile.toSignedUnitsString(glucoseStatus.delta, glucoseStatus.delta * Constants.MGDL_TO_MMOLL, units) - binding.infoLayout.deltaLarge.setTextColor(overviewData.lastBgColor) + binding.infoLayout.deltaLarge.setTextColor(overviewData.lastBgColor(context)) binding.infoLayout.delta.text = Profile.toSignedUnitsString(glucoseStatus.delta, glucoseStatus.delta * Constants.MGDL_TO_MMOLL, units) binding.infoLayout.avgDelta.text = Profile.toSignedUnitsString(glucoseStatus.shortAvgDelta, glucoseStatus.shortAvgDelta * Constants.MGDL_TO_MMOLL, units) binding.infoLayout.longAvgDelta.text = Profile.toSignedUnitsString(glucoseStatus.longAvgDelta, glucoseStatus.longAvgDelta * Constants.MGDL_TO_MMOLL, units) @@ -816,27 +816,27 @@ class OverviewFragment : DaggerFragment(), View.OnClickListener, OnLongClickList profileFunction.getProfile()?.let { if (it is ProfileSealed.EPS) { if (it.value.originalPercentage != 100 || it.value.originalTimeshift != 0L || it.value.originalDuration != 0L) - rh.gc(R.color.ribbonWarning) - else rh.gc(R.color.ribbonDefault) + rh.gac(context, R.attr.ribbonWarningColor) + else rh.gac(context, R.attr.ribbonDefaultColor) } else if (it is ProfileSealed.PS) { - rh.gc(R.color.ribbonDefault) + rh.gac(context, R.attr.ribbonDefaultColor) } else { - rh.gc(R.color.ribbonDefault) + rh.gac(context, R.attr.ribbonDefaultColor) } - } ?: rh.gc(R.color.ribbonCritical) + } ?: rh.gac(context, R.attr.ribbonCriticalColor) val profileTextColor = profileFunction.getProfile()?.let { if (it is ProfileSealed.EPS) { if (it.value.originalPercentage != 100 || it.value.originalTimeshift != 0L || it.value.originalDuration != 0L) - rh.gc(R.color.ribbonTextWarning) - else rh.gc(R.color.ribbonTextDefault) + rh.gac(context, R.attr.ribbonTextWarningColor) + else rh.gac(context, R.attr.ribbonTextDefaultColor) } else if (it is ProfileSealed.PS) { - rh.gc(R.color.ribbonTextDefault) + rh.gac(context, R.attr.ribbonTextDefaultColor) } else { - rh.gc(R.color.ribbonTextDefault) + rh.gac(context, R.attr.ribbonTextDefaultColor) } - } ?: rh.gc(R.color.ribbonTextDefault) + } ?: rh.gac(context, R.attr.ribbonTextDefaultColor) binding.activeProfile.text = profileFunction.getProfileNameWithRemainingTime() binding.activeProfile.setBackgroundColor(profileBackgroundColor) @@ -929,8 +929,8 @@ class OverviewFragment : DaggerFragment(), View.OnClickListener, OnLongClickList if (overviewData.temporaryTarget?.isInProgress(dateUtil) == false) overviewData.temporaryTarget = null val tempTarget = overviewData.temporaryTarget if (tempTarget != null) { - binding.tempTarget.setTextColor(rh.gc(R.color.ribbonTextWarning)) - binding.tempTarget.setBackgroundColor(rh.gc(R.color.ribbonWarning)) + binding.tempTarget.setTextColor(rh.gac(context, R.attr.ribbonTextWarningColor)) + binding.tempTarget.setBackgroundColor(rh.gac(context, R.attr.ribbonWarningColor)) binding.tempTarget.text = Profile.toTargetRangeString(tempTarget.lowTarget, tempTarget.highTarget, GlucoseUnit.MGDL, units) + " " + dateUtil.untilString(tempTarget.end, rh) } else { // If the target is not the same as set in the profile then oref has overridden it @@ -940,11 +940,11 @@ class OverviewFragment : DaggerFragment(), View.OnClickListener, OnLongClickList if (targetUsed != 0.0 && abs(profile.getTargetMgdl() - targetUsed) > 0.01) { aapsLogger.debug("Adjusted target. Profile: ${profile.getTargetMgdl()} APS: $targetUsed") binding.tempTarget.text = Profile.toTargetRangeString(targetUsed, targetUsed, GlucoseUnit.MGDL, units) - binding.tempTarget.setTextColor(rh.gc(R.color.ribbonTextWarning)) - binding.tempTarget.setBackgroundColor(rh.gc(R.color.tempTargetBackground)) + binding.tempTarget.setTextColor(rh.gac(context, R.attr.ribbonTextWarningColor)) + binding.tempTarget.setBackgroundColor(rh.gac(context, R.attr.tempTargetBackgroundColor)) } else { - binding.tempTarget.setTextColor(rh.gc(R.color.ribbonTextDefault)) - binding.tempTarget.setBackgroundColor(rh.gc(R.color.ribbonDefault)) + binding.tempTarget.setTextColor(rh.gac(context, R.attr.ribbonTextDefaultColor)) + binding.tempTarget.setBackgroundColor(rh.gac(context, R.attr.ribbonDefaultColor)) binding.tempTarget.text = Profile.toTargetRangeString(profile.getTargetLowMgdl(), profile.getTargetHighMgdl(), GlucoseUnit.MGDL, units) } } diff --git a/core/src/main/java/info/nightscout/androidaps/utils/resources/ResourceHelper.kt b/core/src/main/java/info/nightscout/androidaps/utils/resources/ResourceHelper.kt index bdfdb76a92..375243e597 100644 --- a/core/src/main/java/info/nightscout/androidaps/utils/resources/ResourceHelper.kt +++ b/core/src/main/java/info/nightscout/androidaps/utils/resources/ResourceHelper.kt @@ -32,5 +32,5 @@ interface ResourceHelper { /** * Get Attribute Color based on theme style for specified context */ - @ColorInt fun gac(context: Context, @AttrRes attributeId: Int): Int + @ColorInt fun gac(context: Context?, @AttrRes attributeId: Int): Int } diff --git a/core/src/main/java/info/nightscout/androidaps/utils/resources/ResourceHelperImplementation.kt b/core/src/main/java/info/nightscout/androidaps/utils/resources/ResourceHelperImplementation.kt index 0c6cd537b9..d7e6d59402 100644 --- a/core/src/main/java/info/nightscout/androidaps/utils/resources/ResourceHelperImplementation.kt +++ b/core/src/main/java/info/nightscout/androidaps/utils/resources/ResourceHelperImplementation.kt @@ -85,8 +85,8 @@ class ResourceHelperImplementation @Inject constructor(private val context: Cont override fun shortTextMode(): Boolean = !gb(R.bool.isTablet) - override fun gac(context: Context, attributeId: Int): Int = - context.getThemeColor(attributeId) + override fun gac(context: Context?, attributeId: Int): Int = + (context ?: ContextThemeWrapper(this.context, R.style.AppTheme)).getThemeColor(attributeId) override fun gac(attributeId: Int): Int = ContextThemeWrapper(this.context, R.style.AppTheme).getThemeColor(attributeId) diff --git a/core/src/main/res/values-night/styles.xml b/core/src/main/res/values-night/styles.xml index c536f319ec..c048c019cc 100644 --- a/core/src/main/res/values-night/styles.xml +++ b/core/src/main/res/values-night/styles.xml @@ -92,6 +92,10 @@ @color/activity_title_background @color/ribbonDefault + @color/ribbonWarning + @color/ribbonCritical + @color/ribbonTextDefault + @color/ribbonTextWarning @color/tabSelectedText @color/tabText @@ -115,6 +119,17 @@ @color/black 12dp + + @color/graphgrid + @color/white_alpha_20 + @color/tempTargetBackground + + @color/colorLightGray + @color/colorCalibrationButton + + @color/low + @color/high + @color/inrange