mirror of
https://github.com/home-assistant/android
synced 2024-10-06 16:19:38 +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
|
||||
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) {
|
||||
"closed" -> context.getString(commonR.string.state_closed)
|
||||
"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
|
||||
}
|
||||
|
||||
|
|
|
@ -90,7 +90,7 @@ import io.homeassistant.companion.android.common.R as commonR
|
|||
Server::class,
|
||||
Setting::class
|
||||
],
|
||||
version = 41,
|
||||
version = 42,
|
||||
autoMigrations = [
|
||||
AutoMigration(from = 24, to = 25),
|
||||
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 = 37, to = 38, spec = AppDatabase.Companion.Migration37to38::class),
|
||||
AutoMigration(from = 38, to = 39),
|
||||
AutoMigration(from = 39, to = 40)
|
||||
AutoMigration(from = 39, to = 40),
|
||||
AutoMigration(from = 41, to = 42)
|
||||
]
|
||||
)
|
||||
@TypeConverters(
|
||||
|
|
|
@ -15,5 +15,7 @@ data class EntityStateComplications(
|
|||
@ColumnInfo(name = "entity_id")
|
||||
val entityId: String,
|
||||
@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">Show and Share Logs</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="slider_decreased">%1$s decreased</string>
|
||||
<string name="slider_increased">%1$s increased</string>
|
||||
|
|
|
@ -58,6 +58,8 @@ class ComplicationConfigViewModel @Inject constructor(
|
|||
private set
|
||||
var entityShowTitle by mutableStateOf(true)
|
||||
private set
|
||||
var entityShowUnit by mutableStateOf(true)
|
||||
private set
|
||||
|
||||
init {
|
||||
loadEntities()
|
||||
|
@ -71,6 +73,7 @@ class ComplicationConfigViewModel @Inject constructor(
|
|||
stored?.let {
|
||||
selectedEntity = SimplifiedEntity(entityId = it.entityId)
|
||||
entityShowTitle = it.showTitle
|
||||
entityShowUnit = it.showUnit
|
||||
if (loadingState == LoadingState.READY) {
|
||||
updateSelectedEntity()
|
||||
}
|
||||
|
@ -153,9 +156,13 @@ class ComplicationConfigViewModel @Inject constructor(
|
|||
entityShowTitle = show
|
||||
}
|
||||
|
||||
fun setShowUnit(show: Boolean) {
|
||||
entityShowUnit = show
|
||||
}
|
||||
|
||||
fun addEntityStateComplication(id: Int, entity: SimplifiedEntity) {
|
||||
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 {
|
||||
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 monochromaticImage = MonochromaticImage.Builder(Icon.createWithBitmap(iconBitmap)).build()
|
||||
val tapAction = ComplicationReceiver.getComplicationToggleIntent(this, request.complicationInstanceId)
|
||||
|
|
|
@ -52,11 +52,13 @@ fun LoadConfigView(
|
|||
MainConfigView(
|
||||
entity = complicationConfigViewModel.selectedEntity,
|
||||
showTitle = complicationConfigViewModel.entityShowTitle,
|
||||
showUnit = complicationConfigViewModel.entityShowUnit,
|
||||
loadingState = complicationConfigViewModel.loadingState,
|
||||
onChooseEntityClicked = {
|
||||
swipeDismissableNavController.navigate(SCREEN_CHOOSE_ENTITY)
|
||||
},
|
||||
onShowTitleClicked = complicationConfigViewModel::setShowTitle,
|
||||
onShowUnitClicked = complicationConfigViewModel::setShowUnit,
|
||||
onAcceptClicked = onAcceptClicked
|
||||
)
|
||||
}
|
||||
|
@ -81,9 +83,11 @@ fun LoadConfigView(
|
|||
fun MainConfigView(
|
||||
entity: SimplifiedEntity?,
|
||||
showTitle: Boolean,
|
||||
showUnit: Boolean,
|
||||
loadingState: ComplicationConfigViewModel.LoadingState,
|
||||
onChooseEntityClicked: () -> Unit,
|
||||
onShowTitleClicked: (Boolean) -> Unit,
|
||||
onShowUnitClicked: (Boolean) -> Unit,
|
||||
onAcceptClicked: () -> Unit
|
||||
) {
|
||||
ThemeLazyColumn {
|
||||
|
@ -145,6 +149,26 @@ fun MainConfigView(
|
|||
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 {
|
||||
Button(
|
||||
|
@ -172,9 +196,11 @@ fun PreviewMainConfigView() {
|
|||
MainConfigView(
|
||||
entity = simplifiedEntity,
|
||||
showTitle = true,
|
||||
showUnit = true,
|
||||
loadingState = ComplicationConfigViewModel.LoadingState.READY,
|
||||
onChooseEntityClicked = {},
|
||||
onShowTitleClicked = {},
|
||||
onShowUnitClicked = {},
|
||||
onAcceptClicked = {}
|
||||
)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue