Skip to content

Commit

Permalink
Merge pull request #165 from 0xLeif/develop
Browse files Browse the repository at this point in the history
Added Later (v2.0.0)
  • Loading branch information
0xLeif authored Aug 27, 2020
2 parents b0f6b26 + 374328e commit 26c493d
Show file tree
Hide file tree
Showing 18 changed files with 406 additions and 142 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/swift.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ jobs:
strategy:
matrix:
destination: ['platform=iOS Simulator,OS=13.1,name=iPhone 8']
xcode: ['/Applications/Xcode_11.1.app/Contents/Developer']
xcode: ['/Applications/Xcode_11.6.app/Contents/Developer']
steps:
- uses: actions/checkout@v1
# Github Actions' machines do in fact have recent versions of Xcode,
# but you may have to explicitly switch to them. We explicitly want
# to use Xcode 11, so we use xcode-select to switch to it.
- name: Switch to Xcode 11
run: sudo xcode-select --switch /Applications/Xcode_11.1.app
run: sudo xcode-select --switch /Applications/Xcode_11.6.app
# Since we want to be running our tests from Xcode, we need to
# generate an .xcodeproj file. Luckly, Swift Package Manager has
# build in functionality to do so.
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
/*.xcodeproj
xcuserdata/
build
*.resolved
5 changes: 4 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@ let package = Package(
dependencies: [
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"),
.package(url: "https://github.com/0xLeif/Later", from: "0.3.0")
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
.target(
name: "SwiftUIKit",
dependencies: []),
dependencies: [
"Later"
]),
.testTarget(
name: "SwiftUIKitTests",
dependencies: ["SwiftUIKit"]),
Expand Down
53 changes: 37 additions & 16 deletions Sources/SwiftUIKit/Containers/HStack.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,26 @@
//

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 var views: [UIView] = []
public lazy var views = Contract<[UIView]>(initialValue: [])
.onChange { [weak self] (views) in
Later.main {
self?.draw(views: views ?? [])
}
}

/// Create a HStack
/// - Parameters:
Expand All @@ -25,14 +39,13 @@ public class HStack: UIView {
alignment: UIStackView.Alignment = .fill,
distribution: UIStackView.Distribution = .fill,
_ closure: () -> [UIView]) {
views = closure()
self.spacing = spacing
self.padding = padding
self.alignment = alignment
self.distribution = distribution
super.init(frame: .zero)

hstack(withSpacing: spacing,
padding: padding,
alignment: alignment,
distribution: distribution,
closure)
views.value = closure()
draw(views: views.value ?? [])
}

/// Create a HStack that accepts an array of UIView?
Expand All @@ -47,18 +60,26 @@ public class HStack: UIView {
alignment: UIStackView.Alignment = .fill,
distribution: UIStackView.Distribution = .fill,
_ closure: () -> [UIView?]) {
views = closure()
.compactMap { $0 }
self.spacing = spacing
self.padding = padding
self.alignment = alignment
self.distribution = distribution
super.init(frame: .zero)

hstack(withSpacing: spacing,
padding: padding,
alignment: alignment,
distribution: distribution)
{ views }
views.value = closure()
.compactMap { $0 }
draw(views: views.value ?? [])
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

internal func draw(views: [UIView]) {
clear()
.hstack(withSpacing: spacing,
padding: padding,
alignment: alignment,
distribution: distribution)
{ views }
}
}
53 changes: 37 additions & 16 deletions Sources/SwiftUIKit/Containers/VStack.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,26 @@
//

import UIKit
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 var views: [UIView] = []
public lazy var views = Contract<[UIView]>()
.onChange { [weak self] (views) in
Later.main {
self?.draw(views: views ?? [])
}
}

/// Create a VStack
/// - Parameters:
Expand All @@ -25,14 +39,13 @@ public class VStack: UIView {
alignment: UIStackView.Alignment = .fill,
distribution: UIStackView.Distribution = .fill,
_ closure: () -> [UIView]) {
views = closure()
self.spacing = spacing
self.padding = padding
self.alignment = alignment
self.distribution = distribution
super.init(frame: .zero)

vstack(withSpacing: spacing,
padding: padding,
alignment: alignment,
distribution: distribution,
closure)
views.value = closure()
draw(views: views.value ?? [])
}

/// Create a VStack that accepts an array of UIView?
Expand All @@ -47,18 +60,26 @@ public class VStack: UIView {
alignment: UIStackView.Alignment = .fill,
distribution: UIStackView.Distribution = .fill,
_ closure: () -> [UIView?]) {
views = closure()
.compactMap { $0 }
self.spacing = spacing
self.padding = padding
self.alignment = alignment
self.distribution = distribution
super.init(frame: .zero)

vstack(withSpacing: spacing,
padding: padding,
alignment: alignment,
distribution: distribution)
{ views }
views.value = closure()
.compactMap { $0 }
draw(views: views.value ?? [])
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

internal func draw(views: [UIView]) {
clear()
.vstack(withSpacing: spacing,
padding: padding,
alignment: alignment,
distribution: distribution)
{ views }
}
}
18 changes: 18 additions & 0 deletions Sources/SwiftUIKit/Extensions/UIAppearance+SwiftUIKit.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//
// UIAppearance+SwiftUIKit.swift
//
//
// Created by Zach Eriksen on 8/25/20.
//

import UIKit
import Later

@available(iOS 9.0, *)
public extension UIAppearance where Self: UIView {
func contract<Value>(_ closure: (Self) -> Contract<Value>) -> ContractView<Self, Value> {
ContractView(view: self) { view in
closure(view)
}
}
}
24 changes: 24 additions & 0 deletions Sources/SwiftUIKit/Extensions/UILabel+SwiftUIKit.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// UILabel+SwiftUIKit.swift
// SwiftUIKit
//
// Created by Zach Eriksen on 7/7/20.
//

import UIKit

public extension UILabel {
@discardableResult
func text(color: UIColor?) -> Self {
textColor = color

return self
}

@discardableResult
func text(_ value: String?) -> Self {
text = value

return self
}
}
7 changes: 7 additions & 0 deletions Sources/SwiftUIKit/Extensions/UITextField+SwiftUIKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,11 @@ public extension UITextField {

return self
}

@discardableResult
func text(_ value: String?) -> Self {
text = value

return self
}
}
7 changes: 7 additions & 0 deletions Sources/SwiftUIKit/Extensions/UITextView+SwiftUIKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ public extension UITextView {
return self
}

@discardableResult
func text(_ value: String?) -> Self {
text = value

return self
}

@discardableResult
func tint(color: UIColor?) -> Self {
tintColor = color
Expand Down
87 changes: 87 additions & 0 deletions Sources/SwiftUIKit/Extensions/UIView+Later.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
//
// UIView+Later.swift
// SwiftUIKit
//
// Created by Zach Eriksen on 8/26/20.
//

import UIKit
import Later

@available(iOS 9.0, *)
public extension UIView {
static func later(_ laterView: (Later.Type) -> LaterValue<UIView>) -> UIView {
let view = UIView()

view.center {
LoadingView()
.start()
}

laterView(Later.self)
.whenSuccess { embeddedView in
Later.main {
view.clear()
.embed {
embeddedView
}
}

}

return view
}

static func later(centeredLoadingView: UIView,
withSize size: CGSize? = nil,
_ laterView: (Later.Type) -> LaterValue<UIView>) -> UIView {

let view = UIView()

view.center {
centeredLoadingView
.configure {
if let size = size {
$0.frame(height: Float(size.height),
width: Float(size.width))
}
}
}

laterView(Later.self)
.whenSuccess { embeddedView in
Later.main {
view.clear()
.embed {
embeddedView
}
}

}

return view
}

static func later(embeddedLoadingView: UIView,
withPadding padding: Float = 0,
_ laterView: (Later.Type) -> LaterValue<UIView>) -> UIView {
let view = UIView()

view.embed(withPadding: padding) {
embeddedLoadingView
}

laterView(Later.self)
.whenSuccess { embeddedView in
Later.main {
view.clear()
.embed {
embeddedView
}
}

}

return view
}
}
Loading

0 comments on commit 26c493d

Please sign in to comment.