ContentView.swift 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. ToolbarItem {
  38. Button(action: { showingPopup = true }) {
  39. Label("New URL Hint", systemImage: "link")
  40. }
  41. }
  42. }
  43. } detail: {
  44. Text("Select an item")
  45. } .popover(isPresented: $showingPopup) {
  46. CreateHintPopover(showingPopup: $showingPopup)
  47. }
  48. }
  49. private func addItem() {
  50. withAnimation {
  51. let newItem = Category(timestamp: Date())
  52. let count = items.count(where: { $0.name.starts(with: "New Category") })
  53. if (count > 0) {
  54. newItem.name += " (\(count))"
  55. }
  56. modelContext.insert(newItem)
  57. }
  58. }
  59. private func deleteItem(item: Category) {
  60. withAnimation {
  61. modelContext.delete(item)
  62. }
  63. }
  64. private func deleteItems(offsets: IndexSet) {
  65. withAnimation {
  66. for index in offsets {
  67. modelContext.delete(items[index])
  68. }
  69. }
  70. }
  71. }
  72. #Preview {
  73. ContentView()
  74. .modelContainer(for: Category.self, inMemory: true)
  75. .environmentObject(URLHintArray())
  76. }