import SwiftUI struct FilesView: View { @EnvironmentObject private var content: Content @State private var selected: FileResource? = nil var body: some View { NavigationSplitView { List(content.files, selection: $selected) { file in Text(file.uniqueId) .tag(file) } .toolbar { ToolbarItem(placement: .primaryAction) { Button(action: openFilePanel) { Label("Add file", systemSymbol: .plus) } } } .navigationSplitViewColumnWidth(min: 250, ideal: 250, max: 250) } content: { if let selected { FileContentView(file: selected) .id(selected.uniqueId) } else { Text("Select a file") } } detail: { if let selected { FileDetailView(file: selected) } else { EmptyView() } } } private func openFilePanel() { let panel = NSOpenPanel() // Sets up so user can only select a single directory panel.canChooseFiles = true panel.canChooseDirectories = false panel.allowsMultipleSelection = true panel.showsHiddenFiles = false panel.title = "Select files to add" panel.prompt = "" let response = panel.runModal() guard response == .OK else { print("Failed to select files to import") return } for url in panel.urls { let fileId = url.lastPathComponent guard !content.files.contains(where: { $0.uniqueId == fileId }) else { print("A file '\(fileId)' already exists") continue } let type = FileType(fileExtension: fileId.fileExtension) let file = FileResource(type: type, uniqueId: fileId, description: "") guard content.storage.copyFile(at: url, fileId: fileId) else { print("Failed to import file '\(fileId)'") continue } content.files.insert(file, at: 0) } } } #Preview { FilesView() .environmentObject(Content.mock) }