Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support the Component/Module Creation for Xcode Previews #63

Closed
1 task done
PSchmiedmayer opened this issue Jun 12, 2023 · 1 comment
Closed
1 task done

Support the Component/Module Creation for Xcode Previews #63

PSchmiedmayer opened this issue Jun 12, 2023 · 1 comment
Labels
enhancement New feature or request

Comments

@PSchmiedmayer
Copy link
Member

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

  • I agree to follow this project's Code of Conduct and Contributing Guidelines
@PSchmiedmayer PSchmiedmayer added the enhancement New feature or request label Jun 12, 2023
Supereg added a commit that referenced this issue Jan 9, 2024
# Improve support for Previews and Unit Testing

## ♻️ Current situation & Problem
As outlined in #63, there is a need to more easily configure the Spezi
framework within SwiftUI Previews. This currently is not easily
possible, as we require an `SpeziAppDelegate` to be configured to
support infrastructure like the `LifecycleHandler` protocol. This is not
available for Previews.

This PR adds two new shorthand `previewWith(simulateLifecycle:_:)` and
`previewWith(standard:simulateLifecycle:_:)` modifiers that are targeted
for preview usage only. As documented, `LifecycleHandler` are not fully
supported with this configuration due to the missing App delegate.
However, there is an option to partially simulate its behavior.

Further, #64 illustrates difficulties to unit test Spezi `Modules` due
to missing abilities to resolve Module dependencies without initializing
a SwiftUI App. This PR adds first-hand support for this scenario using
the methods `withDependencyResolution(simulateLifecycle:_:)` and
`withDependencyResolution(standard:simulateLifecycle:_:)` in the
`XCTSpezi` package.

## :gear: Release Notes 
* Add new `previewWith(simulateLifecycle:_:)` and
`previewWith(standard:simulateLifecycle:_:)` modifiers to be used within
SwiftUI Previews.
* Add new `withDependencyResolution(simulateLifecycle:_:)` and
`withDependencyResolution(standard:simulateLifecycle:_:)` methods to
support unit testing Spezi Modules.


## :books: Documentation
Documentation was added to illustrate that these modifiers should only
be used within Previews. Further, they are placed in a dedicated
`Previews` section in the DocC landing page.

New DocC bundle was added for the XCTSpezi target.


## :white_check_mark: Testing
Several test cases were added to support this new functionality.


## :pencil: Code of Conduct & Contributing Guidelines 

By submitting creating this pull request, you agree to follow our [Code
of
Conduct](https://github.com/StanfordSpezi/.github/blob/main/CODE_OF_CONDUCT.md)
and [Contributing
Guidelines](https://github.com/StanfordSpezi/.github/blob/main/CONTRIBUTING.md):
- [x] I agree to follow the [Code of
Conduct](https://github.com/StanfordSpezi/.github/blob/main/CODE_OF_CONDUCT.md)
and [Contributing
Guidelines](https://github.com/StanfordSpezi/.github/blob/main/CONTRIBUTING.md).
@Supereg
Copy link
Member

Supereg commented Jan 10, 2024

Completed as of #95

@Supereg Supereg closed this as completed Jan 10, 2024
@github-project-automation github-project-automation bot moved this from Backlog to Done in Project Planning Jan 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Archived in project
Development

No branches or pull requests

2 participants