Skip to content

Commit 0a1083b

Browse files
Addressed some comments & Merge home-assistant#4603 changes into mine
2 parents 7a04973 + 2febb51 commit 0a1083b

File tree

24 files changed

+1152
-28
lines changed

24 files changed

+1152
-28
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,19 @@
157157
android:resource="@xml/graph_widget_info" />
158158
</receiver>
159159

160+
<receiver android:name=".widgets.history.HistoryWidget" android:label="@string/widget_history_description"
161+
android:exported="false">
162+
<intent-filter>
163+
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
164+
<action android:name="io.homeassistant.companion.android.widgets.history.HistoryWidget.RECEIVE_DATA" />
165+
<action android:name="io.homeassistant.companion.android.widgets.history.HistoryWidget.UPDATE_VIEW" />
166+
</intent-filter>
167+
168+
<meta-data
169+
android:name="android.appwidget.provider"
170+
android:resource="@xml/history_widget_info" />
171+
</receiver>
172+
160173
<receiver android:name=".widgets.mediaplayer.MediaPlayerControlsWidget" android:label="@string/widget_media_player_description"
161174
android:exported="false">
162175
<intent-filter>
@@ -228,6 +241,13 @@
228241
<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />
229242
</intent-filter>
230243
</activity>
244+
<activity android:name=".widgets.history.HistoryWidgetConfigureActivity"
245+
android:configChanges="orientation|screenSize"
246+
android:exported="true">
247+
<intent-filter>
248+
<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />
249+
</intent-filter>
250+
</activity>
231251
<activity android:name=".widgets.mediaplayer.MediaPlayerControlsWidgetConfigureActivity"
232252
android:configChanges="orientation|screenSize"
233253
android:exported="true">

app/src/main/java/io/homeassistant/companion/android/settings/widgets/ManageWidgetsViewModel.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import androidx.lifecycle.viewModelScope
1313
import dagger.hilt.android.lifecycle.HiltViewModel
1414
import io.homeassistant.companion.android.database.widget.ButtonWidgetDao
1515
import io.homeassistant.companion.android.database.widget.CameraWidgetDao
16+
import io.homeassistant.companion.android.database.widget.HistoryWidgetDao
1617
import io.homeassistant.companion.android.database.widget.MediaPlayerControlsWidgetDao
1718
import io.homeassistant.companion.android.database.widget.StaticWidgetDao
1819
import io.homeassistant.companion.android.database.widget.TemplateWidgetDao
@@ -25,6 +26,7 @@ import kotlinx.coroutines.launch
2526
class ManageWidgetsViewModel @Inject constructor(
2627
buttonWidgetDao: ButtonWidgetDao,
2728
cameraWidgetDao: CameraWidgetDao,
29+
historyWidgetDao: HistoryWidgetDao,
2830
staticWidgetDao: StaticWidgetDao,
2931
graphWidgetDao: GraphWidgetDao,
3032
mediaPlayerControlsWidgetDao: MediaPlayerControlsWidgetDao,
@@ -40,6 +42,7 @@ class ManageWidgetsViewModel @Inject constructor(
4042

4143
val buttonWidgetList = buttonWidgetDao.getAllFlow().collectAsState()
4244
val cameraWidgetList = cameraWidgetDao.getAllFlow().collectAsState()
45+
val historyWidgetList = historyWidgetDao.getAllFlow().collectAsState()
4346
val staticWidgetList = staticWidgetDao.getAllFlow().collectAsState()
4447
val graphWidgetList = graphWidgetDao.getAllFlow().collectAsState()
4548
val mediaWidgetList = mediaPlayerControlsWidgetDao.getAllFlow().collectAsState()

app/src/main/java/io/homeassistant/companion/android/settings/widgets/views/ManageWidgetsView.kt

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import io.homeassistant.companion.android.widgets.button.ButtonWidgetConfigureAc
4343
import io.homeassistant.companion.android.widgets.camera.CameraWidgetConfigureActivity
4444
import io.homeassistant.companion.android.widgets.entity.EntityWidgetConfigureActivity
4545
import io.homeassistant.companion.android.widgets.graph.GraphWidgetConfigureActivity
46+
import io.homeassistant.companion.android.widgets.history.HistoryWidgetConfigureActivity
4647
import io.homeassistant.companion.android.widgets.mediaplayer.MediaPlayerControlsWidgetConfigureActivity
4748
import io.homeassistant.companion.android.widgets.template.TemplateWidgetConfigureActivity
4849

@@ -52,7 +53,8 @@ enum class WidgetType(val widgetIcon: IIcon) {
5253
STATE(CommunityMaterial.Icon3.cmd_shape),
5354
GRAPH(CommunityMaterial.Icon2.cmd_file_chart),
5455
MEDIA(CommunityMaterial.Icon3.cmd_play_box_multiple),
55-
TEMPLATE(CommunityMaterial.Icon.cmd_code_braces);
56+
TEMPLATE(CommunityMaterial.Icon.cmd_code_braces),
57+
HISTORY(CommunityMaterial.Icon3.cmd_sun_clock);
5658

5759
fun configureActivity() = when (this) {
5860
BUTTON -> ButtonWidgetConfigureActivity::class.java
@@ -61,6 +63,7 @@ enum class WidgetType(val widgetIcon: IIcon) {
6163
STATE -> EntityWidgetConfigureActivity::class.java
6264
GRAPH -> GraphWidgetConfigureActivity::class.java
6365
TEMPLATE -> TemplateWidgetConfigureActivity::class.java
66+
HISTORY -> HistoryWidgetConfigureActivity::class.java
6467
}
6568
}
6669

@@ -84,6 +87,7 @@ fun ManageWidgetsView(
8487
val availableWidgets = listOf(
8588
stringResource(R.string.widget_button_image_description) to WidgetType.BUTTON,
8689
stringResource(R.string.widget_camera_description) to WidgetType.CAMERA,
90+
stringResource(R.string.widget_history_description) to WidgetType.HISTORY,
8791
stringResource(R.string.widget_static_image_description) to WidgetType.STATE,
8892
stringResource(R.string.widget_graph_image_description) to WidgetType.GRAPH,
8993
stringResource(R.string.widget_media_player_description) to WidgetType.MEDIA,
@@ -115,6 +119,7 @@ fun ManageWidgetsView(
115119
if (viewModel.buttonWidgetList.value.isEmpty() && viewModel.staticWidgetList.value.isEmpty() &&
116120
viewModel.mediaWidgetList.value.isEmpty() && viewModel.templateWidgetList.value.isEmpty() &&
117121
viewModel.cameraWidgetList.value.isEmpty() && viewModel.graphWidgetList.value.isEmpty()
122+
&& viewModel.historyWidgetList.value.isEmpty()
118123
) {
119124
item {
120125
EmptyState(
@@ -139,6 +144,12 @@ fun ManageWidgetsView(
139144
title = R.string.camera_widgets,
140145
widgetLabel = { item -> item.entityId }
141146
)
147+
widgetItems(
148+
viewModel.historyWidgetList.value,
149+
widgetType = WidgetType.HISTORY,
150+
title = R.string.history_widgets,
151+
widgetLabel = { item -> item.entityId }
152+
)
142153
widgetItems(
143154
viewModel.staticWidgetList.value,
144155
widgetType = WidgetType.STATE,

app/src/main/java/io/homeassistant/companion/android/widgets/graph/GraphWidget.kt

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import com.github.mikephil.charting.formatter.ValueFormatter
2727
import com.google.android.material.color.DynamicColors
2828
import dagger.hilt.android.AndroidEntryPoint
2929
import io.homeassistant.companion.android.R
30-
import io.homeassistant.companion.android.common.R as commonR
3130
import io.homeassistant.companion.android.common.data.integration.Entity
3231
import io.homeassistant.companion.android.common.data.integration.canSupportPrecision
3332
import io.homeassistant.companion.android.common.data.integration.friendlyState
@@ -41,9 +40,10 @@ import io.homeassistant.companion.android.database.widget.graph.GraphWidgetWithH
4140
import io.homeassistant.companion.android.util.getAttribute
4241
import io.homeassistant.companion.android.widgets.BaseWidgetProvider
4342
import io.homeassistant.companion.android.widgets.entity.EntityWidget.Companion.EXTRA_STATE_SEPARATOR
44-
import javax.inject.Inject
4543
import kotlinx.coroutines.flow.first
4644
import kotlinx.coroutines.launch
45+
import javax.inject.Inject
46+
import io.homeassistant.companion.android.common.R as commonR
4747

4848
@AndroidEntryPoint
4949
class GraphWidget : BaseWidgetProvider() {
@@ -69,13 +69,13 @@ class GraphWidget : BaseWidgetProvider() {
6969
}
7070

7171
@Inject
72-
lateinit var graphWidgetRepository: GraphWidgetRepository
72+
lateinit var repository: GraphWidgetRepository
7373

7474
override fun getWidgetProvider(context: Context): ComponentName =
7575
ComponentName(context, GraphWidget::class.java)
7676

7777
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)
7979
val widget = historicData?.graphWidget
8080

8181
val intent = Intent(context, GraphWidget::class.java).apply {
@@ -221,14 +221,15 @@ class GraphWidget : BaseWidgetProvider() {
221221

222222
private fun createLineChart(context: Context, label: String, timeRange: String, entries: List<Entry>, width: Int, height: Int): LineChart {
223223
val lineChart = LineChart(context).apply {
224-
setBackgroundColor(Color.WHITE)
225224

225+
val dynTextColor = ContextCompat.getColor(context, commonR.color.colorWidgetButtonLabel)
226+
setBackgroundResource(commonR.color.colorWidgetButtonBackground)
226227
setDrawBorders(false)
227228

228229
xAxis.apply {
229230
setDrawGridLines(true)
230231
position = XAxis.XAxisPosition.BOTTOM
231-
textColor = Color.DKGRAY
232+
textColor = dynTextColor
232233
textSize = 12F
233234
granularity = 2F
234235
setAvoidFirstLastClipping(true)
@@ -237,7 +238,7 @@ class GraphWidget : BaseWidgetProvider() {
237238

238239
axisLeft.apply {
239240
setDrawGridLines(true)
240-
textColor = Color.DKGRAY
241+
textColor = dynTextColor
241242
textSize = 12F
242243
}
243244

@@ -248,7 +249,7 @@ class GraphWidget : BaseWidgetProvider() {
248249

249250
legend.apply {
250251
isEnabled = true
251-
textColor = Color.DKGRAY
252+
textColor = dynTextColor
252253
textSize = 12F
253254
verticalAlignment = Legend.LegendVerticalAlignment.BOTTOM
254255
horizontalAlignment = Legend.LegendHorizontalAlignment.RIGHT
@@ -262,6 +263,7 @@ class GraphWidget : BaseWidgetProvider() {
262263

263264
legend.isEnabled = true
264265
description.isEnabled = true
266+
description.textColor = dynTextColor
265267
}
266268

267269
val mainGraphColor = ContextCompat.getColor(context, commonR.color.colorPrimary)
@@ -301,7 +303,7 @@ class GraphWidget : BaseWidgetProvider() {
301303
}
302304

303305
override suspend fun getAllWidgetIdsWithEntities(context: Context): Map<Int, Pair<Int, List<String>>> =
304-
graphWidgetRepository.getAllFlow()
306+
repository.getAllFlow()
305307
.first()
306308
.associate { it.id to (it.serverId to listOf(it.entityId)) }
307309

@@ -336,11 +338,11 @@ class GraphWidget : BaseWidgetProvider() {
336338
null
337339
}
338340
if (attributeIds == null) {
339-
graphWidgetRepository.updateWidgetLastUpdate(
341+
repository.updateWidgetLastUpdate(
340342
appWidgetId,
341-
entity?.friendlyState(context, entityOptions) ?: graphWidgetRepository.get(appWidgetId)?.lastUpdate ?: ""
343+
entity?.friendlyState(context, entityOptions) ?: repository.get(appWidgetId)?.lastUpdate ?: ""
342344
)
343-
return ResolvedText(graphWidgetRepository.get(appWidgetId)?.lastUpdate, entityCaughtException)
345+
return ResolvedText(repository.get(appWidgetId)?.lastUpdate, entityCaughtException)
344346
}
345347

346348
try {
@@ -350,12 +352,12 @@ class GraphWidget : BaseWidgetProvider() {
350352
val lastUpdate =
351353
entity?.friendlyState(context, entityOptions).plus(if (attributeValues.isNotEmpty()) stateSeparator else "")
352354
.plus(attributeValues.joinToString(attributeSeparator))
353-
graphWidgetRepository.updateWidgetLastUpdate(appWidgetId, lastUpdate)
355+
repository.updateWidgetLastUpdate(appWidgetId, lastUpdate)
354356
return ResolvedText(lastUpdate)
355357
} catch (e: Exception) {
356358
Log.e(TAG, "Unable to fetch entity state and attributes", e)
357359
}
358-
return ResolvedText(graphWidgetRepository.get(appWidgetId)?.lastUpdate, true)
360+
return ResolvedText(repository.get(appWidgetId)?.lastUpdate, true)
359361
}
360362

361363
override fun saveEntityConfiguration(context: Context, extras: Bundle?, appWidgetId: Int) {
@@ -386,8 +388,8 @@ class GraphWidget : BaseWidgetProvider() {
386388
"entity id: " + entitySelection + System.lineSeparator() +
387389
"attribute: " + (attributeSelection ?: "N/A")
388390
)
389-
if (graphWidgetRepository.get(appWidgetId) == null) {
390-
graphWidgetRepository.add(
391+
if (repository.get(appWidgetId) == null) {
392+
repository.add(
391393
GraphWidgetEntity(
392394
id = appWidgetId,
393395
serverId = serverId,
@@ -398,7 +400,7 @@ class GraphWidget : BaseWidgetProvider() {
398400
stateSeparator = stateSeparatorSelection ?: "",
399401
attributeSeparator = attributeSeparatorSelection ?: "",
400402
tapAction = tapActionSelection,
401-
lastUpdate = graphWidgetRepository.get(appWidgetId)?.lastUpdate ?: "",
403+
lastUpdate = repository.get(appWidgetId)?.lastUpdate ?: "",
402404
backgroundType = backgroundTypeSelection,
403405
textColor = textColorSelection
404406
)
@@ -411,7 +413,7 @@ class GraphWidget : BaseWidgetProvider() {
411413

412414
override suspend fun onEntityStateChanged(context: Context, appWidgetId: Int, entity: Entity<*>) {
413415
widgetScope?.launch {
414-
val graphEntity = graphWidgetRepository.get(appWidgetId)
416+
val graphEntity = repository.get(appWidgetId)
415417

416418
if (graphEntity != null) {
417419
val currentTimeMillis = System.currentTimeMillis()
@@ -420,9 +422,9 @@ class GraphWidget : BaseWidgetProvider() {
420422
val timeRangeInMillis = graphEntity.timeRange * 60 * 60 * 1000
421423
val cutoffTimeMillis = currentTimeMillis - timeRangeInMillis
422424

423-
graphWidgetRepository.deleteEntriesOlderThan(appWidgetId, cutoffTimeMillis)
425+
repository.deleteEntriesOlderThan(appWidgetId, cutoffTimeMillis)
424426

425-
graphWidgetRepository.insertGraphWidgetHistory(
427+
repository.insertGraphWidgetHistory(
426428
GraphWidgetHistoryEntity(
427429
entityId = entity.entityId,
428430
graphWidgetId = appWidgetId,
@@ -448,7 +450,7 @@ class GraphWidget : BaseWidgetProvider() {
448450
appWidgetManager.partiallyUpdateAppWidget(appWidgetId, loadingViews)
449451

450452
var success = false
451-
graphWidgetRepository.get(appWidgetId)?.let {
453+
repository.get(appWidgetId)?.let {
452454
try {
453455
onEntityPressedWithoutState(
454456
it.entityId,
@@ -479,7 +481,7 @@ class GraphWidget : BaseWidgetProvider() {
479481

480482
override fun onDeleted(context: Context, appWidgetIds: IntArray) {
481483
widgetScope?.launch {
482-
graphWidgetRepository.deleteAll(appWidgetIds)
484+
repository.deleteAll(appWidgetIds)
483485
appWidgetIds.forEach { removeSubscription(it) }
484486
}
485487
}

0 commit comments

Comments
 (0)