Skip to content

Commit

Permalink
Vary whole remove lambda on UIStackView subtype (#1711)
Browse files Browse the repository at this point in the history
Rather than checking for every operation.
  • Loading branch information
JakeWharton authored Nov 30, 2023
1 parent 0bd1bb3 commit 6bcf74c
Showing 1 changed file with 9 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,9 @@ public class UIViewChildren(
is UIStackView -> { view, index -> parent.insertArrangedSubview(view, index.convert()) }
else -> { view, index -> parent.insertSubview(view, index.convert<NSInteger>()) }
},
private val remove: (index: Int, count: Int) -> Array<UIView> = { index, count ->
// Per the docs, these properties are read-only copies we can use stable indexing into.
val subviews = when (parent) {
is UIStackView -> parent.typedArrangedSubviews
else -> parent.typedSubviews
}
Array(count) { offset ->
subviews[index + offset].also(UIView::removeFromSuperview)
}
private val remove: (index: Int, count: Int) -> Array<UIView> = when (parent) {
is UIStackView -> { index, count -> parent.typedArrangedSubviews.remove(index, count) }
else -> { index, count -> parent.typedSubviews.remove(index, count) }
},
) : Widget.Children<UIView> {
private val _widgets = ArrayList<Widget<UIView>>()
Expand Down Expand Up @@ -78,3 +72,9 @@ public class UIViewChildren(
(parent.superview ?: parent).setNeedsLayout()
}
}

private fun List<UIView>.remove(index: Int, count: Int): Array<UIView> {
return Array(count) { offset ->
this[index + offset].also(UIView::removeFromSuperview)
}
}

0 comments on commit 6bcf74c

Please sign in to comment.