comparison LazyBear/ContentView.swift @ 463:783b567800d9

Starts new version
author Dennis Concepción Martín <dennisconcepcionmartin@gmail.com>
date Sat, 17 Jul 2021 09:33:26 +0100
parents c6913f0ce46e
children 6953d83060a4
comparison
equal deleted inserted replaced
462:daeac7c7c586 463:783b567800d9
1 // 1 //
2 // ContentView.swift 2 // ContentView.swift
3 // LazyBear 3 // lazybear
4 // 4 //
5 // Created by Dennis Concepción Martín on 21/3/21. 5 // Created by Dennis Concepción Martín on 17/07/2021.
6 // 6 //
7 7
8 import SwiftUI 8 import SwiftUI
9 import CoreHaptics 9 import CoreData
10 10
11 struct ContentView: View { 11 struct ContentView: View {
12 @State private var showWelcome = false 12 @Environment(\.managedObjectContext) private var viewContext
13 @State var selectedTab: Tab = .home 13
14 14 @FetchRequest(
15 @Environment(\.managedObjectContext) private var moc 15 sortDescriptors: [NSSortDescriptor(keyPath: \Item.timestamp, ascending: true)],
16 @FetchRequest(entity: WatchlistCompany.entity(), sortDescriptors: []) var watchlistCompanies: FetchedResults<WatchlistCompany> 16 animation: .default)
17 17 private var items: FetchedResults<Item>
18
18 var body: some View { 19 var body: some View {
19 TabView(selection: $selectedTab) { 20 List {
20 HomeView() 21 ForEach(items) { item in
21 .tabItem { 22 Text("Item at \(item.timestamp!, formatter: itemFormatter)")
22 Image(systemName: "house") 23 }
23 Text("Home") 24 .onDelete(perform: deleteItems)
24 }
25 .tag(Tab.home) /// Do not remove tags. It causes an odd behaviour when showView is activated
26 SearchView()
27 .tabItem {
28 Image(systemName: "magnifyingglass")
29 Text("Search")
30 }
31 .tag(Tab.search)
32 ProfileView()
33 .tabItem {
34 Image(systemName: "person")
35 Text("Profile")
36 }
37 .tag(Tab.profile)
38 } 25 }
39 .onAppear(perform: onAppear) 26 .toolbar {
40 .sheet(isPresented: $showWelcome) { 27 #if os(iOS)
41 28 EditButton()
29 #endif
30
31 Button(action: addItem) {
32 Label("Add Item", systemImage: "plus")
33 }
42 } 34 }
43 } 35 }
44 36
45 /* 37 private func addItem() {
46 1) Create default watchlist if it doesn't exits 38 withAnimation {
47 2) Show WelcomeView if is the first time that the app is opened 39 let newItem = Item(context: viewContext)
48 3) Prepare haptics 40 newItem.timestamp = Date()
49 */ 41
50 private func onAppear() { 42 do {
51 // MARK: - Create Watchlist 43 try viewContext.save()
52 // if watchlistCompanies.isEmpty { 44 } catch {
53 // let defaultCompanies: [DefaultCompanyModel] = parseJSON("DefaultCompanies.json") 45 // Replace this implementation with code to handle the error appropriately.
54 // for defaultCompany in defaultCompanies { 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.
55 // let watchlistCompany = WatchlistCompany(context: moc) 47 let nsError = error as NSError
56 // watchlistCompany.name = defaultCompany.name 48 fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
57 // watchlistCompany.symbol = defaultCompany.symbol 49 }
58 // watchlistCompany.watchlistName = "Default watchlist" 50 }
59 // }
60 //
61 // do {
62 // try moc.save()
63 // print("Default watchlist created")
64 // } catch {
65 // print(error.localizedDescription)
66 // }
67 // }
68
69 // MARK: - Show WelcomeView if is the first time that the app is opened
70 // let defaults = UserDefaults.standard
71 //
72 // if let isAppAlreadyLaunchedOnce = defaults.string(forKey: "IsAppAlreadyLaunchedOnce") {
73 // print("App already launched : \(isAppAlreadyLaunchedOnce)")
74 // self.showWelcome = true
75 // }
76
77 // MARK: - Prepare Haptics
78 // hapticsManager.prepareHaptics()
79 } 51 }
80 } 52
81 extension ContentView { 53 private func deleteItems(offsets: IndexSet) {
82 enum Tab: Hashable { 54 withAnimation {
83 case home 55 offsets.map { items[$0] }.forEach(viewContext.delete)
84 case search 56
85 case profile 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 }
86 } 66 }
87 } 67 }
88 68
69 private let itemFormatter: DateFormatter = {
70 let formatter = DateFormatter()
71 formatter.dateStyle = .short
72 formatter.timeStyle = .medium
73 return formatter
74 }()
75
89 struct ContentView_Previews: PreviewProvider { 76 struct ContentView_Previews: PreviewProvider {
90 static var previews: some View { 77 static var previews: some View {
91 ContentView() 78 ContentView().environment(\.managedObjectContext, PersistenceController.preview.container.viewContext)
92 } 79 }
93 } 80 }