Skip to content

Commit

Permalink
Merge pull request #29 from nakiostudio/issue-28-extend-multiplier-usage
Browse files Browse the repository at this point in the history
ISSUE-28 Extend multipliers usage
  • Loading branch information
nakiostudio committed Aug 12, 2016
2 parents 5c85854 + 2670c68 commit 4abf8c0
Show file tree
Hide file tree
Showing 16 changed files with 312 additions and 342 deletions.
2 changes: 1 addition & 1 deletion .fastlane/Fastfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ platform :ios do
scan(
scheme: scheme,
workspace: workspace,
devices: ["iPhone 6s"],
device: "iPhone 6s",
code_coverage: true
)

Expand Down
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ branches:
- develop
before_install:
- bundle install
- instruments -s
script:
- set -o pipefail
- fastlane travis
Expand Down
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# Changelog

## v.1.1.0

* Now it's possible to combine `multipliers` with `Equal`, `GreaterThatOrEqual`
and `LessThanOrEqual` relations.

```swift
// i.e.
Width(>=20*0.5) // value = 20, multiplier = 0.5, relation = .GreaterThanOrEqual
Width(<=20*0.5) // value = 20, multiplier = 0.5, relation = .LessThanOrEqual
Width(==20*0.5) // value = 20, multiplier = 0.5, relation = .Equal
```

# v.1.0.0

* Improved performance, benchmarks show up to a 250% improvement applying `Attributes` with the apply operator `<-`, resolving
Expand Down
2 changes: 1 addition & 1 deletion EasyPeasy.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "EasyPeasy"
s.version = "1.0.0"
s.version = "1.1.0"
s.summary = "EasyPeasy is a Swift framework that eases the creation of
Autolayout constraints programmatically"
s.description = <<-DESC
Expand Down
34 changes: 26 additions & 8 deletions EasyPeasy/Attribute.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,25 @@ public class Attribute {
/// Element identifying the node this attribute will be
/// stored in
internal lazy var signature: String = {
return String.easy_signature(for: self)
// Signature of the create `ReferenceAttribute` of
// the passed `Attribute`
var signature = self.createAttribute.signatureString

// Signature of the `Relation` of the passed `Attribute`
switch self.constant.relation {
case .Equal:
signature += "eq_"
case .GreaterThanOrEqual:
signature += "gt_"
case .LessThanOrEqual:
signature += "lt_"
}

// Signature of the `Priority` of the passed
// `Attribute`
signature += String(self.priority.layoutPriority())

return signature
}()

/**
Expand All @@ -74,7 +92,7 @@ public class Attribute {
- returns: the `Attribute` instance created
*/
public init() {
self.constant = Constant(0.0)
self.constant = Constant(value: 0.0, relation: .Equal, multiplier: 1.0)
self.priority = .HighPriority
}

Expand All @@ -86,7 +104,7 @@ public class Attribute {
- returns: the `Attribute` instance created
*/
public init(_ value: CGFloat) {
self.constant = Constant(value)
self.constant = Constant(value: value, relation: .Equal, multiplier: 1.0)
self.priority = .HighPriority
}

Expand Down Expand Up @@ -152,11 +170,11 @@ public class Attribute {
let layoutConstraint = NSLayoutConstraint(
item: item,
attribute: self.createAttribute.layoutAttribute,
relatedBy: self.constant.layoutRelation(),
relatedBy: self.constant.relation,
toItem: self.referenceItem,
attribute: self.referenceAttributeHelper().layoutAttribute,
multiplier: self.constant.layoutMultiplier(),
constant: (self.constant.layoutValue() * constantFactor)
multiplier: self.constant.multiplier,
constant: (self.constant.value * constantFactor)
)

// Set priority
Expand Down Expand Up @@ -204,8 +222,8 @@ public class Attribute {
}

/**
Methods applicable to an `Array` of `Attributes`. The modifiers
will affect to each one of the `Attributes` within the `Array`
Methods applicable to an `Array` of `Attributes`. The `Constants`
will apply to each one of the `Attributes` within the `Array`
overriding the values individually set.
*/
public extension Array where Element: Attribute {
Expand Down
119 changes: 46 additions & 73 deletions EasyPeasy/Constant.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,9 @@ import AppKit
#endif

/**
Modifier used when a relationship is created between the target `UIView`
and the reference `UIView`.
Unlike `NSLayoutRelation` this enum also includes a `MultipliedBy` case
which acts as the `multiplier property of a `NSLayoutConstraint`
Alias of `NSLayoutRelation`
*/
public enum Modifier {

case EqualTo
case GreaterThanOrEqualTo
case LessThanOrEqualTo
case MultipliedBy

}
typealias Relation = NSLayoutRelation

/**
Struct that aggregates `NSLayoutRelation`, constant and multiplier of a
Expand All @@ -39,94 +28,62 @@ public struct Constant {
/// Value of the constant
let value: CGFloat

/// Modifier applied to the `value` of the `Constant`
var modifier: Modifier
/// Relation that applies to the `value` of the `Constant`
let relation: Relation

/**
This initializer creates a `Constant` with the value supplied
and the modifier `.EqualTo`
- parameter value: Value of the `Constant`
- returns: the `Constant` struct created
*/
init(_ value: CGFloat) {
self.value = value
self.modifier = .EqualTo
}
/// Multiplier of the `Constant`
let multiplier: CGFloat

/**
This initializer creates a `Constant` with the `value` and `modifier`
supplied.
This initializer creates a `Constant` with the `value`, `relations`
and `multiplier` supplied.
- parameter value: Value of the `Constant`
- parameter modifier: Modifier applied to the `value`
- parameter relation: `Relation that applies to the `value`
- parameter multiplier: Multiplier of the `Constant`
- returns: the `Constant` struct created
*/
init(value: CGFloat, modifier: Modifier) {
init(value: CGFloat, relation: Relation, multiplier: CGFloat) {
self.value = value
self.modifier = modifier
}

/**
`NSLayoutRelation` equivalent to the `modifier` of the `Constant`
- returns: `NSLayoutRelation` equivalent
*/
internal func layoutRelation() -> NSLayoutRelation {
switch self.modifier {
case .EqualTo: return .Equal
case .LessThanOrEqualTo: return .LessThanOrEqual
case .GreaterThanOrEqualTo: return .GreaterThanOrEqual
case .MultipliedBy: return .Equal
}
}

/**
Determines the `CGFloat` value of the multiplier for the `modifier`
property
- returns: `CGFloat` multiplier
*/
internal func layoutMultiplier() -> CGFloat {
switch self.modifier {
case .EqualTo: return 1.0
case .LessThanOrEqualTo: return 1.0
case .GreaterThanOrEqualTo: return 1.0
case .MultipliedBy: return CGFloat(self.value)
}
}

/**
Value of the `Constant`
- returns: `CGFloat` value of the `Constant`
*/
internal func layoutValue() -> CGFloat {
switch self.modifier {
case .MultipliedBy: return 0.0
default: return CGFloat(self.value)
}
self.relation = relation
self.multiplier = multiplier
}

}

prefix operator == {}

/**
Prefix operator that eases the creation of a `Constant` with a
`.Equal` relation
- parameter rhs: Value for the `Constant`
- returns: The resulting `Constant` struct
*/
public prefix func == (rhs: CGFloat) -> Constant {
return Constant(value: rhs, relation: .Equal, multiplier: 1.0)
}

prefix operator >= {}

/**
Prefix operator that eases the creation of a `Constant` with a
`.GreaterThanOrEqualTo` modifier.
`.GreaterThanOrEqual` relation
- parameter rhs: Value for the `Constant`
- returns: The resulting `Constant` struct
*/
public prefix func >= (rhs: CGFloat) -> Constant {
return Constant(value: rhs, modifier: .GreaterThanOrEqualTo)
return Constant(value: rhs, relation: .GreaterThanOrEqual, multiplier: 1.0)
}

prefix operator <= {}

/**
Prefix operator that eases the creation of a `Constant` with a
`.LessThanOrEqualTo` modifier.
`.LessThanOrEqual` relation
- parameter rhs: Value for the `Constant`
- returns: The resulting `Constant` struct
*/
public prefix func <= (rhs: CGFloat) -> Constant {
return Constant(value: rhs, modifier: .LessThanOrEqualTo)
return Constant(value: rhs, relation: .LessThanOrEqual, multiplier: 1.0)
}

prefix operator * {}
Expand All @@ -138,5 +95,21 @@ prefix operator * {}
- returns: The resulting `Constant` struct
*/
public prefix func * (rhs: CGFloat) -> Constant {
return Constant(value: rhs, modifier: .MultipliedBy)
return Constant(value: rhs, relation: .Equal, multiplier: rhs)
}

/**
Infix operator that applies the `multiplier` at the right hand side to the
`Constant` at the left hand side.
i.e. `Width((>=200.0)*0.5)` creates a `Constant` with `multiplier = 0.5`,
`relation = .GreaterThanOrEqual` and `value = 200.0`.
If the left hand side `Constant` already has a `multiplier` defined the
resulting `multiplier` will be the multiplication of both, previous and new
`multipliers`.
- parameter lhs: a `Constant`
- parameter rhs: a `CGFloat` multiplier
- returns: A new `Constant` with the `multiplier` applied
*/
public func * (lhs: Constant, rhs: CGFloat) -> Constant {
return Constant(value: lhs.value, relation: lhs.relation, multiplier: lhs.multiplier * rhs)
}
44 changes: 44 additions & 0 deletions EasyPeasy/ReferenceAttribute.swift
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,47 @@ public enum ReferenceAttribute {
}

}

#if os(iOS) || os(tvOS)

/**
Extends `ReferenceAttribute` to ease the creation of
an `Attribute` signature
*/
internal extension ReferenceAttribute {

/// Signature of a `ReferenceAttribute`. Two possible values
/// depending on the Axis the `ReferenceAttribute` applies
var signatureString: String {
switch self {
case .Left, .Leading, .LeftMargin, .LeadingMargin, .Right, .Trailing, .RightMargin, .TrailingMargin, .CenterX, .CenterXWithinMargins, .Width:
return "h_"
case .Top, .FirstBaseline, .TopMargin, .Bottom, .LastBaseline, .BottomMargin, .CenterY, .CenterYWithinMargins, .Height:
return "v_"
}
}

}

#else

/**
Extends `ReferenceAttribute` to ease the creation of
an `Attribute` signature
*/
internal extension ReferenceAttribute {

/// Signature of a `ReferenceAttribute`. Two possible values
/// depending on the Axis the `ReferenceAttribute` applies
var signatureString: String {
switch self {
case .Left, .Leading, .Right, .Trailing, .CenterX, .Width:
return "h_"
case .Top, .FirstBaseline, .Bottom, .LastBaseline, .CenterY, .Height:
return "v_"
}
}

}

#endif
Loading

0 comments on commit 4abf8c0

Please sign in to comment.