From 751cc0bbe8861f68a2b730e9355a6cd15f4705b0 Mon Sep 17 00:00:00 2001 From: osodebailar Date: Mon, 11 Apr 2022 17:48:25 +0200 Subject: [PATCH] fix ratio color in light theme and make graph colors changable by theme change --- .../plugins/general/overview/OverviewMenus.kt | 28 ++++++------ .../workflow/PrepareBasalDataWorker.kt | 12 ++--- .../PrepareIobAutosensGraphDataWorker.kt | 45 ++++++++++--------- .../PrepareTemporaryTargetDataWorker.kt | 6 ++- .../utils/resources/ResourceHelper.kt | 4 ++ .../resources/ResourceHelperImplementation.kt | 17 ++++++- core/src/main/res/values-night/styles.xml | 10 +++++ core/src/main/res/values/attrs.xml | 10 +++++ core/src/main/res/values/colors.xml | 2 +- core/src/main/res/values/styles.xml | 14 +++++- 10 files changed, 101 insertions(+), 47 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/OverviewMenus.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/OverviewMenus.kt index e0f2ace989..78990dce99 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/OverviewMenus.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/OverviewMenus.kt @@ -34,18 +34,18 @@ class OverviewMenus @Inject constructor( private val loop: Loop, private val config: Config ) { - enum class CharType(@StringRes val nameId: Int, @AttrRes val attrId: Int, val primary: Boolean, val secondary: Boolean, @StringRes val shortnameId: Int) { - PRE(R.string.overview_show_predictions, R.attr.predictionColor, primary = true, secondary = false, shortnameId = R.string.prediction_shortname), - TREAT(R.string.overview_show_treatments, R.attr.predictionColor, primary = true, secondary = false, shortnameId = R.string.treatments_shortname), - BAS(R.string.overview_show_basals, R.attr.basal, primary = true, secondary = false,shortnameId = R.string.basal_shortname), - ABS(R.string.overview_show_absinsulin, R.attr.iobColor, primary = false, secondary = true,shortnameId = R.string.abs_insulin_shortname), - IOB(R.string.overview_show_iob, R.attr.iobColor, primary = false, secondary = true,shortnameId = R.string.iob), - COB(R.string.overview_show_cob, R.attr.cobColor, primary = false, secondary = true,shortnameId = R.string.cob), - DEV(R.string.overview_show_deviations, R.attr.bgiColor, primary = false, secondary = true,shortnameId = R.string.deviation_shortname), - BGI(R.string.overview_show_bgi, R.attr.bgiColor, primary = false, secondary = true,shortnameId = R.string.bgi_shortname), - SEN(R.string.overview_show_sensitivity, R.attr.ratioColor, primary = false, secondary = true,shortnameId = R.string.sensitivity_shortname), - ACT(R.string.overview_show_activity, R.attr.activityColor, primary = true, secondary = false,shortnameId = R.string.activity_shortname), - DEVSLOPE(R.string.overview_show_deviationslope, R.attr.devslopeposColor, primary = false, secondary = true,shortnameId = R.string.devslope_shortname) + enum class CharType(@StringRes val nameId: Int, @AttrRes val attrId: Int, @AttrRes val attrTextId: Int, val primary: Boolean, val secondary: Boolean, @StringRes val shortnameId: Int) { + PRE(R.string.overview_show_predictions, R.attr.predictionColor, R.attr.menuTextColor, primary = true, secondary = false, shortnameId = R.string.prediction_shortname), + TREAT(R.string.overview_show_treatments, R.attr.predictionColor, R.attr.menuTextColor, primary = true, secondary = false, shortnameId = R.string.treatments_shortname), + BAS(R.string.overview_show_basals, R.attr.basal, R.attr.menuTextColor, primary = true, secondary = false,shortnameId = R.string.basal_shortname), + ABS(R.string.overview_show_absinsulin, R.attr.iobColor, R.attr.menuTextColor, primary = false, secondary = true,shortnameId = R.string.abs_insulin_shortname), + IOB(R.string.overview_show_iob, R.attr.iobColor, R.attr.menuTextColor, primary = false, secondary = true,shortnameId = R.string.iob), + COB(R.string.overview_show_cob, R.attr.cobColor, R.attr.menuTextColor, primary = false, secondary = true,shortnameId = R.string.cob), + DEV(R.string.overview_show_deviations, R.attr.bgiColor, R.attr.menuTextColor, primary = false, secondary = true,shortnameId = R.string.deviation_shortname), + BGI(R.string.overview_show_bgi, R.attr.bgiColor, R.attr.menuTextColor, primary = false, secondary = true,shortnameId = R.string.bgi_shortname), + SEN(R.string.overview_show_sensitivity, R.attr.ratioColor, R.attr.menuTextColorInverse, primary = false, secondary = true,shortnameId = R.string.sensitivity_shortname), + ACT(R.string.overview_show_activity, R.attr.activityColor, R.attr.menuTextColor, primary = true, secondary = false,shortnameId = R.string.activity_shortname), + DEVSLOPE(R.string.overview_show_deviationslope, R.attr.devslopeposColor, R.attr.menuTextColor, primary = false, secondary = true,shortnameId = R.string.devslope_shortname) } companion object { @@ -122,8 +122,8 @@ class OverviewMenus @Inject constructor( if (insert) { val item = popup.menu.add(Menu.NONE, m.ordinal + 100 * (g + 1), Menu.NONE, rh.gs(m.nameId)) val title = item.title - val s = SpannableString(title) - s.setSpan(ForegroundColorSpan(rh.gc(R.color.black)), 0, s.length, 0) + val s = SpannableString(" " + title + " ") + s.setSpan(ForegroundColorSpan(rh.gac(context, m.attrTextId)), 0, s.length, 0) s.setSpan(BackgroundColorSpan(rh.gac(context, m.attrId)), 0, s.length, 0) item.title = s item.isCheckable = true diff --git a/app/src/main/java/info/nightscout/androidaps/workflow/PrepareBasalDataWorker.kt b/app/src/main/java/info/nightscout/androidaps/workflow/PrepareBasalDataWorker.kt index 8d47b1a675..d69bde9668 100644 --- a/app/src/main/java/info/nightscout/androidaps/workflow/PrepareBasalDataWorker.kt +++ b/app/src/main/java/info/nightscout/androidaps/workflow/PrepareBasalDataWorker.kt @@ -3,6 +3,7 @@ package info.nightscout.androidaps.workflow import android.content.Context import android.graphics.DashPathEffect import android.graphics.Paint +import androidx.core.content.ContextCompat import androidx.work.Worker import androidx.work.WorkerParameters import androidx.work.workDataOf @@ -30,9 +31,10 @@ class PrepareBasalDataWorker( @Inject lateinit var profileFunction: ProfileFunction @Inject lateinit var rh: ResourceHelper @Inject lateinit var rxBus: RxBus - + var ctx: Context init { (context.applicationContext as HasAndroidInjector).androidInjector().inject(this) + ctx = rh.getThemedCtx(context) } class PrepareBasalData( @@ -116,12 +118,12 @@ class PrepareBasalDataWorker( // create series data.overviewData.baseBasalGraphSeries = LineGraphSeries(Array(baseBasalArray.size) { i -> baseBasalArray[i] }).also { it.isDrawBackground = true - it.backgroundColor = rh.gc(R.color.basebasal) + it.backgroundColor = rh.gac(ctx, R.attr.basebasalColor ) it.thickness = 0 } data.overviewData.tempBasalGraphSeries = LineGraphSeries(Array(tempBasalArray.size) { i -> tempBasalArray[i] }).also { it.isDrawBackground = true - it.backgroundColor = rh.gc(R.color.tempbasal) + it.backgroundColor = rh.gac(ctx, R.attr.tempBasalColor ) it.thickness = 0 } data.overviewData.basalLineGraphSeries = LineGraphSeries(Array(basalLineArray.size) { i -> basalLineArray[i] }).also { @@ -129,14 +131,14 @@ class PrepareBasalDataWorker( paint.style = Paint.Style.STROKE paint.strokeWidth = rh.getDisplayMetrics().scaledDensity * 2 paint.pathEffect = DashPathEffect(floatArrayOf(2f, 4f), 0f) - paint.color = rh.gc(R.color.basal) + paint.color = rh.gac(ctx, R.attr.basal ) }) } data.overviewData.absoluteBasalGraphSeries = LineGraphSeries(Array(absoluteBasalLineArray.size) { i -> absoluteBasalLineArray[i] }).also { it.setCustomPaint(Paint().also { absolutePaint -> absolutePaint.style = Paint.Style.STROKE absolutePaint.strokeWidth = rh.getDisplayMetrics().scaledDensity * 2 - absolutePaint.color = rh.gc(R.color.basal) + absolutePaint.color =rh.gac(ctx, R.attr.basal ) }) } rxBus.send(EventIobCalculationProgress(CalculationWorkflow.ProgressData.PREPARE_BASAL_DATA, 100, null)) diff --git a/app/src/main/java/info/nightscout/androidaps/workflow/PrepareIobAutosensGraphDataWorker.kt b/app/src/main/java/info/nightscout/androidaps/workflow/PrepareIobAutosensGraphDataWorker.kt index 606258106c..cc81bf3593 100644 --- a/app/src/main/java/info/nightscout/androidaps/workflow/PrepareIobAutosensGraphDataWorker.kt +++ b/app/src/main/java/info/nightscout/androidaps/workflow/PrepareIobAutosensGraphDataWorker.kt @@ -30,6 +30,7 @@ import info.nightscout.androidaps.receivers.DataWorker import info.nightscout.androidaps.utils.DateUtil import info.nightscout.androidaps.utils.DecimalFormatter import info.nightscout.androidaps.utils.resources.ResourceHelper +import info.nightscout.androidaps.utils.resources.getThemeColor import info.nightscout.shared.logging.AAPSLogger import info.nightscout.shared.logging.LTag import java.util.ArrayList @@ -51,9 +52,10 @@ class PrepareIobAutosensGraphDataWorker( @Inject lateinit var aapsLogger: AAPSLogger @Inject lateinit var repository: AppRepository @Inject lateinit var rxBus: RxBus - + var ctx: Context init { (context.applicationContext as HasAndroidInjector).androidInjector().inject(this) + ctx = rh.getThemedCtx(context) } class PrepareIobAutosensData( @@ -64,7 +66,6 @@ class PrepareIobAutosensGraphDataWorker( override fun doWork(): Result { val data = dataWorker.pickupObject(inputData.getLong(DataWorker.STORE_KEY, -1)) as PrepareIobAutosensData? ?: return Result.failure(workDataOf("Error" to "missing input data")) - rxBus.send(EventIobCalculationProgress(CalculationWorkflow.ProgressData.PREPARE_IOB_AUTOSENS_DATA, 0, null)) val iobArray: MutableList = ArrayList() val absIobArray: MutableList = ArrayList() @@ -158,15 +159,15 @@ class PrepareIobAutosensGraphDataWorker( // DEVIATIONS if (autosensData != null) { - var color = rh.gc(R.color.deviationblack) // "=" + var color = rh.gac( ctx, R.attr.deviationblackColor) // "=" if (autosensData.type == "" || autosensData.type == "non-meal") { - if (autosensData.pastSensitivity == "C") color = rh.gc(R.color.deviationgrey) - if (autosensData.pastSensitivity == "+") color = rh.gc(R.color.deviationgreen) - if (autosensData.pastSensitivity == "-") color = rh.gc(R.color.deviationred) + if (autosensData.pastSensitivity == "C") color = rh.gac( ctx, R.attr.deviationgreyColor) + if (autosensData.pastSensitivity == "+") color = rh.gac( ctx, R.attr.deviationgreenColor) + if (autosensData.pastSensitivity == "-") color = rh.gac( ctx, R.attr.deviationredColor) } else if (autosensData.type == "uam") { - color = rh.gc(R.color.uam) + color = rh.gac( ctx, R.attr.uamColor) } else if (autosensData.type == "csf") { - color = rh.gc(R.color.deviationgrey) + color = rh.gac( ctx, R.attr.deviationgreyColor) } devArray.add(OverviewPlugin.DeviationDataPoint(time.toDouble(), autosensData.deviation, color, data.overviewData.devScale)) data.overviewData.maxDevValueFound = maxOf(data.overviewData.maxDevValueFound, abs(autosensData.deviation), abs(bgi)) @@ -192,14 +193,14 @@ class PrepareIobAutosensGraphDataWorker( // IOB data.overviewData.iobSeries = FixedLineGraphSeries(Array(iobArray.size) { i -> iobArray[i] }).also { it.isDrawBackground = true - it.backgroundColor = -0x7f000001 and rh.gc(R.color.iob) //50% - it.color = rh.gc(R.color.iob) + it.backgroundColor = -0x7f000001 and rh.gac( ctx, R.attr.iobColor) //50% + it.color = rh.gac( ctx, R.attr.iobColor) it.thickness = 3 } data.overviewData.absIobSeries = FixedLineGraphSeries(Array(absIobArray.size) { i -> absIobArray[i] }).also { it.isDrawBackground = true - it.backgroundColor = -0x7f000001 and rh.gc(R.color.iob) //50% - it.color = rh.gc(R.color.iob) + it.backgroundColor = -0x7f000001 and rh.gac( ctx, R.attr.iobColor) //50% + it.color = rh.gac( ctx, R.attr.iobColor) it.thickness = 3 } @@ -210,7 +211,7 @@ class PrepareIobAutosensGraphDataWorker( val iobPrediction: MutableList = ArrayList() val iobPredictionArray = data.iobCobCalculator.calculateIobArrayForSMB(lastAutosensResult, SMBDefaults.exercise_mode, SMBDefaults.half_basal_exercise_target, isTempTarget) for (i in iobPredictionArray) { - iobPrediction.add(i.setColor(rh.gc(R.color.iobPredAS))) + iobPrediction.add(i.setColor(rh.gac( ctx, R.attr.iobPredASColor))) data.overviewData.maxIobValueFound = max(data.overviewData.maxIobValueFound, abs(i.iob)) } data.overviewData.iobPredictions1Series = PointsWithLabelGraphSeries(Array(iobPrediction.size) { i -> iobPrediction[i] }) @@ -222,8 +223,8 @@ class PrepareIobAutosensGraphDataWorker( // COB data.overviewData.cobSeries = FixedLineGraphSeries(Array(cobArray.size) { i -> cobArray[i] }).also { it.isDrawBackground = true - it.backgroundColor = -0x7f000001 and rh.gc(R.color.cob) //50% - it.color = rh.gc(R.color.cob) + it.backgroundColor = -0x7f000001 and rh.gac( ctx, R.attr.cobColor) //50% + it.color = rh.gac( ctx, R.attr.cobColor) it.thickness = 3 } data.overviewData.cobMinFailOverSeries = PointsWithLabelGraphSeries(Array(minFailOverActiveList.size) { i -> minFailOverActiveList[i] }) @@ -231,7 +232,7 @@ class PrepareIobAutosensGraphDataWorker( // ACTIVITY data.overviewData.activitySeries = FixedLineGraphSeries(Array(actArrayHist.size) { i -> actArrayHist[i] }).also { it.isDrawBackground = false - it.color = rh.gc(R.color.activity) + it.color = rh.gac( ctx, R.attr.activityColor) it.thickness = 3 } data.overviewData.activityPredictionSeries = FixedLineGraphSeries(Array(actArrayPrediction.size) { i -> actArrayPrediction[i] }).also { @@ -239,14 +240,14 @@ class PrepareIobAutosensGraphDataWorker( paint.style = Paint.Style.STROKE paint.strokeWidth = 3f paint.pathEffect = DashPathEffect(floatArrayOf(4f, 4f), 0f) - paint.color = rh.gc(R.color.activity) + paint.color = rh.gac( ctx, R.attr.activityColor) }) } // BGI data.overviewData.minusBgiSeries = FixedLineGraphSeries(Array(bgiArrayHist.size) { i -> bgiArrayHist[i] }).also { it.isDrawBackground = false - it.color = rh.gc(R.color.bgi) + it.color = rh.gac( ctx, R.attr.bgiColor) it.thickness = 3 } data.overviewData.minusBgiHistSeries = FixedLineGraphSeries(Array(bgiArrayPrediction.size) { i -> bgiArrayPrediction[i] }).also { @@ -254,7 +255,7 @@ class PrepareIobAutosensGraphDataWorker( paint.style = Paint.Style.STROKE paint.strokeWidth = 3f paint.pathEffect = DashPathEffect(floatArrayOf(4f, 4f), 0f) - paint.color = rh.gc(R.color.bgi) + paint.color = rh.gac( ctx, R.attr.bgiColor) }) } @@ -265,17 +266,17 @@ class PrepareIobAutosensGraphDataWorker( // RATIO data.overviewData.ratioSeries = LineGraphSeries(Array(ratioArray.size) { i -> ratioArray[i] }).also { - it.color = rh.gc(R.color.ratio) + it.color = rh.gac( ctx, R.attr.ratioColor) it.thickness = 3 } // DEV SLOPE data.overviewData.dsMaxSeries = LineGraphSeries(Array(dsMaxArray.size) { i -> dsMaxArray[i] }).also { - it.color = rh.gc(R.color.devslopepos) + it.color = rh.gac( ctx, R.attr.devslopeposColor) it.thickness = 3 } data.overviewData.dsMinSeries = LineGraphSeries(Array(dsMinArray.size) { i -> dsMinArray[i] }).also { - it.color = rh.gc(R.color.devslopeneg) + it.color = rh.gac( ctx, R.attr.devslopenegColor) it.thickness = 3 } rxBus.send(EventIobCalculationProgress(CalculationWorkflow.ProgressData.PREPARE_IOB_AUTOSENS_DATA, 100, null)) diff --git a/app/src/main/java/info/nightscout/androidaps/workflow/PrepareTemporaryTargetDataWorker.kt b/app/src/main/java/info/nightscout/androidaps/workflow/PrepareTemporaryTargetDataWorker.kt index 4f960fd187..49bedb98e2 100644 --- a/app/src/main/java/info/nightscout/androidaps/workflow/PrepareTemporaryTargetDataWorker.kt +++ b/app/src/main/java/info/nightscout/androidaps/workflow/PrepareTemporaryTargetDataWorker.kt @@ -1,6 +1,7 @@ package info.nightscout.androidaps.workflow import android.content.Context +import androidx.core.content.ContextCompat import androidx.work.Worker import androidx.work.WorkerParameters import androidx.work.workDataOf @@ -33,9 +34,10 @@ class PrepareTemporaryTargetDataWorker( @Inject lateinit var repository: AppRepository @Inject lateinit var loop: Loop @Inject lateinit var rxBus: RxBus - + var ctx: Context init { (context.applicationContext as HasAndroidInjector).androidInjector().inject(this) + ctx = rh.getThemedCtx(context) } class PrepareTemporaryTargetData( @@ -76,7 +78,7 @@ class PrepareTemporaryTargetDataWorker( // create series data.overviewData.temporaryTargetSeries = LineGraphSeries(Array(targetsSeriesArray.size) { i -> targetsSeriesArray[i] }).also { it.isDrawBackground = false - it.color = rh.gc(R.color.tempTargetBackground) + it.color = rh.gac(ctx, R.attr.tempTargetBackgroundColor ) it.thickness = 2 } rxBus.send(EventIobCalculationProgress(CalculationWorkflow.ProgressData.PREPARE_TEMPORARY_TARGET_DATA, 100, null)) 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 375243e597..2c259946fa 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 @@ -33,4 +33,8 @@ interface ResourceHelper { * Get Attribute Color based on theme style for specified context */ @ColorInt fun gac(context: Context?, @AttrRes attributeId: Int): Int + /** + * Get themed context -->> context dependend on light or darkmode + */ + fun getThemedCtx(context: Context): Context } 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 d7e6d59402..75db45e99d 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 @@ -4,11 +4,13 @@ import android.annotation.SuppressLint import android.content.Context import android.content.res.AssetFileDescriptor import android.content.res.Configuration +import android.content.res.Resources import android.graphics.Bitmap import android.graphics.BitmapFactory import android.graphics.drawable.Drawable import android.util.DisplayMetrics import androidx.annotation.* +import androidx.appcompat.app.AppCompatDelegate import androidx.appcompat.view.ContextThemeWrapper import androidx.core.content.ContextCompat import info.nightscout.androidaps.core.R @@ -86,8 +88,21 @@ 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 ?: ContextThemeWrapper(this.context, R.style.AppTheme)).getThemeColor(attributeId) + ( ContextThemeWrapper( context ?: this.context, R.style.AppTheme)).getThemeColor(attributeId) override fun gac(attributeId: Int): Int = ContextThemeWrapper(this.context, R.style.AppTheme).getThemeColor(attributeId) + + override fun getThemedCtx(context: Context): Context { + val res: Resources = context.resources + val configuration = Configuration(res.configuration) + val filter = res.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK.inv() + + configuration.uiMode = when (AppCompatDelegate.getDefaultNightMode()) { + AppCompatDelegate.MODE_NIGHT_NO -> Configuration.UI_MODE_NIGHT_NO or filter + AppCompatDelegate.MODE_NIGHT_YES -> Configuration.UI_MODE_NIGHT_YES or filter + else -> res.configuration.uiMode + } + return context.createConfigurationContext(configuration) + } } diff --git a/core/src/main/res/values-night/styles.xml b/core/src/main/res/values-night/styles.xml index 2bbe7bf680..20dc312782 100644 --- a/core/src/main/res/values-night/styles.xml +++ b/core/src/main/res/values-night/styles.xml @@ -213,10 +213,20 @@ @color/inrangebackground @color/devslopepos + @color/devslopeneg + @color/deviationgrey + @color/deviationblack + @color/deviationgreen + @color/deviationred @color/prediction + @color/iobPredAS @color/bgi @color/ratio @color/activity + @color/basebasal + + @color/black + @color/black @color/plastic_grey @color/textAppearancemediumDark diff --git a/core/src/main/res/values/attrs.xml b/core/src/main/res/values/attrs.xml index 966b35a101..a035d32fa4 100644 --- a/core/src/main/res/values/attrs.xml +++ b/core/src/main/res/values/attrs.xml @@ -192,10 +192,20 @@ + + + + + + + + + + diff --git a/core/src/main/res/values/colors.xml b/core/src/main/res/values/colors.xml index 69a7f15f6d..ec411794c2 100644 --- a/core/src/main/res/values/colors.xml +++ b/core/src/main/res/values/colors.xml @@ -162,7 +162,7 @@ #ff00ff #00d2d2 #1ea3e5 - #FFFFFF + #000000 #d3f166 #00EEEE #FFFFFF00 diff --git a/core/src/main/res/values/styles.xml b/core/src/main/res/values/styles.xml index 323d1f30f9..3f9196f3f8 100644 --- a/core/src/main/res/values/styles.xml +++ b/core/src/main/res/values/styles.xml @@ -112,7 +112,7 @@ 12dp @color/graphgrid - @color/white_alpha_20 + @color/black_alpha_20 @color/tempTargetBackground @color/byodagray @@ -216,10 +216,20 @@ @color/inrangebackground @color/devslopepos + @color/devslopeneg + @color/deviationgrey + @color/deviationblack + @color/deviationgreen + @color/deviationred @color/prediction + @color/iobPredAS @color/bgi - @color/lightSandGray + @color/ratio @color/activity + @color/basebasal + + @color/black + @color/white @color/plastic_grey @color/textAppearancemediumLight