From 15beb5221834e220f099a87beb80cc9d9abb16cf Mon Sep 17 00:00:00 2001 From: "Colin T.A. Gray" Date: Sat, 23 Jun 2018 05:15:34 -0600 Subject: [PATCH] Adds 'constraint.isActive = bool' to Constraint (#496) * delegate identical 'constraint creation' code to 'prepareConstraints' * adds 'set(isActivated:)' method to call appropriate 'activate/deactivate' based on bool cleans up about 30 lines of if foo { activate } else { deactivate } code in my app * instead of 'set(isActivated:)', use 'isActive = value' --- Source/Constraint.swift | 19 +++++++++++++++---- Source/ConstraintMaker.swift | 20 ++------------------ Tests/SnapKitTests/Tests.swift | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 22 deletions(-) diff --git a/Source/Constraint.swift b/Source/Constraint.swift index 0509f61..6e4948e 100644 --- a/Source/Constraint.swift +++ b/Source/Constraint.swift @@ -49,12 +49,23 @@ public final class Constraint { public var layoutConstraints: [LayoutConstraint] public var isActive: Bool { - for layoutConstraint in self.layoutConstraints { - if layoutConstraint.isActive { - return true + set { + if newValue { + activate() + } + else { + deactivate() } } - return false + + get { + for layoutConstraint in self.layoutConstraints { + if layoutConstraint.isActive { + return true + } + } + return false + } } // MARK: Initialization diff --git a/Source/ConstraintMaker.swift b/Source/ConstraintMaker.swift index b20cdb5..91f35f1 100644 --- a/Source/ConstraintMaker.swift +++ b/Source/ConstraintMaker.swift @@ -171,15 +171,7 @@ public class ConstraintMaker { } internal static func makeConstraints(item: LayoutConstraintItem, closure: (_ make: ConstraintMaker) -> Void) { - let maker = ConstraintMaker(item: item) - closure(maker) - var constraints: [Constraint] = [] - for description in maker.descriptions { - guard let constraint = description.constraint else { - continue - } - constraints.append(constraint) - } + let constraints = prepareConstraints(item: item, closure: closure) for constraint in constraints { constraint.activateIfNeeded(updatingExisting: false) } @@ -196,15 +188,7 @@ public class ConstraintMaker { return } - let maker = ConstraintMaker(item: item) - closure(maker) - var constraints: [Constraint] = [] - for description in maker.descriptions { - guard let constraint = description.constraint else { - continue - } - constraints.append(constraint) - } + let constraints = prepareConstraints(item: item, closure: closure) for constraint in constraints { constraint.activateIfNeeded(updatingExisting: true) } diff --git a/Tests/SnapKitTests/Tests.swift b/Tests/SnapKitTests/Tests.swift index db414ff..dd4a309 100644 --- a/Tests/SnapKitTests/Tests.swift +++ b/Tests/SnapKitTests/Tests.swift @@ -264,6 +264,40 @@ class SnapKitTests: XCTestCase { } + func testSetIsActivatedConstraints() { + let v1 = View() + let v2 = View() + self.container.addSubview(v1) + self.container.addSubview(v2) + + var c1: Constraint? = nil + var c2: Constraint? = nil + + v1.snp.prepareConstraints { (make) -> Void in + c1 = make.top.equalTo(v2.snp.top).offset(50).constraint + c2 = make.left.equalTo(v2.snp.top).offset(50).constraint + return + } + + XCTAssertEqual(self.container.snp_constraints.count, 0, "Should have 0 constraints") + + c1?.isActive = true + c2?.isActive = false + + XCTAssertEqual(self.container.snp_constraints.count, 1, "Should have 1 constraint") + + c1?.isActive = true + c2?.isActive = true + + XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints") + + c1?.isActive = false + c2?.isActive = false + + XCTAssertEqual(self.container.snp_constraints.count, 0, "Should have 0 constraints") + + } + func testEdgeConstraints() { let view = View() self.container.addSubview(view)