changeset 357:eb97439e46cd

Implement ExtensiveList in HomeView
author Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
date Thu, 15 Apr 2021 23:37:25 +0200
parents 5385a8f8cc5c
children 280cbc5653b5
files LazyBear.xcodeproj/project.pbxproj LazyBear.xcodeproj/project.xcworkspace/xcuserdata/dennis.xcuserdatad/UserInterfaceState.xcuserstate LazyBear/Views/Global Helpers/PriceView.swift LazyBear/Views/Global Helpers/StockItem.swift LazyBear/Views/Global Helpers/StockListItem.swift LazyBear/Views/Global Helpers/StockRectangleRow.swift LazyBear/Views/Global Helpers/StockRow.swift LazyBear/Views/Home/ExtensiveList.swift LazyBear/Views/Home/Helpers/CurrencyItem.swift LazyBear/Views/Home/Helpers/CurrencyListItem.swift LazyBear/Views/Home/HomeView.swift
diffstat 11 files changed, 157 insertions(+), 124 deletions(-) [+]
line wrap: on
line diff
--- a/LazyBear.xcodeproj/project.pbxproj	Wed Apr 14 23:18:07 2021 +0200
+++ b/LazyBear.xcodeproj/project.pbxproj	Thu Apr 15 23:37:25 2021 +0200
@@ -26,7 +26,7 @@
 		9550443A26111B2B000E0BCB /* HomeView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9550443926111B2B000E0BCB /* HomeView.swift */; };
 		9550444326111E7A000E0BCB /* SectorRow.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9550444226111E7A000E0BCB /* SectorRow.swift */; };
 		9550444626111EE5000E0BCB /* SectorItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9550444526111EE5000E0BCB /* SectorItem.swift */; };
-		9550444926111FC9000E0BCB /* StockRectangleRow.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9550444826111FC9000E0BCB /* StockRectangleRow.swift */; };
+		9550444926111FC9000E0BCB /* StockRow.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9550444826111FC9000E0BCB /* StockRow.swift */; };
 		9550444C26111FED000E0BCB /* StockItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9550444B26111FED000E0BCB /* StockItem.swift */; };
 		955E732D26235561005652FF /* QuoteModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 955E732C26235561005652FF /* QuoteModel.swift */; };
 		955E7330262355FD005652FF /* SectorPerformanceModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 955E732F262355FD005652FF /* SectorPerformanceModel.swift */; };
@@ -42,7 +42,6 @@
 		95721DA6262761E700EC527B /* CurrencyRow.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95721DA5262761E700EC527B /* CurrencyRow.swift */; };
 		95721DB4262787EF00EC527B /* ExtensiveList.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95721DB3262787EF00EC527B /* ExtensiveList.swift */; };
 		95721DB826278EC100EC527B /* CurrencyListItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95721DB726278EC100EC527B /* CurrencyListItem.swift */; };
-		95721DBB26278EDE00EC527B /* StockListItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95721DBA26278EDE00EC527B /* StockListItem.swift */; };
 		958A735225E0170900FD7ECA /* CloudKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 958A735125E0170900FD7ECA /* CloudKit.framework */; };
 		95A5186A26185AAB0002D27C /* GenericRequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95A5186926185AAB0002D27C /* GenericRequest.swift */; };
 		95A5188626186F590002D27C /* PriceView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95A5188526186F590002D27C /* PriceView.swift */; };
@@ -75,7 +74,7 @@
 		9550443926111B2B000E0BCB /* HomeView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HomeView.swift; sourceTree = "<group>"; };
 		9550444226111E7A000E0BCB /* SectorRow.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SectorRow.swift; sourceTree = "<group>"; };
 		9550444526111EE5000E0BCB /* SectorItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SectorItem.swift; sourceTree = "<group>"; };
-		9550444826111FC9000E0BCB /* StockRectangleRow.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StockRectangleRow.swift; sourceTree = "<group>"; };
+		9550444826111FC9000E0BCB /* StockRow.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StockRow.swift; sourceTree = "<group>"; };
 		9550444B26111FED000E0BCB /* StockItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StockItem.swift; sourceTree = "<group>"; };
 		955E732C26235561005652FF /* QuoteModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = QuoteModel.swift; sourceTree = "<group>"; };
 		955E732F262355FD005652FF /* SectorPerformanceModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SectorPerformanceModel.swift; sourceTree = "<group>"; };
@@ -93,7 +92,6 @@
 		95721DA5262761E700EC527B /* CurrencyRow.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CurrencyRow.swift; sourceTree = "<group>"; };
 		95721DB3262787EF00EC527B /* ExtensiveList.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ExtensiveList.swift; sourceTree = "<group>"; };
 		95721DB726278EC100EC527B /* CurrencyListItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CurrencyListItem.swift; sourceTree = "<group>"; };
-		95721DBA26278EDE00EC527B /* StockListItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StockListItem.swift; sourceTree = "<group>"; };
 		958A734E25E016FD00FD7ECA /* LazyBear.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = LazyBear.entitlements; sourceTree = "<group>"; };
 		958A735125E0170900FD7ECA /* CloudKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CloudKit.framework; path = System/Library/Frameworks/CloudKit.framework; sourceTree = SDKROOT; };
 		95A5186926185AAB0002D27C /* GenericRequest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GenericRequest.swift; sourceTree = "<group>"; };
@@ -267,9 +265,8 @@
 				95A5188526186F590002D27C /* PriceView.swift */,
 				950D0E2F2618B34600D17AD7 /* BlurBackground.swift */,
 				950D0E352618B61000D17AD7 /* BackgroundShadow.swift */,
-				9550444826111FC9000E0BCB /* StockRectangleRow.swift */,
+				9550444826111FC9000E0BCB /* StockRow.swift */,
 				9550444B26111FED000E0BCB /* StockItem.swift */,
-				95721DBA26278EDE00EC527B /* StockListItem.swift */,
 			);
 			path = "Global Helpers";
 			sourceTree = "<group>";
@@ -417,7 +414,7 @@
 				95A5186A26185AAB0002D27C /* GenericRequest.swift in Sources */,
 				95E745DA2614624500744A1E /* HomeDataPreview.swift in Sources */,
 				95A5188626186F590002D27C /* PriceView.swift in Sources */,
-				9550444926111FC9000E0BCB /* StockRectangleRow.swift in Sources */,
+				9550444926111FC9000E0BCB /* StockRow.swift in Sources */,
 				9550444326111E7A000E0BCB /* SectorRow.swift in Sources */,
 				95721DB826278EC100EC527B /* CurrencyListItem.swift in Sources */,
 				955E7330262355FD005652FF /* SectorPerformanceModel.swift in Sources */,
@@ -451,7 +448,6 @@
 				95AD4A2D26078C1400498079 /* ContentView.swift in Sources */,
 				95115E982625D15900BDC0F8 /* CurrencyModel.swift in Sources */,
 				95721DB4262787EF00EC527B /* ExtensiveList.swift in Sources */,
-				95721DBB26278EDE00EC527B /* StockListItem.swift in Sources */,
 				955E73362623565F005652FF /* TradingDatesModel.swift in Sources */,
 				952045152610C7C600A76362 /* ConvertEpoch.swift in Sources */,
 			);
Binary file LazyBear.xcodeproj/project.xcworkspace/xcuserdata/dennis.xcuserdatad/UserInterfaceState.xcuserstate has changed
--- a/LazyBear/Views/Global Helpers/PriceView.swift	Wed Apr 14 23:18:07 2021 +0200
+++ b/LazyBear/Views/Global Helpers/PriceView.swift	Thu Apr 15 23:37:25 2021 +0200
@@ -10,16 +10,17 @@
 struct PriceView: View {
     var latestPrice: Double
     var changePercent: Double
+    var align: HorizontalAlignment
     
     var body: some View {
-        VStack(alignment: .leading) {
+        VStack(alignment: align) {
             Text("$\(latestPrice, specifier: "%.2f")")
                 .foregroundColor(changePercent < 0 ? .red: .green)
                 .fontWeight(.semibold)
-                .padding(.top)
             
             Text("\(changePercent*100, specifier: "%.2f")%")
                 .foregroundColor(changePercent < 0 ? .red: .green)
+                .font(.callout)
                 .fontWeight(.semibold)
         }
     }
@@ -28,6 +29,6 @@
 
 struct PriceView_Previews: PreviewProvider {
     static var previews: some View {
-        PriceView(latestPrice: 120.30, changePercent: 0.03)
+        PriceView(latestPrice: 120.30, changePercent: 0.03, align: .leading)
     }
 }
--- a/LazyBear/Views/Global Helpers/StockItem.swift	Wed Apr 14 23:18:07 2021 +0200
+++ b/LazyBear/Views/Global Helpers/StockItem.swift	Thu Apr 15 23:37:25 2021 +0200
@@ -7,12 +7,37 @@
 
 import SwiftUI
 
+enum OrientationView {
+    case horizontal, vertical
+}
+
 struct StockItem: View {
     var company: QuoteModel
     var intradayPrices: [IntradayPricesModel]?
+    var orientation: OrientationView
     
-    private let baseUrl = Bundle.main.infoDictionary?["IEX_URL"] as? String ?? "Empty url"
-    private let apiKey = Bundle.main.infoDictionary?["IEX_API"] as? String ?? "Empty key"
+    var body: some View {
+        if orientation == .vertical {
+            return AnyView(VerticalStockRow(company: company, intradayPrices: intradayPrices))
+        } else {
+            return AnyView(HorizontalStockRow(company: company, intradayPrices: intradayPrices))
+        }
+    }
+}
+
+struct StockItem_Previews: PreviewProvider {
+    static var previews: some View {
+        StockItem(
+            company: QuoteModel(companyName: "apple inc", symbol: "aapl", latestPrice: 130.3, changePercent: 0.03),
+            intradayPrices: [IntradayPricesModel(open: 130.3), IntradayPricesModel(open: 132.3)], orientation: .horizontal
+        )
+    }
+}
+
+
+struct VerticalStockRow: View {
+    var company: QuoteModel
+    var intradayPrices: [IntradayPricesModel]?
     
     var body: some View {
         RoundedRectangle(cornerRadius: 20)
@@ -32,7 +57,8 @@
                             .opacity(0.6)
                             .lineLimit(1)
                         
-                        PriceView(latestPrice: company.latestPrice, changePercent: company.changePercent)
+                        PriceView(latestPrice: company.latestPrice, changePercent: company.changePercent, align: .leading)
+                            .padding(.top)
                             
                     }
                     .padding(.horizontal)
@@ -52,11 +78,37 @@
     }
 }
 
-struct StockItem_Previews: PreviewProvider {
-    static var previews: some View {
-        StockItem(
-            company: QuoteModel(companyName: "apple inc", symbol: "aapl", latestPrice: 130.3, changePercent: 0.03),
-            intradayPrices: [IntradayPricesModel(open: 130.3)]
-        )
+
+struct HorizontalStockRow: View {
+    var company: QuoteModel
+    var intradayPrices: [IntradayPricesModel]?
+    
+    var body: some View {
+        HStack {
+            VStack(alignment: .leading) {
+                Text(company.symbol.uppercased())
+                    .fontWeight(.semibold)
+                
+                Text(company.companyName.capitalized)
+                    .font(.callout)
+                    .fontWeight(.semibold)
+                    .opacity(0.6)
+                    .lineLimit(1)
+            }
+            
+            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)
+            }
+            
+            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/StockListItem.swift	Wed Apr 14 23:18:07 2021 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,20 +0,0 @@
-//
-//  StockListItem.swift
-//  LazyBear
-//
-//  Created by Dennis Concepción Martín on 14/4/21.
-//
-
-import SwiftUI
-
-struct StockListItem: View {
-    var body: some View {
-        Text(/*@START_MENU_TOKEN@*/"Hello, World!"/*@END_MENU_TOKEN@*/)
-    }
-}
-
-struct StockListItem_Previews: PreviewProvider {
-    static var previews: some View {
-        StockListItem()
-    }
-}
--- a/LazyBear/Views/Global Helpers/StockRectangleRow.swift	Wed Apr 14 23:18:07 2021 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,77 +0,0 @@
-//
-//  StockRectangleRow.swift
-//  LazyBear
-//
-//  Created by Dennis Concepción Martín on 28/3/21.
-//
-
-import SwiftUI
-
-
-struct StockRectangleRow: View {
-    var listName: String
-    var list: [QuoteModel]
-    var nestedIntradayPrices: [String: NestedIntradayPricesModel]?
-    
-    @State private var showExtensiveList = false
-    
-    var body: some View {
-        VStack(alignment: .leading) {
-            HStack(alignment: .bottom) {
-                VStack(alignment: .leading) {
-                    Text(adaptTitle(listName))
-                        .font(.title3)
-                        .fontWeight(.semibold)
-                        .padding([.top, .horizontal])
-                    
-                    Text("Real-time quotes")
-                        .font(.caption)
-                        .opacity(0.5)
-                        .padding(.horizontal)
-                }
-                
-                Spacer()
-                Button("See all", action: { self.showExtensiveList = true })
-                    .buttonStyle(BorderlessButtonStyle())
-                    .padding(.horizontal)
-            }
-            
-            ScrollView(.horizontal, showsIndicators: false) {
-                HStack(spacing: 20) {
-                    ForEach(list, id: \.self) { company in
-                        if let intradayPrices = nestedIntradayPrices?[company.symbol.uppercased()] {
-                            StockItem(company: company, intradayPrices: intradayPrices.nestedIntradayPrices)
-                        } else {
-                            StockItem(company: company, intradayPrices: nil)
-                        }
-                    }
-                }
-                .padding()
-            }
-            .frame(height: 250)
-        }
-        .padding(.bottom)
-        .sheet(isPresented: $showExtensiveList) {
-            ExtensiveList(listName: adaptTitle(listName), list: list, nestedIntradayPrices: nestedIntradayPrices, latestCurrencies: nil)
-        }
-    }
-}
-
-private func adaptTitle(_ listType: String) -> String {
-    if listType == "mostactive" {
-        return "Most active"
-    } else {
-        return listType.capitalized
-    }
-}
-
-
-struct StockRectangleRow_Previews: PreviewProvider {
-    static var previews: some View {
-        StockRectangleRow(
-            listName: "mostactive",
-            list: [QuoteModel(companyName: "apple inc", symbol: "aapl", latestPrice: 130.3, changePercent: 0.03)],
-            nestedIntradayPrices: ["AAPL": NestedIntradayPricesModel(nestedIntradayPrices: [IntradayPricesModel(open: 130.3)])]
-        )
-    }
-}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LazyBear/Views/Global Helpers/StockRow.swift	Thu Apr 15 23:37:25 2021 +0200
@@ -0,0 +1,77 @@
+//
+//  StockRow.swift
+//  LazyBear
+//
+//  Created by Dennis Concepción Martín on 28/3/21.
+//
+
+import SwiftUI
+
+
+struct StockRow: View {
+    var listName: String
+    var list: [QuoteModel]
+    var nestedIntradayPrices: [String: NestedIntradayPricesModel]?
+    
+    @State private var showExtensiveList = false
+    
+    var body: some View {
+        VStack(alignment: .leading) {
+            HStack(alignment: .bottom) {
+                VStack(alignment: .leading) {
+                    Text(adaptTitle(listName))
+                        .font(.title3)
+                        .fontWeight(.semibold)
+                        .padding([.top, .horizontal])
+                    
+                    Text("Real-time quotes")
+                        .font(.caption)
+                        .opacity(0.5)
+                        .padding(.horizontal)
+                }
+                
+                Spacer()
+                Button("See all", action: { self.showExtensiveList = true })
+                    .buttonStyle(BorderlessButtonStyle())
+                    .padding(.horizontal)
+            }
+            
+            ScrollView(.horizontal, showsIndicators: false) {
+                HStack(spacing: 20) {
+                    ForEach(list, id: \.self) { company in
+                        if let intradayPrices = nestedIntradayPrices?[company.symbol.uppercased()] {
+                            StockItem(company: company, intradayPrices: intradayPrices.nestedIntradayPrices, orientation: .vertical)
+                        } else {
+                            StockItem(company: company, intradayPrices: nil, orientation: .vertical)
+                        }
+                    }
+                }
+                .padding()
+            }
+            .frame(height: 250)
+        }
+        .padding(.bottom)
+        .sheet(isPresented: $showExtensiveList) {
+            ExtensiveList(listName: adaptTitle(listName), list: list, nestedIntradayPrices: nestedIntradayPrices, latestCurrencies: nil)
+        }
+    }
+}
+
+private func adaptTitle(_ listType: String) -> String {
+    if listType == "mostactive" {
+        return "Most active"
+    } else {
+        return listType.capitalized
+    }
+}
+
+
+struct StockRectangleRow_Previews: PreviewProvider {
+    static var previews: some View {
+        StockRow(
+            listName: "mostactive",
+            list: [QuoteModel(companyName: "apple inc", symbol: "aapl", latestPrice: 130.3, changePercent: 0.03)],
+            nestedIntradayPrices: ["AAPL": NestedIntradayPricesModel(nestedIntradayPrices: [IntradayPricesModel(open: 130.3)])]
+        )
+    }
+}
--- a/LazyBear/Views/Home/ExtensiveList.swift	Wed Apr 14 23:18:07 2021 +0200
+++ b/LazyBear/Views/Home/ExtensiveList.swift	Thu Apr 15 23:37:25 2021 +0200
@@ -25,7 +25,11 @@
             VStack {
                 if let list = list {
                     List(list, id: \.self) { company in
-                        Text(company.companyName)
+                        if let intradayPrices = nestedIntradayPrices?[company.symbol.uppercased()] {
+                            StockItem(company: company, intradayPrices: intradayPrices.nestedIntradayPrices, orientation: .horizontal)
+                        } else {
+                            StockItem(company: company, intradayPrices: nil, orientation: .horizontal)
+                        }
                     }
                 }
                 
--- a/LazyBear/Views/Home/Helpers/CurrencyItem.swift	Wed Apr 14 23:18:07 2021 +0200
+++ b/LazyBear/Views/Home/Helpers/CurrencyItem.swift	Thu Apr 15 23:37:25 2021 +0200
@@ -23,7 +23,7 @@
                             Text(currency.flag)
                         )
                     VStack(alignment: .leading) {
-                        Text(currencySymbol)
+                        Text("USD/\(currencySymbol)")
                             .font(.headline)
                         
                         Text(currency.name)
@@ -31,7 +31,7 @@
                     }
                     
                     Spacer()
-                    Text("$ \(currency.rate, specifier: "%.2f")")
+                    Text("\(currency.rate, specifier: "%.2f")")
                         .padding(.horizontal)
                 }
                 .clipShape(RoundedRectangle(cornerRadius: 8))
--- a/LazyBear/Views/Home/Helpers/CurrencyListItem.swift	Wed Apr 14 23:18:07 2021 +0200
+++ b/LazyBear/Views/Home/Helpers/CurrencyListItem.swift	Thu Apr 15 23:37:25 2021 +0200
@@ -17,7 +17,7 @@
                 .padding(.trailing)
             
             VStack(alignment: .leading) {
-                Text(currencySymbol)
+                Text("USD/\(currencySymbol)")
                     .font(.headline)
                 
                 Text(currency.name)
@@ -25,7 +25,7 @@
             }
             
             Spacer()
-            Text("$ \(currency.rate, specifier: "%.2f")")
+            Text("\(currency.rate, specifier: "%.2f")")
                 .padding(.horizontal)
         }
     }
--- a/LazyBear/Views/Home/HomeView.swift	Wed Apr 14 23:18:07 2021 +0200
+++ b/LazyBear/Views/Home/HomeView.swift	Thu Apr 15 23:37:25 2021 +0200
@@ -34,9 +34,9 @@
                     if let lists = home.data.lists {
                         ForEach(Array(lists.keys.sorted()), id: \.self) { listName in
                             if let intradayPrices = home.data.intradayPrices {
-                                StockRectangleRow(listName: listName, list: lists[listName]!, nestedIntradayPrices: intradayPrices)
+                                StockRow(listName: listName, list: lists[listName]!, nestedIntradayPrices: intradayPrices)
                             } else {
-                                StockRectangleRow(listName: listName, list: lists[listName]!, nestedIntradayPrices: nil)
+                                StockRow(listName: listName, list: lists[listName]!, nestedIntradayPrices: nil)
                             }
                         }
                         .listRowInsets(EdgeInsets())