Skip to content

Commit

Permalink
Merge branch 'main' into move-array
Browse files Browse the repository at this point in the history
# Conflicts:
#	Sources/Layout/NSLayoutConstraint.swift
  • Loading branch information
tinder-garricnahapetian committed Oct 25, 2023
2 parents f4c1cf7 + 331e575 commit ebd6ed9
Show file tree
Hide file tree
Showing 356 changed files with 1,919 additions and 1,393 deletions.
142 changes: 3 additions & 139 deletions Sources/Layout/AnchorAttribute.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// swiftlint:disable:this file_name
//
// All Contributions by Match Group
//
Expand All @@ -9,149 +10,12 @@

import UIKit

// swiftlint:disable file_types_order

internal protocol AnchorAttribute {

associatedtype AnchorType: AnyObject

var attributeType: AnchorAttributeType { get }
var attribute: NSLayoutConstraint.Attribute { get }
}

// swiftlint:enable file_types_order

internal enum AnchorAttributeType {

case xAxisAnchor, yAxisAnchor, dimension
}

public enum XAxisAttribute: AnchorAttribute {
public enum XAxisAttribute {

case left, centerX, right, leading, trailing

internal typealias AnchorType = NSLayoutXAxisAnchor

internal var attributeType: AnchorAttributeType {
.xAxisAnchor
}

internal var attribute: NSLayoutConstraint.Attribute {
switch self {
case .left:
return .left
case .centerX:
return .centerX
case .right:
return .right
case .leading:
return .leading
case .trailing:
return .trailing
}
}
}

public enum YAxisAttribute: AnchorAttribute {
public enum YAxisAttribute {

case top, centerY, firstBaseline, lastBaseline, bottom

internal typealias AnchorType = NSLayoutYAxisAnchor

internal var attributeType: AnchorAttributeType {
.yAxisAnchor
}

internal var attribute: NSLayoutConstraint.Attribute {
switch self {
case .top:
return .top
case .centerY:
return .centerY
case .firstBaseline:
return .firstBaseline
case .lastBaseline:
return .lastBaseline
case .bottom:
return .bottom
}
}
}

public enum DimensionAttribute: AnchorAttribute {

case width, height

internal typealias AnchorType = NSLayoutDimension

internal var attributeType: AnchorAttributeType {
.dimension
}

internal var attribute: NSLayoutConstraint.Attribute {
switch self {
case .width:
return .width
case .height:
return .height
}
}
}

extension LayoutAnchoring {

internal func anchor<T: AnchorAttribute>(for attribute: T) -> NSLayoutAnchor<T.AnchorType> {
// swiftlint:disable force_cast
switch attribute.attributeType {
case .xAxisAnchor:
let attribute: XAxisAttribute = attribute as! XAxisAttribute
return xAnchor(for: attribute) as! NSLayoutAnchor<T.AnchorType>
case .yAxisAnchor:
let attribute: YAxisAttribute = attribute as! YAxisAttribute
return yAnchor(for: attribute) as! NSLayoutAnchor<T.AnchorType>
case .dimension:
let attribute: DimensionAttribute = attribute as! DimensionAttribute
return sizeAnchor(for: attribute) as! NSLayoutAnchor<T.AnchorType>
}
// swiftlint:enable force_cast
}

private func xAnchor(for attribute: XAxisAttribute) -> NSLayoutXAxisAnchor {
switch attribute {
case .left:
return left
case .centerX:
return centerX
case .right:
return right
case .leading:
return leading
case .trailing:
return trailing
}
}

private func yAnchor(for attribute: YAxisAttribute) -> NSLayoutYAxisAnchor {
switch attribute {
case .top:
return top
case .centerY:
return centerY
case .firstBaseline:
return firstBaseline
case .lastBaseline:
return lastBaseline
case .bottom:
return bottom
}
}

private func sizeAnchor(for attribute: DimensionAttribute) -> NSLayoutDimension {
switch attribute {
case .width:
return width
case .height:
return height
}
}
}
4 changes: 2 additions & 2 deletions Sources/Layout/Array.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ extension Array where Element == NSLayoutConstraint {

@preconcurrency
@MainActor
@discardableResult
public func withPriority(
_ priority: UILayoutPriority
) -> [NSLayoutConstraint] {
map { $0.withPriority(priority) }
prioritize(priority)
return self
}

@preconcurrency
Expand Down
25 changes: 25 additions & 0 deletions Sources/Layout/Collection.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//
// All Contributions by Match Group
//
// Copyright © 2023 Tinder (Match Group, LLC)
//
// Licensed under the Match Group Modified 3-Clause BSD License.
// See https://github.com/Tinder/Layout/blob/main/LICENSE for license information.
//

extension Collection where Element == Layout {

/// Activates all constraints of each instance
@preconcurrency
@MainActor
public func activate() {
forEach { $0.activate() }
}

/// Deactivates all constraints of each instance
@preconcurrency
@MainActor
public func deactivate() {
forEach { $0.deactivate() }
}
}
78 changes: 26 additions & 52 deletions Sources/Layout/Layout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -419,18 +419,26 @@ public final class Layout { // swiftlint:disable:this type_body_length
guard views.count >= 2,
let first = views.first
else { return self }
var anchor: NSLayoutAnchor<XAxisAttribute.AnchorType> = first.anchor(for: direction.attributes.1)
for view in views.dropFirst() {
adding(
view
.anchor(for: direction.attributes.0)
.constraint(equalTo: anchor, constant: spacing)
.withPriority(priority)
)
anchor = view.anchor(for: direction.attributes.1)
switch direction {
case .leadingToTrailing:
var anchor: NSLayoutXAxisAnchor = first.trailing
for view in views.dropFirst() {
adding(view.leading.constraint(equalTo: anchor, constant: spacing).withPriority(priority))
anchor = view.trailing
}
case .leftToRight:
var anchor: NSLayoutXAxisAnchor = first.right
for view in views.dropFirst() {
adding(view.left.constraint(equalTo: anchor, constant: spacing).withPriority(priority))
anchor = view.right
}
}
for attribute: YAxisAttribute in alignment {
adding(equalAttribute(attribute, views).withPriority(priority))
let firstAnchor: NSLayoutYAxisAnchor = first.anchor(for: attribute)
let constraints: [NSLayoutConstraint] = views
.dropFirst()
.map { $0.anchor(for: attribute).constraint(equalTo: firstAnchor) }
adding(constraints.withPriority(priority))
}
return self
}
Expand All @@ -451,18 +459,17 @@ public final class Layout { // swiftlint:disable:this type_body_length
guard views.count >= 2,
let first = views.first
else { return self }
var anchor: NSLayoutAnchor<YAxisAttribute.AnchorType> = first.anchor(for: YAxisAttribute.bottom)
var anchor: NSLayoutYAxisAnchor = first.bottom
for view in views.dropFirst() {
adding(
view
.anchor(for: YAxisAttribute.top)
.constraint(equalTo: anchor, constant: spacing)
.withPriority(priority)
)
anchor = view.anchor(for: YAxisAttribute.bottom)
adding(view.top.constraint(equalTo: anchor, constant: spacing).withPriority(priority))
anchor = view.bottom
}
for attribute: XAxisAttribute in alignment {
adding(equalAttribute(attribute, views).withPriority(priority))
let firstAnchor: NSLayoutXAxisAnchor = first.anchor(for: attribute)
let constraints: [NSLayoutConstraint] = views
.dropFirst()
.map { $0.anchor(for: attribute).constraint(equalTo: firstAnchor) }
adding(constraints.withPriority(priority))
}
return self
}
Expand Down Expand Up @@ -585,22 +592,6 @@ public final class Layout { // swiftlint:disable:this type_body_length
])
}

@discardableResult
private func equalAttribute<T: AnchorAttribute>(
_ attribute: T,
_ views: [UIView]
) -> [NSLayoutConstraint] {
guard views.count >= 2,
let first = views.first
else { return [] }
let firstAnchor: NSLayoutAnchor<T.AnchorType> = first.anchor(for: attribute)
return views.dropFirst().map { view in
view
.anchor(for: attribute)
.constraint(equalTo: firstAnchor)
}
}

/// Adds LayoutItems
///
/// - Note:
Expand Down Expand Up @@ -673,20 +664,3 @@ public final class Layout { // swiftlint:disable:this type_body_length
view?.updateConstraintsIfNeeded()
}
}

extension Collection where Element == Layout {

/// Activates all constraints of each instance
@preconcurrency
@MainActor
public func activate() {
forEach { $0.activate() }
}

/// Deactivates all constraints of each instance
@preconcurrency
@MainActor
public func deactivate() {
forEach { $0.deactivate() }
}
}
Loading

0 comments on commit ebd6ed9

Please sign in to comment.