Repository: openhab/openhab-android (language: kotlin)

Issue:
Sitemaps Input widget strips off `[]` characters
### Actual behaviour

Item:
```
String TestString1
```

Sitemap:
```
sitemap test {
  Input item=TestString1
}
```

- Open that sitemap on android 3.15.0
- Enter `abcdefgh[]` into the text field, and click the checkmark (enter) on the Android's virtual keyboard
- The app will send a command `abcdefgh[]` to the item but then changed the text inside the input field to `abcdefgh` stripping away the `[]`
- The same thing would happen if `[]` is entered in the middle of the string, e.g. `abcdefg[]h` - > it strips off everything after `[]` so the text became `abcdefg` (note the `h` is also gone)
- But this "bug" doesn't happen, if `[]` is at the very beginning of the string, e.g. `[]abcdefgh` - the `[]` don't get stripped off, and neither do they get stripped off when I entered `[]abcdefgh[]`.

### Expected behaviour

It shouldn't strip off any text inputs

 
### Steps to reproduce
Described above


### Environment data
#### Client
* Android version: Android 14
* App version 3.15.0
* Build flavor Play Store
* Device language: English

#### Server
* Server version: 4.3.0-SNAPSHOT



Discussion hints:
In your title you wrote "BasicUI", but I guess you mean Sitemaps? Can you check the behavior in Basic UI (Sitemaps in browser)?
Yes I meant Sitemap, sorry. 
BasicUI (sitemaps in browser) has its own, different bug related to this: https://github.com/openhab/openhab-webui/issues/2675

Pull request: Fix stateFromLabel when label contains square brackets in pattern
Fix #3756

The problem was:
- StringItem's state contains `[]`
- Sitemap puts the item's (display) state into the label as `Label [formattedstate]`
- So now we have nested `[]` causing parsing errors when trying to extract the formattedstate from `[mytext with [] in it]`.

Test command: ./gradlew :mobile:testFossBetaDebugUnitTest --tests "org.openhab.habdroid.model.WidgetTest"

Fix patch:
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 745fb941a1..290f72b41c 100644
--- a/mobile/src/main/java/org/openhab/habdroid/model/Widget.kt
+++ b/mobile/src/main/java/org/openhab/habdroid/model/Widget.kt
@@ -73,7 +73,7 @@ data class Widget(
 ) : Parcelable {
     val label get() = rawLabel.split("[", "]")[0].trim()
     val stateFromLabel: String? get() {
-        val value = rawLabel.split("[", "]").getOrNull(1)?.trim()
+        val value = Widget.stateLabelRegex.find(rawLabel)?.groupValues?.getOrNull(1)
         val optionLabel = mappingsOrItemOptions.find { it.value == value }?.label
         return optionLabel ?: value
     }
@@ -243,6 +243,8 @@ data class Widget(
                 state.toParsedState(item.state?.asNumber?.format)
             else -> state.toParsedState()
         }
+
+        internal val stateLabelRegex = Regex("\\[(.*)\\]$")
     }
 }
 


Test patch:
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 f64452caed..2ff256665f 100644
--- a/mobile/src/test/java/org/openhab/habdroid/model/WidgetTest.kt
+++ b/mobile/src/test/java/org/openhab/habdroid/model/WidgetTest.kt
@@ -46,7 +46,7 @@ class WidgetTest {
         assertEquals(sutXml.size, 2)
         assertEquals(sut1.size, 2)
         assertEquals(sut2.size, 1)
-        assertEquals(sut3.size, 4)
+        assertEquals(sut3.size, 5)
     }
 
     @Test
@@ -170,6 +170,7 @@ class WidgetTest {
         assertNull(sut1[0].stateFromLabel)
         assertNull(sut2[0].stateFromLabel)
         assertEquals("81 %", sut3[1].stateFromLabel)
+        assertEquals("Value [42]", sut3[4].stateFromLabel)
     }
 
     @Test
@@ -445,6 +446,26 @@ class WidgetTest {
                       'timeout': false
                     },
                     'widgets': []
+                  }, {
+                    'widgetId': '0202_0_0_1',
+                    'type': 'Switch',
+                    'label': 'Test [Value [42]]',
+                    'icon': 'input',
+                    'mappings': [],
+                    'item': {
+                      'link': 'http://openhab.local:8080/rest/items/DemoString',
+                      'state': 'Value [42]',
+                      'stateDescription': {
+                          'pattern': '%s',
+                          'readOnly': false,
+                          'options': []
+                      },
+                    'type': 'String',
+                      'name': 'DemoString',
+                      'tags': [],
+                      'groupNames': []
+                    },
+                    'widgets': []
                   } ]
                 }
                 """