From da0b4be94d9e158c3cbd45f4514643adaa3a22b2 Mon Sep 17 00:00:00 2001 From: Zach Eriksen Date: Tue, 1 Sep 2020 13:21:20 -0500 Subject: [PATCH 1/2] Add a default UIView init for ContractView --- Sources/SwiftUIKit/Views/ContractView.swift | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/Sources/SwiftUIKit/Views/ContractView.swift b/Sources/SwiftUIKit/Views/ContractView.swift index 99885af..96d71be 100644 --- a/Sources/SwiftUIKit/Views/ContractView.swift +++ b/Sources/SwiftUIKit/Views/ContractView.swift @@ -12,6 +12,20 @@ import Later public class ContractView: UIView where View: UIView { public var contract: Contract? + public init(_ closure: (UIView) -> Contract) { + super.init(frame: .zero) + + let view = UIView() + + self.contract = closure(view) + + embed { + view + } + + contract?.value = contract?.value + } + public init(view: View, _ closure: (View) -> Contract) { super.init(frame: .zero) From cda8c5270dc5d8ce6cfd689a0d7c790dd76a1244 Mon Sep 17 00:00:00 2001 From: Zach Eriksen Date: Wed, 2 Sep 2020 08:24:29 -0500 Subject: [PATCH 2/2] Revert H/V Stack contract views --- Sources/SwiftUIKit/Containers/HStack.swift | 27 +++++++------- Sources/SwiftUIKit/Containers/VStack.swift | 27 +++++++------- .../Core/BasicSwiftUIKitTests.swift | 36 +++++++++---------- 3 files changed, 43 insertions(+), 47 deletions(-) diff --git a/Sources/SwiftUIKit/Containers/HStack.swift b/Sources/SwiftUIKit/Containers/HStack.swift index d887537..cfca621 100644 --- a/Sources/SwiftUIKit/Containers/HStack.swift +++ b/Sources/SwiftUIKit/Containers/HStack.swift @@ -6,26 +6,16 @@ // import UIKit -import Later /// Horizontal StackView @available(iOS 9.0, *) public class HStack: UIView { - deinit { - views.resign() - } - private var spacing: Float private var padding: Float private var alignment: UIStackView.Alignment private var distribution: UIStackView.Distribution /// The views that the HStack contains - public lazy var views = Contract<[UIView]>(initialValue: []) - .onChange { [weak self] (views) in - Later.main { - self?.draw(views: views ?? []) - } - } + private(set) var views = [UIView]() /// Create a HStack /// - Parameters: @@ -44,8 +34,8 @@ public class HStack: UIView { self.alignment = alignment self.distribution = distribution super.init(frame: .zero) - views.value = closure() - draw(views: views.value ?? []) + views = closure() + draw(views: views) } /// Create a HStack that accepts an array of UIView? @@ -65,9 +55,9 @@ public class HStack: UIView { self.alignment = alignment self.distribution = distribution super.init(frame: .zero) - views.value = closure() + views = closure() .compactMap { $0 } - draw(views: views.value ?? []) + draw(views: views) } required init?(coder aDecoder: NSCoder) { @@ -82,4 +72,11 @@ public class HStack: UIView { distribution: distribution) { views } } + + public func update(views closure: (inout [UIView]) -> Void) -> Self { + closure(&views) + draw(views: views) + + return self + } } diff --git a/Sources/SwiftUIKit/Containers/VStack.swift b/Sources/SwiftUIKit/Containers/VStack.swift index 54bb19a..6b9c62d 100644 --- a/Sources/SwiftUIKit/Containers/VStack.swift +++ b/Sources/SwiftUIKit/Containers/VStack.swift @@ -11,21 +11,12 @@ import Later /// Vertical StackView @available(iOS 9.0, *) public class VStack: UIView { - deinit { - views.resign() - } - private var spacing: Float private var padding: Float private var alignment: UIStackView.Alignment private var distribution: UIStackView.Distribution /// The views that the VStack contains - public lazy var views = Contract<[UIView]>() - .onChange { [weak self] (views) in - Later.main { - self?.draw(views: views ?? []) - } - } + private(set) var views = [UIView]() /// Create a VStack /// - Parameters: @@ -44,8 +35,8 @@ public class VStack: UIView { self.alignment = alignment self.distribution = distribution super.init(frame: .zero) - views.value = closure() - draw(views: views.value ?? []) + views = closure() + draw(views: views) } /// Create a VStack that accepts an array of UIView? @@ -65,9 +56,9 @@ public class VStack: UIView { self.alignment = alignment self.distribution = distribution super.init(frame: .zero) - views.value = closure() + views = closure() .compactMap { $0 } - draw(views: views.value ?? []) + draw(views: views) } required init?(coder aDecoder: NSCoder) { @@ -82,4 +73,12 @@ public class VStack: UIView { distribution: distribution) { views } } + + @discardableResult + public func update(views closure: (inout [UIView]) -> Void) -> Self { + closure(&views) + draw(views: views) + + return self + } } diff --git a/Tests/SwiftUIKitTests/Core/BasicSwiftUIKitTests.swift b/Tests/SwiftUIKitTests/Core/BasicSwiftUIKitTests.swift index dfa2698..88c2e85 100644 --- a/Tests/SwiftUIKitTests/Core/BasicSwiftUIKitTests.swift +++ b/Tests/SwiftUIKitTests/Core/BasicSwiftUIKitTests.swift @@ -209,7 +209,7 @@ final class BasicSwiftUIKitTests: XCTestCase { XCTAssert(stack.subviews.first.map { type(of: $0) } == UIStackView.self) XCTAssertEqual(stack.allSubviews.count, 2) - XCTAssertEqual(stack.views.value?.count, 1) + XCTAssertEqual(stack.views.count, 1) } func testVStackViewAppend_one() { @@ -222,13 +222,13 @@ final class BasicSwiftUIKitTests: XCTestCase { ] } - stack.views.value?.append(UIView()) - - stack.draw(views: stack.views.value ?? []) + stack.update { (views) in + views.append(UIView()) + } XCTAssert(stack.subviews.first.map { type(of: $0) } == UIStackView.self) XCTAssertEqual(stack.allSubviews.count, 3) - XCTAssertEqual(stack.views.value?.count, 2) + XCTAssertEqual(stack.views.count, 2) } func testVStackViewAppend_five() { @@ -241,13 +241,13 @@ final class BasicSwiftUIKitTests: XCTestCase { ] } - stack.views.value? += (0 ... 4).map { Label("\($0)") } - - stack.draw(views: stack.views.value ?? []) + stack.update { (views) in + views += (0 ... 4).map { Label("\($0)") } + } XCTAssert(stack.subviews.first.map { type(of: $0) } == UIStackView.self) XCTAssertEqual(stack.allSubviews.count, 7) - XCTAssertEqual(stack.views.value?.count, 6) + XCTAssertEqual(stack.views.count, 6) } func testHStackView() { @@ -262,7 +262,7 @@ final class BasicSwiftUIKitTests: XCTestCase { XCTAssert(stack.subviews.first.map { type(of: $0) } == UIStackView.self) XCTAssertEqual(stack.allSubviews.count, 2) - XCTAssertEqual(stack.views.value?.count, 1) + XCTAssertEqual(stack.views.count, 1) } func testHStackViewAppend_one() { @@ -275,13 +275,13 @@ final class BasicSwiftUIKitTests: XCTestCase { ] } - stack.views.value?.append(UIView()) - - stack.draw(views: stack.views.value ?? []) + stack.update { (views) in + views.append(UIView()) + } XCTAssert(stack.subviews.first.map { type(of: $0) } == UIStackView.self) XCTAssertEqual(stack.allSubviews.count, 3) - XCTAssertEqual(stack.views.value?.count, 2) + XCTAssertEqual(stack.views.count, 2) } func testHStackViewAppend_five() { @@ -293,13 +293,13 @@ final class BasicSwiftUIKitTests: XCTestCase { ] } - stack.views.value? += (0 ... 4).map { Label("\($0)") } - - stack.draw(views: stack.views.value ?? []) + stack.update { (views) in + views += (0 ... 4).map { Label("\($0)") } + } XCTAssert(stack.subviews.first.map { type(of: $0) } == UIStackView.self) XCTAssertEqual(stack.allSubviews.count, 7) - XCTAssertEqual(stack.views.value?.count, 6) + XCTAssertEqual(stack.views.count, 6) } func testZStackView() {