comparison 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
comparison
equal deleted inserted replaced
438:7f2a24a774eb 439:aa1f4b614b2b
1 //
2 // KeyStatsHelper.swift
3 // LazyBear
4 //
5 // Created by Dennis Concepción Martín on 20/6/21.
6 //
7
8 import SwiftUI
9
10 struct KeyStatsHelper: View {
11 var keyStats: KeyStatsModel?
12
13 let displayWords: DisplayWordsModel = parseJSON("DisplayWords.json")
14
15 var body: some View {
16 if let keyStats = keyStats {
17 ScrollView(.horizontal, showsIndicators: false) {
18 HStack(spacing: 40) {
19
20 let mirror = Mirror(reflecting: keyStats)
21 ForEach(Array(mirror.children), id: \.label) { child in /// Iterate over each variable within the class
22
23 if let unwrappedValue = unwrapAnyOptional(value: child.value) {
24 let label = String(child.label!)
25
26 Capsule()
27 .frame(width: 250, height: 40)
28 .foregroundColor(.white)
29 .shadow(color: Color(.systemGray).opacity(0.25), radius: 10, x: 0.0, y: 0.0)
30 .overlay(
31 HStack {
32 Text("\(displayWords.keyStats[label]!):")
33 .font(.callout)
34 .fontWeight(.semibold)
35 .lineLimit(1)
36
37 Spacer()
38 Text(unwrappedValue)
39 .font(.callout)
40 .lineLimit(1)
41 }
42 .padding()
43 )
44 }
45 }
46 }
47 .frame(height: 80)
48 .padding(.horizontal)
49 }
50 }
51 }
52
53 /*
54 Unwrap optional Int, Double, String into String
55 */
56 private func unwrapAnyOptional(value: Any) -> String? {
57 if let value = value as? Int {
58 return "\(value)"
59 } else if let value = value as? Double {
60 return String(format: "%.3f", value)
61 } else {
62 return value as? String
63 }
64 }
65 }
66
67 struct KeyStatsHelper_Previews: PreviewProvider {
68 static var previews: some View {
69 KeyStatsHelper(keyStats:
70 KeyStatsModel(
71 companyName: "Apple inc",
72 employees: 123,
73 marketcap: 123,
74 float: 123,
75 sharesOutstanding: 123,
76 beta: 123.12,
77 peRatio: 123.4,
78 dividendYield: 123.4,
79 ttmDividendRate: 123.4,
80 ttmEPS: 123.4,
81 avg10Volume: 123,
82 avg30Volume: 123,
83 day50MovingAvg: 123.4,
84 day200MovingAvg: 123.4,
85 week52Change: 123.4,
86 week52High: 123.4,
87 week52Low: 123.4,
88 week52HighSplitAdjustOnly: 123.4,
89 week52LowSplitAdjustOnly: 123.4,
90 maxChangePercent: 123.4,
91 ytdChangePercent: 123.4,
92 day5ChangePercent: 123.4,
93 day30ChangePercent: 123.4,
94 month1ChangePercent: 123.4,
95 month3ChangePercent: 123.4,
96 month6ChangePercent: 123.4,
97 year1ChangePercent: 123.4,
98 year2ChangePercent: 123.4,
99 year5ChangePercent: 123.4,
100 exDividendDate: "2020-01-01",
101 nextDividendDate: "2020-01-01",
102 nextEarningsDate: "2020-01-01"
103 )
104 )
105 }
106 }