Skip to content

Commit

Permalink
Simplify icon state selection logic
Browse files Browse the repository at this point in the history
Signed-off-by: Danny Baumann <[email protected]>
  • Loading branch information
maniac103 committed Aug 14, 2023
1 parent fd67b8b commit 603b543
Showing 1 changed file with 23 additions and 23 deletions.
46 changes: 23 additions & 23 deletions mobile/src/main/java/org/openhab/habdroid/model/IconResource.kt
Original file line number Diff line number Diff line change
Expand Up @@ -135,36 +135,36 @@ internal fun String?.toOH2WidgetIconResource(
return null
}

val itemState = item?.state
var iconState = if (state?.asNumber != null) state.asNumber.toString() else state?.asString.orEmpty()
if (itemState != null && useState) {
if (item.isOfTypeOrGroupType(Item.Type.Color)) {
// For items that control a color item fetch the correct icon
if (type == Widget.Type.Slider || type == Widget.Type.Switch && !hasMappings) {
try {
iconState = itemState.asBrightness.toString()
if (type == Widget.Type.Switch) {
iconState = if (iconState == "0") "OFF" else "ON"
}
} catch (e: Exception) {
iconState = "OFF"
}
} else if (itemState.asHsv != null) {
val color = itemState.asHsv.toColor()
iconState = String.format(
Locale.US, "#%02x%02x%02x",
Color.red(color), Color.green(color), Color.blue(color)
)
val stateToUse = state ?: item?.state
val iconState = when {
stateToUse == null || !useState || item == null -> null
// Number items need to use state formatted as per their state description
item.isOfTypeOrGroupType(Item.Type.Number) || item.isOfTypeOrGroupType(Item.Type.NumberWithDimension)-> {
stateToUse.asNumber.toString()
}
item.isOfTypeOrGroupType(Item.Type.Color) -> when {
// Color sliders just use the brightness part of the color
type == Widget.Type.Slider -> stateToUse.asBrightness.toString()
// Color toggles should behave similarly to the logic below (but using the brightness value)
type == Widget.Type.Switch && !hasMappings && stateToUse.asBrightness != null -> {
if (stateToUse.asBrightness > 0) "ON" else "OFF"
}
} else if (type == Widget.Type.Switch && !hasMappings && !item.isOfTypeOrGroupType(Item.Type.Rollershutter)) {
stateToUse.asHsv != null -> {
val color = stateToUse.asHsv.toColor()
String.format(Locale.US, "#%02x%02x%02x", Color.red(color), Color.green(color), Color.blue(color))
}
else -> stateToUse.asString
}
type == Widget.Type.Switch && !hasMappings && !item.isOfTypeOrGroupType(Item.Type.Rollershutter) -> {
// For switch items without mappings (just ON and OFF) that control a dimmer item
// and which are not ON or OFF already, set the state to "OFF" instead of 0
// or to "ON" to fetch the correct icon
iconState = if (itemState.asString == "0" || itemState.asString == "OFF") "OFF" else "ON"
if (stateToUse.asString == "0" || stateToUse.asString == "OFF") "OFF" else "ON"
}
else -> stateToUse.asString
}

return IconResource(this, true, iconState)
return IconResource(this, true, iconState.orEmpty())
}

enum class IconFormat {
Expand Down

0 comments on commit 603b543

Please sign in to comment.