StanfordSpezi / Spezi

Open-source framework for rapid development of modern, interoperable digital health applications.
https://swiftpackageindex.com/StanfordSpezi/Spezi/documentation
MIT License
130 stars 10 forks source link

Support the Component/Module Creation for Xcode Previews #63

Closed PSchmiedmayer closed 6 months ago

PSchmiedmayer commented 1 year ago

Problem

Using some code in a SwiftUI view that, e.g., uses an @EnvironmentObject injected by Spezi requires the manual annotation in the SwiftUI preview to inject this object:

struct ScheduleView: View {
    @EnvironmentObject var scheduler: TemplateApplicationScheduler

    var body: some View {
        // ...
    }
}

struct SchedulerView_Previews: PreviewProvider {
    static var previews: some View {
        ScheduleView()
            .environmentObject(TemplateApplicationScheduler())
    }
}

While this is possible for simple components with no @Dependency-annotated components, injecting more complex components that require the injection of dependencies makes this difficult.

Solution

We propose the creation of a .previewComponent(...) or .previewComponents(...) view modifier that injects a specified amount of components in the environment.

The modifier would also require the definition of the associated standard or any generic requirement that are imposed on a component.

Additional context

Input on the design of this preview injection mechanism is greatly welcome. Please leave your thoughts and comments under this issue.

Code of Conduct

Supereg commented 6 months ago

Completed as of #95