Skip to content

Commit b1a9310

Browse files
committed
Respect state description pattern when formatting number input
Fixes #4000 Signed-off-by: Danny Baumann <dannybaumann@web.de>
1 parent af29c0a commit b1a9310

2 files changed

Lines changed: 18 additions & 10 deletions

File tree

mobile/src/main/java/org/openhab/habdroid/model/ParsedState.kt

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -173,26 +173,30 @@ data class ParsedState internal constructor(
173173
val unit: String? = null,
174174
val format: String? = null
175175
) : Parcelable {
176-
override fun toString() = toString(Locale.getDefault())
176+
override fun toString() = toString(Locale.getDefault(), false)
177177

178178
/**
179179
* Like [toString][.toString], but using a specific locale for formatting.
180180
*/
181-
fun toString(locale: Locale): String {
181+
fun toString(locale: Locale, stripUnit: Boolean): String {
182182
if (!format.isNullOrEmpty()) {
183-
val actualFormat = format
184-
.replace("%unit%", unit.orEmpty())
185-
// In case of 'one' unit, the unit is part of the format pattern, but not part of the value
186-
// sent by the server. Avoid ending the value with a space in that case.
187-
.trim()
183+
val actualFormat = if (stripUnit) {
184+
format.split(" ").first()
185+
} else {
186+
format
187+
.replace("%unit%", unit.orEmpty())
188+
// In case of 'one' unit, the unit is part of the format pattern, but not part of the value
189+
// sent by the server. Avoid ending the value with a space in that case.
190+
.trim()
191+
}
188192
try {
189193
return String.format(locale, actualFormat, getActualValue())
190194
} catch (e: IllegalFormatException) {
191195
// State format pattern doesn't match the actual data type
192196
// -> ignore and fall back to our own formatting
193197
}
194198
}
195-
return if (unit == null) formatValue() else "${formatValue()} $unit"
199+
return if (unit == null || stripUnit) formatValue() else "${formatValue()} $unit"
196200
}
197201

198202
fun formatValue(): String = getActualValue().toString()

mobile/src/main/java/org/openhab/habdroid/ui/WidgetAdapter.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -777,8 +777,12 @@ class WidgetAdapter(
777777

778778
val dataState = when {
779779
widget.state == null -> ""
780-
widget.inputHint == Widget.InputTypeHint.Number -> widget.state.asNumber?.formatValue()
780+
781+
widget.inputHint == Widget.InputTypeHint.Number ->
782+
widget.state.asNumber?.toString(Locale.getDefault(), true)
783+
781784
displayState.isNotEmpty() -> displayState
785+
782786
else -> widget.state.asString
783787
}
784788
binding.inputvalue.apply {
@@ -2153,7 +2157,7 @@ fun ParsedState.NumberState?.toItemCommand(item: Item?): String? {
21532157
}
21542158
return if (item.isOfTypeOrGroupType(Item.Type.NumberWithDimension)) {
21552159
// For number items, include unit (if present) in command
2156-
toString(Locale.US)
2160+
toString(Locale.US, false)
21572161
} else {
21582162
// For all other items, send the plain value
21592163
formatValue()

0 commit comments

Comments
 (0)