Skip to content

Commit

Permalink
Use separate layout files for nested frames
Browse files Browse the repository at this point in the history
Signed-off-by: Danny Baumann <[email protected]>
  • Loading branch information
maniac103 committed Aug 2, 2024
1 parent 6cf71e4 commit ef82e37
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 49 deletions.
97 changes: 48 additions & 49 deletions mobile/src/main/java/org/openhab/habdroid/ui/WidgetAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import android.content.Intent
import android.content.SharedPreferences
import android.content.res.ColorStateList
import android.graphics.Color
import android.graphics.drawable.GradientDrawable
import android.text.InputType.TYPE_CLASS_NUMBER
import android.text.InputType.TYPE_CLASS_TEXT
import android.text.InputType.TYPE_NUMBER_FLAG_DECIMAL
Expand Down Expand Up @@ -53,8 +52,6 @@ import androidx.core.view.get
import androidx.core.view.isGone
import androidx.core.view.isInvisible
import androidx.core.view.isVisible
import androidx.core.view.marginStart
import androidx.core.view.updatePadding
import androidx.core.widget.ContentLoadingProgressBar
import androidx.core.widget.doAfterTextChanged
import androidx.fragment.app.DialogFragment
Expand Down Expand Up @@ -117,7 +114,6 @@ import org.openhab.habdroid.util.MjpegStreamer
import org.openhab.habdroid.util.PrefKeys
import org.openhab.habdroid.util.beautify
import org.openhab.habdroid.util.determineDataUsagePolicy
import org.openhab.habdroid.util.dpToPixel
import org.openhab.habdroid.util.getChartTheme
import org.openhab.habdroid.util.getIconFallbackColor
import org.openhab.habdroid.util.getImageWidgetScalingType
Expand Down Expand Up @@ -226,7 +222,8 @@ class WidgetAdapter(
val initData = ViewHolderInitData(inflater, parent, compactMode)
val holder = when (actualViewType) {
TYPE_GENERICITEM -> GenericViewHolder(initData)
TYPE_FRAME -> FrameViewHolder(initData)
TYPE_FRAME -> FirstLevelFrameViewHolder(initData)
TYPE_NESTED_FRAME -> SecondLevelFrameViewHolder(initData)
TYPE_GROUP -> TextViewHolder(initData)
TYPE_SWITCH -> SwitchViewHolder(initData)
TYPE_TEXT -> TextViewHolder(initData)
Expand Down Expand Up @@ -265,11 +262,10 @@ class WidgetAdapter(
colorMapper,
serverFlags,
chartTheme,
{ widgetsByParentId[widget.id] },
widgetsById[widget.parentId]
{ widgetsByParentId[widget.id] }
)
holder.bind(widget)
if (holder is FrameViewHolder) {
if (holder is AbstractFrameViewHolder) {
holder.setShownAsFirst(position == firstVisibleWidgetPosition)
}
with(holder.itemView) {
Expand Down Expand Up @@ -367,7 +363,10 @@ class WidgetAdapter(
return toInternalViewType(TYPE_INVISIBLE, compactMode)
}
val actualViewType = when (widget.type) {
Widget.Type.Frame -> TYPE_FRAME
Widget.Type.Frame -> when {
widgetsById[widget.parentId]?.type == Widget.Type.Frame -> TYPE_NESTED_FRAME
else -> TYPE_FRAME
}
Widget.Type.Group -> TYPE_GROUP
Widget.Type.Switch -> when {
widget.shouldRenderAsPlayer() -> TYPE_PLAYER
Expand Down Expand Up @@ -411,8 +410,7 @@ class WidgetAdapter(
val colorMapper: ColorMapper,
val serverFlags: Int,
val chartTheme: CharSequence?,
val childWidgetGetter: () -> List<Widget>?,
val parentWidget: Widget?
val childWidgetGetter: () -> List<Widget>?
)

abstract class ViewHolder internal constructor(
Expand All @@ -429,7 +427,6 @@ class WidgetAdapter(
protected val colorMapper get() = requireHolderContext().colorMapper
protected val fragmentPresenter get() = requireHolderContext().fragmentPresenter
protected val childWidgets get() = requireHolderContext().childWidgetGetter()
protected val parentWidget get() = requireHolderContext().parentWidget

abstract fun bind(widget: Widget)

Expand Down Expand Up @@ -599,15 +596,14 @@ class WidgetAdapter(
}
}

class FrameViewHolder internal constructor(initData: ViewHolderInitData) :
ViewHolder(initData, R.layout.widgetlist_frameitem, R.layout.widgetlist_frameitem_compact) {
open class AbstractFrameViewHolder internal constructor(
initData: ViewHolderInitData,
@LayoutRes layoutResId: Int,
@LayoutRes compactModeLayoutResId: Int
) : ViewHolder(initData, layoutResId, compactModeLayoutResId) {
private val labelView: TextView = itemView.findViewById(R.id.widgetlabel)
private val containerView: View = itemView.findViewById(R.id.container)
private val spacer: View = itemView.findViewById(R.id.first_view_spacer)
private val originalPaddingTop = containerView.paddingTop
private val backgroundColorMap =
mapOf(false to R.attr.colorPrimaryContainer, true to R.attr.colorTertiaryContainer)
.mapValues { itemView.context.resolveThemedColor(it.value) }

init {
itemView.isClickable = false
Expand All @@ -621,16 +617,6 @@ class WidgetAdapter(
labelView.text = widget.label + label
labelView.applyWidgetColor(widget.valueColor, colorMapper)
labelView.isGone = widget.label.isEmpty()
val isNested = parentWidget?.type == Widget.Type.Frame

val paddingTop = if (isNested) 0 else originalPaddingTop
containerView.updatePadding(top = paddingTop, bottom = paddingTop)

backgroundColorMap[isNested]?.let { (containerView.background as GradientDrawable).setColor(it) }

labelView.layoutParams = (labelView.layoutParams as ViewGroup.MarginLayoutParams).apply {
marginStart = if (isNested) labelView.resources.dpToPixel(8f).toInt() else 0
}
}

fun setShownAsFirst(shownAsFirst: Boolean) {
Expand All @@ -639,6 +625,18 @@ class WidgetAdapter(
}
}

class FirstLevelFrameViewHolder internal constructor(initData: ViewHolderInitData) : AbstractFrameViewHolder(
initData,
R.layout.widgetlist_frameitem,
R.layout.widgetlist_frameitem_compact
)

class SecondLevelFrameViewHolder internal constructor(initData: ViewHolderInitData) : AbstractFrameViewHolder(
initData,
R.layout.widgetlist_frameitem_nested,
R.layout.widgetlist_frameitem_nested_compact
)

class SwitchViewHolder internal constructor(initData: ViewHolderInitData) :
LabeledItemBaseViewHolder(initData, R.layout.widgetlist_switchitem, R.layout.widgetlist_switchitem_compact) {
private val switch: MaterialSwitch = itemView.findViewById(R.id.toggle)
Expand Down Expand Up @@ -1794,27 +1792,28 @@ class WidgetAdapter(

private const val TYPE_GENERICITEM = 0
private const val TYPE_FRAME = 1
private const val TYPE_GROUP = 2
private const val TYPE_SWITCH = 3
private const val TYPE_TEXT = 4
private const val TYPE_SLIDER = 5
private const val TYPE_IMAGE = 6
private const val TYPE_SELECTION = 7
private const val TYPE_SECTIONSWITCH = 8
private const val TYPE_SECTIONSWITCH_SMALL = 9
private const val TYPE_ROLLERSHUTTER = 10
private const val TYPE_PLAYER = 11
private const val TYPE_SETPOINT = 12
private const val TYPE_CHART = 13
private const val TYPE_VIDEO = 14
private const val TYPE_WEB = 15
private const val TYPE_COLOR = 16
private const val TYPE_VIDEO_MJPEG = 17
private const val TYPE_LOCATION = 18
private const val TYPE_INPUT = 19
private const val TYPE_DATETIMEINPUT = 20
private const val TYPE_BUTTONGRID = 21
private const val TYPE_INVISIBLE = 22
private const val TYPE_NESTED_FRAME = 2
private const val TYPE_GROUP = 3
private const val TYPE_SWITCH = 4
private const val TYPE_TEXT = 5
private const val TYPE_SLIDER = 6
private const val TYPE_IMAGE = 7
private const val TYPE_SELECTION = 8
private const val TYPE_SECTIONSWITCH = 9
private const val TYPE_SECTIONSWITCH_SMALL = 10
private const val TYPE_ROLLERSHUTTER = 11
private const val TYPE_PLAYER = 12
private const val TYPE_SETPOINT = 13
private const val TYPE_CHART = 14
private const val TYPE_VIDEO = 15
private const val TYPE_WEB = 16
private const val TYPE_COLOR = 17
private const val TYPE_VIDEO_MJPEG = 18
private const val TYPE_LOCATION = 19
private const val TYPE_INPUT = 20
private const val TYPE_DATETIMEINPUT = 21
private const val TYPE_BUTTONGRID = 22
private const val TYPE_INVISIBLE = 23

private fun toInternalViewType(viewType: Int, compactMode: Boolean): Int {
return viewType or (if (compactMode) 0x100 else 0)
Expand Down
5 changes: 5 additions & 0 deletions mobile/src/main/res/drawable/frame_nested_chip_background.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:topRightRadius="16dp" android:bottomRightRadius="16dp" />
<solid android:color="?attr/colorTertiaryContainer" />
</shape>
38 changes: 38 additions & 0 deletions mobile/src/main/res/layout/widgetlist_frameitem_nested.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
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">

<androidx.legacy.widget.Space
android:id="@+id/first_view_spacer"
android:layout_width="match_parent"
android:layout_height="1dp"
android:visibility="gone" />

<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:layout_marginBottom="4dp"
android:layout_marginEnd="16dp"
android:paddingHorizontal="16dp"
android:background="@drawable/frame_nested_chip_background">

<TextView
android:id="@+id/widgetlabel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:paddingVertical="8dp"
android:maxLines="2"
android:ellipsize="end"
android:textAppearance="?attr/textAppearanceTitleSmall"
android:textColor="?attr/colorOnTertiaryContainer"
tools:text="Frame title" />

</FrameLayout>

</FrameLayout>
38 changes: 38 additions & 0 deletions mobile/src/main/res/layout/widgetlist_frameitem_nested_compact.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
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">

<androidx.legacy.widget.Space
android:id="@+id/first_view_spacer"
android:layout_width="match_parent"
android:layout_height="1dp"
android:visibility="gone" />

<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:layout_marginBottom="4dp"
android:layout_marginEnd="16dp"
android:paddingVertical="4dp"
android:paddingHorizontal="16dp"
android:background="@drawable/frame_nested_chip_background">

<TextView
android:id="@+id/widgetlabel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:maxLines="2"
android:ellipsize="end"
android:textAppearance="?attr/textAppearanceTitleSmall"
android:textColor="?attr/colorOnTertiaryContainer"
tools:text="Frame title" />

</FrameLayout>

</FrameLayout>

0 comments on commit ef82e37

Please sign in to comment.