Skip to content

Commit

Permalink
Addressed some comments & Merge home-assistant#4603 changes into mine
Browse files Browse the repository at this point in the history
  • Loading branch information
ivorsmorenburg committed Sep 5, 2024
2 parents 7a04973 + 2febb51 commit 0a1083b
Show file tree
Hide file tree
Showing 24 changed files with 1,152 additions and 28 deletions.
20 changes: 20 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,19 @@
android:resource="@xml/graph_widget_info" />
</receiver>

<receiver android:name=".widgets.history.HistoryWidget" android:label="@string/widget_history_description"
android:exported="false">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<action android:name="io.homeassistant.companion.android.widgets.history.HistoryWidget.RECEIVE_DATA" />
<action android:name="io.homeassistant.companion.android.widgets.history.HistoryWidget.UPDATE_VIEW" />
</intent-filter>

<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/history_widget_info" />
</receiver>

<receiver android:name=".widgets.mediaplayer.MediaPlayerControlsWidget" android:label="@string/widget_media_player_description"
android:exported="false">
<intent-filter>
Expand Down Expand Up @@ -228,6 +241,13 @@
<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />
</intent-filter>
</activity>
<activity android:name=".widgets.history.HistoryWidgetConfigureActivity"
android:configChanges="orientation|screenSize"
android:exported="true">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />
</intent-filter>
</activity>
<activity android:name=".widgets.mediaplayer.MediaPlayerControlsWidgetConfigureActivity"
android:configChanges="orientation|screenSize"
android:exported="true">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel
import io.homeassistant.companion.android.database.widget.ButtonWidgetDao
import io.homeassistant.companion.android.database.widget.CameraWidgetDao
import io.homeassistant.companion.android.database.widget.HistoryWidgetDao
import io.homeassistant.companion.android.database.widget.MediaPlayerControlsWidgetDao
import io.homeassistant.companion.android.database.widget.StaticWidgetDao
import io.homeassistant.companion.android.database.widget.TemplateWidgetDao
Expand All @@ -25,6 +26,7 @@ import kotlinx.coroutines.launch
class ManageWidgetsViewModel @Inject constructor(
buttonWidgetDao: ButtonWidgetDao,
cameraWidgetDao: CameraWidgetDao,
historyWidgetDao: HistoryWidgetDao,
staticWidgetDao: StaticWidgetDao,
graphWidgetDao: GraphWidgetDao,
mediaPlayerControlsWidgetDao: MediaPlayerControlsWidgetDao,
Expand All @@ -40,6 +42,7 @@ class ManageWidgetsViewModel @Inject constructor(

val buttonWidgetList = buttonWidgetDao.getAllFlow().collectAsState()
val cameraWidgetList = cameraWidgetDao.getAllFlow().collectAsState()
val historyWidgetList = historyWidgetDao.getAllFlow().collectAsState()
val staticWidgetList = staticWidgetDao.getAllFlow().collectAsState()
val graphWidgetList = graphWidgetDao.getAllFlow().collectAsState()
val mediaWidgetList = mediaPlayerControlsWidgetDao.getAllFlow().collectAsState()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import io.homeassistant.companion.android.widgets.button.ButtonWidgetConfigureAc
import io.homeassistant.companion.android.widgets.camera.CameraWidgetConfigureActivity
import io.homeassistant.companion.android.widgets.entity.EntityWidgetConfigureActivity
import io.homeassistant.companion.android.widgets.graph.GraphWidgetConfigureActivity
import io.homeassistant.companion.android.widgets.history.HistoryWidgetConfigureActivity
import io.homeassistant.companion.android.widgets.mediaplayer.MediaPlayerControlsWidgetConfigureActivity
import io.homeassistant.companion.android.widgets.template.TemplateWidgetConfigureActivity

Expand All @@ -52,7 +53,8 @@ enum class WidgetType(val widgetIcon: IIcon) {
STATE(CommunityMaterial.Icon3.cmd_shape),
GRAPH(CommunityMaterial.Icon2.cmd_file_chart),
MEDIA(CommunityMaterial.Icon3.cmd_play_box_multiple),
TEMPLATE(CommunityMaterial.Icon.cmd_code_braces);
TEMPLATE(CommunityMaterial.Icon.cmd_code_braces),
HISTORY(CommunityMaterial.Icon3.cmd_sun_clock);

fun configureActivity() = when (this) {
BUTTON -> ButtonWidgetConfigureActivity::class.java
Expand All @@ -61,6 +63,7 @@ enum class WidgetType(val widgetIcon: IIcon) {
STATE -> EntityWidgetConfigureActivity::class.java
GRAPH -> GraphWidgetConfigureActivity::class.java
TEMPLATE -> TemplateWidgetConfigureActivity::class.java
HISTORY -> HistoryWidgetConfigureActivity::class.java
}
}

Expand All @@ -84,6 +87,7 @@ fun ManageWidgetsView(
val availableWidgets = listOf(
stringResource(R.string.widget_button_image_description) to WidgetType.BUTTON,
stringResource(R.string.widget_camera_description) to WidgetType.CAMERA,
stringResource(R.string.widget_history_description) to WidgetType.HISTORY,
stringResource(R.string.widget_static_image_description) to WidgetType.STATE,
stringResource(R.string.widget_graph_image_description) to WidgetType.GRAPH,
stringResource(R.string.widget_media_player_description) to WidgetType.MEDIA,
Expand Down Expand Up @@ -115,6 +119,7 @@ fun ManageWidgetsView(
if (viewModel.buttonWidgetList.value.isEmpty() && viewModel.staticWidgetList.value.isEmpty() &&
viewModel.mediaWidgetList.value.isEmpty() && viewModel.templateWidgetList.value.isEmpty() &&
viewModel.cameraWidgetList.value.isEmpty() && viewModel.graphWidgetList.value.isEmpty()
&& viewModel.historyWidgetList.value.isEmpty()
) {
item {
EmptyState(
Expand All @@ -139,6 +144,12 @@ fun ManageWidgetsView(
title = R.string.camera_widgets,
widgetLabel = { item -> item.entityId }
)
widgetItems(
viewModel.historyWidgetList.value,
widgetType = WidgetType.HISTORY,
title = R.string.history_widgets,
widgetLabel = { item -> item.entityId }
)
widgetItems(
viewModel.staticWidgetList.value,
widgetType = WidgetType.STATE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import com.github.mikephil.charting.formatter.ValueFormatter
import com.google.android.material.color.DynamicColors
import dagger.hilt.android.AndroidEntryPoint
import io.homeassistant.companion.android.R
import io.homeassistant.companion.android.common.R as commonR
import io.homeassistant.companion.android.common.data.integration.Entity
import io.homeassistant.companion.android.common.data.integration.canSupportPrecision
import io.homeassistant.companion.android.common.data.integration.friendlyState
Expand All @@ -41,9 +40,10 @@ import io.homeassistant.companion.android.database.widget.graph.GraphWidgetWithH
import io.homeassistant.companion.android.util.getAttribute
import io.homeassistant.companion.android.widgets.BaseWidgetProvider
import io.homeassistant.companion.android.widgets.entity.EntityWidget.Companion.EXTRA_STATE_SEPARATOR
import javax.inject.Inject
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.launch
import javax.inject.Inject
import io.homeassistant.companion.android.common.R as commonR

@AndroidEntryPoint
class GraphWidget : BaseWidgetProvider() {
Expand All @@ -69,13 +69,13 @@ class GraphWidget : BaseWidgetProvider() {
}

@Inject
lateinit var graphWidgetRepository: GraphWidgetRepository
lateinit var repository: GraphWidgetRepository

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

override suspend fun getWidgetRemoteViews(context: Context, appWidgetId: Int, suggestedEntity: Entity<Map<String, Any>>?): RemoteViews {
val historicData = graphWidgetRepository.getGraphWidgetWithHistories(appWidgetId)
val historicData = repository.getGraphWidgetWithHistories(appWidgetId)
val widget = historicData?.graphWidget

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

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

val dynTextColor = ContextCompat.getColor(context, commonR.color.colorWidgetButtonLabel)
setBackgroundResource(commonR.color.colorWidgetButtonBackground)
setDrawBorders(false)

xAxis.apply {
setDrawGridLines(true)
position = XAxis.XAxisPosition.BOTTOM
textColor = Color.DKGRAY
textColor = dynTextColor
textSize = 12F
granularity = 2F
setAvoidFirstLastClipping(true)
Expand All @@ -237,7 +238,7 @@ class GraphWidget : BaseWidgetProvider() {

axisLeft.apply {
setDrawGridLines(true)
textColor = Color.DKGRAY
textColor = dynTextColor
textSize = 12F
}

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

legend.apply {
isEnabled = true
textColor = Color.DKGRAY
textColor = dynTextColor
textSize = 12F
verticalAlignment = Legend.LegendVerticalAlignment.BOTTOM
horizontalAlignment = Legend.LegendHorizontalAlignment.RIGHT
Expand All @@ -262,6 +263,7 @@ class GraphWidget : BaseWidgetProvider() {

legend.isEnabled = true
description.isEnabled = true
description.textColor = dynTextColor
}

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

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

Expand Down Expand Up @@ -336,11 +338,11 @@ class GraphWidget : BaseWidgetProvider() {
null
}
if (attributeIds == null) {
graphWidgetRepository.updateWidgetLastUpdate(
repository.updateWidgetLastUpdate(
appWidgetId,
entity?.friendlyState(context, entityOptions) ?: graphWidgetRepository.get(appWidgetId)?.lastUpdate ?: ""
entity?.friendlyState(context, entityOptions) ?: repository.get(appWidgetId)?.lastUpdate ?: ""
)
return ResolvedText(graphWidgetRepository.get(appWidgetId)?.lastUpdate, entityCaughtException)
return ResolvedText(repository.get(appWidgetId)?.lastUpdate, entityCaughtException)
}

try {
Expand All @@ -350,12 +352,12 @@ class GraphWidget : BaseWidgetProvider() {
val lastUpdate =
entity?.friendlyState(context, entityOptions).plus(if (attributeValues.isNotEmpty()) stateSeparator else "")
.plus(attributeValues.joinToString(attributeSeparator))
graphWidgetRepository.updateWidgetLastUpdate(appWidgetId, lastUpdate)
repository.updateWidgetLastUpdate(appWidgetId, lastUpdate)
return ResolvedText(lastUpdate)
} catch (e: Exception) {
Log.e(TAG, "Unable to fetch entity state and attributes", e)
}
return ResolvedText(graphWidgetRepository.get(appWidgetId)?.lastUpdate, true)
return ResolvedText(repository.get(appWidgetId)?.lastUpdate, true)
}

override fun saveEntityConfiguration(context: Context, extras: Bundle?, appWidgetId: Int) {
Expand Down Expand Up @@ -386,8 +388,8 @@ class GraphWidget : BaseWidgetProvider() {
"entity id: " + entitySelection + System.lineSeparator() +
"attribute: " + (attributeSelection ?: "N/A")
)
if (graphWidgetRepository.get(appWidgetId) == null) {
graphWidgetRepository.add(
if (repository.get(appWidgetId) == null) {
repository.add(
GraphWidgetEntity(
id = appWidgetId,
serverId = serverId,
Expand All @@ -398,7 +400,7 @@ class GraphWidget : BaseWidgetProvider() {
stateSeparator = stateSeparatorSelection ?: "",
attributeSeparator = attributeSeparatorSelection ?: "",
tapAction = tapActionSelection,
lastUpdate = graphWidgetRepository.get(appWidgetId)?.lastUpdate ?: "",
lastUpdate = repository.get(appWidgetId)?.lastUpdate ?: "",
backgroundType = backgroundTypeSelection,
textColor = textColorSelection
)
Expand All @@ -411,7 +413,7 @@ class GraphWidget : BaseWidgetProvider() {

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

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

graphWidgetRepository.deleteEntriesOlderThan(appWidgetId, cutoffTimeMillis)
repository.deleteEntriesOlderThan(appWidgetId, cutoffTimeMillis)

graphWidgetRepository.insertGraphWidgetHistory(
repository.insertGraphWidgetHistory(
GraphWidgetHistoryEntity(
entityId = entity.entityId,
graphWidgetId = appWidgetId,
Expand All @@ -448,7 +450,7 @@ class GraphWidget : BaseWidgetProvider() {
appWidgetManager.partiallyUpdateAppWidget(appWidgetId, loadingViews)

var success = false
graphWidgetRepository.get(appWidgetId)?.let {
repository.get(appWidgetId)?.let {
try {
onEntityPressedWithoutState(
it.entityId,
Expand Down Expand Up @@ -479,7 +481,7 @@ class GraphWidget : BaseWidgetProvider() {

override fun onDeleted(context: Context, appWidgetIds: IntArray) {
widgetScope?.launch {
graphWidgetRepository.deleteAll(appWidgetIds)
repository.deleteAll(appWidgetIds)
appWidgetIds.forEach { removeSubscription(it) }
}
}
Expand Down
Loading

0 comments on commit 0a1083b

Please sign in to comment.