| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- //
- // ContentView.swift
- // Todos
- //
- // Created by Sam Jaffe on 2/28/26.
- //
- import SwiftUI
- import SwiftData
- struct ContentView: View {
- @Environment(\.modelContext) private var modelContext
- @Query private var items: [Category]
- var body: some View {
- NavigationSplitView {
- List {
- ForEach(items) { item in
- NavigationLink {
- Text("Item at \(item.timestamp, format: Date.FormatStyle(date: .numeric, time: .standard)): \(item.name)")
- } label: {
- CategoryView(name: Bindable(item).name)
- }
- }
- .onDelete(perform: deleteItems)
- }
- .navigationSplitViewColumnWidth(min: 180, ideal: 200)
- .toolbar {
- ToolbarItem {
- Button(action: addItem) {
- Label("Add Item", systemImage: "plus")
- }
- }
- }
- } detail: {
- Text("Select an item")
- }
- }
- private func addItem() {
- withAnimation {
- let newItem = Category(timestamp: Date())
- modelContext.insert(newItem)
- }
- }
- private func deleteItems(offsets: IndexSet) {
- withAnimation {
- for index in offsets {
- modelContext.delete(items[index])
- }
- }
- }
- }
- #Preview {
- ContentView()
- .modelContainer(for: Category.self, inMemory: true)
- }
|