Skip to content

Commit

Permalink
Simplify MaterialButton.setTextAndIcon() logic
Browse files Browse the repository at this point in the history
Also fix BlendModeColorFilter only being available on API 29+

Signed-off-by: Danny Baumann <[email protected]>
  • Loading branch information
maniac103 committed Jul 5, 2024
1 parent dfb20a0 commit 28f4685
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 55 deletions.
54 changes: 0 additions & 54 deletions mobile/src/main/java/org/openhab/habdroid/ui/ViewExtensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ package org.openhab.habdroid.ui

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.BlendMode
import android.graphics.BlendModeColorFilter
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 @@ -29,21 +26,11 @@ 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.IconResource
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 @@ -120,44 +107,3 @@ fun RemoteViews.duplicate(): RemoteViews {
clone()
}
}

fun MaterialButton.setTextAndIcon(
connection: Connection,
label: String,
icon: IconResource?,
labelColor: String? = null,
iconColor: String? = null,
mapper: WidgetAdapter.ColorMapper? = null
) {
contentDescription = label
val iconUrl = icon?.toUrl(context, true)
if (iconUrl == null) {
this.icon = null
text = label
mapper?.mapColor(labelColor)?.let { setTextColor(it) }
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) {
if (drawable != null) {
mapper?.mapColor(iconColor)?.let {
drawable.setColorFilter(BlendModeColorFilter(it, BlendMode.SRC_ATOP))
}
text = null
} else {
text = label
mapper?.mapColor(labelColor)?.let { setTextColor(it) }
}
this@setTextAndIcon.icon = drawable
}
}
}
49 changes: 48 additions & 1 deletion mobile/src/main/java/org/openhab/habdroid/ui/WidgetAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ import androidx.annotation.StringRes
import androidx.annotation.VisibleForTesting
import androidx.core.content.ContextCompat
import androidx.core.content.edit
import androidx.core.graphics.BlendModeColorFilterCompat
import androidx.core.graphics.BlendModeCompat
import androidx.core.graphics.drawable.toDrawable
import androidx.core.view.children
import androidx.core.view.get
import androidx.core.view.isGone
Expand Down Expand Up @@ -90,8 +93,10 @@ import kotlinx.coroutines.cancel
import kotlinx.coroutines.delay
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.openhab.habdroid.R
import org.openhab.habdroid.core.connection.Connection
import org.openhab.habdroid.model.IconResource
import org.openhab.habdroid.model.Item
import org.openhab.habdroid.model.LabeledValue
import org.openhab.habdroid.model.ParsedState
Expand All @@ -104,6 +109,7 @@ import org.openhab.habdroid.ui.widget.WidgetSlider
import org.openhab.habdroid.util.CacheManager
import org.openhab.habdroid.util.HttpClient
import org.openhab.habdroid.util.IconBackground
import org.openhab.habdroid.util.ImageConversionPolicy
import org.openhab.habdroid.util.MjpegStreamer
import org.openhab.habdroid.util.PrefKeys
import org.openhab.habdroid.util.beautify
Expand Down Expand Up @@ -879,7 +885,7 @@ class WidgetAdapter(
buttonView.setTextAndIcon(
connection = connection,
label = button.label,
icon = button.icon,
iconRes = button.icon,
labelColor = button.labelColor,
iconColor = button.iconColor,
mapper = colorMapper
Expand Down Expand Up @@ -1802,6 +1808,47 @@ fun WidgetImageView.loadWidgetIcon(connection: Connection, widget: Widget, mappe
}
}

fun MaterialButton.setTextAndIcon(
connection: Connection,
label: String,
iconRes: IconResource?,
labelColor: String? = null,
iconColor: String? = null,
mapper: WidgetAdapter.ColorMapper? = null
) {
contentDescription = label
val iconUrl = iconRes?.toUrl(context, true)
if (iconUrl == null) {
icon = null
text = label
mapper?.let { applyWidgetColor(labelColor, it) }
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?.apply {
mapper?.mapColor(iconColor)?.let {
colorFilter = BlendModeColorFilterCompat.createBlendModeColorFilterCompat(
it,
BlendModeCompat.SRC_ATOP
)
}
}
text = if (drawable == null) label else null
mapper?.let { applyWidgetColor(labelColor, it) }
}
}
}

fun HttpClient.sendItemUpdate(item: Item?, state: ParsedState.NumberState?) {
if (item == null || state == null) {
return
Expand Down

0 comments on commit 28f4685

Please sign in to comment.