diff LazyBear/Views/Search/Helpers/SearchedCompanyItem.swift @ 417:5f21f7c23c5e

Add comments and clean code
author Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
date Fri, 11 Jun 2021 11:37:42 +0200
parents 4c90e5b18632
children 4effac4733b0
line wrap: on
line diff
--- a/LazyBear/Views/Search/Helpers/SearchedCompanyItem.swift	Wed Jun 09 20:26:28 2021 +0200
+++ b/LazyBear/Views/Search/Helpers/SearchedCompanyItem.swift	Fri Jun 11 11:37:42 2021 +0200
@@ -14,12 +14,12 @@
     @FetchRequest(entity: WatchlistCompany.entity(), sortDescriptors: [])
     var watchlistCompany: FetchedResults<WatchlistCompany>
     
-    @State private var showingActionSheet = false
+    @State private var showingWatchlistSelector = false
     
     var body: some View {
-        let watchlistSymbols = watchlistCompany.map { $0.symbol }
         HStack {
-            Button(action: { self.showingActionSheet = true }) {
+            Button(action: { self.showingWatchlistSelector = true }) {
+                let watchlistSymbols = watchlistCompany.map { $0.symbol }
                 if watchlistSymbols.contains(company.symbol!) {
                     Image(systemName: "star.fill")
                         .foregroundColor(.yellow)
@@ -49,12 +49,15 @@
                 Text(company.region!)
             }
         }
-        .actionSheet(isPresented: $showingActionSheet) {
+        .actionSheet(isPresented: $showingWatchlistSelector) {
             ActionSheet(title: Text("Add to watchlist"), message: Text("Select"), buttons: generateButtons())
         }
     }
     
-    // Get watchlist names -> generate buttons
+    /*
+     Generate buttons for each watchlist to let the user selects to which watchlist
+     he wants to add the company
+     */
     private func generateButtons() -> [ActionSheet.Button] {
         var actionButtons = [ActionSheet.Button]()
         let watchlists = Set(watchlistCompany.map { $0.watchlist })
@@ -72,7 +75,9 @@
         return actionButtons
     }
     
-    // Add to watchlist
+    /*
+     When the user taps the watchlist -> save the company to CoreData
+     */
     private func addCompany(_ symbol: String, _ name: String, _ watchlist: String) {
         let watchlistCompany = WatchlistCompany(context: moc)
         watchlistCompany.symbol = symbol