Skip to content

Commit

Permalink
Merge pull request #105 from freshOS/codeQuality
Browse files Browse the repository at this point in the history
Code quality
  • Loading branch information
s4cha committed Dec 27, 2018
2 parents 8eb2a4d + 97f5af4 commit d1715dd
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 87 deletions.
40 changes: 0 additions & 40 deletions Source/Stevia+Constraints.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,46 +12,6 @@ import UIKit

public extension UIView {

@available(*, deprecated: 2.2.1, message: "Use 'addConstraint' instead")
/**
Helper for creating and adding NSLayoutConstraint but with default values provided.
For instance
c(item: view1, attribute: .CenterX, toItem: view2)
is equivalent to
addConstraint(
NSLayoutConstraint(item: view1,
attribute: .CenterX,
relatedBy: .Equal,
toItem: view2,
attribute: .CenterX,
multiplier: 1,
constant: 0
)
)
- Returns: The NSLayoutConstraint created.
*/
@discardableResult
public func c(item view1: AnyObject,
attribute attr1: NSLayoutConstraint.Attribute,
relatedBy: NSLayoutConstraint.Relation = .equal,
toItem view2: AnyObject? = nil,
attribute attr2: NSLayoutConstraint.Attribute? = nil,
multiplier: CGFloat = 1,
constant: CGFloat = 0) -> NSLayoutConstraint {
let c = constraint(
item: view1, attribute: attr1,
relatedBy: relatedBy,
toItem: view2, attribute: attr2,
multiplier: multiplier, constant: constant)
addConstraint(c)
return c
}

/**
Helper for creating and adding NSLayoutConstraint but with default values provided.
Expand Down
65 changes: 18 additions & 47 deletions Source/Stevia+Stacks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
import UIKit

public extension UIView {

/**

/**
Lays out the views on both axis.
Note that this is not needed for Horizontal only layouts.
Expand All @@ -21,13 +21,13 @@ public extension UIView {
```
layout(
100,
|-email-| ~ 80,
8,
|-password-forgot-| ~ 80,
>=20,
|login| ~ 80,
0
100,
|-email-| ~ 80,
8,
|-password-forgot-| ~ 80,
>=20,
|login| ~ 80,
0
)
```
*/
Expand All @@ -40,10 +40,10 @@ public extension UIView {
public func layout(_ objects: [Any]) -> [UIView] {
var previousMargin: CGFloat?
var previousFlexibleMargin: SteviaFlexibleMargin?

for (i, o) in objects.enumerated() {

switch o {
case let v as UIView:
func viewLogic(_ v: UIView) {
if let pm = previousMargin {
if i == 1 {
v.top(pm) // only if first view
Expand Down Expand Up @@ -79,6 +79,11 @@ public extension UIView {
} else {
tryStackViewVerticallyWithPreviousView(v, index: i, objects: objects)
}
}

switch o {
case let v as UIView:
viewLogic(v)
case is Int, is Double, is CGFloat:
let m = cgFloatMarginFromObject(o)
previousMargin = m // Store margin for next pass
Expand All @@ -103,42 +108,8 @@ public extension UIView {
case _ as String:() //Do nothin' !
case let a as [UIView]:
align(horizontally: a)
let v = a.first!
if let pm = previousMargin {
if i == 1 {
v.top(pm) // only if first view
} else {
if let vx = objects[i-2] as? UIView {
vx.stackV(m: pm, v: v)
} else if let va = objects[i-2] as? [UIView] {
va.first!.stackV(m: pm, v: v)
}
}
previousMargin = nil
} else if let pfm = previousFlexibleMargin {
if i == 1 {
v.top(pfm) // only if first view
} else {
if let vx = objects[i-2] as? UIView {
addConstraint(
item: v, attribute: .top,
relatedBy: pfm.relation,
toItem: vx, attribute: .bottom,
multiplier: 1, constant: pfm.points
)
} else if let va = objects[i-2] as? [UIView] {
addConstraint(
item: v, attribute: .top,
relatedBy: pfm.relation,
toItem: va.first!, attribute: .bottom,
multiplier: 1, constant: pfm.points
)
}
}
previousFlexibleMargin = nil
} else {
tryStackViewVerticallyWithPreviousView(v, index: i, objects: objects)
}
let v = a.first!
viewLogic(v)
default: ()
}
}
Expand Down

0 comments on commit d1715dd

Please sign in to comment.