diff LazyBear/Views/Company/Helpers/ChartHelper.swift @ 440:01fa77358b82

Fixes #47
author Dennis Concepción Martín <dennisconcepcionmartin@gmail.com>
date Sun, 20 Jun 2021 16:58:36 +0200
parents aa1f4b614b2b
children 417148200aaf
line wrap: on
line diff
--- a/LazyBear/Views/Company/Helpers/ChartHelper.swift	Sun Jun 20 14:31:39 2021 +0200
+++ b/LazyBear/Views/Company/Helpers/ChartHelper.swift	Sun Jun 20 16:58:36 2021 +0200
@@ -6,18 +6,17 @@
 //
 
 import SwiftUI
+import StockCharts
 
 struct ChartHelper: View {
-    var quote: [QuoteModel]?
-    var historicalPrices: [HistoricalPricesModel]?
+    @ObservedObject var company: Company
     
     var body: some View {
         CustomRectangleBox()
             .frame(height: 270)
-            .padding(.horizontal)
             .overlay(
                 VStack {
-                    if let quote = quote?.first {
+                    if let quote = company.data.quote?.first {
                         HStack(alignment: .center) {
                             Text("\(quote.latestPrice ?? 0, specifier: "%.2f")")
                                 .foregroundColor(quote.changePercent ?? 0 < 0 ? .red: .green)
@@ -27,23 +26,33 @@
                                 .foregroundColor(quote.changePercent ?? 0 < 0 ? .red: .green)
                                 .font(.callout)
                                 .fontWeight(.semibold)
+                            
+                            Spacer()
                         }
-                        .padding(.top)
+                        .padding()
+                        
+                        if let historicalPrices = company.data.historicalPrices {
+                            let prices = historicalPrices.compactMap { $0.close }
+                            let dates = historicalPrices.compactMap { $0.date }
+                            if company.showChart {
+                                LineChartView(data: prices, dates: dates, hours: nil, dragGesture: true)
+                            } else {
+                                Spacer()
+                                ProgressView()
+                                Spacer()
+                            }
+                        }
                     }
+                    
+                    Spacer()
                 }
             )
+            .padding(.horizontal)
     }
 }
 
 struct ChartHelper_Previews: PreviewProvider {
     static var previews: some View {
-        ChartHelper(
-            quote: [
-                QuoteModel(companyName: "apple inc", latestPrice: 120.3, changePercent: 0.03)
-            ],
-            historicalPrices: [
-                HistoricalPricesModel(close: 120.3, date: "2020-01-01", minute: nil)
-            ]
-        )
+        ChartHelper(company: Company())
     }
 }