changeset 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 4c90e5b18632
children bd34e16b01ad
files LazyBear.xcodeproj/project.xcworkspace/xcuserdata/dennis.xcuserdatad/UserInterfaceState.xcuserstate LazyBear/ContentView.swift LazyBear/Views/Global Helpers/Company list/ExtensiveList.swift LazyBear/Views/Global Helpers/Company list/Helpers/ToolbarMenu.swift LazyBear/Views/Home/Helpers/CurrencyRow.swift
diffstat 5 files changed, 62 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
Binary file LazyBear.xcodeproj/project.xcworkspace/xcuserdata/dennis.xcuserdatad/UserInterfaceState.xcuserstate has changed
--- 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 {
--- a/LazyBear/Views/Global Helpers/Company list/ExtensiveList.swift	Fri May 07 11:00:53 2021 +0200
+++ b/LazyBear/Views/Global Helpers/Company list/ExtensiveList.swift	Fri May 07 11:43:47 2021 +0200
@@ -20,8 +20,8 @@
     var watchlistCompany: FetchedResults<WatchlistCompany>
     
     @State private var isEditMode: EditMode = .inactive
-    @State private var showRenameAction = false
-    @State private var showDeleteAlert = false
+    @State private var showRenameListAction = false
+    @State private var showDeleteListAlert = false
     @State private var showSearchView = false
     
     var body: some View {
@@ -54,17 +54,17 @@
                 // Blur background
                 Color(.black)
                     .edgesIgnoringSafeArea(.all)
-                    .opacity(showRenameAction ? 0.2: 0)
+                    .opacity(showRenameListAction ? 0.2: 0)
                     .animation(.easeInOut)
-                    .onTapGesture { showRenameAction = false }
+                    .onTapGesture { showRenameListAction = false }
                 
                 // Show rename Action Sheet
-                TextfieldAlert(listName: listName, showRenameAction: $showRenameAction, presentationMode: presentationMode)
-                    .offset(y: showRenameAction ? 0: 700)
+                TextfieldAlert(listName: listName, showRenameAction: $showRenameListAction, presentationMode: presentationMode)
+                    .offset(y: showRenameListAction ? 0: 700)
                     .animation(.easeInOut)
             }
             // Show delete list alert
-            .alert(isPresented: $showDeleteAlert) {
+            .alert(isPresented: $showDeleteListAlert) {
                 Alert(
                     title: Text("Are you sure you want to delete this list?"),
                     message: Text("This action can't be undo"),
@@ -87,7 +87,7 @@
                 }
                 ToolbarItem(placement: .navigationBarTrailing) {
                     if addOnDelete {
-                        ToolbarMenu(showRenameAction: $showRenameAction, showDeleteAlert: $showDeleteAlert)
+                        ToolbarMenu(showRenameListAction: $showRenameListAction, showDeleteListAlert: $showDeleteListAlert)
                     }
                 }
             }
@@ -95,7 +95,9 @@
         }
     }
     
-    // Delete company from watchlist
+    /*
+     Delete company from watchlist
+     */
     private func deleteCompany(at offsets: IndexSet) {
         for index in offsets {
             let company = watchlistCompany[index]
@@ -109,7 +111,9 @@
         }
     }
     
-    // Remove entire watchlist
+    /*
+     Remove entire list if it's not the last one. It can't be zero watchlists
+     */
     private func deleteList() {
         let selectedWatchlist = watchlistCompany.filter({ $0.watchlist == listName })
         for company in selectedWatchlist {
--- a/LazyBear/Views/Global Helpers/Company list/Helpers/ToolbarMenu.swift	Fri May 07 11:00:53 2021 +0200
+++ b/LazyBear/Views/Global Helpers/Company list/Helpers/ToolbarMenu.swift	Fri May 07 11:43:47 2021 +0200
@@ -8,20 +8,26 @@
 import SwiftUI
 
 struct ToolbarMenu: View {
-    @Binding var showRenameAction: Bool
-    @Binding var showDeleteAlert: Bool
+    @Binding var showRenameListAction: Bool
+    @Binding var showDeleteListAlert: Bool
+    
+    @FetchRequest(entity: WatchlistCompany.entity(), sortDescriptors: [])
+    var watchlistCompany: FetchedResults<WatchlistCompany>
     
     var body: some View {
         Menu {
             Section {
-                Button(action: { showRenameAction = true }) {
+                Button(action: { showRenameListAction = true }) {
                     Label("Rename list", systemImage: "square.and.pencil")
                 }
             }
-
-            Section(header: Text("Secondary actions")) {
-                Button(action: { showDeleteAlert = true }) {
-                    Label("Delete list", systemImage: "trash")
+            
+            // If there are only 1 watchlist -> It cannot be deleted
+            if Set(watchlistCompany.map { $0.watchlist }).count > 1 {
+                Section(header: Text("Secondary actions")) {
+                    Button(action: { showDeleteListAlert = true }) {
+                        Label("Delete list", systemImage: "trash")
+                    }
                 }
             }
         }
@@ -34,6 +40,6 @@
 
 struct ToolbarMenu_Previews: PreviewProvider {
     static var previews: some View {
-        ToolbarMenu(showRenameAction: .constant(false), showDeleteAlert: .constant(false))
+        ToolbarMenu(showRenameListAction: .constant(false), showDeleteListAlert: .constant(false))
     }
 }
--- a/LazyBear/Views/Home/Helpers/CurrencyRow.swift	Fri May 07 11:00:53 2021 +0200
+++ b/LazyBear/Views/Home/Helpers/CurrencyRow.swift	Fri May 07 11:43:47 2021 +0200
@@ -10,6 +10,7 @@
 struct CurrencyRow: View {
     var latestCurrencies: [String: CurrencyModel]
     
+    @Environment(\.managedObjectContext) private var moc
     @State private var showExtensiveList = false
     
     var body: some View {
@@ -44,6 +45,7 @@
         }
         .sheet(isPresented: $showExtensiveList) {
             ExtensiveList(listName: "Currencies", latestCurrencies: latestCurrencies, addOnDelete: false)
+                .environment(\.managedObjectContext, self.moc)
         }
     }
 }