Skip to content

Commit

Permalink
Implement icon in button mappings (#3520)
Browse files Browse the repository at this point in the history
Closes #3493

Signed-off-by: mueller-ma <[email protected]>
  • Loading branch information
mueller-ma authored Nov 24, 2023
1 parent 9a753f1 commit 4cc6c32
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .idea/kotlinc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 7 additions & 4 deletions mobile/src/main/java/org/openhab/habdroid/model/LabeledValue.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@ import android.os.Parcelable
import kotlinx.parcelize.Parcelize
import org.json.JSONException
import org.json.JSONObject
import org.openhab.habdroid.util.optStringOrNull

@Parcelize
data class LabeledValue internal constructor(val value: String, val label: String) : Parcelable
data class LabeledValue internal constructor(val value: String, val label: String, val icon: IconResource?) : Parcelable

@Throws(JSONException::class)
fun JSONObject.toLabeledValue(keyName: String, valueName: String): LabeledValue {
val value = getString(keyName)
return LabeledValue(value, optString(valueName, value))
fun JSONObject.toLabeledValue(valueKey: String, labelKey: String): LabeledValue {
val value = getString(valueKey)
val label = optString(labelKey, value)
val icon = optStringOrNull("icon")?.toOH2IconResource()
return LabeledValue(value, label, icon)
}
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 @@ -286,7 +286,7 @@ fun Node.collectWidgets(parent: Widget?): List<Widget> {
"label" -> mappingLabel = childNode.textContent
}
}
mappings.add(LabeledValue(mappingCommand, mappingLabel))
mappings.add(LabeledValue(mappingCommand, mappingLabel, null))
}
else -> {}
}
Expand Down
35 changes: 35 additions & 0 deletions mobile/src/main/java/org/openhab/habdroid/ui/ViewExtensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package org.openhab.habdroid.ui
import android.annotation.SuppressLint
import android.content.Context
import android.os.Build
import android.util.Log
import android.view.View
import android.view.inputmethod.InputMethodManager
import android.webkit.WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE
Expand All @@ -26,11 +27,21 @@ import android.widget.ImageView
import android.widget.RemoteViews
import androidx.appcompat.widget.TooltipCompat
import androidx.core.graphics.drawable.DrawableCompat
import androidx.core.graphics.drawable.toDrawable
import androidx.core.net.toUri
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.google.android.material.button.MaterialButton
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import okhttp3.HttpUrl
import org.openhab.habdroid.R
import org.openhab.habdroid.core.connection.Connection
import org.openhab.habdroid.model.LabeledValue
import org.openhab.habdroid.util.HttpClient
import org.openhab.habdroid.util.ImageConversionPolicy
import org.openhab.habdroid.util.openInBrowser
import org.openhab.habdroid.util.resolveThemedColor

Expand Down Expand Up @@ -111,3 +122,27 @@ fun RemoteViews.duplicate(): RemoteViews {
clone()
}
}

fun MaterialButton.setTextAndIcon(connection: Connection, mapping: LabeledValue) {
val iconUrl = mapping.icon?.toUrl(context, true)
if (iconUrl == null) {
icon = null
text = mapping.label
return
}
val iconSize = context.resources.getDimensionPixelSize(R.dimen.section_switch_icon)
CoroutineScope(Dispatchers.IO + Job()).launch {
val drawable = try {
connection.httpClient.get(iconUrl, caching = HttpClient.CachingMode.DEFAULT)
.asBitmap(iconSize, 0, ImageConversionPolicy.ForceTargetSize).response
.toDrawable(resources)
} catch (e: HttpClient.HttpException) {
Log.d(WidgetAdapter.TAG, "Error getting icon for button", e)
null
}
withContext(Dispatchers.Main) {
icon = drawable
text = if (drawable == null) mapping.label else null
}
}
}
4 changes: 2 additions & 2 deletions mobile/src/main/java/org/openhab/habdroid/ui/WidgetAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -967,8 +967,8 @@ class WidgetAdapter(
// bind views
mappings.slice(0 until buttonCount).forEachIndexed { index, mapping ->
with(group[index] as MaterialButton) {
text = mapping.label
tag = mapping.value
setTextAndIcon(connection, mapping)
}
}

Expand Down Expand Up @@ -1037,8 +1037,8 @@ class WidgetAdapter(
val applyMapping = { button: MaterialButton, mapping: LabeledValue? ->
button.isGone = mapping == null
if (mapping != null) {
button.text = mapping.label
button.isChecked = widget.state?.asString == mapping.value
button.setTextAndIcon(connection, mapping)
button.tag = mapping.value
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@
android:textAppearance="?attr/textAppearanceLabelMedium"
app:toggleCheckedStateOnClick="false"
tools:layout_width="wrap_content"
app:iconGravity="textStart"
app:iconPadding="0dp"
tools:text="Action 1" />
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@
android:paddingBottom="2dp"
android:textAppearance="?attr/textAppearanceLabelMedium"
app:toggleCheckedStateOnClick="false"
app:iconGravity="textStart"
app:iconPadding="0dp"
tools:text="Action 1" />
1 change: 1 addition & 0 deletions mobile/src/main/res/values/dimens.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<dimen name="notificationlist_icon_size">40dp</dimen>
<dimen name="small_widget_threshold">60dp</dimen>
<dimen name="outlineButtonStrokeWidth">2dp</dimen>
<dimen name="section_switch_icon">16dp</dimen>

<integer name="section_switch_max_buttons">4</integer>
</resources>
7 changes: 4 additions & 3 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"), sut.options!!.component1())
assertEquals(LabeledValue("2", "Two"), sut.options!!.component2())
assertEquals(LabeledValue("1", "One", "switch".toOH2IconResource()), sut.options!!.component1())
assertEquals(LabeledValue("2", "Two", null), sut.options!!.component2())
}

@Test
Expand Down Expand Up @@ -208,7 +208,8 @@ class ItemTest {
'commandOptions': [
{
'command': '1',
'label': 'One'
'label': 'One',
'icon': 'switch'
},
{
'command': '2',
Expand Down

0 comments on commit 4cc6c32

Please sign in to comment.