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