comparison LazyBear/ContentView.swift @ 453:37c13ebda381

Improve hierarchy and minor bugs fixed
author Dennis Concepción Martín <dennisconcepcionmartin@gmail.com>
date Sun, 27 Jun 2021 14:18:29 +0200
parents 4255f94d0767
children c6913f0ce46e
comparison
equal deleted inserted replaced
452:bb69f9d1d20f 453:37c13ebda381
4 // 4 //
5 // Created by Dennis Concepción Martín on 21/3/21. 5 // Created by Dennis Concepción Martín on 21/3/21.
6 // 6 //
7 7
8 import SwiftUI 8 import SwiftUI
9 import CoreHaptics
9 10
10 struct ContentView: View { 11 struct ContentView: View {
11 @State private var showWelcome = false 12 @State private var showWelcome = false
12 @State var selectedTab: Tab = .home 13 @State var selectedTab: Tab = .home
13 @StateObject var hapticsManager = HapticsManager()
14 14
15 @Environment(\.managedObjectContext) private var moc 15 @Environment(\.managedObjectContext) private var moc
16 @FetchRequest(entity: WatchlistCompany.entity(), sortDescriptors: []) 16 @FetchRequest(entity: WatchlistCompany.entity(), sortDescriptors: []) var watchlistCompanies: FetchedResults<WatchlistCompany>
17 var watchlistCompanies: FetchedResults<WatchlistCompany>
18 17
19 var body: some View { 18 var body: some View {
20 TabView(selection: $selectedTab) { 19 TabView(selection: $selectedTab) {
21 HomeView() 20 HomeView()
22 .tabItem { 21 .tabItem {
35 Image(systemName: "person") 34 Image(systemName: "person")
36 Text("Profile") 35 Text("Profile")
37 } 36 }
38 .tag(Tab.profile) 37 .tag(Tab.profile)
39 } 38 }
40 .onAppear { 39 .onAppear(perform: onAppear)
41 // isAppAlreadyLaunchedOnce()
42 hapticsManager.prepareHaptics()
43 createDefaultWatchlist()
44 }
45 .sheet(isPresented: $showWelcome) { 40 .sheet(isPresented: $showWelcome) {
46 41
47 } 42 }
48 } 43 }
49 44
50 /* 45 /*
51 Check if app is already launched one -> If not show welcome 46 1) Create default watchlist if it doesn't exits
47 2) Show WelcomeView if is the first time that the app is opened
48 3) Prepare haptics
52 */ 49 */
53 // private func isAppAlreadyLaunchedOnce() { 50 private func onAppear() {
54 // let defaults = UserDefaults.standard 51 // Create watchlist
55 //
56 // if let isAppAlreadyLaunchedOnce = defaults.string(forKey: "IsAppAlreadyLaunchedOnce") {
57 // print("App already launched : \(isAppAlreadyLaunchedOnce)")
58 // self.showWelcome = true
59 // }
60 // }
61
62 /*
63 Check if exist default watchlist (Core Data) -> if not, create it
64 */
65 private func createDefaultWatchlist() {
66 let defaultCompanies = [("TSLA", "Tesla Inc"), ("AAPL", "Apple Inc"), ("MSFT", "Microsoft Corporation"), ("GS", "Goldman Sachs Group, Inc.")]
67 if watchlistCompanies.isEmpty { 52 if watchlistCompanies.isEmpty {
68 for tupleCompany in defaultCompanies { 53 let defaultCompanies: [DefaultCompanyModel] = parseJSON("DefaultCompanies.json")
54 for defaultCompany in defaultCompanies {
69 let watchlistCompany = WatchlistCompany(context: moc) 55 let watchlistCompany = WatchlistCompany(context: moc)
70 watchlistCompany.symbol = tupleCompany.0 56 watchlistCompany.name = defaultCompany.name
71 watchlistCompany.name = tupleCompany.1 57 watchlistCompany.symbol = defaultCompany.symbol
72 watchlistCompany.watchlistName = "Default watchlist" 58 watchlistCompany.watchlistName = "Default watchlist"
73 } 59 }
60
74 do { 61 do {
75 try moc.save() 62 try moc.save()
76 print("Default watchlist created") 63 print("Default watchlist created")
77 } catch { 64 } catch {
78 print(error.localizedDescription) 65 print(error.localizedDescription)
79 } 66 }
80 } 67 }
68
69 // 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 // Prepare haptics
78 // hapticsManager.prepareHaptics()
81 } 79 }
82 } 80 }
83 extension ContentView { 81 extension ContentView {
84 enum Tab: Hashable { 82 enum Tab: Hashable {
85 case home 83 case home