Skip to content

Commit

Permalink
Replace guide properties with superview property (#361)
Browse files Browse the repository at this point in the history
  • Loading branch information
tinder-cfuller authored Mar 2, 2024
1 parent 41b999d commit 7c8a041
Showing 1 changed file with 25 additions and 38 deletions.
63 changes: 25 additions & 38 deletions Sources/Layout/LayoutItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,22 +75,9 @@ extension LayoutItem {
set { layoutItemView.accessibilityIdentifier = newValue }
}

private var marginsGuide: UILayoutGuide? {
guard let superview: UIView = layoutItemView.superview
else {
assertionFailure("Missing required superview reference")
return nil
}
return superview.layoutMarginsGuide
}

private var safeAreaGuide: UILayoutGuide? {
guard let superview: UIView = layoutItemView.superview
else {
assertionFailure("Missing required superview reference")
return nil
}
return superview.safeAreaLayoutGuide
private var superview: UIView? {
assert(layoutItemView.superview != nil, "`layoutItemView` must have a `superview`")
return layoutItemView.superview
}

// MARK: - Identifier
Expand Down Expand Up @@ -343,7 +330,7 @@ extension LayoutItem {
and trailing: NSLayoutXAxisAnchor
) -> LayoutItem {
addingSuperviewConstraints { layoutItem in
if let superview: UIView = layoutItem.layoutItemView.superview {
if let superview: UIView = layoutItem.superview {
let guide: UILayoutGuide = {
let guide: UILayoutGuide = .init()
superview.addLayoutGuide(guide)
Expand Down Expand Up @@ -399,7 +386,7 @@ extension LayoutItem {
and bottom: NSLayoutYAxisAnchor
) -> LayoutItem {
addingSuperviewConstraints { layoutItem in
if let superview: UIView = layoutItem.layoutItemView.superview {
if let superview: UIView = layoutItem.superview {
let guide: UILayoutGuide = {
let guide: UILayoutGuide = .init()
superview.addLayoutGuide(guide)
Expand Down Expand Up @@ -486,7 +473,7 @@ extension LayoutItem {
priority: UILayoutPriority = .required
) -> LayoutItem {
addingSuperviewConstraints { layoutItem in
if let superview: UIView = layoutItem.layoutItemView.superview {
if let superview: UIView = layoutItem.superview {
for edge: DirectionalEdge in insets.edgeType.allCases {
layoutItem.constraint(to: edge, of: superview, insets: insets, priority: priority)
}
Expand All @@ -507,7 +494,7 @@ extension LayoutItem {
priority: UILayoutPriority = .required
) -> LayoutItem {
addingSuperviewConstraints { layoutItem in
if let superview: UIView = layoutItem.layoutItemView.superview {
if let superview: UIView = layoutItem.superview {
for edge: CanonicalEdge in insets.edgeType.allCases {
layoutItem.constraint(to: edge, of: superview, insets: insets, priority: priority)
}
Expand All @@ -530,7 +517,7 @@ extension LayoutItem {
priority: UILayoutPriority = .required
) -> LayoutItem {
addingSuperviewConstraints { layoutItem in
if let superview: UIView = layoutItem.layoutItemView.superview {
if let superview: UIView = layoutItem.superview {
for edge: DirectionalEdge in edges {
layoutItem.constraint(to: edge, of: superview, inset: inset, priority: priority)
}
Expand All @@ -553,7 +540,7 @@ extension LayoutItem {
priority: UILayoutPriority = .required
) -> LayoutItem {
addingSuperviewConstraints { layoutItem in
if let superview: UIView = layoutItem.layoutItemView.superview {
if let superview: UIView = layoutItem.superview {
for edge: CanonicalEdge in edges {
layoutItem.constraint(to: edge, of: superview, inset: inset, priority: priority)
}
Expand Down Expand Up @@ -593,9 +580,9 @@ extension LayoutItem {
priority: UILayoutPriority = .required
) -> LayoutItem {
addingSuperviewConstraints { layoutItem in
if let layoutGuide: UILayoutGuide = layoutItem.marginsGuide {
if let margins: UILayoutGuide = layoutItem.superview?.margins {
for edge: DirectionalEdge in insets.edgeType.allCases {
layoutItem.constraint(to: edge, of: layoutGuide, insets: insets, priority: priority)
layoutItem.constraint(to: edge, of: margins, insets: insets, priority: priority)
}
}
}
Expand All @@ -614,9 +601,9 @@ extension LayoutItem {
priority: UILayoutPriority = .required
) -> LayoutItem {
addingSuperviewConstraints { layoutItem in
if let layoutGuide: UILayoutGuide = layoutItem.marginsGuide {
if let margins: UILayoutGuide = layoutItem.superview?.margins {
for edge: CanonicalEdge in insets.edgeType.allCases {
layoutItem.constraint(to: edge, of: layoutGuide, insets: insets, priority: priority)
layoutItem.constraint(to: edge, of: margins, insets: insets, priority: priority)
}
}
}
Expand All @@ -637,9 +624,9 @@ extension LayoutItem {
priority: UILayoutPriority = .required
) -> LayoutItem {
addingSuperviewConstraints { layoutItem in
if let layoutGuide: UILayoutGuide = layoutItem.marginsGuide {
if let margins: UILayoutGuide = layoutItem.superview?.margins {
for edge: DirectionalEdge in edges {
layoutItem.constraint(to: edge, of: layoutGuide, inset: inset, priority: priority)
layoutItem.constraint(to: edge, of: margins, inset: inset, priority: priority)
}
}
}
Expand All @@ -660,9 +647,9 @@ extension LayoutItem {
priority: UILayoutPriority = .required
) -> LayoutItem {
addingSuperviewConstraints { layoutItem in
if let layoutGuide: UILayoutGuide = layoutItem.marginsGuide {
if let margins: UILayoutGuide = layoutItem.superview?.margins {
for edge: CanonicalEdge in edges {
layoutItem.constraint(to: edge, of: layoutGuide, inset: inset, priority: priority)
layoutItem.constraint(to: edge, of: margins, inset: inset, priority: priority)
}
}
}
Expand Down Expand Up @@ -700,9 +687,9 @@ extension LayoutItem {
priority: UILayoutPriority = .required
) -> LayoutItem {
addingSuperviewConstraints { layoutItem in
if let layoutGuide: UILayoutGuide = layoutItem.safeAreaGuide {
if let safeArea: UILayoutGuide = layoutItem.superview?.safeArea {
for edge: DirectionalEdge in insets.edgeType.allCases {
layoutItem.constraint(to: edge, of: layoutGuide, insets: insets, priority: priority)
layoutItem.constraint(to: edge, of: safeArea, insets: insets, priority: priority)
}
}
}
Expand All @@ -721,9 +708,9 @@ extension LayoutItem {
priority: UILayoutPriority = .required
) -> LayoutItem {
addingSuperviewConstraints { layoutItem in
if let layoutGuide: UILayoutGuide = layoutItem.safeAreaGuide {
if let safeArea: UILayoutGuide = layoutItem.superview?.safeArea {
for edge: CanonicalEdge in insets.edgeType.allCases {
layoutItem.constraint(to: edge, of: layoutGuide, insets: insets, priority: priority)
layoutItem.constraint(to: edge, of: safeArea, insets: insets, priority: priority)
}
}
}
Expand All @@ -744,9 +731,9 @@ extension LayoutItem {
priority: UILayoutPriority = .required
) -> LayoutItem {
addingSuperviewConstraints { layoutItem in
if let layoutGuide: UILayoutGuide = layoutItem.safeAreaGuide {
if let safeArea: UILayoutGuide = layoutItem.superview?.safeArea {
for edge: DirectionalEdge in edges {
layoutItem.constraint(to: edge, of: layoutGuide, inset: inset, priority: priority)
layoutItem.constraint(to: edge, of: safeArea, inset: inset, priority: priority)
}
}
}
Expand All @@ -767,9 +754,9 @@ extension LayoutItem {
priority: UILayoutPriority = .required
) -> LayoutItem {
addingSuperviewConstraints { layoutItem in
if let layoutGuide: UILayoutGuide = layoutItem.safeAreaGuide {
if let safeArea: UILayoutGuide = layoutItem.superview?.safeArea {
for edge: CanonicalEdge in edges {
layoutItem.constraint(to: edge, of: layoutGuide, inset: inset, priority: priority)
layoutItem.constraint(to: edge, of: safeArea, inset: inset, priority: priority)
}
}
}
Expand Down

0 comments on commit 7c8a041

Please sign in to comment.