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

SwiftUI previews with multiple modules #200

Open
callo90 opened this issue Apr 8, 2024 · 3 comments
Open

SwiftUI previews with multiple modules #200

callo90 opened this issue Apr 8, 2024 · 3 comments

Comments

@callo90
Copy link

callo90 commented Apr 8, 2024

Hello,

I've been facing problems with the SwiftUI preview, I have an app that runs clean architecture in multiple modules, SwiftUI preview doesn't work properly running the app scheme, so I had to run specifically the UI module, this works properly in most of the cases but sometimes injecting with @LazyInjected doesn't solve the issue and get the error resetAndTriggerFatalError("\(T.self) was not registered", #file, #line), I tried changing the build configuration to release for that module but for some reason the preview didn't work at all.

    public func promised<T>(key: StaticString = #function) -> Factory<T?>  {
        Factory<T?>(self, key: key) {
            #if DEBUG
            if self.manager.promiseTriggersError {
                resetAndTriggerFatalError("\(T.self) was not registered", #file, #line)
            } else {
                return nil
            }
            #else
            nil
            #endif
        }
    }
@hmlongco
Copy link
Owner

Would probably need a minimum viable project to try and diagnose this.

@callo90
Copy link
Author

callo90 commented May 7, 2024

I'll prepare a quick demo.

@callo90
Copy link
Author

callo90 commented Oct 8, 2024

Demo:

This demo is a project using clean architecture with multiple modules, where each layer (Data, Domain, and Presentation) is separated into distinct modules. In this setup, the Presentation module is separated into a package, and the App module is responsible for injecting dependencies into the other modules.
https://github.com/callo90/factory-demo

Problem:
The error arises because the promised<T>(key: StaticString = #function) -> Factory<T?> doesn’t account for whether it's running the SwiftUI preview mode or not, which causes issues when injecting dependencies.

Fix:
I forked the project and fixed the error by adding a validation for preview mode with FactoryContext.current.isPreview. This allows the preview to continue working without raising errors if dependencies aren't injected, but still triggers an error in other scenarios where it's appropriate.
https://github.com/callo90/factory
https://github.com/hmlongco/Factory/compare/main...callo90:Factory:main?expand=1

Screenshots:
Here are some screenshots that highlight the issue:

Screenshot 2024-10-08 at 10 40 05 AM Screenshot 2024-10-08 at 10 40 10 AM

Let me know if you need anything else!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants