diff 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
line wrap: on
line diff
--- a/LazyBear/ContentView.swift	Fri May 07 11:00:53 2021 +0200
+++ b/LazyBear/ContentView.swift	Fri May 07 11:43:47 2021 +0200
@@ -11,6 +11,10 @@
     @State private var showWelcome = false
     @State var selectedView = 1
     
+    @Environment(\.managedObjectContext) private var moc
+    @FetchRequest(entity: WatchlistCompany.entity(), sortDescriptors: [])
+    var watchlistCompanies: FetchedResults<WatchlistCompany>
+    
     var body: some View {
         TabView(selection: $selectedView) {
             HomeView()
@@ -37,12 +41,18 @@
 //                        Text("Forth")
 //                    }
         }
-//        .onAppear { isAppAlreadyLaunchedOnce() }
+        .onAppear {
+//            isAppAlreadyLaunchedOnce()
+            createDefaultWatchlist()
+        }
         .sheet(isPresented: $showWelcome) {
             
         }
     }
     
+    /*
+     Check if app is already launched one -> If not show welcome
+     */
 //    private func isAppAlreadyLaunchedOnce() {
 //        let defaults = UserDefaults.standard
 //
@@ -51,6 +61,27 @@
 //            self.showWelcome = true
 //        }
 //    }
+    
+    /*
+     Check if exist default watchlist (Core Data) -> if not, create it
+     */
+    private func createDefaultWatchlist() {
+        let defaultCompanies = [("TSLA", "Tesla Inc"), ("AAPL", "Apple Inc"), ("MSFT", "Microsoft Corporation"), ("GS", "Goldman Sachs Group, Inc.")]
+        if watchlistCompanies.isEmpty {
+            for tupleCompany in defaultCompanies {
+                let watchlistCompany = WatchlistCompany(context: moc)
+                watchlistCompany.symbol = tupleCompany.0
+                watchlistCompany.name = tupleCompany.1
+                watchlistCompany.watchlist = "Default watchlist"
+            }
+            do {
+                try moc.save()
+                print("Default watchlist created")
+            } catch {
+                print(error.localizedDescription)
+            }
+        }
+    }
 }
 
 struct ContentView_Previews: PreviewProvider {