Skip to content

Commit

Permalink
CherryPick PR #222, #223, #224
Browse files Browse the repository at this point in the history
  • Loading branch information
Nuno Barreto committed Oct 30, 2020
1 parent 538f770 commit 6505187
Show file tree
Hide file tree
Showing 10 changed files with 519 additions and 12 deletions.
4 changes: 4 additions & 0 deletions Alicerce.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@
1B7166BC216BE424008A8A46 /* Localizable.strings in Resources */ = {isa = PBXBuildFile; fileRef = 1B7166B9216BE402008A8A46 /* Localizable.strings */; };
1BBEB6091F333E5600D06526 /* UIImage.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1BBEB6081F333E5600D06526 /* UIImage.swift */; };
1BBEB60C1F333E6E00D06526 /* UIImageTestCase.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1BBEB60A1F333E6600D06526 /* UIImageTestCase.swift */; };
3E8D61952546F90400C08EA2 /* ConstraintGroupToggleTestCase.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3E8D61932546F8AF00C08EA2 /* ConstraintGroupToggleTestCase.swift */; };
4833D5B423D0D2CE00EBD925 /* WidthConstrainableProxyTestCase.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4833D5B223D0D28C00EBD925 /* WidthConstrainableProxyTestCase.swift */; };
4838FE3023A94CE0007311F0 /* ConstrainableProxy.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4838FE2823A94CE0007311F0 /* ConstrainableProxy.swift */; };
4838FE3123A94CE0007311F0 /* Array+ConstrainableProxy.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4838FE2923A94CE0007311F0 /* Array+ConstrainableProxy.swift */; };
Expand Down Expand Up @@ -655,6 +656,7 @@
1B7166CD216C231E008A8A46 /* StringTestCase.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StringTestCase.swift; sourceTree = "<group>"; };
1BBEB6081F333E5600D06526 /* UIImage.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UIImage.swift; sourceTree = "<group>"; };
1BBEB60A1F333E6600D06526 /* UIImageTestCase.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UIImageTestCase.swift; sourceTree = "<group>"; };
3E8D61932546F8AF00C08EA2 /* ConstraintGroupToggleTestCase.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConstraintGroupToggleTestCase.swift; sourceTree = "<group>"; };
4833D5B223D0D28C00EBD925 /* WidthConstrainableProxyTestCase.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WidthConstrainableProxyTestCase.swift; sourceTree = "<group>"; };
4838FE2823A94CE0007311F0 /* ConstrainableProxy.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ConstrainableProxy.swift; sourceTree = "<group>"; };
4838FE2923A94CE0007311F0 /* Array+ConstrainableProxy.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Array+ConstrainableProxy.swift"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1530,6 +1532,7 @@
4833D5B223D0D28C00EBD925 /* WidthConstrainableProxyTestCase.swift */,
4838FE3C23A950CA007311F0 /* XCTAssertConstraint.swift */,
48A5ECCF23D5B9F70014B2B7 /* TopBottomConstrainableProxyTestCase.swift */,
3E8D61932546F8AF00C08EA2 /* ConstraintGroupToggleTestCase.swift */,
);
path = AutoLayout;
sourceTree = "<group>";
Expand Down Expand Up @@ -1862,6 +1865,7 @@
0A266FBF1ED59FCD009CD0D7 /* EmptyCoreDataStackModel.xcdatamodeld in Sources */,
4838FE5723A951E6007311F0 /* TopConstrainableProxyTestCase.swift in Sources */,
0A708F6E20E99D9F001784DA /* MockAnalyticsTracker.swift in Sources */,
3E8D61952546F90400C08EA2 /* ConstraintGroupToggleTestCase.swift in Sources */,
0A266F8C1ED59FB6009CD0D7 /* MultiTrackerTestCase.swift in Sources */,
0A85F0E720B3177E0095AFFB /* PublicKeyAlgorithmTestCase.swift in Sources */,
0A266F901ED59FB6009CD0D7 /* Route+ComponentTests.swift in Sources */,
Expand Down
37 changes: 36 additions & 1 deletion Sources/AutoLayout/Constrain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,15 @@ public final class LayoutContext {
}
}

public final class ConstraintGroup {
public class ConstraintGroup {

public init() { }

internal init(constraints: [NSLayoutConstraint] = []) {

self.constraints = constraints
}

fileprivate var constraints: [NSLayoutConstraint] = [] {
willSet {
if isActive { deactivate() }
Expand All @@ -41,6 +46,36 @@ public final class ConstraintGroup {
}
}

public final class ConstraintGroupToggle<T: Hashable> {

private var constraintGroups: [T: ConstraintGroup] = [:]

public init(initial: T? = nil, constraintGroups: [T: ConstraintGroup]) {

self.constraintGroups = constraintGroups

if let initial = initial {
activate(initial)
} else {
deactivate()
}
}

public func activate(_ key: T) {

constraintGroups.lazy.filter { $0 != key && $1.isActive }.forEach { $1.isActive = false }

if let constraintGroup = constraintGroups[key] {
constraintGroup.isActive = true
}
}

public func deactivate() {

constraintGroups.forEach { $1.isActive = false }
}
}

@discardableResult
public func constrain<A: LayoutItem>(
_ a: A,
Expand Down
102 changes: 102 additions & 0 deletions Sources/AutoLayout/ConstrainableProxy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,40 @@ public extension TopConstrainableProxy {
priority: priority
)
}

@discardableResult
func topToFirstBaseline(
of anotherProxy: BaselineConstrainableProxy,
offset: CGFloat = 0,
relation: ConstraintRelation = .equal,
priority: UILayoutPriority = .required
) -> NSLayoutConstraint {

constrain(
from: top,
to: anotherProxy.firstBaseline,
offset: offset,
relation: relation,
priority: priority
)
}

@discardableResult
func topToLastBaseline(
of anotherProxy: BaselineConstrainableProxy,
offset: CGFloat = 0,
relation: ConstraintRelation = .equal,
priority: UILayoutPriority = .required
) -> NSLayoutConstraint {

constrain(
from: top,
to: anotherProxy.lastBaseline,
offset: offset,
relation: relation,
priority: priority
)
}
}

public protocol BottomConstrainableProxy: ConstrainableProxy {
Expand Down Expand Up @@ -92,6 +126,40 @@ public extension BottomConstrainableProxy {
priority: priority
)
}

@discardableResult
func bottomToFirstBaseline(
of anotherProxy: BaselineConstrainableProxy,
offset: CGFloat = 0,
relation: ConstraintRelation = .equal,
priority: UILayoutPriority = .required
) -> NSLayoutConstraint {

constrain(
from: bottom,
to: anotherProxy.firstBaseline,
offset: offset,
relation: relation,
priority: priority
)
}

@discardableResult
func bottomToLastBaseline(
of anotherProxy: BaselineConstrainableProxy,
offset: CGFloat = 0,
relation: ConstraintRelation = .equal,
priority: UILayoutPriority = .required
) -> NSLayoutConstraint {

constrain(
from: bottom,
to: anotherProxy.lastBaseline,
offset: offset,
relation: relation,
priority: priority
)
}
}

public protocol LeadingConstrainableProxy: ConstrainableProxy {
Expand Down Expand Up @@ -389,6 +457,40 @@ public extension CenterYConstrainableProxy {
priority: priority
)
}

@discardableResult
func centerYToFirstBaseline(
of anotherProxy: BaselineConstrainableProxy,
offset: CGFloat = 0,
relation: ConstraintRelation = .equal,
priority: UILayoutPriority = .required
) -> NSLayoutConstraint {

constrain(
from: centerY,
to: anotherProxy.firstBaseline,
offset: offset,
relation: relation,
priority: priority
)
}

@discardableResult
func centerYToLastBaseline(
of anotherProxy: BaselineConstrainableProxy,
offset: CGFloat = 0,
relation: ConstraintRelation = .equal,
priority: UILayoutPriority = .required
) -> NSLayoutConstraint {

constrain(
from: centerY,
to: anotherProxy.lastBaseline,
offset: offset,
relation: relation,
priority: priority
)
}
}

public protocol CenterXConstrainableProxy: ConstrainableProxy {
Expand Down
10 changes: 6 additions & 4 deletions Sources/DeepLinking/Route+TrieRouter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,9 @@ extension Route {
private func parseAnnotatedRoute(_ route: URL) throws -> [Route.Component] {

// use a wildcard for empty schemes/hosts, to match any scheme/host
let schemeComponent = route.scheme.constantOrWildcardComponent
let hostComponent = route.host.constantOrWildcardComponent
// URL scheme and host comparison should be case insensitive in conformance to RFC-3986
let schemeComponent = (route.scheme?.lowercased()).constantOrWildcardComponent
let hostComponent = (route.host?.lowercased()).constantOrWildcardComponent

do {
let pathComponents = try route.pathComponents.filter { $0 != "/" }.map(Route.Component.init(component:))
Expand All @@ -204,8 +205,9 @@ extension Route {
private func parseMatchRoute(_ route: URL) throws -> MatchRoute {

// use an empty string for empty scheme/host, to match wildcard scheme/host
let schemeComponent = route.scheme ?? ""
let hostComponent = route.host ?? ""
// URL scheme and host comparison should be case insensitive in conformance to RFC-3986
let schemeComponent = route.scheme?.lowercased() ?? ""
let hostComponent = route.host?.lowercased() ?? ""
let pathComponents = route.pathComponents.filter { $0 != "/" }

let routeComponents = [schemeComponent, hostComponent] + pathComponents
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,4 +317,48 @@ final class BottomConstrainableProxyTestCase: BaseConstrainableProxyTestCase {
XCTAssert(constraintGroup1.isActive)
XCTAssertEqual(view0.frame.maxY, 400)
}

func testConstrain_WithBottomConstraint_ShouldSupportFirstBaselineAttribute() {

var constraint: NSLayoutConstraint!
constrain(host, view0) { host, view0 in
constraint = view0.bottomToFirstBaseline(of: host)
}

let expected = NSLayoutConstraint(
item: view0!,
attribute: .bottom,
relatedBy: .equal,
toItem: host,
attribute: .firstBaseline,
multiplier: 1,
constant: 0,
priority: .required,
active: true
)

XCTAssertConstraint(constraint, expected)
}

func testConstrain_WithBottomConstraint_ShouldSupportLastBaselineAttribute() {

var constraint: NSLayoutConstraint!
constrain(host, view0) { host, view0 in
constraint = view0.bottomToLastBaseline(of: host)
}

let expected = NSLayoutConstraint(
item: view0!,
attribute: .bottom,
relatedBy: .equal,
toItem: host,
attribute: .lastBaseline,
multiplier: 1,
constant: 0,
priority: .required,
active: true
)

XCTAssertConstraint(constraint, expected)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,50 @@ class CenterYConstrainableProxyTestCase: BaseConstrainableProxyTestCase {
XCTAssert(constraintGroup1.isActive)
XCTAssertEqual(view0.center.y, host.center.y + 100)
}

func testConstrain_WithCenterY_ShouldSupportFirstBaselineAttribute() {

var constraint: NSLayoutConstraint!
constrain(host, view0) { host, view0 in
constraint = view0.centerYToFirstBaseline(of: host)
}

let expected = NSLayoutConstraint(
item: view0!,
attribute: .centerY,
relatedBy: .equal,
toItem: host,
attribute: .firstBaseline,
multiplier: 1,
constant: 0,
priority: .required,
active: true
)

XCTAssertConstraint(constraint, expected)
}

func testConstrain_WithCenterYConstraint_ShouldSupportLastBaselineAttribute() {

var constraint: NSLayoutConstraint!
constrain(host, view0) { host, view0 in
constraint = view0.centerYToLastBaseline(of: host)
}

let expected = NSLayoutConstraint(
item: view0!,
attribute: .centerY,
relatedBy: .equal,
toItem: host,
attribute: .lastBaseline,
multiplier: 1,
constant: 0,
priority: .required,
active: true
)

XCTAssertConstraint(constraint, expected)
}
}

private extension CenterYConstrainableProxyTestCase {
Expand Down
Loading

0 comments on commit 6505187

Please sign in to comment.