Skip to content

Commit

Permalink
Fix text items with newline character
Browse files Browse the repository at this point in the history
Closes openhab#3814

Signed-off-by: mueller-ma <[email protected]>
  • Loading branch information
mueller-ma committed Dec 2, 2024
1 parent ec93eec commit 3735bcb
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
12 changes: 8 additions & 4 deletions mobile/src/main/java/org/openhab/habdroid/model/Widget.kt
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,15 @@ data class Widget(
val visibility: Boolean,
val rawInputHint: InputTypeHint?
) : Parcelable {
val label get() = rawLabel.split("[", "]")[0].trim()
val label get() = rawLabel.substringBefore("[").trim()
val stateFromLabel: String? get() {
val value = Widget.stateLabelRegex.find(rawLabel)?.groupValues?.getOrNull(1)
val value: String? = rawLabel.removePrefix(label).trim().let {
if (it.startsWith("[") && it.endsWith("]")) {
it.removePrefix("[").removeSuffix("]")
} else {
null
}
}
val optionLabel = mappingsOrItemOptions.find { it.value == value }?.label
return optionLabel ?: value
}
Expand Down Expand Up @@ -243,8 +249,6 @@ data class Widget(
state.toParsedState(item.state?.asNumber?.format)
else -> state.toParsedState()
}

internal val stateLabelRegex = Regex("\\[(.*)\\]$")
}
}

Expand Down
6 changes: 3 additions & 3 deletions mobile/src/test/java/org/openhab/habdroid/model/WidgetTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ class WidgetTest {
assertNull(sut1[0].stateFromLabel)
assertNull(sut2[0].stateFromLabel)
assertEquals("81 %", sut3[1].stateFromLabel)
assertEquals("Value [42]", sut3[4].stateFromLabel)
assertEquals("Value [42]\n", sut3[4].stateFromLabel)
}

@Test
Expand Down Expand Up @@ -449,12 +449,12 @@ class WidgetTest {
}, {
'widgetId': '0202_0_0_1',
'type': 'Switch',
'label': 'Test [Value [42]]',
'label': 'Test [Value [42]\n]',
'icon': 'input',
'mappings': [],
'item': {
'link': 'http://openhab.local:8080/rest/items/DemoString',
'state': 'Value [42]',
'state': 'Value [42]\n',
'stateDescription': {
'pattern': '%s',
'readOnly': false,
Expand Down

0 comments on commit 3735bcb

Please sign in to comment.