Skip to content

Commit

Permalink
Move reset() function up to RedwoodView (#1476)
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeWharton authored Sep 13, 2023
1 parent 0ff0bde commit 597c1dd
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ public fun <W : Any> RedwoodComposition(
provider: Widget.Provider<W>,
onEndChanges: () -> Unit = {},
): RedwoodComposition {
view.reset()

return RedwoodComposition(scope, view.children, view.uiConfiguration, provider, onEndChanges)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ public fun RedwoodContent(
object : RedwoodView<@Composable () -> Unit> {
override val children = ComposeWidgetChildren()
override val uiConfiguration = MutableStateFlow(uiConfiguration)
override fun reset() {
children.remove(0, children.widgets.size)
}
}
}
LaunchedEffect(redwoodView, uiConfiguration) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import android.view.ViewGroup.LayoutParams.MATCH_PARENT
import app.cash.redwood.treehouse.TreehouseView.ReadyForContentChangeListener
import app.cash.redwood.treehouse.TreehouseView.WidgetSystem
import app.cash.redwood.widget.RedwoodLayout
import app.cash.redwood.widget.ViewGroupChildren
import java.util.UUID

@Deprecated(
Expand Down Expand Up @@ -56,13 +55,6 @@ public class TreehouseLayout(

override var saveCallback: TreehouseView.SaveCallback? = null

override fun reset() {
children.remove(0, (children as ViewGroupChildren).widgets.size)

// Ensure any out-of-band views are also removed.
removeAllViews()
}

override fun onAttachedToWindow() {
super.onAttachedToWindow()
readyForContent = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ public interface TreehouseView<W : Any> : RedwoodView<W> {
public var saveCallback: SaveCallback?
public val stateSnapshotId: StateSnapshot.Id

/** Invoked when new code is loaded. This should at minimum clear all [children]. */
public fun reset()

@ObjCName("TreehouseViewReadyForContentChangeListener", exact = true)
public fun interface ReadyForContentChangeListener<W : Any> {
/** Called when [TreehouseView.readyForContent] has changed. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package app.cash.redwood.treehouse
import app.cash.redwood.treehouse.TreehouseView.ReadyForContentChangeListener
import app.cash.redwood.treehouse.TreehouseView.WidgetSystem
import app.cash.redwood.widget.RedwoodUIView
import app.cash.redwood.widget.UIViewChildren
import kotlinx.cinterop.cValue
import platform.CoreGraphics.CGRectZero
import platform.UIKit.UITraitCollection
Expand Down Expand Up @@ -54,14 +53,6 @@ public class TreehouseUIView private constructor(
(view as RootUiView).treehouseView = this
}

override fun reset() {
children.remove(0, (children as UIViewChildren).widgets.size)

// Ensure any out-of-band views are also removed.
@Suppress("UNCHECKED_CAST") // Correct generic lost by cinterop.
(view.subviews as List<UIView>).forEach(UIView::removeFromSuperview)
}

private fun superviewChanged() {
readyForContentChangeListener?.onReadyForContentChanged(this)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ public open class RedwoodLayout(
override val uiConfiguration: StateFlow<UiConfiguration>
get() = mutableUiConfiguration

override fun reset() {
_children.remove(0, _children.widgets.size)

// Ensure any out-of-band views are also removed.
removeAllViews()
}

init {
setOnWindowInsetsChangeListener { insets ->
mutableUiConfiguration.value = computeUiConfiguration(insets = insets.safeDrawing)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,11 @@ import kotlinx.coroutines.flow.StateFlow
public interface RedwoodView<W : Any> {
public val children: Widget.Children<W>
public val uiConfiguration: StateFlow<UiConfiguration>

/**
* This should at minimum clear all [children].
*
* Invoke when switching the backing composition to prepare the view for an initial load.
*/
public fun reset()
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ public open class RedwoodUIView(
override val uiConfiguration: StateFlow<UiConfiguration>
get() = mutableUiConfiguration

override fun reset() {
_children.remove(0, _children.widgets.size)

// Ensure any out-of-band views are also removed.
@Suppress("UNCHECKED_CAST") // Correct generic lost by cinterop.
(view.subviews as List<UIView>).forEach(UIView::removeFromSuperview)
}

protected fun updateUiConfiguration() {
mutableUiConfiguration.value = computeUiConfiguration(
traitCollection = view.traitCollection,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import app.cash.redwood.widget.Widget.Children
import kotlinx.browser.window
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.update
import kotlinx.dom.clear
import org.w3c.dom.HTMLElement
import org.w3c.dom.MediaQueryList

Expand All @@ -33,9 +34,10 @@ public fun HTMLElement.asRedwoodView(): RedwoodView<HTMLElement> {
}

private class RedwoodHTMLElementView(
element: HTMLElement,
private val element: HTMLElement,
) : RedwoodView<HTMLElement> {
override val children: Children<HTMLElement> = HTMLElementChildren(element)
private val _children = HTMLElementChildren(element)
override val children: Children<HTMLElement> get() = _children
override val uiConfiguration = MutableStateFlow(
UiConfiguration(
darkMode = window.matchMedia("(prefers-color-scheme: dark)").matches,
Expand All @@ -61,4 +63,11 @@ private class RedwoodHTMLElementView(
// TODO Watch size change
// https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserver
}

override fun reset() {
_children.remove(0, _children.widgets.size)

// Ensure any out-of-band nodes are also removed.
element.clear()
}
}

0 comments on commit 597c1dd

Please sign in to comment.