Mercurial > public > lazybear
comparison LazyBear/ContentView.swift @ 165:125d268db489
Update UI
author | Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com> |
---|---|
date | Fri, 19 Feb 2021 20:25:06 +0100 |
parents | f74a364441ca |
children | c1aa75608c27 |
comparison
equal
deleted
inserted
replaced
164:f74a364441ca | 165:125d268db489 |
---|---|
4 // | 4 // |
5 // Created by Dennis Concepción Martín on 17/2/21. | 5 // Created by Dennis Concepción Martín on 17/2/21. |
6 // | 6 // |
7 | 7 |
8 import SwiftUI | 8 import SwiftUI |
9 import CoreData | |
10 | 9 |
11 struct ContentView: View { | 10 struct ContentView: View { |
12 @Environment(\.managedObjectContext) private var viewContext | |
13 | |
14 @FetchRequest( | |
15 sortDescriptors: [NSSortDescriptor(keyPath: \Item.timestamp, ascending: true)], | |
16 animation: .default) | |
17 private var items: FetchedResults<Item> | |
18 | |
19 var body: some View { | 11 var body: some View { |
20 List { | 12 TabView { |
21 ForEach(items) { item in | 13 // First view |
22 Text("Item at \(item.timestamp!, formatter: itemFormatter)") | 14 Watchlist() |
23 } | 15 .tabItem { |
24 .onDelete(perform: deleteItems) | 16 Label("Watchlist", systemImage: "list.dash") |
25 } | 17 } |
26 .toolbar { | 18 |
27 #if os(iOS) | 19 // First view |
28 EditButton() | 20 Search() |
29 #endif | 21 .tabItem { |
30 | 22 Label("Search", systemImage: "magnifyingglass") |
31 Button(action: addItem) { | 23 } |
32 Label("Add Item", systemImage: "plus") | 24 |
33 } | 25 // First view |
34 } | 26 Settings() |
35 } | 27 .tabItem { |
36 | 28 Label("Settings", systemImage: "gear") |
37 private func addItem() { | 29 } |
38 withAnimation { | |
39 let newItem = Item(context: viewContext) | |
40 newItem.timestamp = Date() | |
41 | |
42 do { | |
43 try viewContext.save() | |
44 } catch { | |
45 // Replace this implementation with code to handle the error appropriately. | |
46 // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. | |
47 let nsError = error as NSError | |
48 fatalError("Unresolved error \(nsError), \(nsError.userInfo)") | |
49 } | |
50 } | |
51 } | |
52 | |
53 private func deleteItems(offsets: IndexSet) { | |
54 withAnimation { | |
55 offsets.map { items[$0] }.forEach(viewContext.delete) | |
56 | |
57 do { | |
58 try viewContext.save() | |
59 } catch { | |
60 // Replace this implementation with code to handle the error appropriately. | |
61 // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. | |
62 let nsError = error as NSError | |
63 fatalError("Unresolved error \(nsError), \(nsError.userInfo)") | |
64 } | |
65 } | 30 } |
66 } | 31 } |
67 } | 32 } |
68 | 33 |
69 private let itemFormatter: DateFormatter = { | |
70 let formatter = DateFormatter() | |
71 formatter.dateStyle = .short | |
72 formatter.timeStyle = .medium | |
73 return formatter | |
74 }() | |
75 | 34 |
76 struct ContentView_Previews: PreviewProvider { | 35 struct ContentView_Previews: PreviewProvider { |
77 static var previews: some View { | 36 static var previews: some View { |
78 ContentView().environment(\.managedObjectContext, PersistenceController.preview.container.viewContext) | 37 ContentView() |
79 } | 38 } |
80 } | 39 } |