From 28f4685fd56cef89ca24c47f0a216e8b6b1dcd4b Mon Sep 17 00:00:00 2001 From: Danny Baumann Date: Fri, 5 Jul 2024 08:24:54 +0200 Subject: [PATCH] Simplify MaterialButton.setTextAndIcon() logic Also fix BlendModeColorFilter only being available on API 29+ Signed-off-by: Danny Baumann --- .../org/openhab/habdroid/ui/ViewExtensions.kt | 54 ------------------- .../org/openhab/habdroid/ui/WidgetAdapter.kt | 49 ++++++++++++++++- 2 files changed, 48 insertions(+), 55 deletions(-) diff --git a/mobile/src/main/java/org/openhab/habdroid/ui/ViewExtensions.kt b/mobile/src/main/java/org/openhab/habdroid/ui/ViewExtensions.kt index d2e33ef2f3f..b74e3bbff32 100644 --- a/mobile/src/main/java/org/openhab/habdroid/ui/ViewExtensions.kt +++ b/mobile/src/main/java/org/openhab/habdroid/ui/ViewExtensions.kt @@ -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 @@ -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 @@ -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 - } - } -} diff --git a/mobile/src/main/java/org/openhab/habdroid/ui/WidgetAdapter.kt b/mobile/src/main/java/org/openhab/habdroid/ui/WidgetAdapter.kt index b8407e15c5b..7b5b981abeb 100644 --- a/mobile/src/main/java/org/openhab/habdroid/ui/WidgetAdapter.kt +++ b/mobile/src/main/java/org/openhab/habdroid/ui/WidgetAdapter.kt @@ -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 @@ -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 @@ -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 @@ -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 @@ -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