Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 5 additions & 8 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,11 @@ let package = Package(
products: [
.library(name: "UIViewKit", targets: ["UIViewKit"])
],
dependencies: [
.package(url: "https://github.com/Adobels/CodeCallTracker.git", revision: "1d27da6706466a5b83bdb0f4097fc86f678b146f")
],
targets: [
.target(
name: "UIViewKit"
),
.testTarget(
name: "UIViewKitTests",
dependencies: [
"UIViewKit"
])
.target(name: "UIViewKit", dependencies: ["CodeCallTracker"]),
.testTarget(name: "UIViewKitTests", dependencies: ["UIViewKit"])
]
)
114 changes: 74 additions & 40 deletions Sources/UIViewKit/IBPreviews/IBPreviewFreeForm.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,61 @@ public class IBPreviewFreeForm: ViewControllerFreeFormContainer {
fatalError()
}

public init(snapFrames: SnapFrame..., view: UIView) {
super.init(snapFrames: snapFrames)
self.viewMaker = { view }
}

public init(view: UIView) {
super.init(nibName: nil, bundle: nil)
self.viewMaker = { view }
}

public init(snapFrames: SnapFrame..., viewMaker: @escaping () -> UIView) {
super.init(snapFrames: snapFrames)
self.viewMaker = viewMaker
}

public init(_ viewMaker: @escaping () -> UIView) {
super.init(nibName: nil, bundle: nil)
self.viewMaker = viewMaker
}

public init(snapFrames: SnapFrame..., viewController: UIViewController) {
super.init(snapFrames: snapFrames)
self.viewControllerMaker = { viewController }
}

public init(viewController: UIViewController) {
super.init(nibName: nil, bundle: nil)
self.viewControllerMaker = { viewController }
}

public init(snapFrames: SnapFrame..., viewControllerMaker: @escaping () -> UIViewController) {
super.init(snapFrames: snapFrames)
self.viewControllerMaker = viewControllerMaker
}

public init(_ viewControllerMaker: @escaping () -> UIViewController) {
super.init(nibName: nil, bundle: nil)
self.viewControllerMaker = viewControllerMaker
}

public init(snapFrames: SnapFrame..., view: some View) {
super.init(snapFrames: snapFrames)
self.viewControllerMaker = { UIHostingController(rootView: view) }
}

public init(view: some View) {
super.init(nibName: nil, bundle: nil)
self.viewControllerMaker = { UIHostingController(rootView: view) }
}

public init(snapFrames: SnapFrame..., viewMaker: @escaping () -> some View) {
super.init(snapFrames: snapFrames )
self.viewControllerMaker = { UIHostingController(rootView: viewMaker()) }
}

public init(_ viewMaker: @escaping () -> some View) {
super.init(nibName: nil, bundle: nil)
self.viewControllerMaker = { UIHostingController(rootView: viewMaker()) }
Expand Down Expand Up @@ -75,52 +105,60 @@ public class IBPreviewFreeForm: ViewControllerFreeFormContainer {

}

extension IBPreviewFreeForm {

public protocol SnapFrame {
var size: CGSize { get }
var name: String { get }
var color: UIColor { get }
var borderWidth: CGFloat { get }
}

}

public class ViewControllerFreeFormContainer: UIViewController {

var snapFrames: [IBPreviewFreeForm.SnapFrame] = []
var deviceWiteframesViews: [UIView] = []
var containerView: UIView!
var heightConstraint: NSLayoutConstraint!
var widthConstraint: NSLayoutConstraint!
private var iPhoneSE2FrameView: UIView!
private var iPhoneSE2WithKeyboardFrameView: UIView!
private var snapToViewFeature: SnapToViewFeature!
private let iPhoneSE2Frame = CGRect(origin: .zero, size: .init(width: 375, height: 667))
private let iPhoneSE2WithKeyboardFrame = CGRect(origin: .zero, size: .init(width: 375, height: 451))

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

public override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
}

init(snapFrames: [IBPreviewFreeForm.SnapFrame]) {
self.snapFrames = snapFrames
super.init(nibName: nil, bundle: nil)
}

public override func loadView() {
super.loadView()
view.backgroundColor = .lightGray
view.ibSubviews { superview in
UIView().ibOutlet(&iPhoneSE2FrameView).ibSubviews { superview in
UILabel().ibAttributes {
$0.ibConstraints(to: superview, guide: .view, anchors: .left, .bottom, .right)
$0.text = "iPhone SE2"
$0.textAlignment = .center
$0.textColor = .cyan
snapFrames.forEach { deviceWireframe in
view.ibSubviews {
UIView().ibSubviews { labelSuperview in
UILabel().ibAttributes {
$0.ibConstraints(to: labelSuperview, guide: .view, anchors: .left, .bottom, .right)
$0.text = deviceWireframe.name
$0.textAlignment = .center
$0.textColor = deviceWireframe.color
}
}.ibAttributes {
$0.topAnchor.constraint(equalTo: view.topAnchor)
$0.leadingAnchor.constraint(equalTo: view.leadingAnchor)
$0.widthAnchor.constraint(equalToConstant: deviceWireframe.size.width)
$0.heightAnchor.constraint(equalToConstant: deviceWireframe.size.height)
$0.layer.borderColor = deviceWireframe.color.cgColor
$0.layer.borderWidth = 2
deviceWiteframesViews.append($0)
}
}.ibAttributes {
$0.topAnchor.constraint(equalTo: superview.topAnchor)
$0.leadingAnchor.constraint(equalTo: superview.leadingAnchor)
$0.widthAnchor.constraint(equalToConstant: iPhoneSE2Frame.width)
$0.heightAnchor.constraint(equalToConstant: iPhoneSE2Frame.height)
$0.layer.borderColor = UIColor.cyan.cgColor
$0.layer.borderWidth = 2
}
}
view.ibSubviews { superview in
UIView().ibOutlet(&iPhoneSE2WithKeyboardFrameView).ibSubviews { superview in
UILabel().ibAttributes {
$0.ibConstraints(to: superview, guide: .view, anchors: .left, .bottom, .right)
$0.text = "iPhone SE2 With Keyboard"
$0.textAlignment = .center
$0.textColor = UIColor.yellow
}
}.ibAttributes {
$0.topAnchor.constraint(equalTo: superview.topAnchor)
$0.leadingAnchor.constraint(equalTo: superview.leadingAnchor)
$0.widthAnchor.constraint(equalToConstant: iPhoneSE2WithKeyboardFrame.width)
$0.heightAnchor.constraint(equalToConstant: iPhoneSE2WithKeyboardFrame.height)
$0.layer.borderColor = UIColor.yellow.cgColor
$0.layer.borderWidth = 2
}
}
view.ibSubviews { superview in
Expand All @@ -132,11 +170,7 @@ public class ViewControllerFreeFormContainer: UIViewController {
}
}
snapToViewFeature = .init(
viewToSnap: [
iPhoneSE2WithKeyboardFrameView,
iPhoneSE2FrameView,
view,
],
viewToSnap: deviceWiteframesViews + [view],
containerView: containerView,
controller: self
)
Expand Down
21 changes: 17 additions & 4 deletions Sources/UIViewKit/IBPreviews/IBRepresentables.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,22 @@
import UIKit
import SwiftUI

@available(iOS, introduced: 13, obsoleted: 17)
public struct IBRepresentableViewController: UIViewControllerRepresentable {
public func IBPreviewRepresentable(view: UIView) -> some UIViewRepresentable {
IBRepresentableView(view)
}

public func IBPreviewRepresentable(_ viewMaker: @escaping () -> UIView) -> some UIViewRepresentable {
IBRepresentableView(viewMaker)
}
public func IBPreviewRepresentable(viewController: UIViewController) -> some UIViewControllerRepresentable {
IBRepresentableViewController(viewController)
}

public func IBPreviewRepresentable(_ viewControllerMaker: @escaping () -> UIViewController) -> some UIViewControllerRepresentable {
IBRepresentableViewController(viewControllerMaker)
}

private struct IBRepresentableViewController: UIViewControllerRepresentable {

public typealias UIViewControllerType = UIViewController

Expand All @@ -30,8 +44,7 @@ public struct IBRepresentableViewController: UIViewControllerRepresentable {
public func updateUIViewController(_ uiViewController: UIViewController, context: Context) { }
}

@available(iOS, introduced: 13, obsoleted: 17)
public struct IBRepresentableView: UIViewRepresentable {
private struct IBRepresentableView: UIViewRepresentable {

public typealias UIViewType = UIView

Expand Down
1 change: 1 addition & 0 deletions Sources/UIViewKit/UIViewKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@

@_exported import UIKit
@_exported import SwiftUI
@_exported import CodeCallTracker