swiftui app with a "projectmanager" @Observable that provides Project instances for…
- User
swiftui app with a "projectmanager" @Observable that provides Project instances for different windows
Assistant
SwiftUI App with Observable ProjectManager
Here's a complete implementation of a SwiftUI app with an
@ObservableProjectManagerthat providesProjectinstances to different windows:1. Project Model
import Foundation struct Project: Identifiable, Codable { let id: UUID var name: String var description: String var tasks: [String] init(id: UUID = UUID(), name: String, description: String = "", tasks: [String] = []) { self.id = id self.name = name self.description = description self.tasks = tasks } }2. ProjectManager (Observable)
import Foundation import SwiftUI @Observable final class ProjectManager { private var projects: [Project] = [] // Singleton instance static let shared = ProjectManager() // Private initializer to enforce singleton private init() { // Load sample data projects = [ Project(name: "Website Redesign", description: "Complete overhaul of company website", tasks: ["Design mockups", "Implement responsive layout", "Test on all devices"]), Project(name: "Mobile App", description: "New iOS application", tasks: ["Design UI", "Implement core features", "Test with users"]), Project(name: "Marketing Campaign", description: "Q4 product launch", tasks: ["Create assets", "Schedule social media posts", "Analyze results"]) ] } // MARK: - Public API var allProjects: [Project] { projects } func addProject(_ project: Project) { projects.append(project) } func updateProject(_ project: Project) { if let index = projects.firstIndex(where: { $0.id == project.id }) { projects[index] = project } } func deleteProject(_ project: Project) { projects.removeAll { $0.id == project.id } } func project(for id: UUID) -> Project? { projects.first { $0.id == id } } }3. Main App Structure
import SwiftUI @main struct ProjectManagerApp: App { var body: some Scene { WindowGroup { ContentView() } .windowStyle(.hiddenTitleBar) // Additional window for project details Window("Project Details", id: "projectDetails") { ProjectDetailView() } .windowStyle(.hiddenTitleBar) } }4. ContentView (Main Window)
struct ContentView: View { @State private var selectedProject: Project? @State private var showingAddProject = false var body: some View { NavigationStack { List(ProjectManager.shared.allProjects) { project in ProjectRow(project: project) .onTapGesture { selectedProject = project // Open the project details window NSApp.openWindow(id: "projectDetails") } } .navigationTitle("Projects") .toolbar { ToolbarItem(placement: .navigationBarTrailing) { Button(action: { showingAddProject = true }) { Image(systemName: "plus") } } } .sheet(isPresented: $showingAddProject) { AddProjectView() } } } } struct ProjectRow: View { let project: Project var body: some View { VStack(alignment: .leading) { Text(project.name) .font(.headline) Text(project.description) .font(.subheadline) .foregroundStyle(.secondary) } } }5. Project Detail View (Second Window)
struct ProjectDetailView: View { @State private var project: Project? @Environment(\.dismiss) private var dismiss var body: some View { if let project { VStack(alignment: .leading, spacing: 16) { Text(project.name) .font(.largeTitle) .padding(.bottom, 8) Text(project.description) .font(.body) .padding(.bottom, 16) List(project.tasks, id: \.self) { task in Text(task) } Spacer() } .padding() .frame(minWidth: 400, minHeight: 300) .onAppear { // This would be more sophisticated in a real app // For demo purposes, we'll just use the first project self.project = ProjectManager.shared.allProjects.first } } else { ProgressView() } } }6. Add Project View
struct AddProjectView: View { @State private var name = "" @State private var description = "" @Environment(\.dismiss) private var dismiss var body: some View { NavigationStack { Form { TextField("Project Name", text: $name) TextField("Description", text: $description) Button("Add Project") { let newProject = Project(name: name, description: description) ProjectManager.shared.addProject(newProject) dismiss() } .disabled(name.isEmpty) } .navigationTitle("New Project") .navigationBarTitleDisplayMode(.inline) .toolbar { ToolbarItem(placement: .cancellationAction) { Button("Cancel") { dismiss() } } } } } }Key Features:
- Observable ProjectManager: Uses
@Observableto automatically publish changes to all views - Multiple Windows: Main window for project list and a details window
- Data Management: CRUD operations for projects
- State Management: Proper handling of state between windows
How to Use:
- The main window shows a list of all projects
- Clicking a project opens a details window
- The "+" button adds a new project
- The ProjectManager handles all data operations
You can extend this by adding more sophisticated window management, better state handling between windows, and more project features.
- Observable ProjectManager: Uses
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.