@@ -27,7 +27,6 @@ import com.github.mikephil.charting.formatter.ValueFormatter
27
27
import com.google.android.material.color.DynamicColors
28
28
import dagger.hilt.android.AndroidEntryPoint
29
29
import io.homeassistant.companion.android.R
30
- import io.homeassistant.companion.android.common.R as commonR
31
30
import io.homeassistant.companion.android.common.data.integration.Entity
32
31
import io.homeassistant.companion.android.common.data.integration.canSupportPrecision
33
32
import io.homeassistant.companion.android.common.data.integration.friendlyState
@@ -41,9 +40,10 @@ import io.homeassistant.companion.android.database.widget.graph.GraphWidgetWithH
41
40
import io.homeassistant.companion.android.util.getAttribute
42
41
import io.homeassistant.companion.android.widgets.BaseWidgetProvider
43
42
import io.homeassistant.companion.android.widgets.entity.EntityWidget.Companion.EXTRA_STATE_SEPARATOR
44
- import javax.inject.Inject
45
43
import kotlinx.coroutines.flow.first
46
44
import kotlinx.coroutines.launch
45
+ import javax.inject.Inject
46
+ import io.homeassistant.companion.android.common.R as commonR
47
47
48
48
@AndroidEntryPoint
49
49
class GraphWidget : BaseWidgetProvider () {
@@ -69,13 +69,13 @@ class GraphWidget : BaseWidgetProvider() {
69
69
}
70
70
71
71
@Inject
72
- lateinit var graphWidgetRepository : GraphWidgetRepository
72
+ lateinit var repository : GraphWidgetRepository
73
73
74
74
override fun getWidgetProvider (context : Context ): ComponentName =
75
75
ComponentName (context, GraphWidget ::class .java)
76
76
77
77
override suspend fun getWidgetRemoteViews (context : Context , appWidgetId : Int , suggestedEntity : Entity <Map <String , Any >>? ): RemoteViews {
78
- val historicData = graphWidgetRepository .getGraphWidgetWithHistories(appWidgetId)
78
+ val historicData = repository .getGraphWidgetWithHistories(appWidgetId)
79
79
val widget = historicData?.graphWidget
80
80
81
81
val intent = Intent (context, GraphWidget ::class .java).apply {
@@ -221,14 +221,15 @@ class GraphWidget : BaseWidgetProvider() {
221
221
222
222
private fun createLineChart (context : Context , label : String , timeRange : String , entries : List <Entry >, width : Int , height : Int ): LineChart {
223
223
val lineChart = LineChart (context).apply {
224
- setBackgroundColor(Color .WHITE )
225
224
225
+ val dynTextColor = ContextCompat .getColor(context, commonR.color.colorWidgetButtonLabel)
226
+ setBackgroundResource(commonR.color.colorWidgetButtonBackground)
226
227
setDrawBorders(false )
227
228
228
229
xAxis.apply {
229
230
setDrawGridLines(true )
230
231
position = XAxis .XAxisPosition .BOTTOM
231
- textColor = Color . DKGRAY
232
+ textColor = dynTextColor
232
233
textSize = 12F
233
234
granularity = 2F
234
235
setAvoidFirstLastClipping(true )
@@ -237,7 +238,7 @@ class GraphWidget : BaseWidgetProvider() {
237
238
238
239
axisLeft.apply {
239
240
setDrawGridLines(true )
240
- textColor = Color . DKGRAY
241
+ textColor = dynTextColor
241
242
textSize = 12F
242
243
}
243
244
@@ -248,7 +249,7 @@ class GraphWidget : BaseWidgetProvider() {
248
249
249
250
legend.apply {
250
251
isEnabled = true
251
- textColor = Color . DKGRAY
252
+ textColor = dynTextColor
252
253
textSize = 12F
253
254
verticalAlignment = Legend .LegendVerticalAlignment .BOTTOM
254
255
horizontalAlignment = Legend .LegendHorizontalAlignment .RIGHT
@@ -262,6 +263,7 @@ class GraphWidget : BaseWidgetProvider() {
262
263
263
264
legend.isEnabled = true
264
265
description.isEnabled = true
266
+ description.textColor = dynTextColor
265
267
}
266
268
267
269
val mainGraphColor = ContextCompat .getColor(context, commonR.color.colorPrimary)
@@ -301,7 +303,7 @@ class GraphWidget : BaseWidgetProvider() {
301
303
}
302
304
303
305
override suspend fun getAllWidgetIdsWithEntities (context : Context ): Map <Int , Pair <Int , List <String >>> =
304
- graphWidgetRepository .getAllFlow()
306
+ repository .getAllFlow()
305
307
.first()
306
308
.associate { it.id to (it.serverId to listOf (it.entityId)) }
307
309
@@ -336,11 +338,11 @@ class GraphWidget : BaseWidgetProvider() {
336
338
null
337
339
}
338
340
if (attributeIds == null ) {
339
- graphWidgetRepository .updateWidgetLastUpdate(
341
+ repository .updateWidgetLastUpdate(
340
342
appWidgetId,
341
- entity?.friendlyState(context, entityOptions) ? : graphWidgetRepository .get(appWidgetId)?.lastUpdate ? : " "
343
+ entity?.friendlyState(context, entityOptions) ? : repository .get(appWidgetId)?.lastUpdate ? : " "
342
344
)
343
- return ResolvedText (graphWidgetRepository .get(appWidgetId)?.lastUpdate, entityCaughtException)
345
+ return ResolvedText (repository .get(appWidgetId)?.lastUpdate, entityCaughtException)
344
346
}
345
347
346
348
try {
@@ -350,12 +352,12 @@ class GraphWidget : BaseWidgetProvider() {
350
352
val lastUpdate =
351
353
entity?.friendlyState(context, entityOptions).plus(if (attributeValues.isNotEmpty()) stateSeparator else " " )
352
354
.plus(attributeValues.joinToString(attributeSeparator))
353
- graphWidgetRepository .updateWidgetLastUpdate(appWidgetId, lastUpdate)
355
+ repository .updateWidgetLastUpdate(appWidgetId, lastUpdate)
354
356
return ResolvedText (lastUpdate)
355
357
} catch (e: Exception ) {
356
358
Log .e(TAG , " Unable to fetch entity state and attributes" , e)
357
359
}
358
- return ResolvedText (graphWidgetRepository .get(appWidgetId)?.lastUpdate, true )
360
+ return ResolvedText (repository .get(appWidgetId)?.lastUpdate, true )
359
361
}
360
362
361
363
override fun saveEntityConfiguration (context : Context , extras : Bundle ? , appWidgetId : Int ) {
@@ -386,8 +388,8 @@ class GraphWidget : BaseWidgetProvider() {
386
388
" entity id: " + entitySelection + System .lineSeparator() +
387
389
" attribute: " + (attributeSelection ? : " N/A" )
388
390
)
389
- if (graphWidgetRepository .get(appWidgetId) == null ) {
390
- graphWidgetRepository .add(
391
+ if (repository .get(appWidgetId) == null ) {
392
+ repository .add(
391
393
GraphWidgetEntity (
392
394
id = appWidgetId,
393
395
serverId = serverId,
@@ -398,7 +400,7 @@ class GraphWidget : BaseWidgetProvider() {
398
400
stateSeparator = stateSeparatorSelection ? : " " ,
399
401
attributeSeparator = attributeSeparatorSelection ? : " " ,
400
402
tapAction = tapActionSelection,
401
- lastUpdate = graphWidgetRepository .get(appWidgetId)?.lastUpdate ? : " " ,
403
+ lastUpdate = repository .get(appWidgetId)?.lastUpdate ? : " " ,
402
404
backgroundType = backgroundTypeSelection,
403
405
textColor = textColorSelection
404
406
)
@@ -411,7 +413,7 @@ class GraphWidget : BaseWidgetProvider() {
411
413
412
414
override suspend fun onEntityStateChanged (context : Context , appWidgetId : Int , entity : Entity <* >) {
413
415
widgetScope?.launch {
414
- val graphEntity = graphWidgetRepository .get(appWidgetId)
416
+ val graphEntity = repository .get(appWidgetId)
415
417
416
418
if (graphEntity != null ) {
417
419
val currentTimeMillis = System .currentTimeMillis()
@@ -420,9 +422,9 @@ class GraphWidget : BaseWidgetProvider() {
420
422
val timeRangeInMillis = graphEntity.timeRange * 60 * 60 * 1000
421
423
val cutoffTimeMillis = currentTimeMillis - timeRangeInMillis
422
424
423
- graphWidgetRepository .deleteEntriesOlderThan(appWidgetId, cutoffTimeMillis)
425
+ repository .deleteEntriesOlderThan(appWidgetId, cutoffTimeMillis)
424
426
425
- graphWidgetRepository .insertGraphWidgetHistory(
427
+ repository .insertGraphWidgetHistory(
426
428
GraphWidgetHistoryEntity (
427
429
entityId = entity.entityId,
428
430
graphWidgetId = appWidgetId,
@@ -448,7 +450,7 @@ class GraphWidget : BaseWidgetProvider() {
448
450
appWidgetManager.partiallyUpdateAppWidget(appWidgetId, loadingViews)
449
451
450
452
var success = false
451
- graphWidgetRepository .get(appWidgetId)?.let {
453
+ repository .get(appWidgetId)?.let {
452
454
try {
453
455
onEntityPressedWithoutState(
454
456
it.entityId,
@@ -479,7 +481,7 @@ class GraphWidget : BaseWidgetProvider() {
479
481
480
482
override fun onDeleted (context : Context , appWidgetIds : IntArray ) {
481
483
widgetScope?.launch {
482
- graphWidgetRepository .deleteAll(appWidgetIds)
484
+ repository .deleteAll(appWidgetIds)
483
485
appWidgetIds.forEach { removeSubscription(it) }
484
486
}
485
487
}
0 commit comments