diff LazyBear/Views/Profile/ProfileView.swift @ 393:0a4c399170c4

Implementing WatchlistCreator
author Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
date Sun, 02 May 2021 12:41:20 +0200
parents 13f3578def61
children 4c90e5b18632
line wrap: on
line diff
--- a/LazyBear/Views/Profile/ProfileView.swift	Sat May 01 13:11:32 2021 +0200
+++ b/LazyBear/Views/Profile/ProfileView.swift	Sun May 02 12:41:20 2021 +0200
@@ -20,11 +20,12 @@
     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 }
 
@@ -35,7 +36,6 @@
                                      intradayPrices: profile.data.intradayPrices,
                                      addOnDelete: true
                             )
-                            .onAppear { updateRows(symbols.count, filteredCompanies.count) }
                             .listRowInsets(EdgeInsets())
                         }
                     }
@@ -51,7 +51,7 @@
                 }
             }
             .fullScreenCover(isPresented: $showCreateNewWatchlist) {
-                CreateNewWatchlist()
+                WatchlistCreator()
                     .environment(\.managedObjectContext, self.moc)
             }
         } else {
@@ -60,6 +60,9 @@
         }
     }
     
+    /*
+     Get symbols in watchlists -> request
+     */
     private func prepareUrl(isInitRequest: Bool) {
         if watchlistCompanies.isEmpty {
             profile.showView = true
@@ -70,6 +73,7 @@
             var counter = 0
             for symbol in symbols {
                 counter += 1
+                
                 if counter == 1 {
                     url += symbol
                 } else {
@@ -79,16 +83,6 @@
             profile.request(url, isInitRequest: isInitRequest)
         }
     }
-    
-    /*
-     If Core Data changes, companies is not updated because the API request is not called ->
-     Check if symbols.count (Core Data) is equal to filteredCompanies -> if not -> call API
-     */
-    private func updateRows(_ numberOfCoreDataCompanies: Int, _ numberOfApiRequestedCompanies: Int) {
-        if numberOfCoreDataCompanies != numberOfApiRequestedCompanies {
-            prepareUrl(isInitRequest: true)  // Call API
-        }
-    }
 }
 
 struct ProfileView_Previews: PreviewProvider {