Skip to content

Commit

Permalink
Reorder methods and add pragma marked sections (#288)
Browse files Browse the repository at this point in the history
* Reorder methods and add pragma marked sections

* Fix SwiftLint violations
  • Loading branch information
tinder-cfuller authored Jan 19, 2024
1 parent 153b8d7 commit 0cfab58
Show file tree
Hide file tree
Showing 6 changed files with 290 additions and 252 deletions.
4 changes: 4 additions & 0 deletions Sources/Layout/Swift/Array.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import UIKit

extension Array where Element == NSLayoutConstraint {

// MARK: - Activation

/// Activates the constraints in the array.
///
/// - Returns: The activated constraints.
Expand All @@ -33,6 +35,8 @@ extension Array where Element == NSLayoutConstraint {
return self
}

// MARK: - Priority

/// Sets the priority of the constraints in the array to required.
///
/// - Returns: The required constraints.
Expand Down
6 changes: 4 additions & 2 deletions Sources/Layout/UIKit/NSLayoutConstraint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import UIKit

extension NSLayoutConstraint {

// MARK: - VFL Convenience APIs
// MARK: - Visual Format Language

/// Creates constraints described by a visual format string.
///
Expand Down Expand Up @@ -49,7 +49,7 @@ extension NSLayoutConstraint {
formats.flatMap { constraints(format: $0, views: views, metrics: metrics, options: options) }
}

// MARK: - Mutators
// MARK: - Activation

/// Activates the constraint.
///
Expand All @@ -69,6 +69,8 @@ extension NSLayoutConstraint {
return self
}

// MARK: - Priority

/// Sets the priority of the constraint to required.
///
/// - Returns: The required constraint.
Expand Down
188 changes: 99 additions & 89 deletions Sources/Layout/UIKit/UIView+AutoLayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import UIKit

extension UIView {

// MARK: - Constraint Builders
// MARK: - Builder

/// Require the layout of the view be determined by adding constraints.
///
Expand Down Expand Up @@ -54,10 +54,106 @@ extension UIView {
return usingConstraints()
}

// MARK: - Constraint Factories
// MARK: - Size

/// Creates a constraint defining the width of the receiver.
///
/// - Parameters:
/// - relation: The relationship between the width and constant value.
/// - constant: The constant width value. When `nil`, the width of the receiver is used.
///
/// - Returns: The created constraint.
public func widthConstraint(
is relation: NSLayoutConstraint.Relation = .equal,
constant: CGFloat? = nil
) -> NSLayoutConstraint {
width.constraint(is: relation, constant: constant ?? bounds.width)
}

/// Creates a constraint defining the height of the receiver.
///
/// - Parameters:
/// - relation: The relationship between the height and constant value.
/// - constant: The constant height value. When `nil`, the height of the receiver is used.
///
/// - Returns: The created constraint.
public func heightConstraint(
is relation: NSLayoutConstraint.Relation = .equal,
constant: CGFloat? = nil
) -> NSLayoutConstraint {
height.constraint(is: relation, constant: constant ?? bounds.height)
}

/// Creates constraints defining the size of the receiver.
///
/// - Parameter size: The constant size value. When `nil`, the size of the receiver is used.
///
/// - Returns: The created constraints.
public func sizeConstraints(
_ size: CGSize? = nil
) -> [NSLayoutConstraint] {
[
width.constraint(constant: size?.width ?? bounds.width),
height.constraint(constant: size?.height ?? bounds.height)
]
}

// MARK: - Aspect Ratio

/// Creates a constraint defining the aspect ratio of the receiver to be square.
///
/// - Returns: The created constraint.
public func squareConstraint() -> NSLayoutConstraint {
constraint(for: .width, to: .height, of: self)
}

/// Creates a constraint defining the aspect ratio of the receiver.
///
/// - Parameter ratio: The aspect ratio.
///
/// - Returns: The created constraint.
public func aspectRatioConstraint(
_ ratio: CGFloat
) -> NSLayoutConstraint {
constraint(for: .width, to: .height, of: self, multiplier: ratio)
}

// MARK: - Equal

/// Creates constraints between the given attribute of the receiver and target views.
///
/// - Parameters:
/// - attribute: The attribute to constrain.
/// - views: The views to constrain to the receiver.
///
/// - Returns: The created constraints.
public func equalConstraints(
for attribute: NSLayoutConstraint.Attribute,
to views: [UIView]
) -> [NSLayoutConstraint] {
views.map { constraint(to: attribute, of: $0) }
}

// MARK: - Center

/// Creates constraints to the center of the superview of the receiver with an offset.
///
/// - Parameter offset: The offset amount.
///
/// - Returns: The created constraints.
public func centerConstraints(
offset: UIOffset = .zero
) -> [NSLayoutConstraint] {
[
constraint(toSuperview: .centerX, constant: offset.horizontal),
constraint(toSuperview: .centerY, constant: offset.vertical)
]
}

// swiftlint:disable function_default_parameter_at_end

// MARK: - Attributes

/// Creates a constraint defining the relationship between the given attribute of the receiver and superview.
///
/// The `superviewAttribute` will be used as the attribute of the receiver if `attribute` is not provided.
Expand Down Expand Up @@ -168,65 +264,7 @@ extension UIView {

// swiftlint:enable function_default_parameter_at_end

/// Creates a constraint defining the width of the receiver.
///
/// - Parameters:
/// - relation: The relationship between the width and constant value.
/// - constant: The constant width value. When `nil`, the width of the receiver is used.
///
/// - Returns: The created constraint.
public func widthConstraint(
is relation: NSLayoutConstraint.Relation = .equal,
constant: CGFloat? = nil
) -> NSLayoutConstraint {
width.constraint(is: relation, constant: constant ?? bounds.width)
}

/// Creates a constraint defining the height of the receiver.
///
/// - Parameters:
/// - relation: The relationship between the height and constant value.
/// - constant: The constant height value. When `nil`, the height of the receiver is used.
///
/// - Returns: The created constraint.
public func heightConstraint(
is relation: NSLayoutConstraint.Relation = .equal,
constant: CGFloat? = nil
) -> NSLayoutConstraint {
height.constraint(is: relation, constant: constant ?? bounds.height)
}

/// Creates constraints defining the size of the receiver.
///
/// - Parameter size: The constant size value. When `nil`, the size of the receiver is used.
///
/// - Returns: The created constraints.
public func sizeConstraints(
_ size: CGSize? = nil
) -> [NSLayoutConstraint] {
[
width.constraint(constant: size?.width ?? bounds.width),
height.constraint(constant: size?.height ?? bounds.height)
]
}

/// Creates a constraint defining the aspect ratio of the receiver to be square.
///
/// - Returns: The created constraint.
public func squareConstraint() -> NSLayoutConstraint {
constraint(for: .width, to: .height, of: self)
}

/// Creates a constraint defining the aspect ratio of the receiver.
///
/// - Parameter ratio: The aspect ratio.
///
/// - Returns: The created constraint.
public func aspectRatioConstraint(
_ ratio: CGFloat
) -> NSLayoutConstraint {
constraint(for: .width, to: .height, of: self, multiplier: ratio)
}
// MARK: - Edges

/// Creates constraints to the edges of the superview of the receiver with an inset.
///
Expand Down Expand Up @@ -270,32 +308,4 @@ extension UIView {
constraint(toSuperview: .right, constant: -insets.right)
]
}

/// Creates constraints to the center of the superview of the receiver with an offset.
///
/// - Parameter offset: The offset amount.
///
/// - Returns: The created constraints.
public func centerConstraints(
offset: UIOffset = .zero
) -> [NSLayoutConstraint] {
[
constraint(toSuperview: .centerX, constant: offset.horizontal),
constraint(toSuperview: .centerY, constant: offset.vertical)
]
}

/// Creates constraints between the given attribute of the receiver and target views.
///
/// - Parameters:
/// - attribute: The attribute to constrain.
/// - views: The views to constrain to the receiver.
///
/// - Returns: The created constraints.
public func equalConstraints(
for attribute: NSLayoutConstraint.Attribute,
to views: [UIView]
) -> [NSLayoutConstraint] {
views.map { constraint(to: attribute, of: $0) }
}
}
4 changes: 4 additions & 0 deletions Tests/LayoutTests/Swift/ArrayTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import XCTest
@MainActor
final class ArrayTests: XCTestCase {

// MARK: - Activation

func testActivation() {

// GIVEN
Expand Down Expand Up @@ -65,6 +67,8 @@ final class ArrayTests: XCTestCase {
expect(deactivatedConstraints) == constraints
}

// MARK: - Priority

func testRequire() {

// GIVEN
Expand Down
76 changes: 41 additions & 35 deletions Tests/LayoutTests/UIKit/NSLayoutConstraintTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,41 +14,7 @@ import XCTest
@MainActor
final class NSLayoutConstraintTests: XCTestCase {

func testActivation() {

// GIVEN

let view: UIView = .init()
let constraint: NSLayoutConstraint = .init(
item: view,
attribute: .height,
relatedBy: .equal,
toItem: nil,
attribute: .notAnAttribute,
multiplier: 1,
constant: 0
)

// THEN

expect(constraint.isActive) == false

// WHEN

constraint.activate()

// THEN

expect(constraint.isActive) == true

// WHEN

constraint.deactivate()

// THEN

expect(constraint.isActive) == false
}
// MARK: - Visual Format Language

func testConstraintsWithVisualFormatLanguage() {

Expand Down Expand Up @@ -108,6 +74,46 @@ final class NSLayoutConstraintTests: XCTestCase {
}
}

// MARK: - Activation

func testActivation() {

// GIVEN

let view: UIView = .init()
let constraint: NSLayoutConstraint = .init(
item: view,
attribute: .height,
relatedBy: .equal,
toItem: nil,
attribute: .notAnAttribute,
multiplier: 1,
constant: 0
)

// THEN

expect(constraint.isActive) == false

// WHEN

constraint.activate()

// THEN

expect(constraint.isActive) == true

// WHEN

constraint.deactivate()

// THEN

expect(constraint.isActive) == false
}

// MARK: - Priority

func testRequire() {

// GIVEN
Expand Down
Loading

0 comments on commit 0cfab58

Please sign in to comment.