changeset 54:9481ed2b1fa0

Update StockCharts for iPad. Almost finished
author Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
date Tue, 19 Jan 2021 12:42:11 +0100
parents 4138da8f24c8
children 4dd944509d42
files LazyBear.xcodeproj/project.xcworkspace/xcuserdata/dennis.xcuserdatad/UserInterfaceState.xcuserstate lazybear/Stock.swift lazybear/Supply views/StockCharts.swift lazybear/Supply views/StockStats.swift
diffstat 4 files changed, 87 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
Binary file LazyBear.xcodeproj/project.xcworkspace/xcuserdata/dennis.xcuserdatad/UserInterfaceState.xcuserstate has changed
--- a/lazybear/Stock.swift	Mon Jan 18 21:52:17 2021 +0100
+++ b/lazybear/Stock.swift	Tue Jan 19 12:42:11 2021 +0100
@@ -19,7 +19,7 @@
                 VStack {
                     PriceOverview(historicalPrices: historicalPrices)
                     let width = geo.size.width*0.9
-                    StockCharts(historicalPrices: historicalPrices, width: width)
+                    StockCharts(historicalPrices: historicalPrices, geoWidth: width)
                 }
             }
         }
--- a/lazybear/Supply views/StockCharts.swift	Mon Jan 18 21:52:17 2021 +0100
+++ b/lazybear/Supply views/StockCharts.swift	Tue Jan 19 12:42:11 2021 +0100
@@ -10,7 +10,7 @@
 
 struct StockCharts: View {
     @State var historicalPrices: HistoricalPrices
-    @State var width: CGFloat
+    @State var geoWidth: CGFloat
     @Environment(\.verticalSizeClass) var verticalSizeClass: UserInterfaceSizeClass?
     @Environment(\.horizontalSizeClass) var horizontalSizeClass: UserInterfaceSizeClass?
     
@@ -24,50 +24,64 @@
                 let specialSize = chartSize(chartType: "special")
                 
                 let prices = historicalPrices.result.map { $0.close }  // Get an array of the variable "Close" in the struct
-                LineChartView(data: prices, title: "Stock price", legend: "Last month",  form: normalSize, rateValue: nil)
+                LineChartView(data: prices, title: "Stock price", legend: "Last month",  form: CGSize(width: normalSize.0, height: normalSize.1), rateValue: nil)
                     .padding()
                 
                 let maxPrice = String(prices.max()!)
-                StockStats(data: maxPrice)
+                let minPrice = String(prices.min()!)
+                StockStats(dataMax: maxPrice, dataMin: minPrice, width: normalSize.0, height: normalSize.1, title: "price")
                 
                 let volume = historicalPrices.result.map { $0.volume }
-                BarChartView(data: ChartData(points: volume), title: "Volume", form: specialSize)
+                BarChartView(data: ChartData(points: volume), title: "Volume", form: CGSize(width: specialSize.0, height: specialSize.1))
                         .padding()
                 
                 let maxVolume = String(volume.max()!)
-                StockStats(data: maxVolume)
+                let minVolume = String(volume.min()!)
+                StockStats(dataMax: maxVolume, dataMin: minVolume, width: normalSize.0, height: normalSize.1, title: "volume")
                 
                 let change = historicalPrices.result.map { $0.changePercent*100 }
-                LineChartView(data: change, title: "Daily percentage change", legend: "Last month",  form: normalSize, rateValue: nil)
+                LineChartView(data: change, title: "Daily percentage change", legend: "Last month",  form: CGSize(width: normalSize.0, height: normalSize.1), rateValue: nil)
                     .padding()
                 
+                let maxChange = String(change.max()!)
+                let minChange = String(change.min()!)
+                StockStats(dataMax: maxChange, dataMin: minChange, width: normalSize.0, height: normalSize.1, title: "daily percentage change")
+                
             }
         }
     }
     
-    func chartSize(chartType: String) -> CGSize {
-        var size = CGSize()
+    func chartSize(chartType: String) -> (CGFloat, CGFloat) {
+        var height = CGFloat()
+        var width = CGFloat()
+        
         if horizontalSizeClass == .compact && verticalSizeClass == .regular {
-            print("Running on iPhone")
+            //print("Running on iPhone")
             
             if chartType == "special" {
-                size = CGSize(width: width, height: width/1.5)
+                //size = CGSize(width: width, height: width/1.5)
+                width = geoWidth
+                height = geoWidth/1.5
             }
             else {
-                size = CGSize(width: width, height: width/2)
+                //size = CGSize(width: width, height: width/2)
+                width = geoWidth
+                height = geoWidth/2
             }
         }
         else {
-            print("Running on iPad")
-            size = CGSize(width: 360, height: 240)
+            //print("Running on iPad")
+            //size = CGSize(width: 360, height: 240)
+            width = CGFloat(360)
+            height = CGFloat(240)
         }
-        
-        return size
+        // Return tupple
+        return  (width, height)
     }
 }
 
 struct ScrollCharts_Previews: PreviewProvider {
     static var previews: some View {
-        StockCharts(historicalPrices: HistoricalPrices.init(), width: 100)
+        StockCharts(historicalPrices: HistoricalPrices.init(), geoWidth: 100)
     }
 }
--- a/lazybear/Supply views/StockStats.swift	Mon Jan 18 21:52:17 2021 +0100
+++ b/lazybear/Supply views/StockStats.swift	Tue Jan 19 12:42:11 2021 +0100
@@ -8,19 +8,71 @@
 import SwiftUI
 
 struct StockStats: View {
+    @State var dataMax: String
+    @State var dataMin: String
+    @State var width: CGFloat
+    @State var height: CGFloat
+    @State var title: String
+    
+    var body: some View {
+        VStack(alignment: .leading) {
+            Text("Highest " + title)
+                .titleModifier()
+            
+            DataObject(data: dataMax, colour: .green, type: title)
+            Text("Lowest " + title)
+                .titleModifier()
+            
+            DataObject(data: dataMin, colour: .red, type: title)
+                
+
+        }
+        .frame(width: width, height: height)
+    }
+}
+
+struct DataObject: View {
     @State var data: String
+    @State var colour: Color
+    @State var type: String
     
     var body: some View {
         RoundedRectangle(cornerRadius: 25)
             .foregroundColor(.white)
-            .overlay(Text(String(data)))
-            .frame(maxWidth: 360)
-            .padding()
+            .overlay(
+                Text(unit() + " " + data)
+                    .foregroundColor(colour)
+                    .dataModifier()
+            )
+    }
+    
+    func unit() -> String {
+        if type == "price" {
+            return "$ "
+        }
+        else {
+            return ""
+        }
+    }
+}
+extension Text {
+    func dataModifier() -> some View {
+        self
+            .font(.title3)
+            .fontWeight(.bold)
+    }
+}
+
+extension Text {
+    func titleModifier() -> some View {
+        self
+            .font(.title2)
+            .fontWeight(.bold)
     }
 }
 
 struct StockStats_Previews: PreviewProvider {
     static var previews: some View {
-        StockStats(data: "500.00")
+        StockStats(dataMax: "500.00", dataMin: "200.00", width: 360, height: 240, title: "price")
     }
 }