make 'layoutConstraints' public again (#382)

This commit is contained in:
Colin T.A. Gray 2016-12-26 04:08:42 -07:00 committed by Robert Payne
parent 9a82379f93
commit ab6c1fd91f
1 changed files with 42 additions and 42 deletions

View File

@ -28,10 +28,10 @@
#endif #endif
public final class Constraint { public final class Constraint {
internal let sourceLocation: (String, UInt) internal let sourceLocation: (String, UInt)
internal let label: String? internal let label: String?
private let from: ConstraintItem private let from: ConstraintItem
private let to: ConstraintItem private let to: ConstraintItem
private let relation: ConstraintRelation private let relation: ConstraintRelation
@ -46,10 +46,10 @@ public final class Constraint {
self.updateConstantAndPriorityIfNeeded() self.updateConstantAndPriorityIfNeeded()
} }
} }
private var layoutConstraints: [LayoutConstraint] public var layoutConstraints: [LayoutConstraint]
// MARK: Initialization // MARK: Initialization
internal init(from: ConstraintItem, internal init(from: ConstraintItem,
to: ConstraintItem, to: ConstraintItem,
relation: ConstraintRelation, relation: ConstraintRelation,
@ -67,17 +67,17 @@ public final class Constraint {
self.constant = constant self.constant = constant
self.priority = priority self.priority = priority
self.layoutConstraints = [] self.layoutConstraints = []
// get attributes // get attributes
let layoutFromAttributes = self.from.attributes.layoutAttributes let layoutFromAttributes = self.from.attributes.layoutAttributes
let layoutToAttributes = self.to.attributes.layoutAttributes let layoutToAttributes = self.to.attributes.layoutAttributes
// get layout from // get layout from
let layoutFrom = self.from.layoutConstraintItem! let layoutFrom = self.from.layoutConstraintItem!
// get relation // get relation
let layoutRelation = self.relation.layoutRelation let layoutRelation = self.relation.layoutRelation
for layoutFromAttribute in layoutFromAttributes { for layoutFromAttribute in layoutFromAttributes {
// get layout to attribute // get layout to attribute
let layoutToAttribute: NSLayoutAttribute let layoutToAttribute: NSLayoutAttribute
@ -130,18 +130,18 @@ public final class Constraint {
layoutToAttribute = layoutFromAttribute layoutToAttribute = layoutFromAttribute
} }
#endif #endif
// get layout constant // get layout constant
let layoutConstant: CGFloat = self.constant.constraintConstantTargetValueFor(layoutAttribute: layoutToAttribute) let layoutConstant: CGFloat = self.constant.constraintConstantTargetValueFor(layoutAttribute: layoutToAttribute)
// get layout to // get layout to
var layoutTo: AnyObject? = self.to.target var layoutTo: AnyObject? = self.to.target
// use superview if possible // use superview if possible
if layoutTo == nil && layoutToAttribute != .width && layoutToAttribute != .height { if layoutTo == nil && layoutToAttribute != .width && layoutToAttribute != .height {
layoutTo = layoutFrom.superview layoutTo = layoutFrom.superview
} }
// create layout constraint // create layout constraint
let layoutConstraint = LayoutConstraint( let layoutConstraint = LayoutConstraint(
item: layoutFrom, item: layoutFrom,
@ -152,119 +152,119 @@ public final class Constraint {
multiplier: self.multiplier.constraintMultiplierTargetValue, multiplier: self.multiplier.constraintMultiplierTargetValue,
constant: layoutConstant constant: layoutConstant
) )
// set label // set label
layoutConstraint.label = self.label layoutConstraint.label = self.label
// set priority // set priority
layoutConstraint.priority = self.priority.constraintPriorityTargetValue layoutConstraint.priority = self.priority.constraintPriorityTargetValue
// set constraint // set constraint
layoutConstraint.constraint = self layoutConstraint.constraint = self
// append // append
self.layoutConstraints.append(layoutConstraint) self.layoutConstraints.append(layoutConstraint)
} }
} }
// MARK: Public // MARK: Public
@available(*, deprecated:3.0, message:"Use activate().") @available(*, deprecated:3.0, message:"Use activate().")
public func install() { public func install() {
self.activate() self.activate()
} }
@available(*, deprecated:3.0, message:"Use deactivate().") @available(*, deprecated:3.0, message:"Use deactivate().")
public func uninstall() { public func uninstall() {
self.deactivate() self.deactivate()
} }
public func activate() { public func activate() {
self.activateIfNeeded() self.activateIfNeeded()
} }
public func deactivate() { public func deactivate() {
self.deactivateIfNeeded() self.deactivateIfNeeded()
} }
@discardableResult @discardableResult
public func update(offset: ConstraintOffsetTarget) -> Constraint { public func update(offset: ConstraintOffsetTarget) -> Constraint {
self.constant = offset.constraintOffsetTargetValue self.constant = offset.constraintOffsetTargetValue
return self return self
} }
@discardableResult @discardableResult
public func update(inset: ConstraintInsetTarget) -> Constraint { public func update(inset: ConstraintInsetTarget) -> Constraint {
self.constant = inset.constraintInsetTargetValue self.constant = inset.constraintInsetTargetValue
return self return self
} }
@discardableResult @discardableResult
public func update(priority: ConstraintPriorityTarget) -> Constraint { public func update(priority: ConstraintPriorityTarget) -> Constraint {
self.priority = priority.constraintPriorityTargetValue self.priority = priority.constraintPriorityTargetValue
return self return self
} }
@available(*, deprecated:3.0, message:"Use update(offset: ConstraintOffsetTarget) instead.") @available(*, deprecated:3.0, message:"Use update(offset: ConstraintOffsetTarget) instead.")
public func updateOffset(amount: ConstraintOffsetTarget) -> Void { self.update(offset: amount) } public func updateOffset(amount: ConstraintOffsetTarget) -> Void { self.update(offset: amount) }
@available(*, deprecated:3.0, message:"Use update(inset: ConstraintInsetTarget) instead.") @available(*, deprecated:3.0, message:"Use update(inset: ConstraintInsetTarget) instead.")
public func updateInsets(amount: ConstraintInsetTarget) -> Void { self.update(inset: amount) } public func updateInsets(amount: ConstraintInsetTarget) -> Void { self.update(inset: amount) }
@available(*, deprecated:3.0, message:"Use update(priority: ConstraintPriorityTarget) instead.") @available(*, deprecated:3.0, message:"Use update(priority: ConstraintPriorityTarget) instead.")
public func updatePriority(amount: ConstraintPriorityTarget) -> Void { self.update(priority: amount) } public func updatePriority(amount: ConstraintPriorityTarget) -> Void { self.update(priority: amount) }
@available(*, obsoleted:3.0, message:"Use update(priority: ConstraintPriorityTarget) instead.") @available(*, obsoleted:3.0, message:"Use update(priority: ConstraintPriorityTarget) instead.")
public func updatePriorityRequired() -> Void {} public func updatePriorityRequired() -> Void {}
@available(*, obsoleted:3.0, message:"Use update(priority: ConstraintPriorityTarget) instead.") @available(*, obsoleted:3.0, message:"Use update(priority: ConstraintPriorityTarget) instead.")
public func updatePriorityHigh() -> Void { fatalError("Must be implemented by Concrete subclass.") } public func updatePriorityHigh() -> Void { fatalError("Must be implemented by Concrete subclass.") }
@available(*, obsoleted:3.0, message:"Use update(priority: ConstraintPriorityTarget) instead.") @available(*, obsoleted:3.0, message:"Use update(priority: ConstraintPriorityTarget) instead.")
public func updatePriorityMedium() -> Void { fatalError("Must be implemented by Concrete subclass.") } public func updatePriorityMedium() -> Void { fatalError("Must be implemented by Concrete subclass.") }
@available(*, obsoleted:3.0, message:"Use update(priority: ConstraintPriorityTarget) instead.") @available(*, obsoleted:3.0, message:"Use update(priority: ConstraintPriorityTarget) instead.")
public func updatePriorityLow() -> Void { fatalError("Must be implemented by Concrete subclass.") } public func updatePriorityLow() -> Void { fatalError("Must be implemented by Concrete subclass.") }
// MARK: Internal // MARK: Internal
internal func updateConstantAndPriorityIfNeeded() { internal func updateConstantAndPriorityIfNeeded() {
for layoutConstraint in self.layoutConstraints { for layoutConstraint in self.layoutConstraints {
let attribute = (layoutConstraint.secondAttribute == .notAnAttribute) ? layoutConstraint.firstAttribute : layoutConstraint.secondAttribute let attribute = (layoutConstraint.secondAttribute == .notAnAttribute) ? layoutConstraint.firstAttribute : layoutConstraint.secondAttribute
layoutConstraint.constant = self.constant.constraintConstantTargetValueFor(layoutAttribute: attribute) layoutConstraint.constant = self.constant.constraintConstantTargetValueFor(layoutAttribute: attribute)
#if os(iOS) || os(tvOS) #if os(iOS) || os(tvOS)
let requiredPriority: UILayoutPriority = UILayoutPriorityRequired let requiredPriority: UILayoutPriority = UILayoutPriorityRequired
#else #else
let requiredPriority: Float = 1000.0 let requiredPriority: Float = 1000.0
#endif #endif
if (layoutConstraint.priority < requiredPriority), (self.priority.constraintPriorityTargetValue != requiredPriority) { if (layoutConstraint.priority < requiredPriority), (self.priority.constraintPriorityTargetValue != requiredPriority) {
layoutConstraint.priority = self.priority.constraintPriorityTargetValue layoutConstraint.priority = self.priority.constraintPriorityTargetValue
} }
} }
} }
internal func activateIfNeeded(updatingExisting: Bool = false) { internal func activateIfNeeded(updatingExisting: Bool = false) {
guard let item = self.from.layoutConstraintItem else { guard let item = self.from.layoutConstraintItem else {
print("WARNING: SnapKit failed to get from item from constraint. Activate will be a no-op.") print("WARNING: SnapKit failed to get from item from constraint. Activate will be a no-op.")
return return
} }
let layoutConstraints = self.layoutConstraints let layoutConstraints = self.layoutConstraints
if updatingExisting { if updatingExisting {
var existingLayoutConstraints: [LayoutConstraint] = [] var existingLayoutConstraints: [LayoutConstraint] = []
for constraint in item.constraints { for constraint in item.constraints {
existingLayoutConstraints += constraint.layoutConstraints existingLayoutConstraints += constraint.layoutConstraints
} }
for layoutConstraint in layoutConstraints { for layoutConstraint in layoutConstraints {
let existingLayoutConstraint = existingLayoutConstraints.first { $0 == layoutConstraint } let existingLayoutConstraint = existingLayoutConstraints.first { $0 == layoutConstraint }
guard let updateLayoutConstraint = existingLayoutConstraint else { guard let updateLayoutConstraint = existingLayoutConstraint else {
fatalError("Updated constraint could not find existing matching constraint to update: \(layoutConstraint)") fatalError("Updated constraint could not find existing matching constraint to update: \(layoutConstraint)")
} }
let updateLayoutAttribute = (updateLayoutConstraint.secondAttribute == .notAnAttribute) ? updateLayoutConstraint.firstAttribute : updateLayoutConstraint.secondAttribute let updateLayoutAttribute = (updateLayoutConstraint.secondAttribute == .notAnAttribute) ? updateLayoutConstraint.firstAttribute : updateLayoutConstraint.secondAttribute
updateLayoutConstraint.constant = self.constant.constraintConstantTargetValueFor(layoutAttribute: updateLayoutAttribute) updateLayoutConstraint.constant = self.constant.constraintConstantTargetValueFor(layoutAttribute: updateLayoutAttribute)
} }
@ -273,7 +273,7 @@ public final class Constraint {
item.add(constraints: [self]) item.add(constraints: [self])
} }
} }
internal func deactivateIfNeeded() { internal func deactivateIfNeeded() {
guard let item = self.from.layoutConstraintItem else { guard let item = self.from.layoutConstraintItem else {
print("WARNING: SnapKit failed to get from item from constraint. Deactivate will be a no-op.") print("WARNING: SnapKit failed to get from item from constraint. Deactivate will be a no-op.")