Prefire icon indicating copy to clipboard operation
Prefire copied to clipboard

#Preview only supports SwiftUI and not other view types

Open aduuub opened this issue 1 year ago • 0 comments

Context 🕵️‍♀️

As per Apple's documentation - https://developer.apple.com/documentation/xcode/previewing-your-apps-interface-in-xcode #Preview supports other forms of views such as UIViewController, NSView, UIView. Right now, unless you exclude those the generated tests from the plugin won't compile.

What 🌱

It would be amazing if we can add support for more view types other than just SwiftUI views.

Proposal 🎉

In the PreviewLoader, it assumes that it's a SwiftUI view and always wraps it in AnyView

I wonder if we could add another function assertSnapshots to the stencil that takes in the other view types like:

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

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

And then instead of always casting to AnyView, use a more generic any View parameter for the assertSnapshots function and then allow it to dynamically call the appropriate function based on the classes implementation type?

Happy to help further with an implementation but I think this would be a great addition!

aduuub avatar May 29 '24 23:05 aduuub