diff --git a/Sources/Layout/Layout.swift b/Sources/Layout/Layout.swift index 689856ff..00cc5535 100644 --- a/Sources/Layout/Layout.swift +++ b/Sources/Layout/Layout.swift @@ -108,39 +108,28 @@ public final class Layout { // swiftlint:disable:this type_body_length // MARK: - Adding Items - /// Adds items to the layout. + /// Adds an item to the layout. /// - /// - Parameter items: The items to be added as subviews. + /// - Parameter item: The item to be added as a subview. /// - /// - Returns: The receiver with the added subviews. + /// - Returns: The receiver with the added subview. @discardableResult - public func addItems( - _ items: LayoutItem... + public func addItem( + _ item: LayoutItem ) -> Layout { - addItems(items) + addItems([item]) } /// Adds items to the layout. /// - /// - Parameter items: The items to be added as subviews. + /// - Parameter items: The builder that creates the items to be added as subviews. /// /// - Returns: The receiver with the added subviews. @discardableResult public func addItems( - _ items: [LayoutItem] + @LayoutBuilder items: () -> [LayoutItem] ) -> Layout { - items.forEach { item in - let subview: UIView = item.layoutItemView - subview.translatesAutoresizingMaskIntoConstraints = false - if subview.superview != view { - view?.addSubview(subview) - } - if let key: String = subview.identifier, !key.isEmpty { - self.items[key] = subview - } - adding(item.superviewConstraints(item)) - } - return self + addItems(items()) } // MARK: - Adding Constraints @@ -744,4 +733,24 @@ public final class Layout { // swiftlint:disable:this type_body_length view.setNeedsUpdateConstraints() view.updateConstraintsIfNeeded() } + + // MARK: - Private + + @discardableResult + private func addItems( + _ items: [LayoutItem] + ) -> Layout { + items.forEach { item in + let subview: UIView = item.layoutItemView + subview.translatesAutoresizingMaskIntoConstraints = false + if subview.superview != view { + view?.addSubview(subview) + } + if let key: String = subview.identifier, !key.isEmpty { + self.items[key] = subview + } + adding(item.superviewConstraints(item)) + } + return self + } } diff --git a/Sources/Layout/LayoutItem.swift b/Sources/Layout/LayoutItem.swift index 6e3f35d6..30fa4f6a 100644 --- a/Sources/Layout/LayoutItem.swift +++ b/Sources/Layout/LayoutItem.swift @@ -30,7 +30,7 @@ public typealias SuperviewConstraints = (LayoutItem) -> [NSLayoutConstraint] * // Creating a layout with multiple items * let item1: LayoutItem = subview1.toEdges() * let item2: LayoutItem = subview2.square().center() - * view.layout().addItems(item1, item2).activate() + * view.layout(item1).addItem(item2).activate() * ``` * * The following code demonstrates the preferred way of constructing and activating a layout with multiple items diff --git a/Tests/LayoutTests/LayoutExampleTests.swift b/Tests/LayoutTests/LayoutExampleTests.swift index b883990d..53c8114d 100644 --- a/Tests/LayoutTests/LayoutExampleTests.swift +++ b/Tests/LayoutTests/LayoutExampleTests.swift @@ -197,7 +197,7 @@ final class LayoutExampleTests: XCTestCase { assertLayout { view in let item1: LayoutItem = subview1.toEdges() let item2: LayoutItem = subview2.square().center() - view.layout().addItems(item1, item2).activate() + view.layout(item1).addItem(item2).activate() } } diff --git a/Tests/LayoutTests/LayoutTests.swift b/Tests/LayoutTests/LayoutTests.swift index ffa04b31..8fb3381f 100644 --- a/Tests/LayoutTests/LayoutTests.swift +++ b/Tests/LayoutTests/LayoutTests.swift @@ -147,14 +147,13 @@ final class LayoutTests: XCTestCase { // MARK: - Adding Items - func testAddItemsVariadic() { + func testAddItem() { // GIVEN let view: UIView = .init() let layout: Layout = .init(view) - let view1: UIView = .init() - let view2: UIView = .init() + let subview: UIView = .init() // THEN @@ -162,13 +161,12 @@ final class LayoutTests: XCTestCase { // WHEN - layout.addItems(view1.id("view1"), view2.id("view2")) + layout.addItem(subview.id("subview")) // THEN - expect(layout.items.count) == 2 - expect(layout.items["view1"]) === view1 - expect(layout.items["view2"]) === view2 + expect(layout.items.count) == 1 + expect(layout.items["subview"]) === subview } func testAddItems() { @@ -177,8 +175,8 @@ final class LayoutTests: XCTestCase { let view: UIView = .init() let layout: Layout = .init(view) - let view1: UIView = .init() - let view2: UIView = .init() + let subview1: UIView = .init() + let subview2: UIView = .init() // THEN @@ -186,13 +184,16 @@ final class LayoutTests: XCTestCase { // WHEN - layout.addItems([view1.id("view1"), view2.id("view2")]) + layout.addItems { + subview1.id("subview1") + subview2.id("subview2") + } // THEN expect(layout.items.count) == 2 - expect(layout.items["view1"]) === view1 - expect(layout.items["view2"]) === view2 + expect(layout.items["subview1"]) === subview1 + expect(layout.items["subview2"]) === subview2 } // MARK: - Adding Constraints @@ -670,7 +671,7 @@ final class LayoutTests: XCTestCase { let superview: UIView = .init() let view1: UIView = .init() let view2: UIView = .init() - let layout: Layout = superview.layout().addItems(view1, view2) + let layout: Layout = superview.layout(view1).addItem(view2) // WHEN @@ -762,7 +763,7 @@ final class LayoutTests: XCTestCase { var superview: UIView? = .init() let view: UIView = .init() let siblingView: UIView = .init() - let layout: Layout = superview!.layout().addItems(view, siblingView) + let layout: Layout = superview!.layout(view).addItem(siblingView) let leadingAnchor: NSLayoutXAxisAnchor = siblingView.trailing let trailingAnchor: NSLayoutXAxisAnchor = superview!.trailing @@ -822,7 +823,7 @@ final class LayoutTests: XCTestCase { var superview: UIView? = .init() let view: UIView = .init() let siblingView: UIView = .init() - let layout: Layout = superview!.layout().addItems(view, siblingView) + let layout: Layout = superview!.layout(view).addItem(siblingView) let topAnchor: NSLayoutYAxisAnchor = siblingView.bottom let bottomAnchor: NSLayoutYAxisAnchor = superview!.bottom @@ -941,7 +942,7 @@ final class LayoutTests: XCTestCase { let superview: UIView = .init() let view1: UIView = .init() let view2: UIView = .init() - let layout: Layout = superview.layout().addItems(view1, view2) + let layout: Layout = superview.layout(view1).addItem(view2) // WHEN @@ -1043,7 +1044,7 @@ final class LayoutTests: XCTestCase { let superview: UIView = .init() let view1: UIView = .init() let view2: UIView = .init() - let layout: Layout = superview.layout().addItems(view1, view2) + let layout: Layout = superview.layout(view1).addItem(view2) // WHEN @@ -1084,7 +1085,7 @@ final class LayoutTests: XCTestCase { // WHEN layout - .addItems(subview.id("subview")) + .addItem(subview.id("subview")) .horizontal(format) // THEN @@ -1118,9 +1119,11 @@ final class LayoutTests: XCTestCase { // WHEN - layout - .addItems(subview1.id("subview1"), subview2.id("subview2")) - .horizontal(format, metrics: metrics, options: .alignAllCenterY) + layout.addItems { + subview1.id("subview1") + subview2.id("subview2") + } + .horizontal(format, metrics: metrics, options: .alignAllCenterY) // THEN @@ -1148,7 +1151,7 @@ final class LayoutTests: XCTestCase { // WHEN layout - .addItems(subview.id("subview")) + .addItem(subview.id("subview")) .vertical(format) // THEN @@ -1182,9 +1185,11 @@ final class LayoutTests: XCTestCase { // WHEN - layout - .addItems(subview1.id("subview1"), subview2.id("subview2")) - .vertical(format, metrics: metrics, options: .alignAllCenterX) + layout.addItems { + subview1.id("subview1") + subview2.id("subview2") + } + .vertical(format, metrics: metrics, options: .alignAllCenterX) // THEN