Mercurial > public > lazybear
diff 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 |
line wrap: on
line diff
--- a/LazyBear/ContentView.swift Sat Jul 17 09:33:44 2021 +0100 +++ b/LazyBear/ContentView.swift Sat Jul 17 17:58:57 2021 +0100 @@ -9,72 +9,71 @@ import CoreData struct ContentView: View { + @State private var tab: Tab = .watchlist @Environment(\.managedObjectContext) private var viewContext - - @FetchRequest( - sortDescriptors: [NSSortDescriptor(keyPath: \Item.timestamp, ascending: true)], - animation: .default) - private var items: FetchedResults<Item> - + @FetchRequest(sortDescriptors: []) private var companies: FetchedResults<Company> + var body: some View { - List { - ForEach(items) { item in - Text("Item at \(item.timestamp!, formatter: itemFormatter)") - } - .onDelete(perform: deleteItems) + TabView(selection: $tab) { + Watchlist() + .tabItem { + Image(systemName: "list.star") + Text("Watchlist") + } + .tag(Tab.watchlist) + + Search() + .tabItem { + Image(systemName: "magnifyingglass") + Text("Search") + } + .tag(Tab.search) } - .toolbar { - #if os(iOS) - EditButton() - #endif - - Button(action: addItem) { - Label("Add Item", systemImage: "plus") - } - } + .onAppear(perform: createDefaultWatchlist) + } + + private enum Tab { + case watchlist, search, settings } + + // MARK: - Create default watchlist + private func createDefaultWatchlist() { + if companies.isEmpty { + let defaultCompanies = [ + "AAPL": "Apple Inc", + "AMZN": "Amazon.com Inc.", + "BRK.A": "Berkshire Hathaway Inc.", + "GS": "Goldman Sachs Group, Inc.", + "TSLA": "Tesla Inc", + "MSFT": "Microsoft Corporation", + "WMT": "Walmart Inc", + "DIS": "Walt Disney Co (The)", + "NKE": "Nike, Inc." + ] + + for defaultCompany in defaultCompanies { + withAnimation { + let company = Company(context: viewContext) + company.symbol = defaultCompany.key + company.companyName = defaultCompany.value - private func addItem() { - withAnimation { - let newItem = Item(context: viewContext) - newItem.timestamp = Date() - - do { - try viewContext.save() - } catch { - // Replace this implementation with code to handle the error appropriately. - // 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. - let nsError = error as NSError - fatalError("Unresolved error \(nsError), \(nsError.userInfo)") - } - } - } - - private func deleteItems(offsets: IndexSet) { - withAnimation { - offsets.map { items[$0] }.forEach(viewContext.delete) - - do { - try viewContext.save() - } catch { - // Replace this implementation with code to handle the error appropriately. - // 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. - let nsError = error as NSError - fatalError("Unresolved error \(nsError), \(nsError.userInfo)") + do { + try viewContext.save() + } catch { + let nsError = error as NSError + fatalError("Unresolved error \(nsError), \(nsError.userInfo)") + } + } } } } } -private let itemFormatter: DateFormatter = { - let formatter = DateFormatter() - formatter.dateStyle = .short - formatter.timeStyle = .medium - return formatter -}() struct ContentView_Previews: PreviewProvider { static var previews: some View { - ContentView().environment(\.managedObjectContext, PersistenceController.preview.container.viewContext) + ContentView() + .environment(\.managedObjectContext, PersistenceController.preview.container.viewContext) + } }