Skip to content

Commit

Permalink
Support switch press & release (#3734)
Browse files Browse the repository at this point in the history
Resolve #3678

Signed-off-by: Jimmy Tanagra <jcode@tanagra.id.au>
  • Loading branch information
jimtng authored Jul 16, 2024
1 parent 135eb70 commit 379dfc2
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import org.openhab.habdroid.util.optStringOrNull
@Parcelize
data class LabeledValue internal constructor(
val value: String,
val valueRelease: String?,
val label: String,
val icon: IconResource?,
val row: Int,
Expand All @@ -31,7 +32,8 @@ data class LabeledValue internal constructor(
@Throws(JSONException::class)
fun JSONObject.toLabeledValue(valueKey: String, labelKey: String): LabeledValue {
val value = getString(valueKey)
val valueRelease = optStringOrNull("releaseCommand")
val label = optString(labelKey, value)
val icon = optStringOrNull("icon")?.toOH2IconResource()
return LabeledValue(value, label, icon, optInt("row"), optInt("column"))
return LabeledValue(value, valueRelease, label, icon, optInt("row"), optInt("column"))
}
2 changes: 1 addition & 1 deletion mobile/src/main/java/org/openhab/habdroid/model/Widget.kt
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ fun Node.collectWidgets(parent: Widget?): List<Widget> {
"label" -> mappingLabel = childNode.textContent
}
}
mappings.add(LabeledValue(mappingCommand, mappingLabel, null, 0, 0))
mappings.add(LabeledValue(mappingCommand, null, mappingLabel, null, 0, 0))
}
else -> {}
}
Expand Down
50 changes: 43 additions & 7 deletions mobile/src/main/java/org/openhab/habdroid/ui/WidgetAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1109,7 +1109,8 @@ class WidgetAdapter(
R.layout.widgetlist_sectionswitchitem,
R.layout.widgetlist_sectionswitchitem_compact
),
View.OnClickListener {
View.OnClickListener,
View.OnTouchListener {
private val group: MaterialButtonToggleGroup = itemView.findViewById(R.id.switch_group)
private val overflowButton: MaterialButton = itemView.findViewById(R.id.overflow_button)
private val spareViews = mutableListOf<View>()
Expand Down Expand Up @@ -1147,6 +1148,7 @@ class WidgetAdapter(
}
val view = initData.inflater.inflate(buttonLayout, group, false)
view.setOnClickListener(this)
view.setOnTouchListener(this)
group.addView(view)
}

Expand All @@ -1160,7 +1162,7 @@ class WidgetAdapter(
// bind views
mappings.slice(0 until buttonCount).forEachIndexed { index, mapping ->
with(group[index] as MaterialButton) {
tag = mapping.value
tag = mapping
setTextAndIcon(connection, mapping.label, mapping.icon)
}
}
Expand All @@ -1174,7 +1176,7 @@ class WidgetAdapter(
val state = widget.state?.asString
val checkedId = group.children
.filter { it.id != R.id.overflow_button }
.filter { it.tag == state }
.filter { (it.tag as LabeledValue).value == state }
.map { it.id }
.firstOrNull()

Expand All @@ -1188,7 +1190,23 @@ class WidgetAdapter(
}

override fun onClick(view: View) {
connection.httpClient.sendItemCommand(boundWidget?.item, view.tag as String)
val mapping = view.tag as LabeledValue
if (mapping.valueRelease.isNullOrEmpty()) {
connection.httpClient.sendItemCommand(boundWidget?.item, mapping.value)
}
}

override fun onTouch(view: View, event: MotionEvent): Boolean {
val mapping = view.tag as LabeledValue
if (!mapping.valueRelease.isNullOrEmpty()) {
val command = when (event.action) {
MotionEvent.ACTION_DOWN -> mapping.value
MotionEvent.ACTION_UP -> mapping.valueRelease
else -> null
}
command?.let { connection.httpClient.sendItemCommand(boundWidget?.item, it) }
}
return false
}

override fun handleRowClick() {
Expand All @@ -1211,7 +1229,8 @@ class WidgetAdapter(

class SmallSectionSwitchViewHolder internal constructor(initData: ViewHolderInitData) :
LabeledItemBaseViewHolder(initData, R.layout.widgetlist_smallsectionswitch_item),
View.OnClickListener {
View.OnClickListener,
View.OnTouchListener {
private val toggles = listOf<MaterialButton>(
itemView.findViewById(R.id.switch_one),
itemView.findViewById(R.id.switch_two)
Expand All @@ -1221,6 +1240,7 @@ class WidgetAdapter(
toggles.forEach { t ->
t.isCheckable = true
t.setOnClickListener(this)
t.setOnTouchListener(this)
}
}

Expand All @@ -1232,7 +1252,7 @@ class WidgetAdapter(
if (mapping != null) {
button.isChecked = widget.state?.asString == mapping.value
button.setTextAndIcon(connection, mapping.label, mapping.icon)
button.tag = mapping.value
button.tag = mapping
}
}
applyMapping(toggles[0], widget.mappingsOrItemOptions[0])
Expand All @@ -1242,7 +1262,23 @@ class WidgetAdapter(
override fun onClick(view: View) {
// Make sure one can't uncheck buttons by clicking a checked one
(view as MaterialButton).isChecked = true
connection.httpClient.sendItemCommand(boundWidget?.item, view.tag as String)
val mapping = view.tag as LabeledValue
if (mapping.valueRelease.isNullOrEmpty()) {
connection.httpClient.sendItemCommand(boundWidget?.item, mapping.value)
}
}

override fun onTouch(view: View, event: MotionEvent): Boolean {
val mapping = view.tag as LabeledValue
if (!mapping.valueRelease.isNullOrEmpty()) {
val command = when (event.action) {
MotionEvent.ACTION_DOWN -> mapping.value
MotionEvent.ACTION_UP -> mapping.valueRelease
else -> null
}
command?.let { connection.httpClient.sendItemCommand(boundWidget?.item, it) }
}
return false
}

override fun handleRowClick() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class SuggestedCommandsFactory(private val context: Context, private val showUnd
return suggestedCommands
}

for ((value, label) in widget.mappingsOrItemOptions) {
for ((value, _, label) in widget.mappingsOrItemOptions) {
add(suggestedCommands, value, label)
}

Expand Down
4 changes: 2 additions & 2 deletions mobile/src/test/java/org/openhab/habdroid/model/ItemTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ class ItemTest {
@Test
fun getCommandOptions() {
val sut = itemWithCommandOptions.toItem()
assertEquals(LabeledValue("1", "One", "switch".toOH2IconResource(), 1, 2), sut.options!!.component1())
assertEquals(LabeledValue("2", "Two", null, 0, 0), sut.options!!.component2())
assertEquals(LabeledValue("1", null, "One", "switch".toOH2IconResource(), 1, 2), sut.options!!.component1())
assertEquals(LabeledValue("2", null, "Two", null, 0, 0), sut.options!!.component2())
}

@Test
Expand Down

0 comments on commit 379dfc2

Please sign in to comment.