Skip to content

Commit

Permalink
Use transparent chart theme if available
Browse files Browse the repository at this point in the history
These themes have been implemented in openhab/openhab-core@ac84206

Fixes openhab#2726

Signed-off-by: mueller-ma <[email protected]>
  • Loading branch information
mueller-ma committed Oct 15, 2021
1 parent 8524e3d commit c91baf2
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ data class ServerProperties(val flags: Int, val sitemaps: List<Sitemap>) : Parce
const val SERVER_FLAG_SITEMAP_HAS_INVISIBLE_WIDGETS = 1 shl 5
const val SERVER_FLAG_SUPPORTS_ANY_FORMAT_ICON = 1 shl 6
const val SERVER_FLAG_OH3_UI = 1 shl 7
const val SERVER_FLAG_TRANSPARENT_CHARTS = 1 shl 8

class UpdateHandle internal constructor(internal val scope: CoroutineScope) {
internal var job: Job? = null
Expand Down Expand Up @@ -125,6 +126,9 @@ data class ServerProperties(val flags: Int, val sitemaps: List<Sitemap>) : Parce
if (version >= 4) {
flags = flags or SERVER_FLAG_OH3_UI
}
if (version >= 5) {
flags = flags or SERVER_FLAG_TRANSPARENT_CHARTS
}
} catch (nfe: NumberFormatException) {
// ignored: older versions without SSE support didn't return a number
Log.i(TAG, "Server has rest api version < 1")
Expand Down
14 changes: 8 additions & 6 deletions mobile/src/main/java/org/openhab/habdroid/ui/ChartActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package org.openhab.habdroid.ui
import android.content.res.Configuration
import android.os.Bundle
import android.util.Log
import android.util.TypedValue
import android.view.Menu
import android.view.MenuItem
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
Expand All @@ -27,6 +26,7 @@ import org.openhab.habdroid.model.Widget
import org.openhab.habdroid.ui.widget.WidgetImageView
import org.openhab.habdroid.util.ScreenLockMode
import org.openhab.habdroid.util.determineDataUsagePolicy
import org.openhab.habdroid.util.getChartTheme
import org.openhab.habdroid.util.getPrefs
import org.openhab.habdroid.util.orDefaultIfEmpty

Expand All @@ -36,6 +36,7 @@ class ChartActivity : AbstractBaseActivity(), SwipeRefreshLayout.OnRefreshListen
private lateinit var period: String
private lateinit var widget: Widget
private lateinit var chartTheme: CharSequence
private var serverFlags: Int = 0
private var density: Int = 0
private var showLegend: Boolean = true

Expand All @@ -44,11 +45,13 @@ class ChartActivity : AbstractBaseActivity(), SwipeRefreshLayout.OnRefreshListen

setContentView(R.layout.activity_chart)

widget = intent.getParcelableExtra(WIDGET)!!
widget = intent.getParcelableExtra(EXTRA_WIDGET)!!
period = widget.period
// If Widget#legend is null, show legend only for groups
showLegend = widget.legend ?: widget.item?.type === Item.Type.Group

serverFlags = intent.getIntExtra(EXTRA_SERVER_FLAGS, 0)

setSupportActionBar(findViewById(R.id.openhab_toolbar))
supportActionBar?.setDisplayHomeAsUpEnabled(true)
supportActionBar?.title = widget.label.orDefaultIfEmpty(getString(R.string.chart_activity_title))
Expand Down Expand Up @@ -187,16 +190,15 @@ class ChartActivity : AbstractBaseActivity(), SwipeRefreshLayout.OnRefreshListen
}

private fun updateChartTheme() {
val tv = TypedValue()
theme.resolveAttribute(R.attr.chartTheme, tv, true)
chartTheme = tv.string
chartTheme = getChartTheme(serverFlags)
}

companion object {
private val TAG = ChartActivity::class.java.simpleName

private const val SHOW_LEGEND = "show_legend"
private const val PERIOD = "period"
const val WIDGET = "widget"
const val EXTRA_WIDGET = "widget"
const val EXTRA_SERVER_FLAGS = "server_flags"
}
}
18 changes: 8 additions & 10 deletions mobile/src/main/java/org/openhab/habdroid/ui/WidgetAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ import org.openhab.habdroid.util.HttpClient
import org.openhab.habdroid.util.MjpegStreamer
import org.openhab.habdroid.util.beautify
import org.openhab.habdroid.util.determineDataUsagePolicy
import org.openhab.habdroid.util.getChartTheme
import org.openhab.habdroid.util.getImageWidgetScalingType
import org.openhab.habdroid.util.getPrefs
import org.openhab.habdroid.util.orDefaultIfEmpty
Expand All @@ -103,6 +104,7 @@ import org.openhab.habdroid.util.orDefaultIfEmpty

class WidgetAdapter(
context: Context,
val serverFlags: Int,
private val connection: Connection,
private val itemClickListener: ItemClickListener
) : RecyclerView.Adapter<WidgetAdapter.ViewHolder>(), View.OnClickListener {
Expand All @@ -113,7 +115,7 @@ class WidgetAdapter(
get() = items.any { widget -> shouldShowWidget(widget) }

private val inflater = LayoutInflater.from(context)
private val chartTheme: CharSequence
private val chartTheme: CharSequence = context.getChartTheme(serverFlags)
private var selectedPosition = RecyclerView.NO_POSITION
private var firstVisibleWidgetPosition = RecyclerView.NO_POSITION
private val colorMapper = ColorMapper(context)
Expand All @@ -122,12 +124,6 @@ class WidgetAdapter(
fun onItemClicked(widget: Widget): Boolean // returns whether click was handled
}

init {
val tv = TypedValue()
context.theme.resolveAttribute(R.attr.chartTheme, tv, true)
chartTheme = tv.string
}

@SuppressLint("NotifyDataSetChanged")
fun update(widgets: List<Widget>, forceFullUpdate: Boolean) {
val compatibleUpdate = !forceFullUpdate &&
Expand Down Expand Up @@ -189,7 +185,7 @@ class WidgetAdapter(
TYPE_SECTIONSWITCH -> SectionSwitchViewHolder(inflater, parent, connection, colorMapper)
TYPE_ROLLERSHUTTER -> RollerShutterViewHolder(inflater, parent, connection, colorMapper)
TYPE_SETPOINT -> SetpointViewHolder(inflater, parent, connection, colorMapper)
TYPE_CHART -> ChartViewHolder(inflater, parent, chartTheme, connection)
TYPE_CHART -> ChartViewHolder(inflater, parent, chartTheme, connection, serverFlags)
TYPE_VIDEO -> VideoViewHolder(inflater, parent, connection)
TYPE_WEB -> WebViewHolder(inflater, parent, connection)
TYPE_COLOR -> ColorViewHolder(inflater, parent, connection, colorMapper)
Expand Down Expand Up @@ -996,7 +992,8 @@ class WidgetAdapter(
inflater: LayoutInflater,
parent: ViewGroup,
private val chartTheme: CharSequence?,
connection: Connection
connection: Connection,
private val serverFlags: Int
) : HeavyDataViewHolder(inflater, parent, R.layout.widgetlist_chartitem, connection), View.OnClickListener {
private val chart = widgetContentView as WidgetImageView
private val prefs: SharedPreferences
Expand Down Expand Up @@ -1039,7 +1036,8 @@ class WidgetAdapter(
val context = v?.context ?: return
boundWidget?.let {
val intent = Intent(context, ChartActivity::class.java)
intent.putExtra(ChartActivity.WIDGET, it)
intent.putExtra(ChartActivity.EXTRA_WIDGET, it)
intent.putExtra(ChartActivity.EXTRA_SERVER_FLAGS, serverFlags)
context.startActivity(intent)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ class WidgetListFragment : Fragment(), WidgetAdapter.ItemClickListener,
super.onViewCreated(view, savedInstanceState)

val activity = activity as MainActivity
adapter = activity.connection?.let { conn -> WidgetAdapter(activity, conn, this) }
adapter = activity.connection?.let { conn -> WidgetAdapter(activity, activity.serverProperties!!.flags, conn, this) }

layoutManager = LinearLayoutManager(activity)
layoutManager.recycleChildrenOnDetach = true
Expand Down
12 changes: 12 additions & 0 deletions mobile/src/main/java/org/openhab/habdroid/util/ExtensionFuncs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ import org.openhab.habdroid.R
import org.openhab.habdroid.core.OpenHabApplication
import org.openhab.habdroid.model.ServerConfiguration
import org.openhab.habdroid.model.ServerPath
import org.openhab.habdroid.model.ServerProperties
import org.openhab.habdroid.util.Util.TAG
import org.w3c.dom.Node
import org.w3c.dom.NodeList
Expand Down Expand Up @@ -451,6 +452,17 @@ fun Context.resolveThemedColor(@AttrRes colorAttr: Int, @ColorInt fallbackColor:
}
}

fun Context.getChartTheme(serverFlags: Int): CharSequence {
val tv = TypedValue()
if (serverFlags and ServerProperties.SERVER_FLAG_TRANSPARENT_CHARTS == 0) {
theme.resolveAttribute(R.attr.chartTheme, tv, true)
} else {
theme.resolveAttribute(R.attr.transparentChartTheme, tv, true)
}

return tv.string
}

fun Context.isDarkModeActive(): Boolean {
return when (getPrefs().getDayNightMode(this)) {
AppCompatDelegate.MODE_NIGHT_NO -> false
Expand Down
1 change: 1 addition & 0 deletions mobile/src/main/res/values-night/themes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<resources>
<style name="openHAB.DayNight" parent="openHAB.Base">
<item name="chartTheme">dark</item>
<item name="transparentChartTheme">dark_transparent</item>
<item name="valueColors">@array/valueColorsDarkBackground</item>
</style>

Expand Down
1 change: 1 addition & 0 deletions mobile/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

<declare-styleable name="customAttrs">
<attr name="chartTheme" format="string" />
<attr name="transparentChartTheme" format="string" />
<attr name="valueColors" format="reference" />
<attr name="selectedWidgetBackgroundColor" format="color" />
</declare-styleable>
Expand Down
4 changes: 4 additions & 0 deletions mobile/src/main/res/values/themes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

<style name="openHAB.DayNight" parent="openHAB.Base">
<item name="chartTheme">white</item>
<item name="transparentChartTheme">white_transparent</item>
<item name="valueColors">@array/valueColorsBrightBackground</item>
</style>

Expand Down Expand Up @@ -48,16 +49,19 @@

<style name="openHAB.Black.orange" parent="openHAB.DayNight.orange">
<item name="chartTheme">black</item>
<item name="transparentChartTheme">black_transparent</item>
<item name="android:windowBackground">@color/black</item>
</style>

<style name="openHAB.Black.basicui" parent="openHAB.DayNight.basicui">
<item name="chartTheme">black</item>
<item name="transparentChartTheme">black_transparent</item>
<item name="android:windowBackground">@color/black</item>
</style>

<style name="openHAB.Black.grey" parent="openHAB.DayNight.grey">
<item name="chartTheme">black</item>
<item name="transparentChartTheme">black_transparent</item>
<item name="android:windowBackground">@color/black</item>
</style>

Expand Down

0 comments on commit c91baf2

Please sign in to comment.