Skip to content

Commit

Permalink
Update structure with LLM
Browse files Browse the repository at this point in the history
  • Loading branch information
eonist committed Oct 1, 2023
1 parent 572ead3 commit d8e2251
Show file tree
Hide file tree
Showing 24 changed files with 494 additions and 103 deletions.
42 changes: 37 additions & 5 deletions Sources/Spatial/ConstraintKind/ConstraintKind+Access.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,32 @@ extension ConstraintKind where Self: View {
public func applyAnchorAndSize(to: View, sizeTo: View? = nil, width: CGFloat? = nil, height: CGFloat? = nil, align: Alignment = .topLeft, alignTo: Alignment = .topLeft, multiplier: CGSize = .init(width: 1, height: 1), offset: CGPoint = .zero, sizeOffset: CGSize = .zero, useMargin: Bool = false) {
// Call the more customizable `applyAnchorAndSize` method with a closure that defines the anchor and size constraints
self.applyAnchorAndSize { _ in
let anchor: AnchorConstraint = Constraint.anchor(self, to: to, align: align, alignTo: alignTo, offset: offset, useMargin: useMargin) // Create an anchor constraint for the view
let anchor: AnchorConstraint = Constraint.anchor(
self, // The source view
to: to, // The target view
align: align, // The alignment to use
alignTo: alignTo, // The alignment to align to
offset: offset, // The offset to use
useMargin: useMargin // Whether to use layout margins or not
) // Create an anchor constraint for the view
let size: SizeConstraint = {
if let width = width, let height = height { // Check if both width and height are defined
// If both width and height are defined, create a size constraint with the specified width and height
return Constraint.size(self, size: .init(width: width, height: height), multiplier: multiplier)
return Constraint.size(
self, // The source view
size: .init(width: width, height: height), // The size to use
multiplier: multiplier // The multiplier to use
)
} else {
// If either width or height is not defined, create a size constraint based on the size of another view or the view itself
return Constraint.size(self, to: sizeTo ?? to, width: width, height: height, offset: sizeOffset, multiplier: multiplier)
return Constraint.size(
self, // The source view
to: sizeTo ?? to, // The target view to use for size
width: width, // The width to use
height: height, // The height to use
offset: sizeOffset, // The offset to use
multiplier: multiplier // The multiplier to use
)
}
}() // Create a size constraint for the view, based on the width, height, sizeTo, and sizeOffset parameters
return (anchor, size) // Return a tuple containing the anchor and size constraints for the view
Expand All @@ -53,7 +71,14 @@ extension ConstraintKind where Self: View {
public func applyAnchor(to: View, align: Alignment = .topLeft, alignTo: Alignment = .topLeft, offset: CGPoint = .zero, useMargin: Bool = false) {
// Call the more customizable `applyAnchor` method with a closure that defines the anchor constraint
self.applyAnchor { _ in // Call the `applyAnchor` method with a closure that defines the anchor constraint
Constraint.anchor(self, to: to, align: align, alignTo: alignTo, offset: offset, useMargin: useMargin) // Create an anchor constraint for the view
Constraint.anchor(
self, // The source view
to: to, // The target view
align: align, // The alignment to use
alignTo: alignTo, // The alignment to align to
offset: offset, // The offset to use
useMargin: useMargin // Whether to use layout margins or not
) // Create an anchor constraint for the view
}
}
/**
Expand All @@ -71,7 +96,14 @@ extension ConstraintKind where Self: View {
public func applySize(to: View, width: CGFloat? = nil, height: CGFloat? = nil, offset: CGSize = .zero, multiplier: CGSize = .init(width: 1, height: 1)) {
// Call the more customizable `applySize` method with a closure that defines the size constraint
self.applySize { _ in // Call the `applySize` method with a closure that defines the size constraint
Constraint.size(self, to: to, width: width, height: height, offset: offset, multiplier: multiplier) // Create a size constraint for the view
Constraint.size(
self, // The source view
to: to, // The target view to use for size
width: width, // The width to use
height: height, // The height to use
offset: offset, // The offset to use
multiplier: multiplier // The multiplier to use
) // Create a size constraint for the view
}
}
}
7 changes: 6 additions & 1 deletion Sources/Spatial/ConstraintKind/ConstraintKind+Apply.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ extension ConstraintKind where Self: View {
self.translatesAutoresizingMaskIntoConstraints = false // Disable the view's translation of autoresizing mask into constraints
let constraints: AnchorAndSize = closure(self) // Call the closure to get the anchor and size constraints for the view
setConstraint(anchor: constraints.anchor, size: constraints.size) // Set the anchor and size constraints for the view
NSLayoutConstraint.activate([constraints.anchor.x, constraints.anchor.y, constraints.size.w, constraints.size.h]) // Activate the anchor and size constraints for the view
NSLayoutConstraint.activate([
constraints.anchor.x, // The X-axis anchor constraint
constraints.anchor.y, // The Y-axis anchor constraint
constraints.size.w, // The width size constraint
constraints.size.h // The height size constraint
]) // Activate the anchor and size constraints for the view
}
/**
* Applies an anchor constraint to the view, with optional parameters for customization.
Expand Down
33 changes: 29 additions & 4 deletions Sources/Spatial/ConstraintKind/ConstraintKind+Bulk+Access.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,14 @@ extension Array where Element: ConstraintKind.ViewConstraintKind {
public func applySizes(to: View, width: CGFloat? = nil, height: CGFloat? = nil, offset: CGSize = .zero, multiplier: CGSize = .init(width: 1, height: 1)) {
self.applySizes { views in // Apply size constraints to the views in the array
views.map { // Map each view to a size constraint
Constraint.size($0, to: to, width: width, height: height, offset: offset, multiplier: multiplier) // Create a size constraint for the view
Constraint.size(
$0, // The source view
to: to, // The target view to use for size
width: width, // The width to use
height: height, // The height to use
offset: offset, // The offset to use
multiplier: multiplier // The multiplier to use
) // Create a size constraint for the view
}
}
}
Expand All @@ -37,7 +44,11 @@ extension Array where Element: ConstraintKind.ViewConstraintKind {
public func applySizes(width: CGFloat, height: CGFloat, multiplier: CGSize = .init(width: 1, height: 1)) {
self.applySizes { views in // Apply size constraints to the views in the array
views.map { // Map each view to a size constraint
Constraint.size($0, size: .init(width: width, height: height), multiplier: multiplier) // Create a size constraint for the view with fixed width and height
Constraint.size(
$0, // The source view
size: .init(width: width, height: height), // The size to use
multiplier: multiplier // The multiplier to use
) // Create a size constraint for the view with fixed width and height
}
}
}
Expand All @@ -56,7 +67,14 @@ extension Array where Element: ConstraintKind.ViewConstraintKind {
public func applyAnchors(to: View, align: VerticalAlign = .top, alignTo: VerticalAlign = .top, offset: CGFloat = .zero, useMargin: Bool = false) {
self.applyAnchors(axis: .ver) { views in // Apply vertical anchor constraints to the views in the array
views.map { // Map each view to an anchor constraint
Constraint.anchor($0, to: to, align: align, alignTo: alignTo, offset: offset, useMargin: useMargin) // Create a vertical anchor constraint for the view
Constraint.anchor(
$0, // The source view
to: to, // The target view
align: align, // The alignment to use
alignTo: alignTo, // The alignment to align to
offset: offset, // The offset to use
useMargin: useMargin // Whether to use layout margins or not
) // Create a vertical anchor constraint for the view
}
}
}
Expand All @@ -75,7 +93,14 @@ extension Array where Element: ConstraintKind.ViewConstraintKind {
public func applyAnchors(to: View, align: HorizontalAlign = .left, alignTo: HorizontalAlign = .left, offset: CGFloat = .zero, useMargin: Bool = false) {
self.applyAnchors(axis: .hor) { views in // Apply horizontal anchor constraints to the views in the array
views.map { // Map each view to an anchor constraint
Constraint.anchor($0, to: to, align: align, alignTo: alignTo, offset: offset, useMargin: useMargin) // Create a horizontal anchor constraint for the view
Constraint.anchor(
$0, // The source view
to: to, // The target view
align: align, // The alignment to use
alignTo: alignTo, // The alignment to align to
offset: offset, // The offset to use
useMargin: useMargin // Whether to use layout margins or not
) // Create a horizontal anchor constraint for the view
}
}
}
Expand Down
30 changes: 26 additions & 4 deletions Sources/Spatial/ConstraintKind/ConstraintKind+SpaceAround.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,18 @@ extension Array where Element: ConstraintKind.ViewConstraintKind {
*/
public func spaceAround(dir: Axis, parent: View, inset: EdgeInsets = .init()) {
switch dir {
case .hor: SpaceAroundUtil.spaceAround(horizontally: parent, views: self, inset: inset) // Add equal spacing between views horizontally
case .ver: SpaceAroundUtil.spaceAround(vertically: parent, views: self, inset: inset) // Add equal spacing between views vertically
case .hor:
SpaceAroundUtil.spaceAround(
horizontally: parent, // The parent view to align horizontally to
views: self, // The views to align
inset: inset // The inset to use
) // Add equal spacing between views horizontally
case .ver:
SpaceAroundUtil.spaceAround(
vertically: parent, // The parent view to align vertically to
views: self, // The views to align
inset: inset // The inset to use
) // Add equal spacing between views vertically
}
}
}
Expand All @@ -50,7 +60,13 @@ fileprivate class SpaceAroundUtil {
var x: CGFloat = rect.origin.x + itemVoid // Interim x
views.forEach { item in
item.activateConstraint { _ in
let constraint = Constraint.anchor(item, to: parent, align: .left, alignTo: .left, offset: x)
let constraint = Constraint.anchor(
item, // The item to anchor
to: parent, // The parent view to anchor to
align: .left, // The alignment to use for the item
alignTo: .left, // The alignment to align to on the parent view
offset: x // The offset to use
)
item.anchor?.x = constraint
return constraint
}
Expand All @@ -71,7 +87,13 @@ fileprivate class SpaceAroundUtil {
var y: CGFloat = rect.origin.y + itemVoid // Interim y
views.forEach { item in
item.activateConstraint { _ in
let constraint = Constraint.anchor(item, to: parent, align: .top, alignTo: .top, offset: y)
let constraint = Constraint.anchor(
item, // The item to anchor
to: parent, // The parent view to anchor to
align: .top, // The alignment to use for the item
alignTo: .top, // The alignment to align to on the parent view
offset: y // The offset to use
)
item.anchor?.y = constraint
return constraint
}
Expand Down
30 changes: 26 additions & 4 deletions Sources/Spatial/ConstraintKind/ConstraintKind+SpaceBetween.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,19 @@ extension Array where Element: ConstraintKind.ViewConstraintKind {
public func spaceBetween(dir: Axis, parent: View, inset: EdgeInsets = .init()) {
switch dir {
// If the direction is horizontal, call the spaceBetween method with the parent view, the views to distribute, and the inset value.
case .hor: SpaceBetweenUtil.spaceBetween(horizontally: parent, views: self, inset: inset)
case .hor:
SpaceBetweenUtil.spaceBetween(
horizontally: parent, // The parent view to align horizontally to
views: self, // The views to align
inset: inset // The inset to use
)
// If the direction is vertical, call the spaceBetween method with the parent view, the views to distribute, and the inset value.
case .ver: SpaceBetweenUtil.spaceBetween(vertically: parent, views: self, inset: inset)
case .ver:
SpaceBetweenUtil.spaceBetween(
vertically: parent, // The parent view to align vertically to
views: self, // The views to align
inset: inset // The inset to use
)
}
}
}
Expand All @@ -49,7 +59,13 @@ private class SpaceBetweenUtil {
// Loop through each view and activate a constraint to align it to the left of the parent view
views.forEach { item in
item.activateConstraint { _ in // Fixme: ⚠️️ Create applyAnchor for hor and ver
let constraint = Constraint.anchor(item, to: parent, align: .left, alignTo: .left, offset: x)
let constraint = Constraint.anchor(
item, // The item to anchor
to: parent, // The parent view to anchor to
align: .left, // The alignment to use for the item
alignTo: .left, // The alignment to align to on the parent view
offset: x // The offset to use
)
item.anchor?.x = constraint
return constraint
}
Expand All @@ -70,7 +86,13 @@ private class SpaceBetweenUtil {
var y: CGFloat = rect.origin.y // Set the initial y position to the top of the parent view
views.forEach { item in // Loop through each view to align
item.activateConstraint { _ in // Activate the view's constraints
let constraint = Constraint.anchor(item, to: parent, align: .top, alignTo: .top, offset: y) // Align the view to the top of the parent view with the given y offset
let constraint = Constraint.anchor(
item, // The item to anchor
to: parent, // The parent view to anchor to
align: .top, // The alignment to use for the item
alignTo: .top, // The alignment to align to on the parent view
offset: y // The offset to use
) // Align the view to the top of the parent view with the given y offset
item.anchor?.y = constraint // Store the constraint in the view's anchor property
return constraint // Return the constraint
}
Expand Down
20 changes: 16 additions & 4 deletions Sources/Spatial/ConstraintKind/ConstraintKind+Type.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,20 @@ extension ConstraintKind where Self: View {
* Defines closures that operate on an array of `ViewConstraintKind` elements.
*/
extension Array where Element: ConstraintKind.ViewConstraintKind {
public typealias AnchorAndSizeClosure = (_ views: [View]) -> AnchorConstraintsAndSizeConstraints // A closure that returns anchor and size constraints for an array of views
public typealias SizesClosure = (_ views: [View]) -> [SizeConstraint] // A closure that returns size constraints for an array of views
public typealias AnchorClosure = (_ views: [View]) -> [AnchorConstraint] // A closure that returns anchor constraints for an array of views
public typealias AxisClosure = (_ views: [View]) -> [NSLayoutConstraint] // A closure that returns axis constraints for an array of views
/**
* A closure that returns anchor and size constraints for an array of views
*/
public typealias AnchorAndSizeClosure = (_ views: [View]) -> AnchorConstraintsAndSizeConstraints
/**
* A closure that returns size constraints for an array of views
*/
public typealias SizesClosure = (_ views: [View]) -> [SizeConstraint]
/**
* A closure that returns anchor constraints for an array of views
*/
public typealias AnchorClosure = (_ views: [View]) -> [AnchorConstraint]
/**
* A closure that returns axis constraints for an array of views
*/
public typealias AxisClosure = (_ views: [View]) -> [NSLayoutConstraint]
}
8 changes: 4 additions & 4 deletions Sources/Spatial/ConstraintKind/ConstraintKind.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ extension ConstraintKind {
*/
public var anchorAndSize: AnchorAndSize? {
get {
guard let anchor = anchor, let size = size else { return nil }
return (anchor, size)
guard let anchor = anchor, let size = size else { return nil } // If either anchor or size is nil, return nil
return (anchor, size) // Return a tuple of anchor and size
} set {
anchor = newValue?.anchor
size = newValue?.size
anchor = newValue?.anchor // Set the anchor to the anchor value of the new tuple
size = newValue?.size // Set the size to the size value of the new tuple
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ import Foundation
* - Note: To use this class, the view must implement the `ConstraintKind` protocol and set the `anchorAndSize` property.
*/
open class ConstraintView: View, ConstraintKind {
public var anchorAndSize: AnchorAndSize? // The anchor and size constraints for the view
// The anchor and size constraints for the view
public var anchorAndSize: AnchorAndSize?
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ extension ConstraintKind where Self: UIView {
*/
public func animate(to: CGFloat, align: HorizontalAlign, alignTo: HorizontalAlign, onComplete:@escaping AnimComplete = Self.defaultOnComplete) {
// Call the `UIView.animate` method with a closure that updates the view's horizontal constraint by calling the `update` method with the specified offset, alignment, and canvas alignment
UIView.animate({
self.update(offset: to, align: align, alignTo: alignTo)
}, onComplete: onComplete)
UIView.animate({ // Start the animation block
self.update(offset: to, align: align, alignTo: alignTo) // Update the offset and alignment
}, onComplete: onComplete) // Call the completion handler when the animation is complete
}
/**
* Provides a method for animating a view's vertical constraint.
Expand All @@ -35,9 +35,9 @@ extension ConstraintKind where Self: UIView {
*/
public func animate(to: CGFloat, align: VerticalAlign, alignTo: VerticalAlign, onComplete:@escaping AnimComplete = Self.defaultOnComplete) {
// Call the `UIView.animate` method with a closure that updates the view's vertical constraint by calling the `update` method with the specified offset, alignment, and canvas alignment
UIView.animate({
self.update(offset: to, align: align, alignTo: alignTo)
}, onComplete: onComplete)
UIView.animate({ // Start the animation block
self.update(offset: to, align: align, alignTo: alignTo) // Update the offset and alignment
}, onComplete: onComplete) // Call the completion handler when the animation is complete
}
/**
* Provides a method for animating a view's vertical and horizontal constraints.
Expand All @@ -52,8 +52,12 @@ extension ConstraintKind where Self: UIView {
public func animate(to: CGPoint, align: Alignment, alignTo: Alignment, onComplete:@escaping AnimComplete = Self.defaultOnComplete) {
// Call the `UIView.animate` method with a closure that updates the view's constraints by calling the `update` method with the specified offset, alignment, and canvas alignment
UIView.animate({
self.update(offset: to, align: align, alignTo: alignTo)
}, onComplete: onComplete)
self.update(
offset: to, // The offset to update to
align: align, // The alignment to use
alignTo: alignTo // The alignment to align to
)
}, onComplete: onComplete) // The completion handler to call when the animation is complete
}
}
#endif
Loading

0 comments on commit d8e2251

Please sign in to comment.