From 6bcf74c688f168f6236fc094013bbcf15bb470c0 Mon Sep 17 00:00:00 2001 From: Jake Wharton Date: Thu, 30 Nov 2023 07:35:04 -0500 Subject: [PATCH] Vary whole remove lambda on UIStackView subtype (#1711) Rather than checking for every operation. --- .../app/cash/redwood/widget/UIViewChildren.kt | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/redwood-widget/src/iosMain/kotlin/app/cash/redwood/widget/UIViewChildren.kt b/redwood-widget/src/iosMain/kotlin/app/cash/redwood/widget/UIViewChildren.kt index a5b37e6352..7f33c801a0 100644 --- a/redwood-widget/src/iosMain/kotlin/app/cash/redwood/widget/UIViewChildren.kt +++ b/redwood-widget/src/iosMain/kotlin/app/cash/redwood/widget/UIViewChildren.kt @@ -27,15 +27,9 @@ public class UIViewChildren( is UIStackView -> { view, index -> parent.insertArrangedSubview(view, index.convert()) } else -> { view, index -> parent.insertSubview(view, index.convert()) } }, - private val remove: (index: Int, count: Int) -> Array = { 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 = when (parent) { + is UIStackView -> { index, count -> parent.typedArrangedSubviews.remove(index, count) } + else -> { index, count -> parent.typedSubviews.remove(index, count) } }, ) : Widget.Children { private val _widgets = ArrayList>() @@ -78,3 +72,9 @@ public class UIViewChildren( (parent.superview ?: parent).setNeedsLayout() } } + +private fun List.remove(index: Int, count: Int): Array { + return Array(count) { offset -> + this[index + offset].also(UIView::removeFromSuperview) + } +}