Skip to content

Commit 92565c1

Browse files
implememt as enum
Signed-off-by: Holger Friedrich <[email protected]>
1 parent dcae562 commit 92565c1

File tree

1 file changed

+18
-3
lines changed
  • mobile/src/main/java/org/openhab/habdroid/model

1 file changed

+18
-3
lines changed

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

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ data class Widget(
6565
val legend: Boolean?,
6666
val forceAsItem: Boolean,
6767
val yAxisDecimalPattern: String?,
68-
val interpolation: String?,
68+
val interpolation: Interpolation?,
6969
val switchSupport: Boolean,
7070
val releaseOnly: Boolean?,
7171
val height: Int,
@@ -150,6 +150,13 @@ data class Widget(
150150
Datetime
151151
}
152152

153+
enum class Interpolation {
154+
Linear,
155+
Step;
156+
157+
override fun toString(): String = name.lowercase()
158+
}
159+
153160
enum class LabelSource {
154161
Unknown,
155162
ItemLabel,
@@ -186,7 +193,7 @@ data class Widget(
186193
chartTheme?.let { chartUrl.appendQueryParameter("theme", it.toString()) }
187194
forcedLegend?.let { chartUrl.appendQueryParameter("legend", it) }
188195
yAxisDecimalPattern?.let { chartUrl.appendQueryParameter("yAxisDecimalPattern", it) }
189-
interpolation?.let { chartUrl.appendQueryParameter("interpolation", it) }
196+
interpolation?.let { chartUrl.appendQueryParameter("interpolation", it.toString()) }
190197

191198
if (width > 0) {
192199
chartUrl.appendQueryParameter("w", width / resDivider)
@@ -277,6 +284,14 @@ fun String?.toInputHint(): Widget.InputTypeHint? = this?.let { value ->
277284
}
278285
}
279286

287+
fun String?.toInterpolation(): Widget.Interpolation? = this?.let { value ->
288+
try {
289+
return Widget.Interpolation.valueOf(value.lowercase().replaceFirstChar { c -> c.uppercase() })
290+
} catch (e: IllegalArgumentException) {
291+
return null
292+
}
293+
}
294+
280295
fun String?.toLabelSource(): Widget.LabelSource = when (this) {
281296
"SITEMAP_WIDGET" -> Widget.LabelSource.SitemapDefinition
282297
"ITEM_LABEL" -> Widget.LabelSource.ItemLabel
@@ -436,7 +451,7 @@ fun JSONObject.collectWidgets(parent: Widget?): List<Widget> {
436451
legend = optBooleanOrNull("legend"),
437452
forceAsItem = optBoolean("forceAsItem", false),
438453
yAxisDecimalPattern = optString("yAxisDecimalPattern"),
439-
interpolation = optString("interpolation"),
454+
interpolation = optString("interpolation").toInterpolation(),
440455
switchSupport = optBoolean("switchSupport", false),
441456
releaseOnly = optBooleanOrNull("releaseOnly"),
442457
height = optInt("height"),

0 commit comments

Comments
 (0)