Skip to content

Commit

Permalink
Extend existing friendlyState method
Browse files Browse the repository at this point in the history
  • Loading branch information
slovdahl committed Jul 24, 2023
1 parent 693d381 commit 6889c71
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
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()

if (unit?.isNotEmpty() == true) {
return "$friendlyState $unit"
}
}

return friendlyState
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,13 @@ class EntityStateDataSourceService : SuspendingComplicationDataSourceService() {
null
}

var stateText = entity.friendlyState(this, entityOptions)
if (entity.attributes.containsKey("unit_of_measurement")) {
stateText += " " + entity.attributes["unit_of_measurement"]
}

val text = PlainComplicationText.Builder(stateText).build()
val text = PlainComplicationText.Builder(
entity.friendlyState(
this,
entityOptions,
appendUnitOfMeasurement = true
)
).build()

val contentDescription = PlainComplicationText.Builder(getText(R.string.complication_entity_state_content_description)).build()
val monochromaticImage = MonochromaticImage.Builder(Icon.createWithBitmap(iconBitmap)).build()
Expand Down

0 comments on commit 6889c71

Please sign in to comment.