diff options
Diffstat (limited to 'Captura/ContentView.swift')
| -rw-r--r-- | Captura/ContentView.swift | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/Captura/ContentView.swift b/Captura/ContentView.swift new file mode 100644 index 0000000..63fa7b8 --- /dev/null +++ b/Captura/ContentView.swift @@ -0,0 +1,57 @@ +// +// ContentView.swift +// Captura +// +// Created by Ruben Beltran del Rio on 7/24/23. +// + +import SwiftUI +import SwiftData + +struct ContentView: View { + @Environment(\.modelContext) private var modelContext + @Query private var items: [Item] + + var body: some View { + NavigationView { + List { + ForEach(items) { item in + NavigationLink { + Text("Item at \(item.timestamp, format: Date.FormatStyle(date: .numeric, time: .standard))") + } label: { + Text(item.timestamp, format: Date.FormatStyle(date: .numeric, time: .standard)) + } + } + .onDelete(perform: deleteItems) + } + .toolbar { + ToolbarItem { + Button(action: addItem) { + Label("Add Item", systemImage: "plus") + } + } + } + Text("Select an item") + } + } + + private func addItem() { + withAnimation { + let newItem = Item(timestamp: Date()) + modelContext.insert(newItem) + } + } + + private func deleteItems(offsets: IndexSet) { + withAnimation { + for index in offsets { + modelContext.delete(items[index]) + } + } + } +} + +#Preview { + ContentView() + .modelContainer(for: Item.self, inMemory: true) +} |