changeset 183:4d677cfcaa91

Implement LineChart
author Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
date Sun, 21 Feb 2021 19:41:47 +0100
parents a896f0e0e625
children ee9f3aff962f
files LazyBear/Models/HistoricalPriceModel.swift LazyBear/UI/CompanyView.swift LazyBear/UI/HistoricalPriceView.swift
diffstat 3 files changed, 59 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/LazyBear/Models/HistoricalPriceModel.swift	Sun Feb 21 19:41:23 2021 +0100
+++ b/LazyBear/Models/HistoricalPriceModel.swift	Sun Feb 21 19:41:47 2021 +0100
@@ -7,14 +7,12 @@
 
 import SwiftUI
 
-struct HistoricalPrice: View {
-    var body: some View {
-        Text(/*@START_MENU_TOKEN@*/"Hello, World!"/*@END_MENU_TOKEN@*/)
-    }
-}
+struct HistoricalPriceModel: Codable {
+    var date: String
+    var close: Double
+    var volume: Int
+    var change: Double
+    var changePercent: Double
+    var changeOverTime: Double
 
-struct HistoricalPrice_Previews: PreviewProvider {
-    static var previews: some View {
-        HistoricalPrice()
-    }
 }
--- a/LazyBear/UI/CompanyView.swift	Sun Feb 21 19:41:23 2021 +0100
+++ b/LazyBear/UI/CompanyView.swift	Sun Feb 21 19:41:47 2021 +0100
@@ -16,9 +16,12 @@
     var symbol: String
     
     var body: some View {
-        ScrollView {
-            PriceView(symbol: symbol)
-            NewsView(symbol: symbol)
+        GeometryReader { geo in
+            ScrollView {
+                //PriceView(symbol: symbol)
+                HistoricalPriceView(symbol: symbol, chartHeight: geo.size.width / 2)
+                NewsView(symbol: symbol)
+            }
         }
         .toolbar {
             ToolbarItem(placement: .principal) {
--- a/LazyBear/UI/HistoricalPriceView.swift	Sun Feb 21 19:41:23 2021 +0100
+++ b/LazyBear/UI/HistoricalPriceView.swift	Sun Feb 21 19:41:47 2021 +0100
@@ -6,15 +6,59 @@
 //
 
 import SwiftUI
+import Charts
 
 struct HistoricalPriceView: View {
+    var symbol: String
+    var chartHeight: CGFloat
+    @State private var historicalPrices = [HistoricalPriceModel]()
+    
     var body: some View {
-        Text(/*@START_MENU_TOKEN@*/"Hello, World!"/*@END_MENU_TOKEN@*/)
+        VStack {
+            let (colour, prices) = prepareChart()
+            Chart(data: prices)
+                .frame(height: chartHeight)
+                .chartStyle(AreaChartStyle(.quadCurve, fill:
+                                            LinearGradient(gradient: .init(colors: [colour.opacity(0.5), colour.opacity(0.2)]),
+                                                           startPoint: .top,
+                                                           endPoint: .bottom)))
+        }
+        .onAppear {
+            request(url: getUrl(), model: [HistoricalPriceModel].self) { self.historicalPrices = $0 }
+        }
+    }
+    
+    private func getUrl() -> String {
+        let baseUrl = Bundle.main.infoDictionary?["IEX_URL"] as? String ?? "Empty url"
+        let apiKey = Bundle.main.infoDictionary?["IEX_API"] as? String ?? "Empty key"
+        let url = "\(baseUrl)/stock/\(symbol)/chart/1m?chartCloseOnly=true&token=\(apiKey)"
+        
+        return url
+    }
+    
+    private func prepareChart() -> (Color, [Double]) {
+        if !historicalPrices.isEmpty {
+            // Historical is been requested and successfully received
+            let prices = historicalPrices.map { $0.close } // Close prices array (for chart)
+            // Modify chart colour depending on % change over time
+            // If it's < 0 -> lost value -> red
+            var colour: Color = .red
+            let changeOverTime = historicalPrices.map { $0.changeOverTime }.last
+            if changeOverTime! >= 0 {
+                colour = .green
+            }
+            
+            return (colour, normalize(prices))
+        }
+        
+        // Handle error here after x seconds of requesting.
+        
+        return (.blue, [Double]())
     }
 }
 
 struct HistoricalPriceView_Previews: PreviewProvider {
     static var previews: some View {
-        HistoricalPriceView()
+        HistoricalPriceView(symbol: "aapl", chartHeight: 200)
     }
 }