From 3735bcb39a17487c496c29123422548a3e884408 Mon Sep 17 00:00:00 2001 From: mueller-ma Date: Sun, 1 Dec 2024 11:12:13 +0100 Subject: [PATCH] Fix text items with newline character Closes #3814 Signed-off-by: mueller-ma --- .../main/java/org/openhab/habdroid/model/Widget.kt | 12 ++++++++---- .../java/org/openhab/habdroid/model/WidgetTest.kt | 6 +++--- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/mobile/src/main/java/org/openhab/habdroid/model/Widget.kt b/mobile/src/main/java/org/openhab/habdroid/model/Widget.kt index 290f72b41c..d74942dc61 100644 --- a/mobile/src/main/java/org/openhab/habdroid/model/Widget.kt +++ b/mobile/src/main/java/org/openhab/habdroid/model/Widget.kt @@ -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 } @@ -243,8 +249,6 @@ data class Widget( state.toParsedState(item.state?.asNumber?.format) else -> state.toParsedState() } - - internal val stateLabelRegex = Regex("\\[(.*)\\]$") } } diff --git a/mobile/src/test/java/org/openhab/habdroid/model/WidgetTest.kt b/mobile/src/test/java/org/openhab/habdroid/model/WidgetTest.kt index 2ff256665f..b99bdaa080 100644 --- a/mobile/src/test/java/org/openhab/habdroid/model/WidgetTest.kt +++ b/mobile/src/test/java/org/openhab/habdroid/model/WidgetTest.kt @@ -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 @@ -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,