changeset 281:6835e2885aa6

Implement KeyStats
author Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
date Fri, 19 Mar 2021 19:22:16 +0100
parents 6254ae414a50
children d85f6dde3284
files LazyBear/Functions/GetUrl.swift LazyBear/Models/KeyStatsModel.swift LazyBear/UI/CompanyView.swift LazyBear/UI/KeyStatComponent.swift LazyBear/UI/KeyStatsView.swift
diffstat 5 files changed, 157 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/LazyBear/Functions/GetUrl.swift	Fri Mar 19 17:13:45 2021 +0100
+++ b/LazyBear/Functions/GetUrl.swift	Fri Mar 19 19:22:16 2021 +0100
@@ -13,6 +13,7 @@
     case historicalPrices
     case insiderTransactions
     case insiderSummary
+    case keyStats
 }
 
 func getUrl(endpoint: Endpoint, symbol: String, range: String = "") -> String {
@@ -30,5 +31,7 @@
         return "\(baseUrl)/stock/\(symbol)/insider-transactions?token=\(apiKey)"
     case .insiderSummary:
         return "\(baseUrl)/stock/\(symbol)/insider-roster?token=\(apiKey)"
+    case .keyStats:
+        return "\(baseUrl)/stock/\(symbol)/stats?token=\(apiKey)"
     }
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LazyBear/Models/KeyStatsModel.swift	Fri Mar 19 19:22:16 2021 +0100
@@ -0,0 +1,47 @@
+//
+//  KeyStatsModel.swift
+//  LazyBear
+//
+//  Created by Dennis Concepción Martín on 19/3/21.
+//
+
+import SwiftUI
+
+struct KeyStatsModel: Hashable, Codable {
+    var Market_Cap: Double?
+    var Weeks52_High: Double?
+    var Weeks52_Low: Double?
+    var Shares: Double?
+    var Days10_Avg_Volume: Double?
+    var Days30_Avg_Volume: Double?
+    var Days200_Moving_Avg: Double?
+    var Days50_Moving_Avg: Double?
+    var Employees: Double?
+    var Trailing_EPS: Double?
+    var Trailing_Dividend_Rate: Double?
+    var Dividend_Yield: Double?
+    var Next_Dividend_Date: String?
+    var PE_Ratio: Double?
+    var Beta: Double?
+    
+    
+    // Custom keys
+    enum CodingKeys: String, CodingKey {
+        case Market_Cap = "marketcap"
+        case Weeks52_High = "week52high"
+        case Weeks52_Low = "week52low"
+        case Shares = "sharesOutstanding"
+        case Days10_Avg_Volume = "avg10Volume"
+        case Days30_Avg_Volume = "avg30Volume"
+        case Days200_Moving_Avg = "day200MovingAvg"
+        case Days50_Moving_Avg = "day50MovingAvg"
+        case Employees = "employees"
+        case Trailing_EPS = "ttmEPS"
+        case Trailing_Dividend_Rate = "ttmDividendRate"
+        case Dividend_Yield = "dividendYield"
+        case Next_Dividend_Date = "nextDividendDate"
+        case PE_Ratio = "peRatio"
+        case Beta = "beta"
+    }
+}
+
--- a/LazyBear/UI/CompanyView.swift	Fri Mar 19 17:13:45 2021 +0100
+++ b/LazyBear/UI/CompanyView.swift	Fri Mar 19 19:22:16 2021 +0100
@@ -26,6 +26,7 @@
                 if companyOption.view == .stock {
                     PriceView(symbol: symbol, showVertical: false)
                     ChartView(symbol: symbol)
+                    KeyStatsView(symbol: symbol)
                     NewsView(symbol: symbol)
                         
                 } else if companyOption.view == .insiders {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LazyBear/UI/KeyStatComponent.swift	Fri Mar 19 19:22:16 2021 +0100
@@ -0,0 +1,52 @@
+//
+//  KeyStatComponent.swift
+//  LazyBear
+//
+//  Created by Dennis Concepción Martín on 19/3/21.
+//
+
+import SwiftUI
+
+enum CustomStatLabel {
+    
+}
+
+struct KeyStatComponent: View {
+    var text: String
+    var data: Any
+    
+    var body: some View {
+        HStack {
+            let cleanText = text.replacingOccurrences(of: "_", with: " ")
+            Text(cleanText)
+                .font(.caption)
+                .fontWeight(.semibold)
+            
+            // Check type of data
+            if let dataString = data as? String {
+                if dataString.isEmpty {
+                    Text("-")
+                        .font(.caption)
+                } else {
+                Text(dataString)
+                    .font(.caption)
+                }
+            } else if let dataNumber = data as? Double {
+                Text("\(dataNumber, specifier: "%.2f")")
+                    .font(.caption)
+            }
+        }
+        .padding(10)
+        .background(
+            Capsule()
+                .foregroundColor(.white)
+                .shadow(color: .gray, radius: 5, x: 0.0, y: 0.0).opacity(0.3)
+        )
+    }
+}
+
+struct KeyStatComponent_Previews: PreviewProvider {
+    static var previews: some View {
+        KeyStatComponent(text: "Market cap", data: 123456.00)
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LazyBear/UI/KeyStatsView.swift	Fri Mar 19 19:22:16 2021 +0100
@@ -0,0 +1,54 @@
+//
+//  KeyStatsView.swift
+//  LazyBear
+//
+//  Created by Dennis Concepción Martín on 19/3/21.
+//
+
+import SwiftUI
+
+struct KeyStatsView: View {
+    var symbol: String
+    @State private var keyStats = KeyStatsModel()
+    
+    var body: some View {
+        ScrollView(.horizontal, showsIndicators: false) {
+            HStack(spacing: 20) {
+                let keys = Array(mirrorStructure().keys)
+                let values = Array(mirrorStructure().values)
+                ForEach(keys.indices, id: \.self) { index in
+                    KeyStatComponent(text: keys[index], data: values[index])
+                }
+            }
+            .padding()
+        }
+        .onAppear {
+            let url = getUrl(endpoint: .keyStats, symbol: symbol)
+            request(url: url, model: KeyStatsModel.self) { result in
+                self.keyStats = result
+            }
+        }
+    }
+    
+    // Get an array with all the stats from the struct
+    func mirrorStructure() -> ([String: Any]) {
+        let mirrorKeyStats = Mirror(reflecting: keyStats)
+        var statsDict = [String: Any]()
+
+        for stat in mirrorKeyStats.children {
+            if let numValue = stat.value as? Double {
+                statsDict[stat.label!] = numValue
+            } else if let stringValue = stat.value as? String {
+                statsDict[stat.label!] = stringValue
+            }
+        }
+        
+        return statsDict
+    }
+}
+
+struct KeyStatsView_Previews: PreviewProvider {
+    static var previews: some View {
+        KeyStatsView(symbol: "aapl")
+    }
+}