changeset 130:012148730331

Update toggle buttons. Add CreateButton and the logic to remove or add a company to the watchlist
author Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
date Sun, 07 Feb 2021 20:19:34 +0100
parents 4ce0ff2a38ae
children 0299ad98b4de
files LazyBear.xcodeproj/project.pbxproj LazyBear.xcodeproj/project.xcworkspace/xcuserdata/dennis.xcuserdatad/UserInterfaceState.xcuserstate lazybear/Views/Company.swift lazybear/Views/CreateButton.swift lazybear/Views/HistoryList.swift lazybear/Views/ViewSelector.swift
diffstat 6 files changed, 94 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/LazyBear.xcodeproj/project.pbxproj	Sun Feb 07 19:31:49 2021 +0100
+++ b/LazyBear.xcodeproj/project.pbxproj	Sun Feb 07 20:19:34 2021 +0100
@@ -22,6 +22,7 @@
 		9589C94D25CE0B4C0045DFF3 /* AddWatchlist.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9589C94C25CE0B4C0045DFF3 /* AddWatchlist.swift */; };
 		958B678525C42B2400BF9F89 /* ApiAccess.swift in Sources */ = {isa = PBXBuildFile; fileRef = 958B678425C42B2400BF9F89 /* ApiAccess.swift */; };
 		9597CE0425C1DFE7004DDFED /* LogoPlaceholder.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9597CE0325C1DFE7004DDFED /* LogoPlaceholder.swift */; };
+		9598CFE325D06A6D00FA9E1B /* CreateButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9598CFE225D06A6C00FA9E1B /* CreateButton.swift */; };
 		959D28DC25CC99710029F689 /* NewsModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 959D28DB25CC99710029F689 /* NewsModel.swift */; };
 		959D28DF25CC9A1A0029F689 /* News.swift in Sources */ = {isa = PBXBuildFile; fileRef = 959D28DE25CC9A1A0029F689 /* News.swift */; };
 		959D28E225CC9B370029F689 /* NewsRow.swift in Sources */ = {isa = PBXBuildFile; fileRef = 959D28E125CC9B370029F689 /* NewsRow.swift */; };
@@ -78,6 +79,7 @@
 		9589C94C25CE0B4C0045DFF3 /* AddWatchlist.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = AddWatchlist.swift; path = LazyBear/Views/AddWatchlist.swift; sourceTree = SOURCE_ROOT; };
 		958B678425C42B2400BF9F89 /* ApiAccess.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = ApiAccess.swift; path = lazybear/ApiAccess.swift; sourceTree = SOURCE_ROOT; };
 		9597CE0325C1DFE7004DDFED /* LogoPlaceholder.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LogoPlaceholder.swift; sourceTree = "<group>"; };
+		9598CFE225D06A6C00FA9E1B /* CreateButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = CreateButton.swift; path = lazybear/Views/CreateButton.swift; sourceTree = SOURCE_ROOT; };
 		959D28DB25CC99710029F689 /* NewsModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = NewsModel.swift; path = lazybear/Models/NewsModel.swift; sourceTree = SOURCE_ROOT; };
 		959D28DE25CC9A1A0029F689 /* News.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = News.swift; path = LazyBear/Views/News.swift; sourceTree = SOURCE_ROOT; };
 		959D28E125CC9B370029F689 /* NewsRow.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = NewsRow.swift; path = LazyBear/Views/NewsRow.swift; sourceTree = SOURCE_ROOT; };
@@ -177,6 +179,7 @@
 				95F6F45B25C20D8D002AC66A /* Price.swift */,
 				95E07B7125CE95D9001718AB /* Settings.swift */,
 				95F6C2EF25BAE2ED003CF389 /* Company.swift */,
+				9598CFE225D06A6C00FA9E1B /* CreateButton.swift */,
 				95F6CA0425CF2A4E0064E4E9 /* ViewSelector.swift */,
 				95E411B525BEE84E00A9C23F /* Stock.swift */,
 				95F6C30825BAF7C2003CF389 /* Selection.swift */,
@@ -369,6 +372,7 @@
 				95D1BF4925ADCF7700E5D063 /* Persistence.swift in Sources */,
 				958B678525C42B2400BF9F89 /* ApiAccess.swift in Sources */,
 				95B04EB325212369000AD27F /* LazyBearApp.swift in Sources */,
+				9598CFE325D06A6D00FA9E1B /* CreateButton.swift in Sources */,
 				95E07B6B25CE9398001718AB /* AppView.swift in Sources */,
 				95ED176C25CEFE1B00AE34B3 /* RecentSearch+CoreDataProperties.swift in Sources */,
 				95ED176B25CEFE1B00AE34B3 /* RecentSearch+CoreDataClass.swift in Sources */,
Binary file LazyBear.xcodeproj/project.xcworkspace/xcuserdata/dennis.xcuserdatad/UserInterfaceState.xcuserstate has changed
--- a/lazybear/Views/Company.swift	Sun Feb 07 19:31:49 2021 +0100
+++ b/lazybear/Views/Company.swift	Sun Feb 07 20:19:34 2021 +0100
@@ -28,9 +28,10 @@
         .navigationBarTitle(symbol, displayMode: .inline)
         .navigationBarItems(trailing:
             HStack {
-                AddWatchlist(symbol: symbol, name: name)
+                CreateButton(symbol: symbol, name: name)
                     .padding(.trailing)
                 
+                Spacer()
                 ViewSelector(viewSelected: $viewSelected)
             }
         )
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lazybear/Views/CreateButton.swift	Sun Feb 07 20:19:34 2021 +0100
@@ -0,0 +1,85 @@
+//
+//  CreateButton.swift
+//  LazyBear
+//
+//  Created by Dennis Concepción Martín on 7/2/21.
+//
+
+import SwiftUI
+import SPAlert
+
+struct CreateButton: View {
+    var symbol: String
+    var name: String
+    
+    @State private var showingActionSheet = false
+    
+    @Environment(\.managedObjectContext) private var viewContext
+    @FetchRequest(entity: WatchlistCompany.entity(), sortDescriptors: [])
+    var companies: FetchedResults<WatchlistCompany>  // Fetch core data
+    
+    var body: some View {
+        Button(action: { self.showingActionSheet = true }) {
+            Image(systemName: "plus")
+                .imageScale(.large)
+                .frame(width: 30, height: 30)  // This frame change the tappable area
+                .actionSheet(isPresented: $showingActionSheet) { alert() }
+        }
+    }
+    
+    // Create Action sheet
+    private func alert() -> ActionSheet {
+        var buttons: [ActionSheet.Button] = [.cancel(Text("Cancel")) { self.showingActionSheet = false }]
+        
+        // Logic to create buttons
+        let watchlistSymbols = companies.map { $0.symbol }
+        
+        if watchlistSymbols.contains(symbol) {
+            buttons.append(.destructive(Text("Remove from watchlist")) { removeWatchlist() } )
+        } else {
+            buttons.append(.default(Text("Add to wachtlist")) { addWatchlist() })
+        }
+        
+        let action = ActionSheet(
+            title: Text("Options"),
+            buttons: buttons)
+        
+        return action
+    }
+    
+    private func addWatchlist() {
+        let alertView = SPAlertView(title: "Added to watchlist", preset: .done)
+        let watchlistCompany = WatchlistCompany(context: viewContext)
+        watchlistCompany.name = name
+        watchlistCompany.symbol = symbol
+        do {
+            try viewContext.save()
+            print("Added to watchlist")
+            alertView.present(haptic: .success)
+        } catch {
+            print(error.localizedDescription)
+        }
+    }
+    
+    func removeWatchlist() {
+        let alertView = SPAlertView(title: "Removed from watchlist", preset: .done)
+        let watchlistSymbols = companies.map { $0.symbol }
+        let index = watchlistSymbols.firstIndex(of: symbol)
+        
+        viewContext.delete(companies[index!])
+        do {
+            try viewContext.save()
+            print("Deleted from watchlist")
+            alertView.present(haptic: .success)
+        } catch {
+            print(error.localizedDescription)
+        }
+    }
+}
+
+
+struct CreateButton_Previews: PreviewProvider {
+    static var previews: some View {
+        CreateButton(symbol: "aapl", name: "apple inc")
+    }
+}
--- a/lazybear/Views/HistoryList.swift	Sun Feb 07 19:31:49 2021 +0100
+++ b/lazybear/Views/HistoryList.swift	Sun Feb 07 20:19:34 2021 +0100
@@ -49,7 +49,7 @@
     private func alert() -> ActionSheet {
         let action = ActionSheet(
             title: Text("Clear history"),
-            message: Text("History will be claered from your device"),
+            message: Text("History will be cleared from your device"),
             buttons: [.cancel(Text("Cancel")), .destructive(Text("Clear history")) { delete() }])
         
         return action
--- a/lazybear/Views/ViewSelector.swift	Sun Feb 07 19:31:49 2021 +0100
+++ b/lazybear/Views/ViewSelector.swift	Sun Feb 07 20:19:34 2021 +0100
@@ -12,9 +12,10 @@
     @Binding var viewSelected: Int
     
     var body: some View {
-        Button(action: { self.showingActionSheet = true; print(self.showingActionSheet) }) {
+        Button(action: { self.showingActionSheet = true }) {
             Image(systemName: "ellipsis")
                 .imageScale(.large)
+                .frame(width: 30, height: 30)  // This frame change the tappable area
                 .actionSheet(isPresented: $showingActionSheet) { alert() }
                 
         }