diff --git a/Sources/UIViewKit/AutoresizingStrategy/InferredAttributesOwnerStrategy.swift b/Sources/UIViewKit/AutoresizingStrategy/InferredAttributesOwnerStrategy.swift index a3c86be..4e2e15c 100644 --- a/Sources/UIViewKit/AutoresizingStrategy/InferredAttributesOwnerStrategy.swift +++ b/Sources/UIViewKit/AutoresizingStrategy/InferredAttributesOwnerStrategy.swift @@ -1,6 +1,6 @@ // // InferredAttributesOwnerStrategy.swift -// +// UIViewKit // // Created by Blazej SLEBODA on 29/09/2023. // diff --git a/Sources/UIViewKit/AutoresizingStrategy/InferredConstraintsStrategy.swift b/Sources/UIViewKit/AutoresizingStrategy/InferredConstraintsStrategy.swift index 33b9f05..69af39f 100644 --- a/Sources/UIViewKit/AutoresizingStrategy/InferredConstraintsStrategy.swift +++ b/Sources/UIViewKit/AutoresizingStrategy/InferredConstraintsStrategy.swift @@ -1,6 +1,6 @@ // // InferredConstraintsStrategy.swift -// +// UIViewKit // // Created by Blazej SLEBODA on 29/09/2023. // diff --git a/Sources/UIViewKit/AutoresizingStrategy/UIViewDSLEngineConstraintsProtocol.swift b/Sources/UIViewKit/AutoresizingStrategy/UIViewDSLEngineConstraintsProtocol.swift index c2ba4f0..40f6632 100644 --- a/Sources/UIViewKit/AutoresizingStrategy/UIViewDSLEngineConstraintsProtocol.swift +++ b/Sources/UIViewKit/AutoresizingStrategy/UIViewDSLEngineConstraintsProtocol.swift @@ -1,6 +1,6 @@ // // UIViewDSLEngineConstraintsProtocol.swift -// +// UIViewKit // // Created by Blazej SLEBODA on 29/09/2023. // diff --git a/Sources/UIViewKit/UIViewDSL+Attributes.swift b/Sources/UIViewKit/UIViewDSL+Attributes.swift index 43f611c..0701d0f 100644 --- a/Sources/UIViewKit/UIViewDSL+Attributes.swift +++ b/Sources/UIViewKit/UIViewDSL+Attributes.swift @@ -1,6 +1,6 @@ // // UIViewDSL+Attributes.swift -// +// UIViewKit // // Created by Blazej SLEBODA on 29/09/2023. // diff --git a/Sources/UIViewKit/UIViewDSL+Constraints.swift b/Sources/UIViewKit/UIViewDSL+Constraints.swift index d57803e..c990e11 100644 --- a/Sources/UIViewKit/UIViewDSL+Constraints.swift +++ b/Sources/UIViewKit/UIViewDSL+Constraints.swift @@ -1,6 +1,6 @@ // // UIViewDSL+Constraints.swift -// +// UIViewKit // // Created by Blazej SLEBODA on 29/09/2023. // diff --git a/Sources/UIViewKit/UIViewDSL+Helper.swift b/Sources/UIViewKit/UIViewDSL+Helper.swift index 1a322e0..8c4ea25 100644 --- a/Sources/UIViewKit/UIViewDSL+Helper.swift +++ b/Sources/UIViewKit/UIViewDSL+Helper.swift @@ -1,8 +1,8 @@ // -// File.swift -// +// UIViewDSL+Helper.swift +// UIViewKit // -// Created by MaxAir on 29/09/2023. +// Created by Blazej SLEBODA on 29/09/2023. // import UIKit @@ -10,7 +10,21 @@ import UIKit enum UIViewDSLHelper { static func involvesOwnerView(_ owner: UIView, in constraint: NSLayoutConstraint) -> Bool { - (constraint.firstItem as? UIView) == owner || (constraint.secondItem as? UIView) == owner + var ownerView: [UIView] = [] + + if let layoutGuide = constraint.firstItem as? UILayoutGuide, let owningView = layoutGuide.owningView { + ownerView.append(owningView) + } + if let layoutGuide = constraint.secondItem as? UILayoutGuide, let owningView = layoutGuide.owningView { + ownerView.append(owningView) + } + if let owningView = constraint.firstItem as? UIView { + ownerView.append(owningView) + } + if let owningView = constraint.secondItem as? UIView { + ownerView.append(owningView) + } + return ownerView.contains(where: { $0 == owner } ) } static func addSubviews(_ subviews: [UIView], to target: UIView) { diff --git a/Sources/UIViewKit/UIViewDSL+IBOutlet.swift b/Sources/UIViewKit/UIViewDSL+IBOutlet.swift index 830de6b..b3a8135 100644 --- a/Sources/UIViewKit/UIViewDSL+IBOutlet.swift +++ b/Sources/UIViewKit/UIViewDSL+IBOutlet.swift @@ -1,6 +1,6 @@ // // UIViewDSL+IBOutlet.swift -// +// UIViewKit // // Created by Blazej SLEBODA on 29/09/2023. // diff --git a/Sources/UIViewKit/UIViewDSL+ResultBuilders.swift b/Sources/UIViewKit/UIViewDSL+ResultBuilders.swift index 35ca151..311c26c 100644 --- a/Sources/UIViewKit/UIViewDSL+ResultBuilders.swift +++ b/Sources/UIViewKit/UIViewDSL+ResultBuilders.swift @@ -1,6 +1,6 @@ // // UIViewDSL+ResultBuilders.swift -// +// UIViewKit // // Created by Blazej SLEBODA on 29/09/2023. // @@ -69,10 +69,6 @@ public enum NSLayoutConstraintBuilder { public static func buildExpression(_ expression: [NSLayoutConstraint]) -> [NSLayoutConstraint] { expression } - - public static func buildExpression(_ expression: Void) -> [NSLayoutConstraint] { - [] - } public static func buildExpression(_ expression: Any?) -> [NSLayoutConstraint] { [] diff --git a/Sources/UIViewKit/UIViewDSL+Subviews.swift b/Sources/UIViewKit/UIViewDSL+Subviews.swift index b425e06..d4098d9 100644 --- a/Sources/UIViewKit/UIViewDSL+Subviews.swift +++ b/Sources/UIViewKit/UIViewDSL+Subviews.swift @@ -1,6 +1,6 @@ // // UIViewDSL+Subviews.swift -// +// UIViewKit // // Created by Blazej SLEBODA on 29/09/2023. // @@ -17,8 +17,11 @@ extension UIViewDSL where Self: UIView { } @discardableResult - public func ibSubviews(@UIViewBuilder _ content: (UIView) -> [UIView]) -> Self { - UIViewDSLEngine.shared.addSubviews(content, to: self) + public func ibSubviews(@UIViewBuilder _ content: (Self) -> [UIView]) -> Self { + let contentWrapper: (UIView) -> [UIView] = { arg1 in + content(arg1 as! Self) + } + UIViewDSLEngine.shared.addSubviews(contentWrapper, to: self) return self } @@ -29,8 +32,11 @@ extension UIViewDSL where Self: UIView { } @discardableResult - public func callAsFunction(@UIViewBuilder _ content: (UIView) -> [UIView]) -> Self { - UIViewDSLEngine.shared.addSubviews(content, to: self) + public func callAsFunction(@UIViewBuilder _ content: (Self) -> [UIView]) -> Self { + let contentWrapper: (UIView) -> [UIView] = { arg1 in + content(arg1 as! Self) + } + UIViewDSLEngine.shared.addSubviews(contentWrapper, to: self) return self } } diff --git a/Sources/UIViewKit/UIViewDSL+SwiftUIStyleStacks.swift b/Sources/UIViewKit/UIViewDSL+SwiftUIStyleStacks.swift index 6a28046..bc51148 100644 --- a/Sources/UIViewKit/UIViewDSL+SwiftUIStyleStacks.swift +++ b/Sources/UIViewKit/UIViewDSL+SwiftUIStyleStacks.swift @@ -1,6 +1,6 @@ // // UIViewDSL+SwiftUIStyleStacks.swift -// +// UIViewKit // // Created by Blazej SLEBODA on 29/09/2023. // diff --git a/Sources/UIViewKit/ViewInCode.swift b/Sources/UIViewKit/ViewInCode.swift deleted file mode 100644 index bc5fc69..0000000 --- a/Sources/UIViewKit/ViewInCode.swift +++ /dev/null @@ -1,22 +0,0 @@ -// -// ViewInCode.swift -// UIViewKit -// -// Created by MaxAir on 19/10/2023. -// - -import UIKit - -open class ViewInCode: UIView { - - override public init(frame: CGRect) { - super.init(frame: frame) - createView() - } - - required public init?(coder: NSCoder) { - fatalError() - } - - open func createView() { } -} diff --git a/Sources/UIViewKit/Views/IBView.swift b/Sources/UIViewKit/Views/IBView.swift new file mode 100644 index 0000000..e15c208 --- /dev/null +++ b/Sources/UIViewKit/Views/IBView.swift @@ -0,0 +1,30 @@ +// +// IBView.swift +// UIViewKit +// +// Created by Blazej SLEBODA on 19/10/2023. +// + +import UIKit + +open class IBView: UIView { + + override public init(frame: CGRect) { + super.init(frame: frame) + createView(frame: frame) + } + + required public init?(coder: NSCoder) { + fatalError() + } + + open func createView(frame: CGRect) { } +} + +extension UILabel { + + public convenience init(_ text: String) { + self.init(frame: .zero) + self.text = text + } +} diff --git a/Sources/UIViewKitPreviewsDemo/HelloWordView.swift b/Sources/UIViewKitPreviewsDemo/HelloWordView.swift index 165fb3f..9310ba2 100644 --- a/Sources/UIViewKitPreviewsDemo/HelloWordView.swift +++ b/Sources/UIViewKitPreviewsDemo/HelloWordView.swift @@ -27,12 +27,9 @@ public class HelloWordView: UIView { #if DEBUG -import SwiftUI - -struct HelloWordViewPreviews: PreviewProvider { - static var previews: some View { - ViewPreview(HelloWordView()) - } +@available(iOS 17.0, *) +#Preview("Default") { + HelloWordView() } #endif diff --git a/Tests/UIViewKitTests/IBAttributesTests.swift b/Tests/UIViewKitTests/IBAttributesTests.swift new file mode 100644 index 0000000..2cdce99 --- /dev/null +++ b/Tests/UIViewKitTests/IBAttributesTests.swift @@ -0,0 +1,38 @@ +// +// IBAttributesTests.swift +// UIViewKit +// +// Created by Blazej SLEBODA on 26/10/2023. +// + +import XCTest +@testable import UIViewKit + + +@MainActor +class IBAttributesTests: XCTestCase { + + func testLabelText() throws { + _ = UIView() { + ViewWithLabel().ibAttributes { + print($0) + $0.font = .italicSystemFont(ofSize: 20) + Optional.none + } + } + } +} + +fileprivate class ViewWithLabel: IBView { + + var font: UIFont! + + override func createView(frame: CGRect) { + super.createView(frame: frame) + self { + UILabel().ibAttributes { + $0.font = font + } + } + } +} diff --git a/Tests/UIViewKitTests/IBConstraintsTests.swift b/Tests/UIViewKitTests/IBConstraintsTests.swift index b349d1a..3da6eb0 100644 --- a/Tests/UIViewKitTests/IBConstraintsTests.swift +++ b/Tests/UIViewKitTests/IBConstraintsTests.swift @@ -10,6 +10,97 @@ import XCTest @MainActor class ibConstraintsTests: XCTestCase { + + func testViewAll() throws { + let view = UIView() + let subview = UIView() + + view { + subview.ibAttributes { + $0.ibConstraints(to: view, guide: .view, anchors: .all) + } + } + + XCTAssertEqual(view.constraints.count, 4) + XCTAssertEqual(subview.constraints.count, 0) + + let firstConstraint = try XCTUnwrap(view.constraints[0]) + let secondConstraint = try XCTUnwrap(view.constraints[1]) + let thirdConstraint = try XCTUnwrap(view.constraints[2]) + let fourthConstraint = try XCTUnwrap(view.constraints[3]) + + try { + let firstConstraintFirstItem = try XCTUnwrap(firstConstraint.firstItem as? UIView) + let secondConstraintFirstItem = try XCTUnwrap(secondConstraint.firstItem as? UIView) + let thirdConstraintFirstItem = try XCTUnwrap(thirdConstraint.firstItem as? UIView) + let fourthConstraintFirstItem = try XCTUnwrap(fourthConstraint.firstItem as? UIView) + + XCTAssertEqual(firstConstraintFirstItem, subview) + XCTAssertEqual(secondConstraintFirstItem, subview) + XCTAssertEqual(thirdConstraintFirstItem, subview) + XCTAssertEqual(fourthConstraintFirstItem, subview) + }() + + try { + let firstConstraintSecondItem = try XCTUnwrap(firstConstraint.secondItem as? UIView) + let secondConstraintSecondItem = try XCTUnwrap(secondConstraint.secondItem as? UIView) + let thirdConstraintSecondItem = try XCTUnwrap(thirdConstraint.secondItem as? UIView) + let fourthConstraintSecondItem = try XCTUnwrap(fourthConstraint.secondItem as? UIView) + + XCTAssertEqual(firstConstraintSecondItem, view) + XCTAssertEqual(secondConstraintSecondItem, view) + XCTAssertEqual(thirdConstraintSecondItem, view) + XCTAssertEqual(fourthConstraintSecondItem, view) + }() + + try { + let firstConstraintFirstAttribute = try XCTUnwrap(firstConstraint.firstAttribute) + let secondConstraintFirstAttribute = try XCTUnwrap(secondConstraint.firstAttribute) + let thirdConstraintFirstAttribute = try XCTUnwrap(thirdConstraint.firstAttribute) + let fourthConstraintFirstAttribute = try XCTUnwrap(fourthConstraint.firstAttribute) + + XCTAssertEqual(firstConstraintFirstAttribute.rawValue, 3) + XCTAssertEqual(secondConstraintFirstAttribute.rawValue, 4) + XCTAssertEqual(thirdConstraintFirstAttribute.rawValue, 1) + XCTAssertEqual(fourthConstraintFirstAttribute.rawValue, 2) + }() + + try { + let firstConstraintSecondAttribute = try XCTUnwrap(firstConstraint.secondAttribute) + let secondConstraintSecondAttribute = try XCTUnwrap(secondConstraint.secondAttribute) + let thirdConstraintSecondAttribute = try XCTUnwrap(thirdConstraint.secondAttribute) + let fourthConstraintSecondAttribute = try XCTUnwrap(fourthConstraint.secondAttribute) + + XCTAssertEqual(firstConstraintSecondAttribute.rawValue, 3) + XCTAssertEqual(secondConstraintSecondAttribute.rawValue, 4) + XCTAssertEqual(thirdConstraintSecondAttribute.rawValue, 1) + XCTAssertEqual(fourthConstraintSecondAttribute.rawValue, 2) + }() + + try { + let firstConstraintSecondAnchor = try XCTUnwrap(firstConstraint.secondAnchor) + let secondConstraintSecondAnchor = try XCTUnwrap(secondConstraint.secondAnchor) + let thirdConstraintSecondAnchor = try XCTUnwrap(thirdConstraint.secondAnchor) + let fourthConstraintSecondAnchor = try XCTUnwrap(fourthConstraint.secondAnchor) + + XCTAssertEqual(firstConstraintSecondAnchor, view.topAnchor) + XCTAssertEqual(secondConstraintSecondAnchor, view.bottomAnchor) + XCTAssertEqual(thirdConstraintSecondAnchor, view.leftAnchor) + XCTAssertEqual(fourthConstraintSecondAnchor, view.rightAnchor) + }() + + try { + let firstConstraintSecondAnchor = try XCTUnwrap(firstConstraint.secondAnchor) + let secondConstraintSecondAnchor = try XCTUnwrap(secondConstraint.secondAnchor) + let thirdConstraintSecondAnchor = try XCTUnwrap(thirdConstraint.secondAnchor) + let fourthConstraintSecondAnchor = try XCTUnwrap(fourthConstraint.secondAnchor) + + XCTAssertEqual(firstConstraintSecondAnchor, view.topAnchor) + XCTAssertEqual(secondConstraintSecondAnchor, view.bottomAnchor) + XCTAssertEqual(thirdConstraintSecondAnchor, view.leftAnchor) + XCTAssertEqual(fourthConstraintSecondAnchor, view.rightAnchor) + }() + } func testRunDifferentConfigurations() throws { let view = UIView() @@ -28,5 +119,7 @@ class ibConstraintsTests: XCTestCase { $0.ibConstraints(to: view, guide: .viewSafeArea, anchors: .all) } } + + XCTAssertEqual(view.constraints.count, 40) } } diff --git a/Tests/UIViewKitTests/IBSubviewsTests.swift b/Tests/UIViewKitTests/IBSubviewsTests.swift index 291bc15..0ee2a0e 100644 --- a/Tests/UIViewKitTests/IBSubviewsTests.swift +++ b/Tests/UIViewKitTests/IBSubviewsTests.swift @@ -63,5 +63,14 @@ class IBSubviewsTests: XCTestCase { UILabel() } } - + + func testIBSubviewsWithForEach() throws { + let subviews = [ + UILabel(), + UIView() + ] + _ = UIStackView() { + subviews.filter { _ = $0; return true } + } + } }