Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Say no to commas: add array builder to layout #108

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
50 changes: 50 additions & 0 deletions ArrayBuilder.swift
@@ -0,0 +1,50 @@
// The MIT License (MIT) - Copyright (c) 2016 Carlos Vidal
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

#if swift(>=5.4)
import Foundation

@resultBuilder
public enum EasyPeasyArrayBuilder<T> {
public typealias Component = [T]

public static func buildExpression(_ expression: T) -> Component {
[expression]
}

public static func buildExpression(_ expression: Component) -> Component {
expression
}

public static func buildExpression(_ expression: Void) -> Component {
[]
}

public static func buildBlock(_ components: Component...) -> Component {
buildArray(components)
}

public static func buildEither(first component: Component) -> Component {
component
}

public static func buildEither(second component: Component) -> Component {
component
}

public static func buildOptional(_ component: Component?) -> Component {
component ?? []
}

public static func buildArray(_ components: [Self.Component]) -> Self.Component {
components.flatMap{ $0 }
}
}
#endif
12 changes: 12 additions & 0 deletions EasyPeasy/EasyPeasy.swift
Expand Up @@ -62,3 +62,15 @@ public struct EasyPeasy {

}

#if swift(>=5.4)
extension EasyPeasy {
/**
Applies the attributes given to the current item
- parameter attributes: `Attributes` applied to the `Item`
- returns: The array of `NSLayoutConstraints` created and applied
*/
@discardableResult public func layout(@EasyPeasyArrayBuilder<Attribute> _ builder: () -> [Attribute]) -> [NSLayoutConstraint] {
return self.item?.apply(attributes: builder()) ?? []
}
}
#endif