mirror of
https://github.com/home-assistant/android
synced 2024-07-22 10:54:12 +00:00
Show measurement unit in Wear OS complication (#3703)
* Show measurement unit in Wear OS complication * Extend existing `friendlyState` method * Make showing unit configurable (defaulting to false) * Feedback fixes
This commit is contained in:
parent
6744b61a80
commit
3db34ec408
File diff suppressed because it is too large
Load diff
|
@ -604,7 +604,7 @@ suspend fun <T> Entity<T>.onPressed(
|
||||||
val <T> Entity<T>.friendlyName: String
|
val <T> Entity<T>.friendlyName: String
|
||||||
get() = (attributes as? Map<*, *>)?.get("friendly_name")?.toString() ?: entityId
|
get() = (attributes as? Map<*, *>)?.get("friendly_name")?.toString() ?: entityId
|
||||||
|
|
||||||
fun <T> Entity<T>.friendlyState(context: Context, options: EntityRegistryOptions? = null): String {
|
fun <T> Entity<T>.friendlyState(context: Context, options: EntityRegistryOptions? = null, appendUnitOfMeasurement: Boolean = false): String {
|
||||||
var friendlyState = when (state) {
|
var friendlyState = when (state) {
|
||||||
"closed" -> context.getString(commonR.string.state_closed)
|
"closed" -> context.getString(commonR.string.state_closed)
|
||||||
"closing" -> context.getString(commonR.string.state_closing)
|
"closing" -> context.getString(commonR.string.state_closing)
|
||||||
|
@ -649,6 +649,15 @@ fun <T> Entity<T>.friendlyState(context: Context, options: EntityRegistryOptions
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (appendUnitOfMeasurement) {
|
||||||
|
val unit = (attributes as? Map<*, *>)?.get("unit_of_measurement")?.toString()
|
||||||
|
|
||||||
|
if (unit?.isNotBlank() == true) {
|
||||||
|
return "$friendlyState $unit"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return friendlyState
|
return friendlyState
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -90,7 +90,7 @@ import io.homeassistant.companion.android.common.R as commonR
|
||||||
Server::class,
|
Server::class,
|
||||||
Setting::class
|
Setting::class
|
||||||
],
|
],
|
||||||
version = 41,
|
version = 42,
|
||||||
autoMigrations = [
|
autoMigrations = [
|
||||||
AutoMigration(from = 24, to = 25),
|
AutoMigration(from = 24, to = 25),
|
||||||
AutoMigration(from = 25, to = 26),
|
AutoMigration(from = 25, to = 26),
|
||||||
|
@ -107,7 +107,8 @@ import io.homeassistant.companion.android.common.R as commonR
|
||||||
AutoMigration(from = 36, to = 37, spec = AppDatabase.Companion.Migration36to37::class),
|
AutoMigration(from = 36, to = 37, spec = AppDatabase.Companion.Migration36to37::class),
|
||||||
AutoMigration(from = 37, to = 38, spec = AppDatabase.Companion.Migration37to38::class),
|
AutoMigration(from = 37, to = 38, spec = AppDatabase.Companion.Migration37to38::class),
|
||||||
AutoMigration(from = 38, to = 39),
|
AutoMigration(from = 38, to = 39),
|
||||||
AutoMigration(from = 39, to = 40)
|
AutoMigration(from = 39, to = 40),
|
||||||
|
AutoMigration(from = 41, to = 42)
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
@TypeConverters(
|
@TypeConverters(
|
||||||
|
|
|
@ -15,5 +15,7 @@ data class EntityStateComplications(
|
||||||
@ColumnInfo(name = "entity_id")
|
@ColumnInfo(name = "entity_id")
|
||||||
val entityId: String,
|
val entityId: String,
|
||||||
@ColumnInfo(name = "show_title", defaultValue = "1")
|
@ColumnInfo(name = "show_title", defaultValue = "1")
|
||||||
val showTitle: Boolean
|
val showTitle: Boolean,
|
||||||
|
@ColumnInfo(name = "show_unit", defaultValue = "0")
|
||||||
|
val showUnit: Boolean
|
||||||
)
|
)
|
||||||
|
|
|
@ -742,6 +742,7 @@
|
||||||
<string name="show_share_logs_summary">Sharing logs with the Home Assistant team will help to solve issues. Please share the logs only if you have been asked to do so by a Home Assistant developer</string>
|
<string name="show_share_logs_summary">Sharing logs with the Home Assistant team will help to solve issues. Please share the logs only if you have been asked to do so by a Home Assistant developer</string>
|
||||||
<string name="show_share_logs">Show and Share Logs</string>
|
<string name="show_share_logs">Show and Share Logs</string>
|
||||||
<string name="show_entity_title">Show entity name</string>
|
<string name="show_entity_title">Show entity name</string>
|
||||||
|
<string name="show_unit_title">Show entity unit</string>
|
||||||
<string name="sign_in_on_phone">Sign in on phone</string>
|
<string name="sign_in_on_phone">Sign in on phone</string>
|
||||||
<string name="slider_decreased">%1$s decreased</string>
|
<string name="slider_decreased">%1$s decreased</string>
|
||||||
<string name="slider_increased">%1$s increased</string>
|
<string name="slider_increased">%1$s increased</string>
|
||||||
|
|
|
@ -58,6 +58,8 @@ class ComplicationConfigViewModel @Inject constructor(
|
||||||
private set
|
private set
|
||||||
var entityShowTitle by mutableStateOf(true)
|
var entityShowTitle by mutableStateOf(true)
|
||||||
private set
|
private set
|
||||||
|
var entityShowUnit by mutableStateOf(true)
|
||||||
|
private set
|
||||||
|
|
||||||
init {
|
init {
|
||||||
loadEntities()
|
loadEntities()
|
||||||
|
@ -71,6 +73,7 @@ class ComplicationConfigViewModel @Inject constructor(
|
||||||
stored?.let {
|
stored?.let {
|
||||||
selectedEntity = SimplifiedEntity(entityId = it.entityId)
|
selectedEntity = SimplifiedEntity(entityId = it.entityId)
|
||||||
entityShowTitle = it.showTitle
|
entityShowTitle = it.showTitle
|
||||||
|
entityShowUnit = it.showUnit
|
||||||
if (loadingState == LoadingState.READY) {
|
if (loadingState == LoadingState.READY) {
|
||||||
updateSelectedEntity()
|
updateSelectedEntity()
|
||||||
}
|
}
|
||||||
|
@ -153,9 +156,13 @@ class ComplicationConfigViewModel @Inject constructor(
|
||||||
entityShowTitle = show
|
entityShowTitle = show
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun setShowUnit(show: Boolean) {
|
||||||
|
entityShowUnit = show
|
||||||
|
}
|
||||||
|
|
||||||
fun addEntityStateComplication(id: Int, entity: SimplifiedEntity) {
|
fun addEntityStateComplication(id: Int, entity: SimplifiedEntity) {
|
||||||
viewModelScope.launch {
|
viewModelScope.launch {
|
||||||
entityStateComplicationsDao.add(EntityStateComplications(id, entity.entityId, entityShowTitle))
|
entityStateComplicationsDao.add(EntityStateComplications(id, entity.entityId, entityShowTitle, entityShowUnit))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -79,7 +79,15 @@ class EntityStateDataSourceService : SuspendingComplicationDataSourceService() {
|
||||||
} else {
|
} else {
|
||||||
null
|
null
|
||||||
}
|
}
|
||||||
val text = PlainComplicationText.Builder(entity.friendlyState(this, entityOptions)).build()
|
|
||||||
|
val text = PlainComplicationText.Builder(
|
||||||
|
entity.friendlyState(
|
||||||
|
this,
|
||||||
|
entityOptions,
|
||||||
|
appendUnitOfMeasurement = settings.showUnit
|
||||||
|
)
|
||||||
|
).build()
|
||||||
|
|
||||||
val contentDescription = PlainComplicationText.Builder(getText(R.string.complication_entity_state_content_description)).build()
|
val contentDescription = PlainComplicationText.Builder(getText(R.string.complication_entity_state_content_description)).build()
|
||||||
val monochromaticImage = MonochromaticImage.Builder(Icon.createWithBitmap(iconBitmap)).build()
|
val monochromaticImage = MonochromaticImage.Builder(Icon.createWithBitmap(iconBitmap)).build()
|
||||||
val tapAction = ComplicationReceiver.getComplicationToggleIntent(this, request.complicationInstanceId)
|
val tapAction = ComplicationReceiver.getComplicationToggleIntent(this, request.complicationInstanceId)
|
||||||
|
|
|
@ -52,11 +52,13 @@ fun LoadConfigView(
|
||||||
MainConfigView(
|
MainConfigView(
|
||||||
entity = complicationConfigViewModel.selectedEntity,
|
entity = complicationConfigViewModel.selectedEntity,
|
||||||
showTitle = complicationConfigViewModel.entityShowTitle,
|
showTitle = complicationConfigViewModel.entityShowTitle,
|
||||||
|
showUnit = complicationConfigViewModel.entityShowUnit,
|
||||||
loadingState = complicationConfigViewModel.loadingState,
|
loadingState = complicationConfigViewModel.loadingState,
|
||||||
onChooseEntityClicked = {
|
onChooseEntityClicked = {
|
||||||
swipeDismissableNavController.navigate(SCREEN_CHOOSE_ENTITY)
|
swipeDismissableNavController.navigate(SCREEN_CHOOSE_ENTITY)
|
||||||
},
|
},
|
||||||
onShowTitleClicked = complicationConfigViewModel::setShowTitle,
|
onShowTitleClicked = complicationConfigViewModel::setShowTitle,
|
||||||
|
onShowUnitClicked = complicationConfigViewModel::setShowUnit,
|
||||||
onAcceptClicked = onAcceptClicked
|
onAcceptClicked = onAcceptClicked
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -81,9 +83,11 @@ fun LoadConfigView(
|
||||||
fun MainConfigView(
|
fun MainConfigView(
|
||||||
entity: SimplifiedEntity?,
|
entity: SimplifiedEntity?,
|
||||||
showTitle: Boolean,
|
showTitle: Boolean,
|
||||||
|
showUnit: Boolean,
|
||||||
loadingState: ComplicationConfigViewModel.LoadingState,
|
loadingState: ComplicationConfigViewModel.LoadingState,
|
||||||
onChooseEntityClicked: () -> Unit,
|
onChooseEntityClicked: () -> Unit,
|
||||||
onShowTitleClicked: (Boolean) -> Unit,
|
onShowTitleClicked: (Boolean) -> Unit,
|
||||||
|
onShowUnitClicked: (Boolean) -> Unit,
|
||||||
onAcceptClicked: () -> Unit
|
onAcceptClicked: () -> Unit
|
||||||
) {
|
) {
|
||||||
ThemeLazyColumn {
|
ThemeLazyColumn {
|
||||||
|
@ -145,6 +149,26 @@ fun MainConfigView(
|
||||||
enabled = loaded && entity != null
|
enabled = loaded && entity != null
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
item {
|
||||||
|
val isChecked = !loaded || showUnit
|
||||||
|
ToggleChip(
|
||||||
|
checked = isChecked,
|
||||||
|
onCheckedChange = onShowUnitClicked,
|
||||||
|
label = { Text(stringResource(R.string.show_unit_title)) },
|
||||||
|
toggleControl = {
|
||||||
|
Icon(
|
||||||
|
imageVector = ToggleChipDefaults.switchIcon(isChecked),
|
||||||
|
contentDescription = if (isChecked) {
|
||||||
|
stringResource(R.string.enabled)
|
||||||
|
} else {
|
||||||
|
stringResource(R.string.disabled)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
},
|
||||||
|
modifier = Modifier.fillMaxWidth(),
|
||||||
|
enabled = loaded && entity != null
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
item {
|
item {
|
||||||
Button(
|
Button(
|
||||||
|
@ -172,9 +196,11 @@ fun PreviewMainConfigView() {
|
||||||
MainConfigView(
|
MainConfigView(
|
||||||
entity = simplifiedEntity,
|
entity = simplifiedEntity,
|
||||||
showTitle = true,
|
showTitle = true,
|
||||||
|
showUnit = true,
|
||||||
loadingState = ComplicationConfigViewModel.LoadingState.READY,
|
loadingState = ComplicationConfigViewModel.LoadingState.READY,
|
||||||
onChooseEntityClicked = {},
|
onChooseEntityClicked = {},
|
||||||
onShowTitleClicked = {},
|
onShowTitleClicked = {},
|
||||||
|
onShowUnitClicked = {},
|
||||||
onAcceptClicked = {}
|
onAcceptClicked = {}
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue