ContentView.swift 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. //
  2. // ContentView.swift
  3. // Todos
  4. //
  5. // Created by Sam Jaffe on 2/28/26.
  6. //
  7. import SwiftUI
  8. import SwiftData
  9. struct ContentView: View {
  10. @Environment(\.modelContext) private var modelContext
  11. @Query private var items: [Category]
  12. @State private var showingPopup = false
  13. @State private var currentHint: URLHint = URLHint()
  14. var body: some View {
  15. NavigationSplitView {
  16. List {
  17. ForEach(items) { item in
  18. NavigationLink {
  19. CategoryPanelView(item: item)
  20. } label: {
  21. CategorySidebarView(name: Bindable(item).name)
  22. } .contextMenu {
  23. Button(action: { deleteItem(item: item) }) {
  24. Label("Delete", systemImage: "trash")
  25. }
  26. }
  27. }
  28. .onDelete(perform: deleteItems)
  29. }
  30. .navigationSplitViewColumnWidth(min: 180, ideal: 200)
  31. .toolbar {
  32. ToolbarItem {
  33. Button(action: addItem) {
  34. Label("New Category", systemImage: "plus")
  35. }
  36. }
  37. }
  38. } detail: {
  39. Text("Select an item")
  40. }
  41. }
  42. private func addItem() {
  43. withAnimation {
  44. let newItem = Category(timestamp: Date())
  45. let count = items.count(where: { $0.name.starts(with: "New Category") })
  46. if (count > 0) {
  47. newItem.name += " (\(count))"
  48. }
  49. modelContext.insert(newItem)
  50. }
  51. }
  52. private func deleteItem(item: Category) {
  53. withAnimation {
  54. modelContext.delete(item)
  55. }
  56. }
  57. private func deleteItems(offsets: IndexSet) {
  58. withAnimation {
  59. for index in offsets {
  60. modelContext.delete(items[index])
  61. }
  62. }
  63. }
  64. }
  65. #Preview {
  66. ContentView()
  67. .modelContainer(for: Category.self, inMemory: true)
  68. .environmentObject(URLHintArray())
  69. }