Skip to content

Commit

Permalink
Disable controls for read-only items
Browse files Browse the repository at this point in the history
Signed-off-by: mueller-ma <[email protected]>
  • Loading branch information
mueller-ma committed Feb 7, 2025
1 parent b1b094d commit 00eaa95
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
2 changes: 2 additions & 0 deletions mobile/src/main/java/org/openhab/habdroid/model/Widget.kt
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ data class Widget(
return InputTypeHint.Text
}

val readOnly get() = item?.readOnly == true

private val configuredMinValue get() = when {
rawMinValue != null -> rawMinValue
item?.minimum != null && item.type != Item.Type.Dimmer -> item.minimum
Expand Down
41 changes: 38 additions & 3 deletions mobile/src/main/java/org/openhab/habdroid/ui/WidgetAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -664,11 +664,15 @@ class WidgetAdapter(
} else {
null
}
switch.isEnabled = !widget.readOnly

isBinding = false
}

override fun handleRowClick() {
if (boundWidget?.readOnly == true) {
return
}
switch.toggle()
}
}
Expand Down Expand Up @@ -730,6 +734,7 @@ class WidgetAdapter(
}
inputText.setText(dataState)
inputText.text?.let { inputText.setSelection(it.length) }
inputText.isEnabled = !widget.readOnly

inputTextLayout.placeholderText = if (widget.state != null) "" else displayState
inputTextLayout.suffixText = when (widget.inputHint) {
Expand Down Expand Up @@ -800,6 +805,9 @@ class WidgetAdapter(

override fun handleRowClick() {
val widget = boundWidget ?: return
if (widget.readOnly) {
return
}
val dt = widget.state?.asDateTime
when (widget.inputHint) {
Widget.InputTypeHint.Date -> showDatePicker(widget, dt, false)
Expand Down Expand Up @@ -950,7 +958,8 @@ class WidgetAdapter(
iconRes = button.icon,
labelColor = button.labelColor,
iconColor = button.iconColor,
mapper = colorMapper
mapper = colorMapper,
readOnly = widget.readOnly
)
if (button.stateless == false) {
// stateful button: make checkable and set checked state afterwards
Expand Down Expand Up @@ -1017,6 +1026,7 @@ class WidgetAdapter(

val hasValidValues = widget.minValue < widget.maxValue
slider.isVisible = hasValidValues
slider.isEnabled = !widget.readOnly
if (hasValidValues) {
slider.bindToWidget(widget, widget.shouldUseSliderUpdatesDuringMove())
} else {
Expand All @@ -1026,6 +1036,9 @@ class WidgetAdapter(

override fun handleRowClick() {
val widget = boundWidget ?: return
if (widget.readOnly) {
return
}
if (widget.switchSupport) {
connection.httpClient.sendItemCommand(
widget.item,
Expand Down Expand Up @@ -1189,7 +1202,8 @@ class WidgetAdapter(
mappings.slice(0 until buttonCount).forEachIndexed { index, mapping ->
with(group[index] as MaterialButton) {
tag = mapping
setTextAndIcon(connection, mapping.label, mapping.icon)
setTextAndIcon(connection, mapping.label, mapping.icon, widget.readOnly)
isEnabled = !widget.readOnly
}
}

Expand Down Expand Up @@ -1282,7 +1296,7 @@ class WidgetAdapter(
button.isGone = mapping == null
if (mapping != null) {
button.isChecked = widget.state?.asString == mapping.value
button.setTextAndIcon(connection, mapping.label, mapping.icon)
button.setTextAndIcon(connection, mapping.label, mapping.icon, widget.readOnly)
button.tag = mapping
}
}
Expand Down Expand Up @@ -1424,6 +1438,9 @@ class WidgetAdapter(

private fun openSelection() {
val widget = boundWidget ?: return
if (widget.readOnly) {
return
}
fragmentPresenter.showBottomSheet(SliderBottomSheet(), widget)
}

Expand All @@ -1441,6 +1458,13 @@ class WidgetAdapter(
connection.httpClient.sendItemUpdate(widget.item, state.withValue(newValue))
}
}

override fun bind(widget: Widget) {
super.bind(widget)
itemView.findViewById<View>(R.id.select_button).isEnabled = !widget.readOnly
itemView.findViewById<View>(R.id.up_button).isEnabled = !widget.readOnly
itemView.findViewById<View>(R.id.down_button).isEnabled = !widget.readOnly
}
}

class ChartViewHolder internal constructor(private val initData: ViewHolderInitData) :
Expand Down Expand Up @@ -1684,6 +1708,9 @@ class WidgetAdapter(
} else {
selectColorButton.setImageDrawable(color.toColoredRoundedRect(selectColorButton.context))
}
selectColorButton.isEnabled = !widget.readOnly
upButton.isEnabled = !widget.readOnly
downButton.isEnabled = !widget.readOnly
}

override fun onClick(view: View) {
Expand Down Expand Up @@ -1712,6 +1739,9 @@ class WidgetAdapter(

override fun handleRowClick() {
val widget = boundWidget ?: return
if (widget.readOnly) {
return
}
fragmentPresenter.showBottomSheet(ColorChooserBottomSheet(), widget)
}
}
Expand All @@ -1736,6 +1766,9 @@ class WidgetAdapter(

override fun handleRowClick() {
val widget = boundWidget ?: return
if (widget.readOnly) {
return
}
fragmentPresenter.showBottomSheet(ColorTemperatureSliderBottomSheet(), widget)
}
}
Expand Down Expand Up @@ -1952,10 +1985,12 @@ fun MaterialButton.setTextAndIcon(
connection: Connection,
label: String,
iconRes: IconResource?,
readOnly: Boolean,
labelColor: String? = null,
iconColor: String? = null,
mapper: WidgetAdapter.ColorMapper? = null
) {
isEnabled = !readOnly
contentDescription = label
val iconUrl = iconRes?.toUrl(context, true)
if (iconUrl == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ class SelectionBottomSheet : AbstractWidgetBottomSheet() {
connection?.httpClient?.sendItemCommand(widget.item, mapping.value)
dismissAllowingStateLoss()
}
radio.isEnabled = !widget.readOnly
group.addView(radio)
}

Expand Down

0 comments on commit 00eaa95

Please sign in to comment.