Loading Preview Proxy View

Loading Preview Proxy View is a concept I created, it as view that replicate the initializations made in the iOS Main App but instead of doing that, for example in a WindowGroup will do it in a Group View . This struct has as a second particularity that receive a @ViewBuilder let content: () -> Content which use it after finish the loading work.
This proxy view is used for inner View in a SwiftUI App that relies on Objects or data originally initiated in the main App struct and saves the developer from having to copy and paste the code in there over and over.

See below an example of the Proxy View implementation that will implement the same initializations as in SwiftData Loading Strategy#Solution

struct LoadingPreviewProxy<Content>: View
where Content : View {
    @ViewBuilder let content: () -> Content
    @State private var isLoading = true
    private var appManager: AppManager = .init()
    
    public init(@ViewBuilder content: @escaping () -> Content) {
        self.content = content
    }
    
    var body: some View {
        Group {
            if isLoading == false,
                let modelContainer = appManager.modelContainer {
                content()
                    .environment(appManager)
                    .modelContainer(modelContainer)
            } else {
                Text("Loading Model Data")
            }
        }
        .task {
            do {
                appManager
	                .modelContainer = try await ModelContainerFactory()
		                .makeOne(isStoredInMemoryOnly: true)
                let googleFileId = "1HomqPGU5CW6Wqk5ykM0goZLAiAgtTtl2"
                try await appManager.loadWorkouts(googleFileId: googleFileId)
                isLoading = false
            } catch {
                logger.critical("Failure loading model")
            }
        }
    }
}

See code here OneRepMaxApp.swift at main · ideastouch/OneRepMax · GitHub

And here is a way to use it

fileprivate
struct Proxy: View {
    @Query private var workoutHistoricalList: [WorkoutHistorical]
    var body: some View {
        WorkoutDetailView(workoutHistorical: workoutHistoricalList.first ?? WorkoutHistorical(exercise: "Brench", maxOneRM: 245))
    }
}
#Preview("Detail View", traits: .sizeThatFitsLayout) {
    LoadingPreviewProxy {
        Proxy()
            .padding()
    }
}

Below we can see the view that presents data, this data isn't mocked but real data loaded inside the LoadingPreviewProxy View.

Check the code here View/SubViews/WorkoutDetailView.swift at main · ideastouch/OneRepMax · GitHub.

Conclusion

The Loading Preview Proxy View technique offers several advantages for SwiftUI development:

  1. Consistency: It ensures that preview environments closely match the actual runtime environment of the app.
  2. Efficiency: Developers can avoid repetitive setup code across multiple preview providers.
  3. Realistic Data: It allows previews to use real, dynamically loaded data instead of mock data.
  4. Easier Testing: By mimicking the app's initialization process, it facilitates more accurate testing of views in various states.
  5. Improved Workflow: It streamlines the development process by providing a more realistic preview environment.

By implementing this approach, developers can create more reliable previews, catch potential issues earlier in the development cycle, and ultimately build more robust SwiftUI applications. This technique is particularly valuable for complex apps with dependencies on initialized data or services.