changeset 394:4c90e5b18632

Fixes #46
author Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
date Fri, 07 May 2021 11:00:53 +0200
parents 0a4c399170c4
children a0cf8fe47044
files LazyBear.xcodeproj/project.pbxproj LazyBear.xcodeproj/project.xcworkspace/xcuserdata/dennis.xcuserdatad/UserInterfaceState.xcuserstate LazyBear/Global Models/WatchlistCompany+CoreDataProperties.swift LazyBear/Views/Global Helpers/Company list/ExtensiveList.swift LazyBear/Views/Global Helpers/Company list/Helpers/ToolbarMenu.swift LazyBear/Views/Home/TradingDates.swift LazyBear/Views/Profile/Helpers/WatchlistCreatorList.swift LazyBear/Views/Profile/Helpers/WatchlistLists.swift LazyBear/Views/Profile/Networking/Profile.swift LazyBear/Views/Profile/ProfileView.swift LazyBear/Views/Search/Helpers/SearchedCompanyItem.swift
diffstat 11 files changed, 45 insertions(+), 79 deletions(-) [+]
line wrap: on
line diff
--- a/LazyBear.xcodeproj/project.pbxproj	Sun May 02 12:41:20 2021 +0200
+++ b/LazyBear.xcodeproj/project.pbxproj	Fri May 07 11:00:53 2021 +0200
@@ -32,6 +32,7 @@
 		955E733C262356F3005652FF /* HomeResponse.swift in Sources */ = {isa = PBXBuildFile; fileRef = 955E733B262356F3005652FF /* HomeResponse.swift */; };
 		9562404C263C766D00C6C511 /* WatchlistCreator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9562404B263C766D00C6C511 /* WatchlistCreator.swift */; };
 		9562404E263C7D8800C6C511 /* WatchlistCreatorClass.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9562404D263C7D8800C6C511 /* WatchlistCreatorClass.swift */; };
+		95629DA02645298E007AF020 /* Profile.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95629D9F2645298E007AF020 /* Profile.swift */; };
 		95672B8F25DDA54700DCBE4A /* LazyBearApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95672B8E25DDA54700DCBE4A /* LazyBearApp.swift */; };
 		95672B9325DDA54700DCBE4A /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 95672B9225DDA54700DCBE4A /* Assets.xcassets */; };
 		95672B9625DDA54700DCBE4A /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 95672B9525DDA54700DCBE4A /* Preview Assets.xcassets */; };
@@ -50,7 +51,6 @@
 		95A4B933263E9F810056F036 /* Introspect in Frameworks */ = {isa = PBXBuildFile; productRef = 95A4B932263E9F810056F036 /* Introspect */; };
 		95A4B935263EA31C0056F036 /* WatchlistCreatorSearchBar.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95A4B934263EA31C0056F036 /* WatchlistCreatorSearchBar.swift */; };
 		95A4B937263EA5C20056F036 /* WatchlistCreatorRow.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95A4B936263EA5C20056F036 /* WatchlistCreatorRow.swift */; };
-		95A4B939263EB1C20056F036 /* WatchlistLists.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95A4B938263EB1C20056F036 /* WatchlistLists.swift */; };
 		95A5186A26185AAB0002D27C /* GenericRequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95A5186926185AAB0002D27C /* GenericRequest.swift */; };
 		95A5188626186F590002D27C /* PriceView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95A5188526186F590002D27C /* PriceView.swift */; };
 		95A7C066261639E0003E2EC1 /* SearchView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95A7C065261639DF003E2EC1 /* SearchView.swift */; };
@@ -58,7 +58,6 @@
 		95AD4A2D26078C1400498079 /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95AD4A2C26078C1400498079 /* ContentView.swift */; };
 		95BD2FAE26341BD1008B6752 /* TextfieldAlert.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95BD2FAD26341BD1008B6752 /* TextfieldAlert.swift */; };
 		95BD2FB326341D36008B6752 /* BlurBackground.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95BD2FB226341D36008B6752 /* BlurBackground.swift */; };
-		95C8C0DB262A36990082D1D9 /* Profile.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95C8C0DA262A36990082D1D9 /* Profile.swift */; };
 		95C8C0E0262A369F0082D1D9 /* ProfileResponse.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95C8C0DF262A369F0082D1D9 /* ProfileResponse.swift */; };
 		95D308F82624B3A400A39F77 /* CurrencyItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95D308F72624B3A400A39F77 /* CurrencyItem.swift */; };
 		95E6C57626345CE700AC1518 /* WatchlistManagerTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95E6C57526345CE700AC1518 /* WatchlistManagerTest.swift */; };
@@ -115,6 +114,7 @@
 		9562404A263C72E800C6C511 /* README.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = "<group>"; };
 		9562404B263C766D00C6C511 /* WatchlistCreator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WatchlistCreator.swift; sourceTree = "<group>"; };
 		9562404D263C7D8800C6C511 /* WatchlistCreatorClass.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WatchlistCreatorClass.swift; sourceTree = "<group>"; };
+		95629D9F2645298E007AF020 /* Profile.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Profile.swift; sourceTree = "<group>"; };
 		95672B8B25DDA54700DCBE4A /* LazyBear.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = LazyBear.app; sourceTree = BUILT_PRODUCTS_DIR; };
 		95672B8E25DDA54700DCBE4A /* LazyBearApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LazyBearApp.swift; sourceTree = "<group>"; };
 		95672B9225DDA54700DCBE4A /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
@@ -135,7 +135,6 @@
 		95A4B92F263E9F530056F036 /* WatchlistCreatorList.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WatchlistCreatorList.swift; sourceTree = "<group>"; };
 		95A4B934263EA31C0056F036 /* WatchlistCreatorSearchBar.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WatchlistCreatorSearchBar.swift; sourceTree = "<group>"; };
 		95A4B936263EA5C20056F036 /* WatchlistCreatorRow.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WatchlistCreatorRow.swift; sourceTree = "<group>"; };
-		95A4B938263EB1C20056F036 /* WatchlistLists.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WatchlistLists.swift; sourceTree = "<group>"; };
 		95A5186926185AAB0002D27C /* GenericRequest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GenericRequest.swift; sourceTree = "<group>"; };
 		95A5188526186F590002D27C /* PriceView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PriceView.swift; sourceTree = "<group>"; };
 		95A7C065261639DF003E2EC1 /* SearchView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SearchView.swift; sourceTree = "<group>"; };
@@ -143,7 +142,6 @@
 		95AD4A2C26078C1400498079 /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = "<group>"; };
 		95BD2FAD26341BD1008B6752 /* TextfieldAlert.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TextfieldAlert.swift; sourceTree = "<group>"; };
 		95BD2FB226341D36008B6752 /* BlurBackground.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BlurBackground.swift; sourceTree = "<group>"; };
-		95C8C0DA262A36990082D1D9 /* Profile.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Profile.swift; sourceTree = "<group>"; };
 		95C8C0DF262A369F0082D1D9 /* ProfileResponse.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProfileResponse.swift; sourceTree = "<group>"; };
 		95D308F72624B3A400A39F77 /* CurrencyItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CurrencyItem.swift; sourceTree = "<group>"; };
 		95E6C57526345CE700AC1518 /* WatchlistManagerTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WatchlistManagerTest.swift; sourceTree = "<group>"; };
@@ -405,7 +403,6 @@
 		95BD2F91263418F7008B6752 /* Helpers */ = {
 			isa = PBXGroup;
 			children = (
-				95A4B938263EB1C20056F036 /* WatchlistLists.swift */,
 				95BD2FAD26341BD1008B6752 /* TextfieldAlert.swift */,
 				9562404B263C766D00C6C511 /* WatchlistCreator.swift */,
 				9562404D263C7D8800C6C511 /* WatchlistCreatorClass.swift */,
@@ -419,7 +416,7 @@
 		95C8C0D9262A31DA0082D1D9 /* Networking */ = {
 			isa = PBXGroup;
 			children = (
-				95C8C0DA262A36990082D1D9 /* Profile.swift */,
+				95629D9F2645298E007AF020 /* Profile.swift */,
 				95C8C0DF262A369F0082D1D9 /* ProfileResponse.swift */,
 			);
 			path = Networking;
@@ -630,11 +627,10 @@
 				95C8C0E0262A369F0082D1D9 /* ProfileResponse.swift in Sources */,
 				95A4B935263EA31C0056F036 /* WatchlistCreatorSearchBar.swift in Sources */,
 				95AD4A2D26078C1400498079 /* ContentView.swift in Sources */,
-				95A4B939263EB1C20056F036 /* WatchlistLists.swift in Sources */,
+				95629DA02645298E007AF020 /* Profile.swift in Sources */,
 				95A4B930263E9F530056F036 /* WatchlistCreatorList.swift in Sources */,
 				9562404E263C7D8800C6C511 /* WatchlistCreatorClass.swift in Sources */,
 				95721DB4262787EF00EC527B /* ExtensiveList.swift in Sources */,
-				95C8C0DB262A36990082D1D9 /* Profile.swift in Sources */,
 				952045152610C7C600A76362 /* ConvertEpoch.swift in Sources */,
 				95BD2FAE26341BD1008B6752 /* TextfieldAlert.swift in Sources */,
 				95A07F5D26305A8F009865AA /* IntradayPricesModel.swift in Sources */,
Binary file LazyBear.xcodeproj/project.xcworkspace/xcuserdata/dennis.xcuserdatad/UserInterfaceState.xcuserstate has changed
--- a/LazyBear/Global Models/WatchlistCompany+CoreDataProperties.swift	Sun May 02 12:41:20 2021 +0200
+++ b/LazyBear/Global Models/WatchlistCompany+CoreDataProperties.swift	Fri May 07 11:00:53 2021 +0200
@@ -25,3 +25,10 @@
 extension WatchlistCompany : Identifiable {
 
 }
+
+extension WatchlistCompany {
+    static func == (lhs: WatchlistCompany, rhs: WatchlistCompany) -> Bool {
+        print("Custom equation has been called")
+        return lhs.id == rhs.id
+    }
+}
--- a/LazyBear/Views/Global Helpers/Company list/ExtensiveList.swift	Sun May 02 12:41:20 2021 +0200
+++ b/LazyBear/Views/Global Helpers/Company list/ExtensiveList.swift	Fri May 07 11:00:53 2021 +0200
@@ -72,9 +72,6 @@
                     secondaryButton: .cancel()
                 )
             }
-            .sheet(isPresented: $showSearchView) {
-                SearchView()
-            }
             .navigationTitle(listName)
             .navigationBarTitleDisplayMode(.inline)
             .toolbar {
@@ -90,10 +87,7 @@
                 }
                 ToolbarItem(placement: .navigationBarTrailing) {
                     if addOnDelete {
-                        ToolbarMenu(showRenameAction: $showRenameAction,
-                                    showSearchView: $showSearchView,
-                                    showDeleteAlert: $showDeleteAlert
-                        )
+                        ToolbarMenu(showRenameAction: $showRenameAction, showDeleteAlert: $showDeleteAlert)
                     }
                 }
             }
--- a/LazyBear/Views/Global Helpers/Company list/Helpers/ToolbarMenu.swift	Sun May 02 12:41:20 2021 +0200
+++ b/LazyBear/Views/Global Helpers/Company list/Helpers/ToolbarMenu.swift	Fri May 07 11:00:53 2021 +0200
@@ -9,7 +9,6 @@
 
 struct ToolbarMenu: View {
     @Binding var showRenameAction: Bool
-    @Binding var showSearchView: Bool
     @Binding var showDeleteAlert: Bool
     
     var body: some View {
@@ -18,10 +17,6 @@
                 Button(action: { showRenameAction = true }) {
                     Label("Rename list", systemImage: "square.and.pencil")
                 }
-
-                Button(action: { showSearchView = true }) {
-                    Label("Add company", systemImage: "plus")
-                }
             }
 
             Section(header: Text("Secondary actions")) {
@@ -39,6 +34,6 @@
 
 struct ToolbarMenu_Previews: PreviewProvider {
     static var previews: some View {
-        ToolbarMenu(showRenameAction: .constant(false), showSearchView: .constant(false), showDeleteAlert: .constant(false))
+        ToolbarMenu(showRenameAction: .constant(false), showDeleteAlert: .constant(false))
     }
 }
--- a/LazyBear/Views/Home/TradingDates.swift	Sun May 02 12:41:20 2021 +0200
+++ b/LazyBear/Views/Home/TradingDates.swift	Fri May 07 11:00:53 2021 +0200
@@ -25,7 +25,7 @@
             .navigationTitle("Holiday dates")
             .navigationBarTitleDisplayMode(.inline)
             .toolbar {
-                ToolbarItem(placement: .navigationBarTrailing) {
+                ToolbarItem(placement: .navigationBarLeading) {
                     Button(action: { tradingDatesPresent.wrappedValue.dismiss() }) {
                         Image(systemName: "multiply")
                             .imageScale(.large)
--- a/LazyBear/Views/Profile/Helpers/WatchlistCreatorList.swift	Sun May 02 12:41:20 2021 +0200
+++ b/LazyBear/Views/Profile/Helpers/WatchlistCreatorList.swift	Fri May 07 11:00:53 2021 +0200
@@ -13,6 +13,7 @@
     @State private var companies = [SearchResponse]()
     
     @Environment(\.presentationMode) private var presentationMode
+    @Environment(\.managedObjectContext) private var moc
     
     var body: some View {
         NavigationView {
@@ -21,6 +22,7 @@
                 
                 List(companies, id: \.self) { company in
                     WatchlistCreatorRow(company: company, presentationMode: presentationMode, watchlistCreatorClass: watchlistCreatorClass)
+                        .environment(\.managedObjectContext, self.moc)
                 }
             }
             .navigationTitle("Search")
--- a/LazyBear/Views/Profile/Helpers/WatchlistLists.swift	Sun May 02 12:41:20 2021 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,38 +0,0 @@
-//
-//  WatchlistLists.swift
-//  LazyBear
-//
-//  Created by Dennis Concepción Martín on 2/5/21.
-//
-
-import SwiftUI
-import CoreData
-
-struct WatchlistLists: View {
-    @ObservedObject var profile: Profile
-    var watchlistCompanies: [WatchlistCompany]
-    
-    var body: some View {
-        Text("Hello, World!")
-    }
-    
-    private func prepareUrl(isInitRequest: Bool) {
-        if watchlistCompanies.isEmpty {
-            profile.showView = true
-        } else {
-            let symbols = watchlistCompanies.map { $0.symbol }  // Get symbols in watchlists
-            var url = "https://api.lazybear.app/profile/type=init/symbols="
-
-            var counter = 0
-            for symbol in symbols {
-                counter += 1
-                if counter == 1 {
-                    url += symbol
-                } else {
-                    url += ",\(symbol)"
-                }
-            }
-            profile.request(url, isInitRequest: isInitRequest)
-        }
-    }
-}
--- a/LazyBear/Views/Profile/Networking/Profile.swift	Sun May 02 12:41:20 2021 +0200
+++ b/LazyBear/Views/Profile/Networking/Profile.swift	Fri May 07 11:00:53 2021 +0200
@@ -2,7 +2,7 @@
 //  Profile.swift
 //  LazyBear
 //
-//  Created by Dennis Concepción Martín on 16/4/21.
+//  Created by Dennis Concepción Martín on 7/5/21.
 //
 
 import SwiftUI
--- a/LazyBear/Views/Profile/ProfileView.swift	Sun May 02 12:41:20 2021 +0200
+++ b/LazyBear/Views/Profile/ProfileView.swift	Fri May 07 11:00:53 2021 +0200
@@ -10,35 +10,32 @@
 
 struct ProfileView: View {
     @ObservedObject var profile = Profile()
-    
     @Environment(\.managedObjectContext) private var moc
     @FetchRequest(entity: WatchlistCompany.entity(), sortDescriptors: [])
     var watchlistCompanies: FetchedResults<WatchlistCompany>
-    
+
     @State private var showCreateNewWatchlist = false
 
     var body: some View {
         if profile.showView {
             NavigationView {
-                
                 List {
-                    
                     // Get Watchlist names -> Create rows for each watchlist -> in each row, show companies
                     let watchlists = Set(watchlistCompanies.map { $0.watchlist })  // Set -> avoid duplicates names
-
-                    ForEach(Array(watchlists), id: \.self) { watchlist in
-                        let symbols = watchlistCompanies.filter({ $0.watchlist == watchlist }).map { $0.symbol }
-
+                    
+                    ForEach(Array(watchlists).sorted(), id: \.self) { listName in
+                        let symbols = watchlistCompanies.filter({ $0.watchlist == listName }).map { $0.symbol }
+                        
                         if let companies = profile.data.quotes {
-                            let filteredCompanies = companies.filter({ symbols.contains($0.key) })
-                            StockRow(listName: watchlist,
-                                     list: filteredCompanies,
-                                     intradayPrices: profile.data.intradayPrices,
-                                     addOnDelete: true
-                            )
-                            .listRowInsets(EdgeInsets())
+                            // Select from API requested companies only the ones withing the watchlist
+                            let list = companies.filter({ symbols.contains($0.key) })
+                            StockRow(listName: listName, list: list, intradayPrices: profile.data.intradayPrices, addOnDelete: true)
                         }
                     }
+                    .listRowInsets(EdgeInsets())
+                    .onAppear {  // Refresh API requested companies when Core Data changes
+                        refreshList()
+                    }
                 }
                 .navigationTitle("My profile")
                 .navigationBarTitleDisplayMode(.inline)
@@ -61,13 +58,13 @@
     }
     
     /*
-     Get symbols in watchlists -> request
+     Get symbols in watchlists -> Prepare url -> Request
      */
     private func prepareUrl(isInitRequest: Bool) {
         if watchlistCompanies.isEmpty {
             profile.showView = true
         } else {
-            let symbols = watchlistCompanies.map { $0.symbol }  // Get symbols in watchlists
+            let symbols = watchlistCompanies.map { $0.symbol }
             var url = "https://api.lazybear.app/profile/type=init/symbols="
 
             var counter = 0
@@ -83,6 +80,19 @@
             profile.request(url, isInitRequest: isInitRequest)
         }
     }
+    
+    /*
+     When a company is added to a watchlist or a new watchlist is created -> call function
+     to make the API request and refresh correctly the list
+     */
+    private func refreshList() {
+        print("Companies in watchlist -> \(watchlistCompanies.count)")
+        print("Companies requested -> \(profile.data.quotes!.count)")
+        
+        if profile.data.quotes!.count < watchlistCompanies.count {
+            prepareUrl(isInitRequest: true)
+        }
+    }
 }
 
 struct ProfileView_Previews: PreviewProvider {
--- a/LazyBear/Views/Search/Helpers/SearchedCompanyItem.swift	Sun May 02 12:41:20 2021 +0200
+++ b/LazyBear/Views/Search/Helpers/SearchedCompanyItem.swift	Fri May 07 11:00:53 2021 +0200
@@ -50,7 +50,7 @@
             }
         }
         .actionSheet(isPresented: $showingActionSheet) {
-            ActionSheet(title: Text("Your watchlists"), message: Text("Select"), buttons: generateButtons())
+            ActionSheet(title: Text("Add to watchlist"), message: Text("Select"), buttons: generateButtons())
         }
     }