diff options
| author | Ruben Beltran del Rio <ruben@unlimited.pizza> | 2023-07-24 20:19:46 +0200 |
|---|---|---|
| committer | Ruben Beltran del Rio <ruben@unlimited.pizza> | 2023-07-24 20:19:46 +0200 |
| commit | bf063563436a12c003968ae4d00991f49fac226c (patch) | |
| tree | baa95b20f000813c9692f085ece363194041ec94 /Captura/ContentView.swift | |
Initial commit
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) +} |