changeset 379:a7e2c5a7b4f6

Implement onDelete in watchlists
author Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
date Thu, 22 Apr 2021 23:44:20 +0200
parents 6802c2393203
children 30548c1a7f76
files LazyBear.xcodeproj/project.xcworkspace/xcuserdata/dennis.xcuserdatad/UserInterfaceState.xcuserstate LazyBear/Views/Global Helpers/StockItem.swift LazyBear/Views/Global Helpers/StockRow.swift LazyBear/Views/Home/ExtensiveList.swift LazyBear/Views/Home/Helpers/CurrencyRow.swift LazyBear/Views/Home/HomeView.swift LazyBear/Views/Profile/ProfileView.swift
diffstat 7 files changed, 48 insertions(+), 23 deletions(-) [+]
line wrap: on
line diff
Binary file LazyBear.xcodeproj/project.xcworkspace/xcuserdata/dennis.xcuserdatad/UserInterfaceState.xcuserstate has changed
--- a/LazyBear/Views/Global Helpers/StockItem.swift	Wed Apr 21 23:12:56 2021 +0200
+++ b/LazyBear/Views/Global Helpers/StockItem.swift	Thu Apr 22 23:44:20 2021 +0200
@@ -16,12 +16,13 @@
     var company: QuoteModel
     var intradayPrices: [IntradayPriceModel]?
     var orientation: OrientationView
+    var hidePriceView: Bool?
     
     var body: some View {
         if orientation == .vertical {
             return AnyView(VerticalStockRow(symbol: symbol, company: company, intradayPrices: intradayPrices))
         } else {
-            return AnyView(HorizontalStockRow(symbol: symbol, company: company, intradayPrices: intradayPrices))
+            return AnyView(HorizontalStockRow(symbol: symbol, company: company, intradayPrices: intradayPrices, hidePriceView: hidePriceView ?? false))
         }
     }
 }
@@ -85,6 +86,7 @@
     var symbol: String
     var company: QuoteModel
     var intradayPrices: [IntradayPriceModel]?
+    var hidePriceView: Bool
     
     var body: some View {
         HStack {
@@ -100,17 +102,19 @@
             }
             
             Spacer()
-            if let prices = intradayPrices?.compactMap { $0.open } {
-                LineView(data: prices)
-                    .foregroundColor(company.changePercent < 0 ? .red: .green)
-                    .frame(width: 80)
-                    .padding(.vertical, 10)
-                    .padding(.leading)
+            if !hidePriceView {
+                if let prices = intradayPrices?.compactMap { $0.open } {
+                    LineView(data: prices)
+                        .foregroundColor(company.changePercent < 0 ? .red: .green)
+                        .frame(width: 80)
+                        .padding(.vertical, 10)
+                        .padding(.leading)
+                }
+                
+                PriceView(latestPrice: company.latestPrice, changePercent: company.changePercent, align: .trailing)
+                    // Center PriceView with the other rows
+                    .frame(minWidth: 80, alignment: .trailing)
             }
-            
-            PriceView(latestPrice: company.latestPrice, changePercent: company.changePercent, align: .trailing)
-                // Avoid moving LineView along the HStack when numbers increases
-                .frame(minWidth: 80, alignment: .trailing)
         }
         .padding(5)
     }
--- a/LazyBear/Views/Global Helpers/StockRow.swift	Wed Apr 21 23:12:56 2021 +0200
+++ b/LazyBear/Views/Global Helpers/StockRow.swift	Thu Apr 22 23:44:20 2021 +0200
@@ -12,6 +12,7 @@
     var listName: String
     var list: [String: QuoteModel]
     var intradayPrices: [String: [IntradayPriceModel]]?
+    var addOnDelete: Bool
     
     @State private var showExtensiveList = false
     
@@ -48,7 +49,7 @@
         }
         .padding(.bottom)
         .sheet(isPresented: $showExtensiveList) {
-            ExtensiveList(listName: listName, list: list, intradayPrices: intradayPrices, latestCurrencies: nil)
+            ExtensiveList(listName: listName, list: list, intradayPrices: intradayPrices, latestCurrencies: nil, addOnDelete: addOnDelete)
         }
     }
 }
@@ -59,7 +60,7 @@
         StockRow(
             listName: "Gainers",
             list: ["AAPL": QuoteModel(changePercent: 0.03, companyName: "Apple Inc", latestPrice: 130.3)],
-            intradayPrices: ["AAPL": [IntradayPriceModel(open: 130.2)]]
+            intradayPrices: ["AAPL": [IntradayPriceModel(open: 130.2)]], addOnDelete: false
         )
     }
 }
--- a/LazyBear/Views/Home/ExtensiveList.swift	Wed Apr 21 23:12:56 2021 +0200
+++ b/LazyBear/Views/Home/ExtensiveList.swift	Thu Apr 22 23:44:20 2021 +0200
@@ -12,21 +12,33 @@
     var list: [String: QuoteModel]?
     var intradayPrices: [String: [IntradayPriceModel]]?
     var latestCurrencies: [String: CurrencyModel]?
+    var addOnDelete: Bool
     
-    @Environment(\.presentationMode) var extensiveListPresent
+    @Environment(\.presentationMode) private var extensiveListPresent
+    @State var isEditMode: EditMode = .inactive
     
     var body: some View {
         NavigationView {
             VStack {
                 if let list = list {
-                    List(Array(list.keys.sorted()), id: \.self) { companySymbol in
-                        StockItem(symbol: companySymbol, company: list[companySymbol]!, intradayPrices: intradayPrices?[companySymbol], orientation: .horizontal)
+                    List {
+                        ForEach(Array(list.keys.sorted()), id: \.self) { companySymbol in
+                                StockItem(symbol: companySymbol,
+                                          company: list[companySymbol]!,
+                                          intradayPrices: intradayPrices?[companySymbol],
+                                          orientation: .horizontal,
+                                          hidePriceView: self.isEditMode == .active  // Hide on EditMode
+                                )
+                            
+                        }
+                        .onDelete(perform: addOnDelete ? removeCompany: nil)
                     }
                 }
                 
                 if let latestCurrencies = latestCurrencies {
                     List(Array(latestCurrencies.keys.sorted()), id: \.self) { currencySymbol in
                         CurrencyListItem(currencySymbol: currencySymbol, currency: latestCurrencies[currencySymbol]!)
+                        
                     }
                 }
             }
@@ -39,13 +51,22 @@
                             .imageScale(.large)
                     }
                 }
+                ToolbarItem(placement: .navigationBarLeading) {
+                    if addOnDelete {
+                        EditButton()
+                    }
+                }
             }
+            .environment(\.editMode, self.$isEditMode)  // Always after Toolbar
         }
     }
+    private func removeCompany(at offsets: IndexSet) {
+        print("Hello")
+    }
 }
 
 struct ExtensiveList_Previews: PreviewProvider {
     static var previews: some View {
-        ExtensiveList(listName: "List name")
+        ExtensiveList(listName: "List name", addOnDelete: false)
     }
 }
--- a/LazyBear/Views/Home/Helpers/CurrencyRow.swift	Wed Apr 21 23:12:56 2021 +0200
+++ b/LazyBear/Views/Home/Helpers/CurrencyRow.swift	Thu Apr 22 23:44:20 2021 +0200
@@ -43,7 +43,7 @@
             }
         }
         .sheet(isPresented: $showExtensiveList) {
-            ExtensiveList(listName: "Currencies", latestCurrencies: latestCurrencies)
+            ExtensiveList(listName: "Currencies", latestCurrencies: latestCurrencies, addOnDelete: false)
         }
     }
 }
--- a/LazyBear/Views/Home/HomeView.swift	Wed Apr 21 23:12:56 2021 +0200
+++ b/LazyBear/Views/Home/HomeView.swift	Thu Apr 22 23:44:20 2021 +0200
@@ -33,15 +33,15 @@
                     
                     if let lists = home.data.lists {
                         if let gainers = lists.gainers {
-                            StockRow(listName: "Gainers", list: gainers, intradayPrices: home.data.intradayPrices)
+                            StockRow(listName: "Gainers", list: gainers, intradayPrices: home.data.intradayPrices, addOnDelete: false)
                                 .listRowInsets(EdgeInsets())
                         }
                         if let losers = lists.losers {
-                            StockRow(listName: "Losers", list: losers, intradayPrices: home.data.intradayPrices)
+                            StockRow(listName: "Losers", list: losers, intradayPrices: home.data.intradayPrices, addOnDelete: false)
                                 .listRowInsets(EdgeInsets())
                         }
                         if let mostActive = lists.mostactive {
-                            StockRow(listName: "Most active", list: mostActive, intradayPrices: home.data.intradayPrices)
+                            StockRow(listName: "Most active", list: mostActive, intradayPrices: home.data.intradayPrices, addOnDelete: false)
                                 .listRowInsets(EdgeInsets())
                         }
                     }
--- a/LazyBear/Views/Profile/ProfileView.swift	Wed Apr 21 23:12:56 2021 +0200
+++ b/LazyBear/Views/Profile/ProfileView.swift	Thu Apr 22 23:44:20 2021 +0200
@@ -26,7 +26,7 @@
                         
                         if let companies = profile.data.quotes {
                             let filteredCompanies = companies.filter({ symbols.contains($0.key) })
-                            StockRow(listName: watchlist, list: filteredCompanies, intradayPrices: profile.data.intradayPrices)
+                            StockRow(listName: watchlist, list: filteredCompanies, intradayPrices: profile.data.intradayPrices, addOnDelete: true)
                                 .listRowInsets(EdgeInsets())
                         }
                     }
@@ -43,7 +43,6 @@
     private func prepareUrl() {
         let symbols = watchlistCompanies.map { $0.symbol }  // Get symbols in watchlists
         var url = "https://api.lazybear.app/profile/type=init/symbols="
-        print(url)
 
         var counter = 0
         for symbol in symbols {