diff LazyBear/Views/Company/Chart.swift @ 400:6055a867d2b6

Implementing Historical Prices in Company.swift
author Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
date Sat, 15 May 2021 19:54:20 +0200
parents 5c99883c7964
children f843c6382529
line wrap: on
line diff
--- a/LazyBear/Views/Company/Chart.swift	Sat May 15 12:31:40 2021 +0200
+++ b/LazyBear/Views/Company/Chart.swift	Sat May 15 19:54:20 2021 +0200
@@ -9,78 +9,78 @@
 import StockCharts
 
 struct Chart: View {
-    var chartData: ChartResponse
+    @ObservedObject var company: Company
     var symbol: String
     
     // Date picker
     var ranges = ["1D", "5D", "1M", "3M", "6M", "1Y", "5Y"]
     @State private var selectedRange = "3M"
     
+    // Set recurrent price request
+    @State private var timer = Timer.publish(every: 10, on: .main, in: .common).autoconnect()
     
     var body: some View {
-        let priceViewStyle = PriceViewStyle(
-            alignment: .leading,
-            priceFont: .headline,
-            priceFontWeight: .semibold,
-            percentFont: .body,
-            percentFontWeight: .medium
-        )
-        
-        VStack {
-            DatePicker(ranges: ranges, selectedRange: $selectedRange)
-            
-            RoundedRectangle(cornerRadius: 15)
-                .foregroundColor(Color(.secondarySystemBackground))
-                .frame(height: 270)
-                .overlay(
-                    VStack {
-                        HStack {
-                            if let quote = chartData.quote![symbol.uppercased()] {
-                                let latestPrice = quote.latestPrice ?? 0
-                                let changePercent = quote.changePercent ?? 0
-                                PriceView(latestPrice: latestPrice, changePercent: changePercent, style: priceViewStyle)
+        if company.showChartView {
+            VStack {
+                DatePicker(ranges: ranges, selectedRange: $selectedRange)
+                    .onChange(of: selectedRange, perform: { value in
+                        print(value.lowercased())
+                    })
+                
+                RoundedRectangle(cornerRadius: 15)
+                    .foregroundColor(Color(.secondarySystemBackground))
+                    .frame(height: 270)
+                    .overlay(
+                        VStack {
+                            HStack {
+                                if let quote = company.chartData.quote![symbol.uppercased()] {
+                                    let latestPrice = quote.latestPrice ?? 0
+                                    let changePercent = quote.changePercent ?? 0
+                                    let priceViewStyle = PriceViewStyle(
+                                        alignment: .leading,
+                                        priceFont: .headline,
+                                        priceFontWeight: .semibold,
+                                        percentFont: .body,
+                                        percentFontWeight: .medium
+                                    )
+                                    PriceView(latestPrice: latestPrice, changePercent: changePercent, style: priceViewStyle)
+                                }
+                                Spacer()
                             }
-                            Spacer()
-                        }
-                        .padding([.top, .leading, .trailing])
-                        
-                        if let intradayPrices = chartData.intradayPrices![symbol.uppercased()] {
-                            if let prices = intradayPrices.compactMap { $0.open } {
-                                LineChartView(data: prices,
-                                              dates: nil,
-                                              hours: nil,
-                                              dragGesture: true
-                                )
-                                    .padding(.bottom)
+                            .padding([.top, .leading, .trailing])
+                            
+                            if let intradayPrices = company.chartData.intradayPrices![symbol.uppercased()] {
+                                if let prices = intradayPrices.compactMap { $0.open } {  // Map without nil
+                                    LineChartView(data: prices, dates: nil, hours: nil, dragGesture: true)
+                                        .padding(.bottom)
+                                }
                             }
                         }
+                    )
+                
+                if let latestNews = company.chartData.latestNews {
+                    ForEach(latestNews, id: \.self) { new in
+                        NewsRow(new: new)
                     }
-                )
-            
-            if let latestNews = chartData.latestNews {
-                ForEach(latestNews, id: \.self) { new in
-                    NewsRow(new: new)
                 }
             }
+            .onAppear { self.timer = Timer.publish(every: 10, on: .main, in: .common).autoconnect() }  // Start timer
+            .onDisappear { self.timer.upstream.connect().cancel() }  // Stop timer
+            .onReceive(timer) { _ in
+                let url = "https://api.lazybear.app/company/chart/type=streaming/symbol=\(symbol)"
+                company.request(url, isInitRequest: false, "chart") }  // Receive timer notification
+        } else {
+            ProgressView()
+                .onAppear {
+                    let url = "https://api.lazybear.app/company/chart/type=init/symbol=\(symbol)"
+                    company.request(url, isInitRequest: true, "chart")
+                }
         }
     }
 }
 
 struct Chart_Previews: PreviewProvider {
     static var previews: some View {
-        Chart(
-            chartData: ChartResponse(
-                intradayPrices: ["AAPL": [IntradayPriceModel(open: 120.3)]],
-                quote: ["aapl": QuoteModel(changePercent: 0.03, companyName: "Apple Inc", latestPrice: 120.3)],
-                latestNews: [LatestNewsModel(
-                                datetime: 1621037430000,
-                                headline: "Chaos Monkeys' author calls Apple's statement on his departure defamatory",
-                                image: "https://cloud.iexapis.com/v1/news/image/99abeb99-6d9e-47c8-ae7b-53404eacccec",
-                                source: "Investing.com",
-                                summary: "https://www.investing.com/news/stock-market-news",
-                                url: "https://cloud.iexapis.com/v1/news/article/99abeb99-6d9e-47c8-ae7b-53404eacccec")]),
-            
-            symbol: "aapl"
-        )
+        Chart(company: Company(), symbol: "aapl")
     }
 }