Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revise addItem and addItems methods #368

Merged
merged 3 commits into from
Mar 7, 2024
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
49 changes: 29 additions & 20 deletions Sources/Layout/Layout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
}
2 changes: 1 addition & 1 deletion Sources/Layout/LayoutItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Tests/LayoutTests/LayoutExampleTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}

Expand Down
55 changes: 30 additions & 25 deletions Tests/LayoutTests/LayoutTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -147,28 +147,26 @@ 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

expect(layout.items.isEmpty) == true

// 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() {
Expand All @@ -177,22 +175,25 @@ 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

expect(layout.items.isEmpty) == true

// 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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -1084,7 +1085,7 @@ final class LayoutTests: XCTestCase {
// WHEN

layout
.addItems(subview.id("subview"))
.addItem(subview.id("subview"))
.horizontal(format)

// THEN
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -1148,7 +1151,7 @@ final class LayoutTests: XCTestCase {
// WHEN

layout
.addItems(subview.id("subview"))
.addItem(subview.id("subview"))
.vertical(format)

// THEN
Expand Down Expand Up @@ -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

Expand Down
Loading