import Foundation import SwiftData final class WorkoutsContainer { static let logger = AppLogger(subsystem: "Workouts", category: "WorkoutsContainer") static func create(shouldCreateDefaults: inout Bool) -> ModelContainer { let schema = Schema(versionedSchema: SchemaV1.self) let container = try! ModelContainer(for: schema, migrationPlan: WorkoutsMigrationPlan.self) let context = ModelContext(container) let descriptor = FetchDescriptor() let results = try! context.fetch(descriptor) if results.isEmpty { shouldCreateDefaults = true } return container } @MainActor static var preview: ModelContainer { let schema = Schema(versionedSchema: SchemaV1.self) let configuration = ModelConfiguration(schema: schema, isStoredInMemoryOnly: true) do { let container = try ModelContainer(for: schema, configurations: [configuration]) let context = ModelContext(container) // Create default data for previews InitialData.create(modelContext: context) return container } catch { fatalError("Failed to create preview ModelContainer: \(error.localizedDescription)") } } }