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'
This commit is contained in:
Colin T.A. Gray 2018-06-23 05:15:34 -06:00 committed by Robert Payne
parent 163f5369d3
commit 15beb52218
3 changed files with 51 additions and 22 deletions

View File

@ -49,12 +49,23 @@ public final class Constraint {
public var layoutConstraints: [LayoutConstraint] public var layoutConstraints: [LayoutConstraint]
public var isActive: Bool { public var isActive: Bool {
for layoutConstraint in self.layoutConstraints { set {
if layoutConstraint.isActive { if newValue {
return true activate()
}
else {
deactivate()
} }
} }
return false
get {
for layoutConstraint in self.layoutConstraints {
if layoutConstraint.isActive {
return true
}
}
return false
}
} }
// MARK: Initialization // MARK: Initialization

View File

@ -171,15 +171,7 @@ public class ConstraintMaker {
} }
internal static func makeConstraints(item: LayoutConstraintItem, closure: (_ make: ConstraintMaker) -> Void) { internal static func makeConstraints(item: LayoutConstraintItem, closure: (_ make: ConstraintMaker) -> Void) {
let maker = ConstraintMaker(item: item) let constraints = prepareConstraints(item: item, closure: closure)
closure(maker)
var constraints: [Constraint] = []
for description in maker.descriptions {
guard let constraint = description.constraint else {
continue
}
constraints.append(constraint)
}
for constraint in constraints { for constraint in constraints {
constraint.activateIfNeeded(updatingExisting: false) constraint.activateIfNeeded(updatingExisting: false)
} }
@ -196,15 +188,7 @@ public class ConstraintMaker {
return return
} }
let maker = ConstraintMaker(item: item) let constraints = prepareConstraints(item: item, closure: closure)
closure(maker)
var constraints: [Constraint] = []
for description in maker.descriptions {
guard let constraint = description.constraint else {
continue
}
constraints.append(constraint)
}
for constraint in constraints { for constraint in constraints {
constraint.activateIfNeeded(updatingExisting: true) constraint.activateIfNeeded(updatingExisting: true)
} }

View File

@ -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() { func testEdgeConstraints() {
let view = View() let view = View()
self.container.addSubview(view) self.container.addSubview(view)