comparison LazyBear/ContentView.swift @ 395:a0cf8fe47044

Fix minor bugs
author Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
date Fri, 07 May 2021 11:43:47 +0200
parents 3599d303d1a0
children f9611c94d636
comparison
equal deleted inserted replaced
394:4c90e5b18632 395:a0cf8fe47044
8 import SwiftUI 8 import SwiftUI
9 9
10 struct ContentView: View { 10 struct ContentView: View {
11 @State private var showWelcome = false 11 @State private var showWelcome = false
12 @State var selectedView = 1 12 @State var selectedView = 1
13
14 @Environment(\.managedObjectContext) private var moc
15 @FetchRequest(entity: WatchlistCompany.entity(), sortDescriptors: [])
16 var watchlistCompanies: FetchedResults<WatchlistCompany>
13 17
14 var body: some View { 18 var body: some View {
15 TabView(selection: $selectedView) { 19 TabView(selection: $selectedView) {
16 HomeView() 20 HomeView()
17 .tabItem { 21 .tabItem {
35 // .tabItem { 39 // .tabItem {
36 // Image(systemName: "4.square.fill") 40 // Image(systemName: "4.square.fill")
37 // Text("Forth") 41 // Text("Forth")
38 // } 42 // }
39 } 43 }
40 // .onAppear { isAppAlreadyLaunchedOnce() } 44 .onAppear {
45 // isAppAlreadyLaunchedOnce()
46 createDefaultWatchlist()
47 }
41 .sheet(isPresented: $showWelcome) { 48 .sheet(isPresented: $showWelcome) {
42 49
43 } 50 }
44 } 51 }
45 52
53 /*
54 Check if app is already launched one -> If not show welcome
55 */
46 // private func isAppAlreadyLaunchedOnce() { 56 // private func isAppAlreadyLaunchedOnce() {
47 // let defaults = UserDefaults.standard 57 // let defaults = UserDefaults.standard
48 // 58 //
49 // if let isAppAlreadyLaunchedOnce = defaults.string(forKey: "IsAppAlreadyLaunchedOnce") { 59 // if let isAppAlreadyLaunchedOnce = defaults.string(forKey: "IsAppAlreadyLaunchedOnce") {
50 // print("App already launched : \(isAppAlreadyLaunchedOnce)") 60 // print("App already launched : \(isAppAlreadyLaunchedOnce)")
51 // self.showWelcome = true 61 // self.showWelcome = true
52 // } 62 // }
53 // } 63 // }
64
65 /*
66 Check if exist default watchlist (Core Data) -> if not, create it
67 */
68 private func createDefaultWatchlist() {
69 let defaultCompanies = [("TSLA", "Tesla Inc"), ("AAPL", "Apple Inc"), ("MSFT", "Microsoft Corporation"), ("GS", "Goldman Sachs Group, Inc.")]
70 if watchlistCompanies.isEmpty {
71 for tupleCompany in defaultCompanies {
72 let watchlistCompany = WatchlistCompany(context: moc)
73 watchlistCompany.symbol = tupleCompany.0
74 watchlistCompany.name = tupleCompany.1
75 watchlistCompany.watchlist = "Default watchlist"
76 }
77 do {
78 try moc.save()
79 print("Default watchlist created")
80 } catch {
81 print(error.localizedDescription)
82 }
83 }
84 }
54 } 85 }
55 86
56 struct ContentView_Previews: PreviewProvider { 87 struct ContentView_Previews: PreviewProvider {
57 static var previews: some View { 88 static var previews: some View {
58 ContentView() 89 ContentView()