Skip to content

Commit

Permalink
Route child widget fetching via ViewHolderContext
Browse files Browse the repository at this point in the history
Signed-off-by: Danny Baumann <[email protected]>
  • Loading branch information
maniac103 committed Jul 5, 2024
1 parent f3bb532 commit 3e6eaa8
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions mobile/src/main/java/org/openhab/habdroid/ui/WidgetAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ class WidgetAdapter(
val itemList: List<Widget> get() = items
private val widgetsById = mutableMapOf<String, Widget>()
private val widgetsByParentId = mutableMapOf<String, MutableList<Widget>>()
val widgetsByParentIdMap: Map<String, List<Widget>> get() = widgetsByParentId
val hasVisibleWidgets: Boolean
get() = items.any { widget -> shouldShowWidget(widget) }

Expand Down Expand Up @@ -255,8 +254,16 @@ class WidgetAdapter(

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val wasStarted = holder.stop()
holder.vhc = ViewHolderContext(connection, fragmentPresenter, colorMapper, serverFlags, chartTheme)
holder.bind(items[position])
val widget = items[position]
holder.vhc = ViewHolderContext(
connection,
fragmentPresenter,
colorMapper,
serverFlags,
chartTheme,
{ widgetsByParentId[widget.id] }
)
holder.bind(widget)
if (holder is FrameViewHolder) {
holder.setShownAsFirst(position == firstVisibleWidgetPosition)
}
Expand Down Expand Up @@ -398,7 +405,8 @@ class WidgetAdapter(
val fragmentPresenter: FragmentPresenter,
val colorMapper: ColorMapper,
val serverFlags: Int,
val chartTheme: CharSequence?
val chartTheme: CharSequence?,
val childWidgetGetter: () -> List<Widget>?
)

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

abstract fun bind(widget: Widget)

Expand Down Expand Up @@ -859,7 +868,7 @@ class WidgetAdapter(
labelView.isVisible = showLabelAndIcon
iconView.isVisible = showLabelAndIcon

val buttons = (bindingAdapter as WidgetAdapter).widgetsByParentIdMap[widget.id].orEmpty() +
val buttons = childWidgets.orEmpty() +
widget.mappings.mapIndexed { index, it -> it.toWidget("${widget.id}-mappings-$index", widget.item) }

val rowCount = buttons.maxOfOrNull { it.row ?: 0 } ?: 0
Expand Down

0 comments on commit 3e6eaa8

Please sign in to comment.