Merge pull request #1601 from osodebailar/fix/graphcolor_changable-by-theme-change

fix issue ratio color in light theme and make graph colors changable
This commit is contained in:
Milos Kozak 2022-04-12 10:20:05 +02:00 committed by GitHub
commit 2a087c8d84
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 101 additions and 47 deletions

View file

@ -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

View file

@ -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))

View file

@ -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<ScaledDataPoint> = ArrayList()
val absIobArray: MutableList<ScaledDataPoint> = 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<DataPointWithLabelInterface> = 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))

View file

@ -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))

View file

@ -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
}

View file

@ -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)
}
}

View file

@ -213,10 +213,20 @@
<!-- Graph specific colors -->
<item name="inrangeBackground">@color/inrangebackground</item>
<item name="devslopeposColor">@color/devslopepos</item>
<item name="devslopenegColor">@color/devslopeneg</item>
<item name="deviationgreyColor">@color/deviationgrey</item>
<item name="deviationblackColor">@color/deviationblack</item>
<item name="deviationgreenColor">@color/deviationgreen</item>
<item name="deviationredColor">@color/deviationred</item>
<item name="predictionColor">@color/prediction</item>
<item name="iobPredASColor">@color/iobPredAS</item>
<item name="bgiColor">@color/bgi</item>
<item name="ratioColor">@color/ratio</item>
<item name="activityColor">@color/activity</item>
<item name="basebasalColor">@color/basebasal</item>
<!---Popupmenu TextColor -->
<item name="menuTextColor">@color/black</item>
<item name="menuTextColorInverse">@color/black</item>
<!-- CardView specific colors -->
<item name="strokeColor">@color/plastic_grey</item>
<item name="textAppearancemediumColor">@color/textAppearancemediumDark</item>

View file

@ -192,10 +192,20 @@
<attr name="inrangeBackground" format="reference|color" />
<attr name="graphGrid" format="reference|color" />
<attr name="devslopeposColor" format="reference|color" />
<attr name="devslopenegColor" format="reference|color" />
<attr name="deviationgreyColor" format="reference|color" />
<attr name="deviationblackColor" format="reference|color" />
<attr name="deviationgreenColor" format="reference|color" />
<attr name="deviationredColor" format="reference|color" />
<attr name="predictionColor" format="reference|color" />
<attr name="iobPredASColor" format="reference|color" />
<attr name="bgiColor" format="reference|color" />
<attr name="ratioColor" format="reference|color" />
<attr name="activityColor" format="reference|color" />
<attr name="basebasalColor" format="reference|color" />
<!---Popupmenu TextColor -->
<attr name="menuTextColor" format="reference|color" />
<attr name="menuTextColorInverse" format="reference|color" />
<!-- CardView specific colors -->
<attr name="strokeColor" format="reference|color" />
<attr name="textAppearancemediumColor" format="reference|color" />

View file

@ -162,7 +162,7 @@
<color name="prediction">#ff00ff</color>
<color name="iobPred">#00d2d2</color>
<color name="bolus">#1ea3e5</color>
<color name="ratio">#FFFFFF</color>
<color name="ratio">#000000</color>
<color name="activity">#d3f166</color>
<color name="bgi">#00EEEE</color>
<color name="devslopepos">#FFFFFF00</color>

View file

@ -112,7 +112,7 @@
<item name="android:dialogCornerRadius">12dp</item>
<!---Overview and Historybrowser -->
<item name="graphgrid">@color/graphgrid</item>
<item name="viewPortbackgroundColor">@color/white_alpha_20</item>
<item name="viewPortbackgroundColor">@color/black_alpha_20</item>
<item name="tempTargetBackgroundColor">@color/tempTargetBackground</item>
<!---CGM source-->
<item name="cgmdexColor">@color/byodagray</item>
@ -216,10 +216,20 @@
<!-- Graph specific colors -->
<item name="inrangeBackground">@color/inrangebackground</item>
<item name="devslopeposColor">@color/devslopepos</item>
<item name="devslopenegColor">@color/devslopeneg</item>
<item name="deviationgreyColor">@color/deviationgrey</item>
<item name="deviationblackColor">@color/deviationblack</item>
<item name="deviationgreenColor">@color/deviationgreen</item>
<item name="deviationredColor">@color/deviationred</item>
<item name="predictionColor">@color/prediction</item>
<item name="iobPredASColor">@color/iobPredAS</item>
<item name="bgiColor">@color/bgi</item>
<item name="ratioColor">@color/lightSandGray</item>
<item name="ratioColor">@color/ratio</item>
<item name="activityColor">@color/activity</item>
<item name="basebasalColor">@color/basebasal</item>
<!---Popupmenu TextColor -->
<item name="menuTextColor">@color/black</item>
<item name="menuTextColorInverse">@color/white</item>
<!-- CardView specific colors -->
<item name="strokeColor">@color/plastic_grey</item>
<item name="textAppearancemediumColor">@color/textAppearancemediumLight</item>