Skip to content

Commit

Permalink
Merge pull request #1135 from square/ray/do-not-stomp-bounds
Browse files Browse the repository at this point in the history
BREAKING: Stop stomping `View.layoutParams`, new parameter for `replaceViewInParent`
  • Loading branch information
rjrjr authored Nov 15, 2023
2 parents 87ade41 + b11e61e commit 91ab3e8
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 15 deletions.
6 changes: 4 additions & 2 deletions workflow-ui/core-android/api/core-android.api
Original file line number Diff line number Diff line change
Expand Up @@ -270,13 +270,15 @@ public final class com/squareup/workflow1/ui/WorkflowViewStub : android/view/Vie
public synthetic fun <init> (Landroid/content/Context;Landroid/util/AttributeSet;IIILkotlin/jvm/internal/DefaultConstructorMarker;)V
public final fun getActual ()Landroid/view/View;
public final fun getInflatedId ()I
public final fun getReplaceOldViewInParent ()Lkotlin/jvm/functions/Function2;
public final fun getPropagatesLayoutParams ()Z
public final fun getReplaceOldViewInParent ()Lkotlin/jvm/functions/Function3;
public final fun getUpdatesVisibility ()Z
public fun getVisibility ()I
public fun setBackground (Landroid/graphics/drawable/Drawable;)V
public fun setId (I)V
public final fun setInflatedId (I)V
public final fun setReplaceOldViewInParent (Lkotlin/jvm/functions/Function2;)V
public final fun setPropagatesLayoutParams (Z)V
public final fun setReplaceOldViewInParent (Lkotlin/jvm/functions/Function3;)V
public final fun setUpdatesVisibility (Z)V
public fun setVisibility (I)V
public final fun show (Lcom/squareup/workflow1/ui/Screen;Lcom/squareup/workflow1/ui/ViewEnvironment;)V
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import com.squareup.workflow1.ui.container.EnvironmentScreen
* by ScreenViewFactory(
* buildView = { environment, context, _ ->
* val view = MyBackStackContainer(context)
* .apply { layoutParams = (LayoutParams(MATCH_PARENT, MATCH_PARENT)) }
* ScreenViewHolder(environment, view) { rendering, environment ->
* view.update(rendering, environment)
* }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import android.os.Parcelable.Creator
import android.util.AttributeSet
import android.util.SparseArray
import android.view.View
import android.view.ViewGroup
import android.view.ViewGroup.LayoutParams.MATCH_PARENT
import android.widget.FrameLayout
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.Lifecycle.State
Expand Down Expand Up @@ -51,7 +49,8 @@ public class WorkflowLayout(

private val showing: WorkflowViewStub = WorkflowViewStub(context).also { rootStub ->
rootStub.updatesVisibility = false
addView(rootStub, ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT))
rootStub.propagatesLayoutParams = false
addView(rootStub)
}

private var restoredChildState: SparseArray<Parcelable>? = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.graphics.drawable.Drawable
import android.util.AttributeSet
import android.view.View
import android.view.ViewGroup
import android.view.ViewGroup.LayoutParams
import androidx.annotation.IdRes
import androidx.savedstate.SavedStateRegistryOwner
import androidx.savedstate.findViewTreeSavedStateRegistryOwner
Expand Down Expand Up @@ -85,6 +86,14 @@ public class WorkflowViewStub @JvmOverloads constructor(
*/
public var updatesVisibility: Boolean = true

/**
* If true, the [layoutParams][getLayoutParams] of this stub will be applied
* to new views as they are added to the stub's original parent.
* Specifically, the third parameter passed to [replaceOldViewInParent]
* will be non-null.
*/
public var propagatesLayoutParams: Boolean = true

/**
* The id to be assigned to new views created by [update]. If the inflated id is
* [View.NO_ID] (its default value), new views keep their original ids.
Expand Down Expand Up @@ -121,15 +130,19 @@ public class WorkflowViewStub @JvmOverloads constructor(
/**
* Function called from [update] to replace this stub, or the current [actual],
* with a new view. Can be updated to provide custom transition effects.
* The default implementation simply calls [ViewGroup.addView], including
* the `layoutParams` parameter if it is non-null.
*
* Note that this method is responsible for copying the [layoutParams][getLayoutParams]
* from the stub to the new view. Also note that in a [WorkflowViewStub] that has never
* been updated, [actual] is the stub itself.
* @see propagatesLayoutParams
*/
public var replaceOldViewInParent: (ViewGroup, View) -> Unit = { parent, newView ->
public var replaceOldViewInParent: (
parent: ViewGroup,
newView: View,
layoutParams: LayoutParams?
) -> Unit = { parent, newView, layoutParams ->
val index = parent.indexOfChild(actual)
parent.removeView(actual)
actual.layoutParams
layoutParams
?.let { parent.addView(newView, index, it) }
?: run { parent.addView(newView, index) }
}
Expand Down Expand Up @@ -241,7 +254,7 @@ public class WorkflowViewStub @JvmOverloads constructor(
if (updatesVisibility) newView.visibility = visibility
background?.let { newView.background = it }
propagateSavedStateRegistryOwner(newView)
replaceOldViewInParent(parent, newView)
replaceOldViewInParent(parent, newView, layoutParams?.takeIf { propagatesLayoutParams })
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import android.os.Parcelable.Creator
import android.util.AttributeSet
import android.view.KeyEvent
import android.view.MotionEvent
import android.view.ViewGroup
import android.view.ViewGroup.LayoutParams.MATCH_PARENT
import android.widget.FrameLayout
import com.squareup.workflow1.ui.Compatible
Expand Down Expand Up @@ -42,8 +41,9 @@ internal class BodyAndOverlaysContainer @JvmOverloads constructor(
*/
private lateinit var savedStateParentKey: String

private val baseViewStub: WorkflowViewStub = WorkflowViewStub(context).also {
addView(it, ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT))
private val baseViewStub: WorkflowViewStub = WorkflowViewStub(context).apply {
propagatesLayoutParams = false
addView(this)
}

private val dialogs = LayeredDialogSessions.forView(
Expand Down

0 comments on commit 91ab3e8

Please sign in to comment.