]>
Commit | Line | Data |
---|---|---|
bf063563 RBR |
1 | // |
2 | // ContentView.swift | |
3 | // Captura | |
4 | // | |
5 | // Created by Ruben Beltran del Rio on 7/24/23. | |
6 | // | |
7 | ||
8 | import SwiftUI | |
9 | import SwiftData | |
10 | ||
11 | struct ContentView: View { | |
12 | @Environment(\.modelContext) private var modelContext | |
13 | @Query private var items: [Item] | |
14 | ||
15 | var body: some View { | |
16 | NavigationView { | |
17 | List { | |
18 | ForEach(items) { item in | |
19 | NavigationLink { | |
20 | Text("Item at \(item.timestamp, format: Date.FormatStyle(date: .numeric, time: .standard))") | |
21 | } label: { | |
22 | Text(item.timestamp, format: Date.FormatStyle(date: .numeric, time: .standard)) | |
23 | } | |
24 | } | |
25 | .onDelete(perform: deleteItems) | |
26 | } | |
27 | .toolbar { | |
28 | ToolbarItem { | |
29 | Button(action: addItem) { | |
30 | Label("Add Item", systemImage: "plus") | |
31 | } | |
32 | } | |
33 | } | |
34 | Text("Select an item") | |
35 | } | |
36 | } | |
37 | ||
38 | private func addItem() { | |
39 | withAnimation { | |
40 | let newItem = Item(timestamp: Date()) | |
41 | modelContext.insert(newItem) | |
42 | } | |
43 | } | |
44 | ||
45 | private func deleteItems(offsets: IndexSet) { | |
46 | withAnimation { | |
47 | for index in offsets { | |
48 | modelContext.delete(items[index]) | |
49 | } | |
50 | } | |
51 | } | |
52 | } | |
53 | ||
54 | #Preview { | |
55 | ContentView() | |
56 | .modelContainer(for: Item.self, inMemory: true) | |
57 | } |