Modernize syntax & improve performance

This commit is contained in:
Robert Payne 2015-04-11 17:41:05 +12:00
parent d04d78979e
commit 62c0318c00
2 changed files with 44 additions and 46 deletions

View File

@ -276,7 +276,7 @@ final public class Constraint {
// MARK: install / uninstall // MARK: install / uninstall
public func install() -> Array<LayoutConstraint> { public func install() -> [LayoutConstraint] {
return self.installOnView(updateExisting: false) return self.installOnView(updateExisting: false)
} }
@ -286,7 +286,7 @@ final public class Constraint {
// MARK: internal // MARK: internal
internal func installOnView(updateExisting: Bool = false) -> Array<LayoutConstraint> { internal func installOnView(updateExisting: Bool = false) -> [LayoutConstraint] {
var installOnView: View? = nil var installOnView: View? = nil
if self.toItem.view != nil { if self.toItem.view != nil {
installOnView = Constraint.closestCommonSuperviewFromView(self.fromItem.view, toView: self.toItem.view) installOnView = Constraint.closestCommonSuperviewFromView(self.fromItem.view, toView: self.toItem.view)
@ -308,15 +308,15 @@ final public class Constraint {
} }
} }
if self.installedOnView != nil { if let installedOnView = self.installInfo?.view {
if self.installedOnView != installOnView { if installedOnView != installOnView {
NSException(name: "Cannot Install Constraint", reason: "Already installed on different view.", userInfo: nil).raise() NSException(name: "Cannot Install Constraint", reason: "Already installed on different view.", userInfo: nil).raise()
return [] return []
} }
return self.installedLayoutConstraints?.allObjects as! Array<LayoutConstraint> return self.installInfo?.layoutConstraints.allObjects as? [LayoutConstraint] ?? []
} }
var newLayoutConstraints = Array<LayoutConstraint>() var newLayoutConstraints = [LayoutConstraint]()
let layoutFromAttributes = self.fromItem.attributes.layoutAttributes let layoutFromAttributes = self.fromItem.attributes.layoutAttributes
let layoutToAttributes = self.toItem.attributes.layoutAttributes let layoutToAttributes = self.toItem.attributes.layoutAttributes
@ -354,7 +354,7 @@ final public class Constraint {
layoutConstraint.priority = self.priority layoutConstraint.priority = self.priority
// set constraint // set constraint
layoutConstraint.constraint = self layoutConstraint.snp_constraint = self
newLayoutConstraints.append(layoutConstraint) newLayoutConstraints.append(layoutConstraint)
} }
@ -365,7 +365,7 @@ final public class Constraint {
let existingLayoutConstraints = reverse(layoutFrom!.snp_installedLayoutConstraints) let existingLayoutConstraints = reverse(layoutFrom!.snp_installedLayoutConstraints)
// array that will contain only new layout constraints to keep // array that will contain only new layout constraints to keep
var newLayoutConstraintsToKeep = Array<LayoutConstraint>() var newLayoutConstraintsToKeep = [LayoutConstraint]()
// begin looping // begin looping
for layoutConstraint in newLayoutConstraints { for layoutConstraint in newLayoutConstraints {
@ -397,50 +397,38 @@ final public class Constraint {
// add constraints // add constraints
installOnView!.addConstraints(newLayoutConstraints) installOnView!.addConstraints(newLayoutConstraints)
// store which view this constraint was installed on // set install info
self.installedOnView = installOnView self.installInfo = ConstraintInstallInfo(view: installOnView, layoutConstraints: NSHashTable.weakObjectsHashTable())
// store which layout constraints are installed for this constraint // store which layout constraints are installed for this constraint
self.installedLayoutConstraints = NSHashTable.weakObjectsHashTable()
for layoutConstraint in newLayoutConstraints { for layoutConstraint in newLayoutConstraints {
self.installedLayoutConstraints!.addObject(layoutConstraint) self.installInfo!.layoutConstraints.addObject(layoutConstraint)
} }
// store the layout constraints against the installed on view // store the layout constraints against the installed on view
var layoutConstraints = Array<LayoutConstraint>(layoutFrom!.snp_installedLayoutConstraints) layoutFrom!.snp_installedLayoutConstraints += newLayoutConstraints
layoutConstraints += newLayoutConstraints
layoutFrom!.snp_installedLayoutConstraints = layoutConstraints
// return the new constraints // return the new constraints
return newLayoutConstraints return newLayoutConstraints
} }
internal func uninstallFromView() { internal func uninstallFromView() {
if let view = self.installedOnView { if let installInfo = self.installInfo,
// remove all installed layout constraints let installedOnView = installInfo.view,
var layoutConstraintsToRemove = Array<LayoutConstraint>() let installedLayoutConstraints = installInfo.layoutConstraints.allObjects as? [LayoutConstraint] {
if let allObjects = self.installedLayoutConstraints?.allObjects {
if let installedLayoutConstraints = allObjects as? Array<LayoutConstraint> { if installedLayoutConstraints.count > 0 {
layoutConstraintsToRemove += installedLayoutConstraints // remove the constraints from the UIView's storage
installedOnView.removeConstraints(installedLayoutConstraints)
// remove the constraints from our associated object storage
installedOnView.snp_installedLayoutConstraints = installedOnView.snp_installedLayoutConstraints.filter {
return !contains(installedLayoutConstraints, $0)
} }
} }
if layoutConstraintsToRemove.count > 0 {
view.removeConstraints(layoutConstraintsToRemove)
}
// clean up the snp_installedLayoutConstraints
var layoutConstraints = view.snp_installedLayoutConstraints
var layoutConstraintsToKeep = Array<LayoutConstraint>()
for layoutConstraint in layoutConstraints {
if !contains(layoutConstraintsToRemove, layoutConstraint) {
layoutConstraintsToKeep.append(layoutConstraint)
}
}
view.snp_installedLayoutConstraints = layoutConstraintsToKeep
} }
self.installedOnView = nil self.installInfo = nil
self.installedLayoutConstraints = nil
} }
// MARK: private // MARK: private
@ -448,13 +436,15 @@ final public class Constraint {
private let fromItem: ConstraintItem private let fromItem: ConstraintItem
private var toItem: ConstraintItem private var toItem: ConstraintItem
private var relation: ConstraintRelation? private var relation: ConstraintRelation?
private var constant: Any? private var constant: Any = Float(0.0)
private var multiplier: Float = 1.0 private var multiplier: Float = 1.0
private var priority: Float = 1000.0 private var priority: Float = 1000.0
private var offset: Any? private var offset: Any = Float(0.0)
private var installedLayoutConstraints: NSHashTable? private var installInfo: ConstraintInstallInfo?
private weak var installedOnView: View? private var installed: Bool {
return (self.installInfo != nil)
}
private func addConstraint(attributes: ConstraintAttributes) -> Constraint { private func addConstraint(attributes: ConstraintAttributes) -> Constraint {
if self.relation == nil { if self.relation == nil {
@ -650,6 +640,14 @@ private extension NSLayoutAttribute {
} }
} }
private struct ConstraintInstallInfo {
weak var view: UIView? = nil
let layoutConstraints: NSHashTable
}
internal func ==(left: Constraint, right: Constraint) -> Bool { internal func ==(left: Constraint, right: Constraint) -> Bool {
return (left.fromItem == right.fromItem && return (left.fromItem == right.fromItem &&
left.toItem == right.toItem && left.toItem == right.toItem &&

View File

@ -52,7 +52,7 @@ final public class ConstraintMaker {
} }
internal weak var view: View? internal weak var view: View?
internal var constraints = Array<Constraint>() internal var constraints = [Constraint]()
internal func addConstraint(attributes: ConstraintAttributes) -> Constraint { internal func addConstraint(attributes: ConstraintAttributes) -> Constraint {
let item = ConstraintItem(object: self.view, attributes: attributes) let item = ConstraintItem(object: self.view, attributes: attributes)
@ -61,7 +61,7 @@ final public class ConstraintMaker {
return constraint return constraint
} }
internal class func prepareConstraints(view: View, @noescape block: (make: ConstraintMaker) -> Void) -> Array<Constraint> { internal class func prepareConstraints(view: View, @noescape block: (make: ConstraintMaker) -> Void) -> [Constraint] {
let maker = ConstraintMaker(view: view) let maker = ConstraintMaker(view: view)
block(make: maker) block(make: maker)
return maker.constraints return maker.constraints
@ -89,9 +89,9 @@ final public class ConstraintMaker {
let maker = ConstraintMaker(view: view) let maker = ConstraintMaker(view: view)
block(make: maker) block(make: maker)
var layoutConstraints = Array<LayoutConstraint>(view.snp_installedLayoutConstraints) var layoutConstraints = [LayoutConstraint](view.snp_installedLayoutConstraints)
for existingLayoutConstraint in layoutConstraints { for existingLayoutConstraint in layoutConstraints {
existingLayoutConstraint.constraint?.uninstallFromView() existingLayoutConstraint.snp_constraint?.uninstallFromView()
} }
for constraint in maker.constraints { for constraint in maker.constraints {
@ -114,9 +114,9 @@ final public class ConstraintMaker {
} }
internal class func removeConstraints(view: View) { internal class func removeConstraints(view: View) {
let existingLayoutConstraints = Array<LayoutConstraint>(view.snp_installedLayoutConstraints) let existingLayoutConstraints = [LayoutConstraint](view.snp_installedLayoutConstraints)
for existingLayoutConstraint in existingLayoutConstraints { for existingLayoutConstraint in existingLayoutConstraints {
existingLayoutConstraint.constraint?.uninstallFromView() existingLayoutConstraint.snp_constraint?.uninstallFromView()
} }
} }
} }