Skip to content

Commit

Permalink
Show label and icon for all Widgets
Browse files Browse the repository at this point in the history
Closes openhab#3421

Signed-off-by: mueller-ma <[email protected]>
  • Loading branch information
mueller-ma committed Aug 24, 2023
1 parent 124b203 commit 8e08afd
Show file tree
Hide file tree
Showing 11 changed files with 94 additions and 113 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

package org.openhab.habdroid.ui

import android.content.DialogInterface
import android.location.Location
import android.os.Bundle
import android.os.Handler
Expand Down Expand Up @@ -83,6 +82,7 @@ object MapViewHelper {
}

override fun bindAfterDataSaverCheck(widget: Widget) {
super.bindAfterDataSaverCheck(widget)
handler.post {
mapView.applyPositionAndLabel(
boundWidget?.item,
Expand Down
28 changes: 2 additions & 26 deletions mobile/src/foss/res/layout/widgetlist_mapitem.xml
Original file line number Diff line number Diff line change
@@ -1,39 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
style="@style/WidgetListItemContainer">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"
android:orientation="horizontal">

<org.openhab.habdroid.ui.widget.WidgetImageView
android:id="@+id/widgeticon"
android:layout_width="24dp"
android:layout_height="24dp"
tools:src="@drawable/ic_openhab_appicon_24dp" />

<TextView
android:id="@+id/widgetlabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginStart="4dp"
android:ellipsize="end"
android:maxLines="1"
android:textAppearance="?attr/textAppearanceBodyMedium"
tools:text="Widget title" />

</LinearLayout>
<include layout="@layout/widgetlist_icontext_for_heavy_data" />

<org.osmdroid.views.MapView
android:id="@+id/mapview"
android:id="@+id/widget_content"
android:layout_width="match_parent"
android:layout_height="180dp" />

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ object MapViewHelper {
}

override fun bindAfterDataSaverCheck(widget: Widget) {
super.bindAfterDataSaverCheck(widget)
map?.clear()
map?.applyPositionAndLabel(widget, 15.0f, false)
}
Expand Down
28 changes: 2 additions & 26 deletions mobile/src/full/res/layout/widgetlist_mapitem.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,15 @@
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
style="@style/WidgetListItemContainer">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"
android:orientation="horizontal">

<org.openhab.habdroid.ui.widget.WidgetImageView
android:id="@+id/widgeticon"
android:layout_width="24dp"
android:layout_height="24dp"
tools:src="@drawable/ic_openhab_appicon_24dp" />

<TextView
android:id="@+id/widgetlabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginStart="8dp"
android:ellipsize="end"
android:maxLines="1"
android:textAppearance="?attr/textAppearanceBodyMedium"
tools:text="Widget title" />

</LinearLayout>
<include layout="@layout/widgetlist_icontext_for_heavy_data" />

<com.google.android.gms.maps.MapView
android:id="@+id/mapview"
android:id="@+id/widget_content"
android:layout_width="match_parent"
android:layout_height="180dp"
app:liteMode="true" />
Expand Down
54 changes: 15 additions & 39 deletions mobile/src/main/java/org/openhab/habdroid/ui/WidgetAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import android.widget.Button
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.annotation.CallSuper
import androidx.annotation.LayoutRes
import androidx.annotation.StringRes
import androidx.annotation.VisibleForTesting
Expand Down Expand Up @@ -426,7 +427,7 @@ class WidgetAdapter(
) : ViewHolder(initData, layoutResId, compactModeLayoutResId) {
protected val labelView: TextView = itemView.findViewById(R.id.widgetlabel)
protected val valueView: TextView? = itemView.findViewById(R.id.widgetvalue)
private val iconView: WidgetImageView = itemView.findViewById(R.id.widgeticon)
protected val iconView: WidgetImageView = itemView.findViewById(R.id.widgeticon)
protected var boundWidget: Widget? = null
private set

Expand All @@ -453,16 +454,17 @@ class WidgetAdapter(
initData: ViewHolderInitData,
@LayoutRes layoutResId: Int,
@LayoutRes compactModeLayoutResId: Int = layoutResId
) : ViewHolder(initData, layoutResId, compactModeLayoutResId) {
protected var boundWidget: Widget? = null
private set
) : LabeledItemBaseViewHolder(initData, layoutResId, compactModeLayoutResId) {
protected val widgetContentView: View = itemView.findViewById(R.id.widget_content)
private val dataSaverView: View = itemView.findViewById(R.id.data_saver)
private val dataSaverButton: Button = itemView.findViewById(R.id.data_saver_button)
private val dataSaverHint: TextView = itemView.findViewById(R.id.data_saver_hint)

override fun bind(widget: Widget) {
boundWidget = widget
super.bind(widget)
val showLabelAndIcon = widget.label.isNotEmpty()
labelView.isVisible = showLabelAndIcon
iconView.isVisible = showLabelAndIcon
if (!showDataSaverPlaceholderIfNeeded(widget, canBindWithoutDataTransfer(widget))) {
bindAfterDataSaverCheck(widget)
}
Expand All @@ -486,6 +488,7 @@ class WidgetAdapter(
Widget.Type.Webview -> R.string.widget_type_webview
Widget.Type.Video -> R.string.widget_type_video
Widget.Type.Chart -> R.string.widget_type_chart
Widget.Type.Mapview -> R.string.widget_type_mapview
else -> throw IllegalArgumentException("Cannot show data saver hint for ${widget.type}")
}

Expand Down Expand Up @@ -1462,57 +1465,30 @@ class WidgetAdapter(
}

abstract class AbstractMapViewHolder(initData: ViewHolderInitData) :
LabeledItemBaseViewHolder(initData, R.layout.widgetlist_mapitem) {
HeavyDataViewHolder(initData, R.layout.widgetlist_mapitem) {
private val hasPositions
get() = boundWidget?.item?.state?.asLocation != null || boundWidget?.item?.members?.isNotEmpty() == true

protected val baseMapView: View = itemView.findViewById(R.id.mapview)
protected val baseMapView: View = itemView.findViewById(R.id.widget_content)
private val emptyView: LinearLayout = itemView.findViewById(android.R.id.empty)
private val dataSaverView: View = itemView.findViewById(R.id.data_saver)
private val dataSaverButton: Button = itemView.findViewById(R.id.data_saver_button)
private val dataSaverHint: TextView = itemView.findViewById(R.id.data_saver_hint)

override fun bind(widget: Widget) {
super.bind(widget)
baseMapView.adjustForWidgetHeight(widget, 5)
handleDataSaver(false)
emptyView.isVisible = !hasPositions
}

private fun handleDataSaver(overrideDataSaver: Boolean) {
val widget = boundWidget ?: return
val dataSaverActive = !itemView.context.determineDataUsagePolicy(connection).canDoLargeTransfers &&
!overrideDataSaver

dataSaverView.isVisible = dataSaverActive && hasPositions
baseMapView.isVisible = !dataSaverView.isVisible && hasPositions
emptyView.isVisible = !dataSaverView.isVisible && !hasPositions

if (dataSaverActive) {
dataSaverButton.setOnClickListener {
handleDataSaver(true)
}

dataSaverHint.text = itemView.context.getString(
R.string.data_saver_hint,
widget.label.orDefaultIfEmpty(itemView.context.getString(R.string.widget_type_mapview))
)
} else {
dataSaverButton.setOnClickListener(null)
bindAfterDataSaverCheck(widget)
}
}

fun handleDataUsagePolicyChange() {
boundWidget?.let { bind(it) }
@CallSuper
override fun bindAfterDataSaverCheck(widget: Widget) {
emptyView.isVisible = !hasPositions
baseMapView.isVisible = hasPositions
}

override fun handleRowClick() {
if (hasPositions) {
openPopup()
}
}

protected abstract fun bindAfterDataSaverCheck(widget: Widget)
protected abstract fun openPopup()
}

Expand Down
29 changes: 20 additions & 9 deletions mobile/src/main/res/layout/widgetlist_chartitem.xml
Original file line number Diff line number Diff line change
@@ -1,20 +1,31 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
style="@style/WidgetListItemContainer">

<org.openhab.habdroid.ui.widget.WidgetImageView
android:id="@+id/widget_content"
<include layout="@layout/widgetlist_icontext_for_heavy_data" />

<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:imageScalingType="scaleToFitWithViewAdjustment"
app:emptyHeightToWidthRatio="50%"
app:progressIndicator="@drawable/ic_image_loading_themed"
app:addRandomnessToUrl="true" />
android:descendantFocusability="blocksDescendants">

<org.openhab.habdroid.ui.widget.WidgetImageView
android:id="@+id/widget_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:imageScalingType="scaleToFitWithViewAdjustment"
app:emptyHeightToWidthRatio="50%"
app:progressIndicator="@drawable/ic_image_loading_themed"
app:addRandomnessToUrl="true" />

<include layout="@layout/widgetlist_data_saver" />
</FrameLayout>


<include layout="@layout/widgetlist_data_saver" />

</FrameLayout>
</LinearLayout>
29 changes: 29 additions & 0 deletions mobile/src/main/res/layout/widgetlist_icontext_for_heavy_data.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="wrap_content"
android:layout_width="match_parent">

<org.openhab.habdroid.ui.widget.WidgetImageView
android:id="@+id/widgeticon"
android:layout_width="@dimen/widgetlist_icon_size"
android:layout_height="@dimen/widgetlist_icon_size"
android:layout_gravity="center_vertical"
app:imageScalingType="scaleToFit"
tools:src="@drawable/ic_openhab_appicon_24dp" />

<TextView
android:id="@+id/widgetlabel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginStart="16dp"
android:layout_weight="1"
android:ellipsize="end"
android:maxLines="1"
android:textDirection="locale"
android:textAppearance="?attr/textAppearanceBodyLarge"
tools:text="Widget title" />

</LinearLayout>
7 changes: 5 additions & 2 deletions mobile/src/main/res/layout/widgetlist_imageitem.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
style="@style/WidgetListItemContainer">

<include layout="@layout/widgetlist_icontext_for_heavy_data" />

<org.openhab.habdroid.ui.widget.WidgetImageView
android:id="@+id/widget_content"
android:layout_width="match_parent"
Expand All @@ -14,4 +17,4 @@

<include layout="@layout/widgetlist_data_saver" />

</FrameLayout>
</LinearLayout>
7 changes: 5 additions & 2 deletions mobile/src/main/res/layout/widgetlist_videoitem.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="vertical"
style="@style/WidgetListItemContainer">

<include layout="@layout/widgetlist_icontext_for_heavy_data" />

<com.google.android.material.progressindicator.CircularProgressIndicator
android:id="@+id/video_player_loading"
android:layout_width="wrap_content"
Expand Down Expand Up @@ -49,4 +52,4 @@
android:layout_gravity="center_horizontal"
android:text="@string/retry" />
</LinearLayout>
</FrameLayout>
</LinearLayout>
7 changes: 5 additions & 2 deletions mobile/src/main/res/layout/widgetlist_videomjpegitem.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
style="@style/WidgetListItemContainer">

<include layout="@layout/widgetlist_icontext_for_heavy_data" />

<org.openhab.habdroid.ui.widget.WidgetImageView
android:id="@+id/widget_content"
android:layout_width="match_parent"
Expand All @@ -16,4 +19,4 @@

<include layout="@layout/widgetlist_data_saver" />

</FrameLayout>
</LinearLayout>
15 changes: 9 additions & 6 deletions mobile/src/main/res/layout/widgetlist_webitem.xml
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
style="@style/WidgetListItemContainer"
tools:ignore="WebViewLayout">

<WebView
android:id="@+id/widget_content"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<include layout="@layout/widgetlist_icontext_for_heavy_data" />

<androidx.core.widget.ContentLoadingProgressBar
android:id="@+id/progress_bar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<WebView
android:id="@+id/widget_content"
android:layout_width="match_parent"
android:layout_height="match_parent" />

<include layout="@layout/widgetlist_data_saver" />

</FrameLayout>
</LinearLayout>

0 comments on commit 8e08afd

Please sign in to comment.