Skip to content

Commit

Permalink
Merge pull request #171 from 0xLeif/develop
Browse files Browse the repository at this point in the history
2.2.0
  • Loading branch information
0xLeif authored Sep 2, 2020
2 parents fc3c90a + 54733f4 commit 8ff6c45
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 47 deletions.
27 changes: 12 additions & 15 deletions Sources/SwiftUIKit/Containers/HStack.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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?
Expand All @@ -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) {
Expand All @@ -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
}
}
27 changes: 13 additions & 14 deletions Sources/SwiftUIKit/Containers/VStack.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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?
Expand All @@ -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) {
Expand All @@ -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
}
}
14 changes: 14 additions & 0 deletions Sources/SwiftUIKit/Views/ContractView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,20 @@ import Later
public class ContractView<View, Value>: UIView where View: UIView {
public var contract: Contract<Value>?

public init(_ closure: (UIView) -> Contract<Value>) {
super.init(frame: .zero)

let view = UIView()

self.contract = closure(view)

embed {
view
}

contract?.value = contract?.value
}

public init(view: View, _ closure: (View) -> Contract<Value>) {
super.init(frame: .zero)

Expand Down
36 changes: 18 additions & 18 deletions Tests/SwiftUIKitTests/Core/BasicSwiftUIKitTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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() {
Expand All @@ -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() {
Expand All @@ -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() {
Expand All @@ -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() {
Expand All @@ -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() {
Expand Down

0 comments on commit 8ff6c45

Please sign in to comment.