diff LazyBear/Views/Company/Helpers/KeyStatsHelper.swift @ 439:aa1f4b614b2b

Implementing CompanyView
author Dennis Concepción Martín <dennisconcepcionmartin@gmail.com>
date Sun, 20 Jun 2021 14:31:39 +0200
parents
children 01fa77358b82
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LazyBear/Views/Company/Helpers/KeyStatsHelper.swift	Sun Jun 20 14:31:39 2021 +0200
@@ -0,0 +1,106 @@
+//
+//  KeyStatsHelper.swift
+//  LazyBear
+//
+//  Created by Dennis Concepción Martín on 20/6/21.
+//
+
+import SwiftUI
+
+struct KeyStatsHelper: View {
+    var keyStats: KeyStatsModel?
+    
+    let displayWords: DisplayWordsModel = parseJSON("DisplayWords.json")
+    
+    var body: some View {
+        if let keyStats = keyStats {
+            ScrollView(.horizontal, showsIndicators: false) {
+                HStack(spacing: 40) {
+                    
+                    let mirror = Mirror(reflecting: keyStats)
+                    ForEach(Array(mirror.children), id: \.label) { child in  /// Iterate over each variable within the class
+                        
+                        if let unwrappedValue = unwrapAnyOptional(value: child.value) {
+                            let label = String(child.label!)
+                            
+                            Capsule()
+                                .frame(width: 250, height: 40)
+                                .foregroundColor(.white)
+                                .shadow(color: Color(.systemGray).opacity(0.25), radius: 10, x: 0.0, y: 0.0)
+                                .overlay(
+                                    HStack {
+                                        Text("\(displayWords.keyStats[label]!):")
+                                            .font(.callout)
+                                            .fontWeight(.semibold)
+                                            .lineLimit(1)
+                                        
+                                        Spacer()
+                                        Text(unwrappedValue)
+                                            .font(.callout)
+                                            .lineLimit(1)
+                                    }
+                                    .padding()
+                                )
+                        }
+                    }
+                }
+                .frame(height: 80)
+                .padding(.horizontal)
+            }
+        }
+    }
+    
+    /*
+     Unwrap optional Int, Double, String into String
+     */
+    private func unwrapAnyOptional(value: Any) -> String? {
+        if let value = value as? Int {
+            return "\(value)"
+        } else if let value = value as? Double {
+            return String(format: "%.3f", value)
+        } else {
+            return value as? String
+        }
+    }
+}
+
+struct KeyStatsHelper_Previews: PreviewProvider {
+    static var previews: some View {
+        KeyStatsHelper(keyStats:
+                KeyStatsModel(
+                    companyName: "Apple inc",
+                    employees: 123,
+                    marketcap: 123,
+                    float: 123,
+                    sharesOutstanding: 123,
+                    beta: 123.12,
+                    peRatio: 123.4,
+                    dividendYield: 123.4,
+                    ttmDividendRate: 123.4,
+                    ttmEPS: 123.4,
+                    avg10Volume: 123,
+                    avg30Volume: 123,
+                    day50MovingAvg: 123.4,
+                    day200MovingAvg: 123.4,
+                    week52Change: 123.4,
+                    week52High: 123.4,
+                    week52Low: 123.4,
+                    week52HighSplitAdjustOnly: 123.4,
+                    week52LowSplitAdjustOnly: 123.4,
+                    maxChangePercent: 123.4,
+                    ytdChangePercent: 123.4,
+                    day5ChangePercent: 123.4,
+                    day30ChangePercent: 123.4,
+                    month1ChangePercent: 123.4,
+                    month3ChangePercent: 123.4,
+                    month6ChangePercent: 123.4,
+                    year1ChangePercent: 123.4,
+                    year2ChangePercent: 123.4,
+                    year5ChangePercent: 123.4,
+                    exDividendDate: "2020-01-01",
+                    nextDividendDate: "2020-01-01",
+                    nextEarningsDate: "2020-01-01"
+            )
+        )
+    }
+}