comparison LazyBear/ContentView.swift @ 465:6953d83060a4

New design
author Dennis Concepción Martín <dennisconcepcionmartin@gmail.com>
date Sat, 17 Jul 2021 17:58:57 +0100
parents 783b567800d9
children
comparison
equal deleted inserted replaced
464:04e430ef254a 465:6953d83060a4
7 7
8 import SwiftUI 8 import SwiftUI
9 import CoreData 9 import CoreData
10 10
11 struct ContentView: View { 11 struct ContentView: View {
12 @State private var tab: Tab = .watchlist
12 @Environment(\.managedObjectContext) private var viewContext 13 @Environment(\.managedObjectContext) private var viewContext
14 @FetchRequest(sortDescriptors: []) private var companies: FetchedResults<Company>
15
16 var body: some View {
17 TabView(selection: $tab) {
18 Watchlist()
19 .tabItem {
20 Image(systemName: "list.star")
21 Text("Watchlist")
22 }
23 .tag(Tab.watchlist)
24
25 Search()
26 .tabItem {
27 Image(systemName: "magnifyingglass")
28 Text("Search")
29 }
30 .tag(Tab.search)
31 }
32 .onAppear(perform: createDefaultWatchlist)
33 }
34
35 private enum Tab {
36 case watchlist, search, settings
37 }
38
39 // MARK: - Create default watchlist
40 private func createDefaultWatchlist() {
41 if companies.isEmpty {
42 let defaultCompanies = [
43 "AAPL": "Apple Inc",
44 "AMZN": "Amazon.com Inc.",
45 "BRK.A": "Berkshire Hathaway Inc.",
46 "GS": "Goldman Sachs Group, Inc.",
47 "TSLA": "Tesla Inc",
48 "MSFT": "Microsoft Corporation",
49 "WMT": "Walmart Inc",
50 "DIS": "Walt Disney Co (The)",
51 "NKE": "Nike, Inc."
52 ]
53
54 for defaultCompany in defaultCompanies {
55 withAnimation {
56 let company = Company(context: viewContext)
57 company.symbol = defaultCompany.key
58 company.companyName = defaultCompany.value
13 59
14 @FetchRequest( 60 do {
15 sortDescriptors: [NSSortDescriptor(keyPath: \Item.timestamp, ascending: true)], 61 try viewContext.save()
16 animation: .default) 62 } catch {
17 private var items: FetchedResults<Item> 63 let nsError = error as NSError
18 64 fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
19 var body: some View { 65 }
20 List { 66 }
21 ForEach(items) { item in
22 Text("Item at \(item.timestamp!, formatter: itemFormatter)")
23 }
24 .onDelete(perform: deleteItems)
25 }
26 .toolbar {
27 #if os(iOS)
28 EditButton()
29 #endif
30
31 Button(action: addItem) {
32 Label("Add Item", systemImage: "plus")
33 }
34 }
35 }
36
37 private func addItem() {
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 } 67 }
65 } 68 }
66 } 69 }
67 } 70 }
68 71
69 private let itemFormatter: DateFormatter = {
70 let formatter = DateFormatter()
71 formatter.dateStyle = .short
72 formatter.timeStyle = .medium
73 return formatter
74 }()
75 72
76 struct ContentView_Previews: PreviewProvider { 73 struct ContentView_Previews: PreviewProvider {
77 static var previews: some View { 74 static var previews: some View {
78 ContentView().environment(\.managedObjectContext, PersistenceController.preview.container.viewContext) 75 ContentView()
76 .environment(\.managedObjectContext, PersistenceController.preview.container.viewContext)
77
79 } 78 }
80 } 79 }