Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show measurement unit in Wear OS complication #3703

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1,001 changes: 1,001 additions & 0 deletions common/schemas/io.homeassistant.companion.android.database.AppDatabase/42.json

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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()
jpelgrom marked this conversation as resolved.
Show resolved Hide resolved

if (unit?.isNotEmpty() == true) {
jpelgrom marked this conversation as resolved.
Show resolved Hide resolved
return "$friendlyState $unit"
}
}

return friendlyState
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
1 change: 1 addition & 0 deletions common/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ class ComplicationConfigViewModel @Inject constructor(
private set
var entityShowTitle by mutableStateOf(true)
private set
var entityShowUnit by mutableStateOf(false)
private set

init {
loadEntities()
Expand All @@ -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()
}
Expand Down Expand Up @@ -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))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
}
Expand All @@ -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 {
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -172,9 +196,11 @@ fun PreviewMainConfigView() {
MainConfigView(
entity = simplifiedEntity,
showTitle = true,
showUnit = true,
jpelgrom marked this conversation as resolved.
Show resolved Hide resolved
loadingState = ComplicationConfigViewModel.LoadingState.READY,
onChooseEntityClicked = {},
onShowTitleClicked = {},
onShowUnitClicked = {},
onAcceptClicked = {}
)
}