Priority enum (#345)

* Adds ConstraintPriority enum to ConstraintMakerPrioritizable

* Constraints priority is now more robust

* Adds priority enum function

* Fixes compile error

* Adds failable initializer. Custom macOS medium priority. Adds back deleted methods

* Updates depracation messages
This commit is contained in:
Shehryar 2017-02-11 04:37:51 -05:00 committed by Robert Payne
parent c206e9b944
commit a7053adc4a
2 changed files with 66 additions and 20 deletions

View File

@ -242,13 +242,7 @@ public final class Constraint {
let attribute = (layoutConstraint.secondAttribute == .notAnAttribute) ? layoutConstraint.firstAttribute : layoutConstraint.secondAttribute
layoutConstraint.constant = self.constant.constraintConstantTargetValueFor(layoutAttribute: attribute)
#if os(iOS) || os(tvOS)
let requiredPriority: UILayoutPriority = UILayoutPriorityRequired
#else
let requiredPriority: Float = 1000.0
#endif
let requiredPriority = ConstraintMakerPriortizable.Priority.required.rawValue
if (layoutConstraint.priority < requiredPriority), (self.priority.constraintPriorityTargetValue != requiredPriority) {
layoutConstraint.priority = self.priority.constraintPriorityTargetValue
}

View File

@ -30,38 +30,90 @@
public class ConstraintMakerPriortizable: ConstraintMakerFinalizable {
public enum Priority: Strideable, RawRepresentable, Equatable {
case low
case medium
case high
case required
case custom(value: Float)
public init?(rawValue: Float) {
if rawValue < 0 || rawValue > 1000 {
return nil
}
self = .custom(value: rawValue)
}
private var macOSMedium: Float {
#if os(OSX)
return 501.0
#else
return 500.0
#endif
}
public var rawValue: Float {
switch self {
case .low:
return 250.0
case .medium:
return macOSMedium
case .high:
return 750.0
case .required:
return 1000.0
case let .custom(value):
return value
}
}
public func advanced(by n: Float) -> Priority {
return .custom(value: self.rawValue + n)
}
public func distance(to other: Priority) -> Float {
return other.rawValue - self.rawValue
}
public static func ==(lhs: Priority, rhs: Priority) -> Bool {
return lhs.rawValue == rhs.rawValue
}
}
@discardableResult
@available(*, deprecated:3.0, message:"Use priority(level: .custom(Float)) instead.")
public func priority(_ amount: ConstraintPriorityTarget) -> ConstraintMakerFinalizable {
self.description.priority = amount
return self
}
@available(*, deprecated:3.0, message:"Use priority(_ amount: ConstraintPriorityTarget) instead.")
@discardableResult
public func priority(level: Priority) -> ConstraintMakerFinalizable {
return self.priority(level: ConstraintMakerPriortizable.Priority(rawValue: level.rawValue)!)
}
@available(*, deprecated:3.0, message:"Use priority(level: Priority) instead.")
@discardableResult
public func priorityRequired() -> ConstraintMakerFinalizable {
return self.priority(1000)
return self.priority(level: .required)
}
@available(*, deprecated:3.0, message:"Use priority(_ amount: ConstraintPriorityTarget) instead.")
@available(*, deprecated:3.0, message:"Use priority(level: Priority) instead.")
@discardableResult
public func priorityHigh() -> ConstraintMakerFinalizable {
return self.priority(750)
return self.priority(level: .high)
}
@available(*, deprecated:3.0, message:"Use priority(_ amount: ConstraintPriorityTarget) instead.")
@available(*, deprecated:3.0, message:"Use priority(level: Priority) instead.")
@discardableResult
public func priorityMedium() -> ConstraintMakerFinalizable {
#if os(iOS) || os(tvOS)
return self.priority(500)
#else
return self.priority(501)
#endif
return self.priority(level: .medium)
}
@available(*, deprecated:3.0, message:"Use priority(_ amount: ConstraintPriorityTarget) instead.")
@available(*, deprecated:3.0, message:"Use priority(level: Priority) instead.")
@discardableResult
public func priorityLow() -> ConstraintMakerFinalizable {
return self.priority(250)
return self.priority(level: .low)
}
}