Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 49 additions & 3 deletions android/src/main/java/expo/modules/swipeable/SwipeableView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -372,13 +372,33 @@ class SwipeableView(context: Context, appContext: AppContext) : ExpoView(context
private val requestedActions = mutableSetOf<SwipeSide>()
private val observedSlotCounts = mutableMapOf<SwipeSide, Int>()
private val configurationRunnable = Runnable { applyPendingConfiguration() }
// Fabric adds a three-child row one child at a time. Until the next main-thread turn,
// a lone child can be the first rail of that batch, so it gets no role yet.
private var childrenSettled = true
private val settleChildrenRunnable = Runnable {
childrenSettled = true
reconcileSideChildren()
}

private val activeSide: SwipeSide?
get() = gestureSide ?: animationSide ?: openSide ?: defaultOpenSide()

private var contentView: View? = null
private var leftActionsView: View? = null
private var rightActionsView: View? = null
// The flags of each rail before refreshRailVisibility() first changed them.
private class RailFlags(view: View) {
private val visibility = view.visibility
private val importantForAccessibility = view.importantForAccessibility
private val pointerEvents = (view as? ReactViewGroup)?.pointerEvents

fun restoreTo(view: View) {
view.visibility = visibility
view.importantForAccessibility = importantForAccessibility
pointerEvents?.let { (view as? ReactViewGroup)?.pointerEvents = it }
}
}
private val railOriginalFlags = HashMap<View, RailFlags>()
private val actionsView: View?
get() = railFor(activeSide)

Expand Down Expand Up @@ -475,14 +495,14 @@ class SwipeableView(context: Context, appContext: AppContext) : ExpoView(context
override fun onViewAdded(child: View?) {
super.onViewAdded(child)
child ?: return
markChildrenChanging()
reconcileSideChildren()
}

override fun onViewRemoved(child: View?) {
super.onViewRemoved(child)
if (child === leftActionsView || child === rightActionsView) {
(child as? ReactViewGroup)?.pointerEvents = PointerEvents.AUTO
}
markChildrenChanging()
if (child != null) restoreRailFlags(child)
if (child === contentView) {
contentView = null
isCurrentTouchCancelled = true
Expand Down Expand Up @@ -1505,6 +1525,18 @@ class SwipeableView(context: Context, appContext: AppContext) : ExpoView(context
}
}

private fun markChildrenChanging() {
childrenSettled = false
removeCallbacks(settleChildrenRunnable)
post(settleChildrenRunnable)
}

/** Returns the visibility, accessibility and pointer events a rail had before this view hid it. */
private fun restoreRailFlags(view: View) {
railOriginalFlags.remove(view)?.restoreTo(view)
}

/** One child is the content only. Three children are the left rail, the right rail and the content. */
private fun reconcileSideChildren() {
val previousContent = contentView
val previousLeft = leftActionsView
Expand All @@ -1518,6 +1550,11 @@ class SwipeableView(context: Context, appContext: AppContext) : ExpoView(context
if (contentView?.parent !== this) contentView = null
if (leftActionsView?.parent !== this) leftActionsView = null
if (rightActionsView?.parent !== this) rightActionsView = null
val only = if (childCount == 1) getChildAt(0) else null
if (only != null && contentView == null && childrenSettled &&
only !== leftActionsView && only !== rightActionsView) {
contentView = only
}
}
for (side in SwipeSide.values()) {
val rail = railFor(side)
Expand All @@ -1530,6 +1567,14 @@ class SwipeableView(context: Context, appContext: AppContext) : ExpoView(context
clipToPadding = false
}
}
// A view that lost its rail role, by removal or by a new role, gets its own flags back.
val savedRails = railOriginalFlags.entries.iterator()
while (savedRails.hasNext()) {
val (view, flags) = savedRails.next()
if (view === leftActionsView || view === rightActionsView) continue
savedRails.remove()
flags.restoreTo(view)
}
if (contentView !== previousContent) contentView?.translationX = currentTranslation
if (isOpen || isDragging || isAnimating) activeSide?.let(::requestActions)
updateActionsTransform()
Expand All @@ -1540,6 +1585,7 @@ class SwipeableView(context: Context, appContext: AppContext) : ExpoView(context
val visible = childCount == 3 && isSideEnabled(side) && side == activeSide &&
(isOpen || isDragging || isAnimating) && (currentTranslation != 0f || isOpen)
railFor(side)?.apply {
railOriginalFlags.getOrPut(this) { RailFlags(this) }
visibility = if (visible) View.VISIBLE else View.INVISIBLE
// RN's TouchTargetHelper ignores Android visibility. An empty
// zero-width rail still spans the row and otherwise steals taps
Expand Down
125 changes: 125 additions & 0 deletions android/src/test/java/expo/modules/swipeable/SwipeableViewTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,82 @@ class SwipeableViewTest {
assertEquals(0f, left.translationX, .01f)
}

@Test fun contentOnlyRowOpensAndClosesWithoutRails() {
val (row, content) = contentRow()
row.open(SwipeSide.LEFT)
settle()
assertEquals(160f, content.translationX, .01f)
assertEquals(listOf("left"), named("onActionsNeeded").map { it["side"] })
row.close()
settle()
assertEquals(0f, content.translationX, 0f)
}

@Test fun loneChildBecomesContentOnlyAfterTheMountTurn() {
val row = row(children = false)
val content = FrameLayout(activity)
row.addView(content)
row.open(SwipeSide.LEFT)
row.layout(0, 0, 400, 64)
assertEquals(0f, content.translationX, 0f)
settle()
assertEquals(160f, content.translationX, .01f)
}

@Test fun railPairMountKeepsContentIdentityAndTranslation() {
val (row, content) = contentRow()
row.open(SwipeSide.LEFT)
settle()
events.clear()
val left = FrameLayout(activity).apply { addView(View(activity)) }
val right = FrameLayout(activity)
row.addView(left, 0)
row.addView(right, 1)
row.layout(0, 0, 400, 64)
assertSame(content, row.getChildAt(2))
assertEquals(160f, content.translationX, .01f)
assertEquals(0f, left.translationX, .01f)
assertEquals(View.VISIBLE, left.visibility)
assertHidden(right)
}

@Test fun railPairUnmountKeepsContentIdentityAndTranslation() {
val row = row()
val content = content(row)
row.open(SwipeSide.LEFT)
settle()
row.removeViewAt(0)
row.removeViewAt(0)
row.layout(0, 0, 400, 64)
settle()
assertSame(content, row.getChildAt(0))
assertEquals(160f, content.translationX, .01f)
row.close()
settle()
assertEquals(0f, content.translationX, 0f)
}

@Test fun railPairMountedMidDragShowsTheDraggedSide() {
val (row, content) = contentRow()
row.handleGestureStart()
row.handleGestureUpdate(100f, 0f)
assertEquals(100f, content.translationX, .01f)
val left = FrameLayout(activity).apply { addView(View(activity)) }
val right = FrameLayout(activity)
row.addView(left, 0)
row.addView(right, 1)
row.layout(0, 0, 400, 64)
assertSame(content, row.getChildAt(2))
assertEquals(100f, content.translationX, .01f)
assertEquals(View.VISIBLE, left.visibility)
assertHidden(right)
row.handleGestureEnd(100f, 0f)
settle()
assertEquals(160f, content.translationX, .01f)
assertEquals(listOf("open:left"), ends())
assertEquals(setOf("left"), named("onActionsNeeded").map { it["side"] }.toSet())
}

@Test fun remountingAUsedSlotRequestsActionsAgain() {
val row = row()
row.open(SwipeSide.LEFT)
Expand Down Expand Up @@ -854,6 +930,44 @@ class SwipeableViewTest {
assertEquals(right.getChildAt(0).id, TouchTargetHelper.findTargetTagForTouch(280f, 32f, row))
}

@Test fun removedHiddenRailRestoresVisibilityAndAccessibilityBeforeReuse() {
val row = row()
val right = row.getChildAt(1)
assertHidden(right)
row.removeView(right)
assertEquals(View.VISIBLE, right.visibility)
assertEquals(View.IMPORTANT_FOR_ACCESSIBILITY_AUTO, right.importantForAccessibility)
row.addView(right, 1)
assertHidden(right)
}

@Test fun hiddenRailThatBecomesContentIsShownAgain() {
val row = row()
val right = row.getChildAt(1)
assertHidden(right)
row.removeViewAt(2)
row.removeViewAt(0)
row.addView(FrameLayout(activity), 0)
row.addView(FrameLayout(activity), 1)
row.layout(0, 0, 400, 64)
assertSame(right, row.getChildAt(2))
assertEquals(View.VISIBLE, right.visibility)
assertEquals(View.IMPORTANT_FOR_ACCESSIBILITY_AUTO, right.importantForAccessibility)
}

@Test fun hiddenReactRailThatBecomesContentTakesTouchesAgain() {
val row = reactRow()
val right = row.getChildAt(1) as ReactViewGroup
assertEquals(PointerEvents.NONE, right.pointerEvents)
row.removeViewAt(2)
row.removeViewAt(0)
row.addView(ReactViewGroup(activity), 0)
row.addView(ReactViewGroup(activity), 1)
row.layout(0, 0, 400, 64)
assertSame(right, row.getChildAt(2))
assertEquals(PointerEvents.AUTO, right.pointerEvents)
}

@Test fun cancelPendingDirectGestureCannotActivateUntilAnotherStart() {
val row = row()
row.handleGestureStart()
Expand Down Expand Up @@ -3070,6 +3184,17 @@ class SwipeableViewTest {
return row
}

/** A closed row as JS mounts it: the content view only, after the mount turn. */
private fun contentRow(): Pair<SwipeableView, View> {
val row = row(children = false)
val content = FrameLayout(activity)
row.addView(content)
row.layout(0, 0, 400, 64)
// A detached view runs its posted settle at the next attach traversal.
shadowOf(Looper.getMainLooper()).idleFor(Duration.ofMillis(16))
return row to content
}

private fun content(row: SwipeableView): View = row.getChildAt(2)
private fun settle() {
// Paused vsync delivers on clock advancement; advance a frame at a time
Expand Down
39 changes: 37 additions & 2 deletions ios/SwipePhysics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,13 @@ struct SwipeChildRoles: Equatable {
let content: Int
}

/// The child views that hold each role. A role is nil when no child holds it.
struct SwipeChildAssignment<Child: AnyObject> {
var left: Child?
var right: Child?
var content: Child?
}

protocol SwipeChildVisibility: AnyObject {
var isHidden: Bool { get set }
var isUserInteractionEnabled: Bool { get set }
Expand Down Expand Up @@ -488,9 +495,37 @@ extension SwipePhysics {
width.isFinite && width > 0 ? width : 0
}

/// One child is the content only. Three children are the left rail, the right rail and the content.
static func childRoles(count: Int) -> SwipeChildRoles? {
guard count == 3 else { return nil }
return SwipeChildRoles(left: 0, right: 1, content: 2)
switch count {
case 1: return SwipeChildRoles(left: nil, right: nil, content: 0)
case 3: return SwipeChildRoles(left: 0, right: 1, content: 2)
default: return nil
}
}

/// Any other child count is a Fabric transaction in progress: only the surviving roles stay.
/// A known rail never becomes content. Fabric mounts a three-child row one child at a time,
/// so a lone child with no role becomes content only when the transaction is `settled`.
static func assignChildRoles<Child: AnyObject>(
children: [Child], current: SwipeChildAssignment<Child>, settled: Bool
) -> SwipeChildAssignment<Child> {
func surviving(_ view: Child?) -> Child? {
guard let view = view, children.contains(where: { $0 === view }) else { return nil }
return view
}
let kept = SwipeChildAssignment(
left: surviving(current.left), right: surviving(current.right), content: surviving(current.content)
)
guard let roles = childRoles(count: children.count) else { return kept }
let content = children[roles.content]
if roles.left == nil {
if content === kept.left || content === kept.right { return kept }
if content !== kept.content && !settled { return kept }
}
return SwipeChildAssignment(
left: roles.left.map { children[$0] }, right: roles.right.map { children[$0] }, content: content
)
}

static func latchedSide(
Expand Down
33 changes: 20 additions & 13 deletions ios/SwipeableView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -948,24 +948,31 @@ public class SwipeableView: ExpoView {
slotChildren.removeAll()
}

private func reconcileChildRoles() {
/// `settled` is false inside Fabric's child mount callbacks, where a transaction can be in progress.
private func reconcileChildRoles(settled: Bool = true) {
guard !isDetached else { return }
let children = reactChildren.views
// On removal, preserve surviving identities instead of promoting a rail to content.
if let view = contentView, !children.contains(where: { $0 === view }) { releaseChild(view); contentView = nil }
if let view = leftActionsView, !children.contains(where: { $0 === view }) { releaseChild(view); leftActionsView = nil }
if let view = rightActionsView, !children.contains(where: { $0 === view }) { releaseChild(view); rightActionsView = nil }
guard let roles = SwipePhysics.childRoles(count: children.count) else { return }
let previousLeft = leftActionsView
let previousRight = rightActionsView
contentView = children[roles.content]
let roles = SwipePhysics.assignChildRoles(
children: children,
current: SwipeChildAssignment(left: leftActionsView, right: rightActionsView, content: contentView),
settled: settled
)
// On removal, preserve surviving identities instead of promoting a rail to content.
for view in [contentView, leftActionsView, rightActionsView].compactMap({ $0 })
where !children.contains(where: { $0 === view }) {
releaseChild(view)
}
contentView = roles.content
leftActionsView = roles.left
rightActionsView = roles.right
if previousLeft !== leftActionsView { requestedSides.remove(.left) }
if previousRight !== rightActionsView { requestedSides.remove(.right) }
guard contentView != nil, SwipePhysics.childRoles(count: children.count) != nil else { return }
// A complete Fabric transaction can give a previously hidden rail the content role.
// Restore only flags that this view owned, for all side configurations.
if let contentView = contentView { railVisibility.release(contentView) }
leftActionsView = roles.left.map { children[$0] }
rightActionsView = roles.right.map { children[$0] }
if previousLeft !== leftActionsView { requestedSides.remove(.left) }
if previousRight !== rightActionsView { requestedSides.remove(.right) }
contentView?.layer.zPosition = 1
for side in SwipeSide.allCases {
guard let rail = actionsView(for: side) else { continue }
Expand All @@ -989,7 +996,7 @@ public class SwipeableView: ExpoView {
releaseChild(childComponentView)
reactChildren.mount(childComponentView, at: index)
// Fabric mounts slots individually; commit only a complete child protocol.
reconcileChildRoles()
reconcileChildRoles(settled: false)
scheduleReconciliation()
}

Expand Down Expand Up @@ -1028,7 +1035,7 @@ public class SwipeableView: ExpoView {
let generation = workGeneration
if let cancelled = cancelled { onGestureCancelled(cancelled) }
guard generation == workGeneration else { return }
reconcileChildRoles()
reconcileChildRoles(settled: false)
scheduleReconciliation()
}

Expand Down
Loading
Loading