ContentView.swift 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. var body: some View {
  13. NavigationSplitView {
  14. List {
  15. ForEach(items) { item in
  16. NavigationLink {
  17. Text("Item at \(item.timestamp, format: Date.FormatStyle(date: .numeric, time: .standard)): \(item.name)")
  18. } label: {
  19. CategoryView(name: Bindable(item).name)
  20. }
  21. }
  22. .onDelete(perform: deleteItems)
  23. }
  24. .navigationSplitViewColumnWidth(min: 180, ideal: 200)
  25. .toolbar {
  26. ToolbarItem {
  27. Button(action: addItem) {
  28. Label("Add Item", systemImage: "plus")
  29. }
  30. }
  31. }
  32. } detail: {
  33. Text("Select an item")
  34. }
  35. }
  36. private func addItem() {
  37. withAnimation {
  38. let newItem = Category(timestamp: Date())
  39. modelContext.insert(newItem)
  40. }
  41. }
  42. private func deleteItems(offsets: IndexSet) {
  43. withAnimation {
  44. for index in offsets {
  45. modelContext.delete(items[index])
  46. }
  47. }
  48. }
  49. }
  50. #Preview {
  51. ContentView()
  52. .modelContainer(for: Category.self, inMemory: true)
  53. }