Skip to content

Commit

Permalink
Added UIKit support to the #Preview macro
Browse files Browse the repository at this point in the history
  • Loading branch information
BarredEwe committed Aug 22, 2024
1 parent c56e6a0 commit 857d06f
Show file tree
Hide file tree
Showing 11 changed files with 92 additions and 13 deletions.
4 changes: 2 additions & 2 deletions Binaries/PrefireBinary.artifactbundle/info.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
"artifacts": {
"PrefireBinary": {
"type": "executable",
"version": "2.8.0",
"version": "2.9.0",
"variants": [
{
"path": "prefire-2.8.0-macos/bin/prefire",
"path": "prefire-2.9.0-macos/bin/prefire",
"supportedTriples": ["x86_64-apple-macosx", "arm64-apple-macosx"]
},
]
Expand Down
Binary file not shown.
Binary file not shown.
4 changes: 4 additions & 0 deletions Example/PreFireExample.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
9735EE49289C5F3A00309267 /* TestView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9735EE48289C5F3A00309267 /* TestView.swift */; };
9777F17528D0F4200072827B /* CicrleImage.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9777F17428D0F4200072827B /* CicrleImage.swift */; };
9779D5F428A3F13F0083DBD3 /* SnapshotTesting in Frameworks */ = {isa = PBXBuildFile; productRef = 9779D5F328A3F13F0083DBD3 /* SnapshotTesting */; };
978DDB192C775F5300F45D45 /* UIKitView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 978DDB182C775F5300F45D45 /* UIKitView.swift */; };
97B1477828EA335A0014DEC5 /* AuthView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 97B1477728EA335A0014DEC5 /* AuthView.swift */; };
97B1477A28EA3CF60014DEC5 /* PrefireView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 97B1477928EA3CF60014DEC5 /* PrefireView.swift */; };
97BA130128A1AAE800CA8F36 /* MainMenu.swift in Sources */ = {isa = PBXBuildFile; fileRef = 97BA130028A1AAE800CA8F36 /* MainMenu.swift */; };
Expand All @@ -38,6 +39,7 @@
9735EE48289C5F3A00309267 /* TestView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestView.swift; sourceTree = "<group>"; };
9777F17428D0F4200072827B /* CicrleImage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CicrleImage.swift; sourceTree = "<group>"; };
9779D5E628A3ECBA0083DBD3 /* PrefireExampleTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = PrefireExampleTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
978DDB182C775F5300F45D45 /* UIKitView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UIKitView.swift; sourceTree = "<group>"; };
97AB804128D9F0DB00414704 /* Prefire */ = {isa = PBXFileReference; lastKnownFileType = wrapper; name = Prefire; path = ..; sourceTree = "<group>"; };
97B1477728EA335A0014DEC5 /* AuthView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AuthView.swift; sourceTree = "<group>"; };
97B1477928EA3CF60014DEC5 /* PrefireView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PrefireView.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -103,6 +105,7 @@
9735EE48289C5F3A00309267 /* TestView.swift */,
9777F17428D0F4200072827B /* CicrleImage.swift */,
97B1477728EA335A0014DEC5 /* AuthView.swift */,
978DDB182C775F5300F45D45 /* UIKitView.swift */,
);
path = Examples;
sourceTree = "<group>";
Expand Down Expand Up @@ -247,6 +250,7 @@
97BA130128A1AAE800CA8F36 /* MainMenu.swift in Sources */,
9735EE49289C5F3A00309267 /* TestView.swift in Sources */,
97B1477828EA335A0014DEC5 /* AuthView.swift in Sources */,
978DDB192C775F5300F45D45 /* UIKitView.swift in Sources */,
9777F17528D0F4200072827B /* CicrleImage.swift in Sources */,
9735EE38289C5CF300309267 /* PrefireExampleApp.swift in Sources */,
);
Expand Down
18 changes: 18 additions & 0 deletions Example/Shared/Examples/UIKitView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import SwiftUI
import Prefire

#Preview {
{
let view = UIView(frame: .init(origin: .zero, size: .init(width: 100, height: 100)))
view.backgroundColor = .red
return view
}()
}

#Preview {
{
let viewController = UIViewController()
viewController.view.backgroundColor = .green
return viewController
}()
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ extension Prefire {
struct Version: ParsableCommand {
static let configuration = CommandConfiguration(abstract: "Display the current version of Prefire")

static var value: String = "2.8.0"
static var value: String = "2.9.0"

func run() throws {
print(Self.value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ extension PreviewLoader {
let preview = {
\(rawPreviewModel.body)
}
if let failure = assertSnapshots(matching: AnyView(preview()), name: "\(rawPreviewModel.displayName)", isScreen: \(isScreen), device: deviceConfig) {
if let failure = assertSnapshots(matching: preview(), name: "\(rawPreviewModel.displayName)", isScreen: \(isScreen), device: deviceConfig) {

This comment has been minimized.

Copy link
@markst

markst Sep 2, 2024

Contributor

ah this seems to break #38 since it uses name rather than testName

XCTFail(failure)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@ extension RawPreviewModel {
"""
PreviewModel(
content: {
AnyView(
\(body)
)
},
name: \"\(displayName)\",
type: \(traits == ".device" ? ".screen" : ".component"),
Expand Down
23 changes: 17 additions & 6 deletions Sources/Prefire/Preview/PreviewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,25 +53,36 @@ public struct PreviewModel: Identifiable {
public var renderTime: String?

public init(
id: String,
content: @escaping () -> AnyView,
id: String? = nil,
content: @escaping () -> any View,
name: String,
type: LayoutType = .component,
device: PreviewDevice?
) {
self.id = id
self.content = content
self.id = id ?? name + String(describing: content.self)
self.content = { AnyView(content()) }
self.name = name
self.type = type
self.device = device
}

public init(
content: @escaping () -> AnyView,
id: String? = nil,
content: @escaping () -> UIView,
name: String,
type: LayoutType = .component,
device: PreviewDevice?
) {
self.init(id: name + String(describing: content()), content: content, name: name, type: type, device: device)
self.init(id: id, content: { AnyView(ViewRepresentable(view: content())) }, name: name, type: type, device: device)
}

public init(
id: String? = nil,
content: @escaping () -> UIViewController,
name: String,
type: LayoutType = .component,
device: PreviewDevice?
) {
self.init(id: id, content: { AnyView(ViewControllerRepresentable(viewController: content())) }, name: name, type: type, device: device)
}
}
32 changes: 32 additions & 0 deletions Sources/Prefire/Preview/UIKitRepresentable.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import SwiftUI
import UIKit

public struct ViewRepresentable<WrappedView: UIView>: UIViewRepresentable {
let view: WrappedView

public init(view: WrappedView) {
self.view = view
}

public func makeUIView(context: Context) -> WrappedView {
view.setContentHuggingPriority(.defaultHigh, for: .vertical)
view.setContentHuggingPriority(.defaultHigh, for: .horizontal)
return view
}

public func updateUIView(_ uiView: WrappedView, context: Context) { }
}

public struct ViewControllerRepresentable<WrappedViewController: UIViewController>: UIViewControllerRepresentable {
let viewController: WrappedViewController

public init(viewController: WrappedViewController) {
self.viewController = viewController
}

public func makeUIViewController(context: Context) -> WrappedViewController {
return viewController
}

public func updateUIViewController(_ uiViewController: WrappedViewController, context: Context) { }
}
18 changes: 17 additions & 1 deletion Templates/PreviewTests.stencil
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,23 @@ class PreviewTests: XCTestCase {
}
}

private func assertSnapshots(matching view: AnyView,
private func assertSnapshots(matching view: UIView,
name: String?, isScreen: Bool,
device: ViewImageConfig,
testName: String = #function,
traits: UITraitCollection = .init()) -> String? {
assertSnapshots(matching: ViewRepresentable(view: view), name: name, isScreen: isScreen, device: device, testName: testName, traits: traits)
}

private func assertSnapshots(matching view: UIViewController,
name: String?, isScreen: Bool,
device: ViewImageConfig,
testName: String = #function,
traits: UITraitCollection = .init()) -> String? {
assertSnapshots(matching: ViewControllerRepresentable(viewController: view), name: name, isScreen: isScreen, device: device, testName: testName, traits: traits)
}

private func assertSnapshots(matching view: any SwiftUI.View,
name: String?, isScreen: Bool,
device: ViewImageConfig,
testName: String = #function,
Expand Down

0 comments on commit 857d06f

Please sign in to comment.