From a7053adc4a57feeb640d402bbde7b9ca88bd5da1 Mon Sep 17 00:00:00 2001 From: Shehryar Date: Sat, 11 Feb 2017 04:37:51 -0500 Subject: [PATCH] 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 --- Source/Constraint.swift | 8 +-- Source/ConstraintMakerPriortizable.swift | 78 ++++++++++++++++++++---- 2 files changed, 66 insertions(+), 20 deletions(-) diff --git a/Source/Constraint.swift b/Source/Constraint.swift index a21343b..e58fb86 100644 --- a/Source/Constraint.swift +++ b/Source/Constraint.swift @@ -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 } diff --git a/Source/ConstraintMakerPriortizable.swift b/Source/ConstraintMakerPriortizable.swift index 82a6de6..e5bd9c2 100644 --- a/Source/ConstraintMakerPriortizable.swift +++ b/Source/ConstraintMakerPriortizable.swift @@ -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) } - }