Skip to content

Commit 70c5057

Browse files
Revise addItem and addItems methods (#368)
* Revise `addItem` and `addItems` methods * Remove DocC documentation * Improve DocC documentation
1 parent 76a29aa commit 70c5057

File tree

4 files changed

+61
-47
lines changed

4 files changed

+61
-47
lines changed

Sources/Layout/Layout.swift

+29-20
Original file line numberDiff line numberDiff line change
@@ -108,39 +108,28 @@ public final class Layout { // swiftlint:disable:this type_body_length
108108

109109
// MARK: - Adding Items
110110

111-
/// Adds items to the layout.
111+
/// Adds an item to the layout.
112112
///
113-
/// - Parameter items: The items to be added as subviews.
113+
/// - Parameter item: The item to be added as a subview.
114114
///
115-
/// - Returns: The receiver with the added subviews.
115+
/// - Returns: The receiver with the added subview.
116116
@discardableResult
117-
public func addItems(
118-
_ items: LayoutItem...
117+
public func addItem(
118+
_ item: LayoutItem
119119
) -> Layout {
120-
addItems(items)
120+
addItems([item])
121121
}
122122

123123
/// Adds items to the layout.
124124
///
125-
/// - Parameter items: The items to be added as subviews.
125+
/// - Parameter items: The builder that creates the items to be added as subviews.
126126
///
127127
/// - Returns: The receiver with the added subviews.
128128
@discardableResult
129129
public func addItems(
130-
_ items: [LayoutItem]
130+
@LayoutBuilder items: () -> [LayoutItem]
131131
) -> Layout {
132-
items.forEach { item in
133-
let subview: UIView = item.layoutItemView
134-
subview.translatesAutoresizingMaskIntoConstraints = false
135-
if subview.superview != view {
136-
view?.addSubview(subview)
137-
}
138-
if let key: String = subview.identifier, !key.isEmpty {
139-
self.items[key] = subview
140-
}
141-
adding(item.superviewConstraints(item))
142-
}
143-
return self
132+
addItems(items())
144133
}
145134

146135
// MARK: - Adding Constraints
@@ -744,4 +733,24 @@ public final class Layout { // swiftlint:disable:this type_body_length
744733
view.setNeedsUpdateConstraints()
745734
view.updateConstraintsIfNeeded()
746735
}
736+
737+
// MARK: - Private
738+
739+
@discardableResult
740+
private func addItems(
741+
_ items: [LayoutItem]
742+
) -> Layout {
743+
items.forEach { item in
744+
let subview: UIView = item.layoutItemView
745+
subview.translatesAutoresizingMaskIntoConstraints = false
746+
if subview.superview != view {
747+
view?.addSubview(subview)
748+
}
749+
if let key: String = subview.identifier, !key.isEmpty {
750+
self.items[key] = subview
751+
}
752+
adding(item.superviewConstraints(item))
753+
}
754+
return self
755+
}
747756
}

Sources/Layout/LayoutItem.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public typealias SuperviewConstraints = (LayoutItem) -> [NSLayoutConstraint]
3030
* // Creating a layout with multiple items
3131
* let item1: LayoutItem = subview1.toEdges()
3232
* let item2: LayoutItem = subview2.square().center()
33-
* view.layout().addItems(item1, item2).activate()
33+
* view.layout(item1).addItem(item2).activate()
3434
* ```
3535
*
3636
* The following code demonstrates the preferred way of constructing and activating a layout with multiple items

Tests/LayoutTests/LayoutExampleTests.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ final class LayoutExampleTests: XCTestCase {
197197
assertLayout { view in
198198
let item1: LayoutItem = subview1.toEdges()
199199
let item2: LayoutItem = subview2.square().center()
200-
view.layout().addItems(item1, item2).activate()
200+
view.layout(item1).addItem(item2).activate()
201201
}
202202
}
203203

Tests/LayoutTests/LayoutTests.swift

+30-25
Original file line numberDiff line numberDiff line change
@@ -147,28 +147,26 @@ final class LayoutTests: XCTestCase {
147147

148148
// MARK: - Adding Items
149149

150-
func testAddItemsVariadic() {
150+
func testAddItem() {
151151

152152
// GIVEN
153153

154154
let view: UIView = .init()
155155
let layout: Layout = .init(view)
156-
let view1: UIView = .init()
157-
let view2: UIView = .init()
156+
let subview: UIView = .init()
158157

159158
// THEN
160159

161160
expect(layout.items.isEmpty) == true
162161

163162
// WHEN
164163

165-
layout.addItems(view1.id("view1"), view2.id("view2"))
164+
layout.addItem(subview.id("subview"))
166165

167166
// THEN
168167

169-
expect(layout.items.count) == 2
170-
expect(layout.items["view1"]) === view1
171-
expect(layout.items["view2"]) === view2
168+
expect(layout.items.count) == 1
169+
expect(layout.items["subview"]) === subview
172170
}
173171

174172
func testAddItems() {
@@ -177,22 +175,25 @@ final class LayoutTests: XCTestCase {
177175

178176
let view: UIView = .init()
179177
let layout: Layout = .init(view)
180-
let view1: UIView = .init()
181-
let view2: UIView = .init()
178+
let subview1: UIView = .init()
179+
let subview2: UIView = .init()
182180

183181
// THEN
184182

185183
expect(layout.items.isEmpty) == true
186184

187185
// WHEN
188186

189-
layout.addItems([view1.id("view1"), view2.id("view2")])
187+
layout.addItems {
188+
subview1.id("subview1")
189+
subview2.id("subview2")
190+
}
190191

191192
// THEN
192193

193194
expect(layout.items.count) == 2
194-
expect(layout.items["view1"]) === view1
195-
expect(layout.items["view2"]) === view2
195+
expect(layout.items["subview1"]) === subview1
196+
expect(layout.items["subview2"]) === subview2
196197
}
197198

198199
// MARK: - Adding Constraints
@@ -670,7 +671,7 @@ final class LayoutTests: XCTestCase {
670671
let superview: UIView = .init()
671672
let view1: UIView = .init()
672673
let view2: UIView = .init()
673-
let layout: Layout = superview.layout().addItems(view1, view2)
674+
let layout: Layout = superview.layout(view1).addItem(view2)
674675

675676
// WHEN
676677

@@ -762,7 +763,7 @@ final class LayoutTests: XCTestCase {
762763
var superview: UIView? = .init()
763764
let view: UIView = .init()
764765
let siblingView: UIView = .init()
765-
let layout: Layout = superview!.layout().addItems(view, siblingView)
766+
let layout: Layout = superview!.layout(view).addItem(siblingView)
766767
let leadingAnchor: NSLayoutXAxisAnchor = siblingView.trailing
767768
let trailingAnchor: NSLayoutXAxisAnchor = superview!.trailing
768769

@@ -822,7 +823,7 @@ final class LayoutTests: XCTestCase {
822823
var superview: UIView? = .init()
823824
let view: UIView = .init()
824825
let siblingView: UIView = .init()
825-
let layout: Layout = superview!.layout().addItems(view, siblingView)
826+
let layout: Layout = superview!.layout(view).addItem(siblingView)
826827
let topAnchor: NSLayoutYAxisAnchor = siblingView.bottom
827828
let bottomAnchor: NSLayoutYAxisAnchor = superview!.bottom
828829

@@ -941,7 +942,7 @@ final class LayoutTests: XCTestCase {
941942
let superview: UIView = .init()
942943
let view1: UIView = .init()
943944
let view2: UIView = .init()
944-
let layout: Layout = superview.layout().addItems(view1, view2)
945+
let layout: Layout = superview.layout(view1).addItem(view2)
945946

946947
// WHEN
947948

@@ -1043,7 +1044,7 @@ final class LayoutTests: XCTestCase {
10431044
let superview: UIView = .init()
10441045
let view1: UIView = .init()
10451046
let view2: UIView = .init()
1046-
let layout: Layout = superview.layout().addItems(view1, view2)
1047+
let layout: Layout = superview.layout(view1).addItem(view2)
10471048

10481049
// WHEN
10491050

@@ -1084,7 +1085,7 @@ final class LayoutTests: XCTestCase {
10841085
// WHEN
10851086

10861087
layout
1087-
.addItems(subview.id("subview"))
1088+
.addItem(subview.id("subview"))
10881089
.horizontal(format)
10891090

10901091
// THEN
@@ -1118,9 +1119,11 @@ final class LayoutTests: XCTestCase {
11181119

11191120
// WHEN
11201121

1121-
layout
1122-
.addItems(subview1.id("subview1"), subview2.id("subview2"))
1123-
.horizontal(format, metrics: metrics, options: .alignAllCenterY)
1122+
layout.addItems {
1123+
subview1.id("subview1")
1124+
subview2.id("subview2")
1125+
}
1126+
.horizontal(format, metrics: metrics, options: .alignAllCenterY)
11241127

11251128
// THEN
11261129

@@ -1148,7 +1151,7 @@ final class LayoutTests: XCTestCase {
11481151
// WHEN
11491152

11501153
layout
1151-
.addItems(subview.id("subview"))
1154+
.addItem(subview.id("subview"))
11521155
.vertical(format)
11531156

11541157
// THEN
@@ -1182,9 +1185,11 @@ final class LayoutTests: XCTestCase {
11821185

11831186
// WHEN
11841187

1185-
layout
1186-
.addItems(subview1.id("subview1"), subview2.id("subview2"))
1187-
.vertical(format, metrics: metrics, options: .alignAllCenterX)
1188+
layout.addItems {
1189+
subview1.id("subview1")
1190+
subview2.id("subview2")
1191+
}
1192+
.vertical(format, metrics: metrics, options: .alignAllCenterX)
11881193

11891194
// THEN
11901195

0 commit comments

Comments
 (0)