comparison LazyBear/Views/Company/Helpers/StatsView.swift @ 416:1662a41e2c1a

KeyStatsView implemented
author Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
date Wed, 09 Jun 2021 20:26:28 +0200
parents 34f9e408b861
children 5f21f7c23c5e
comparison
equal deleted inserted replaced
415:34f9e408b861 416:1662a41e2c1a
8 import SwiftUI 8 import SwiftUI
9 9
10 struct StatsView: View { 10 struct StatsView: View {
11 var keyStats: KeyStatsModel 11 var keyStats: KeyStatsModel
12 @Environment(\.presentationMode) private var presentationStatsView 12 @Environment(\.presentationMode) private var presentationStatsView
13 let displayWords: DisplayWordsModel = parseJSON("DisplayWords.json")
13 14
14 var body: some View { 15 var body: some View {
15 NavigationView { 16 NavigationView {
16 Form { 17 Form {
17 let mirror = Mirror(reflecting: keyStats) 18 let mirror = Mirror(reflecting: keyStats)
18 ForEach(Array(mirror.children), id: \.label) { child in 19 ForEach(Array(mirror.children), id: \.label) { child in
19 HStack { 20 if let unwrappedValue = unwrapAnyOptional(value: child.value) {
20 let label = String(child.label!) 21 HStack {
21 22 let label = String(child.label!)
22 Text("\(label)".capitalized) 23 Text("\(displayWords.keyStats[label]!):")
23 .fontWeight(.semibold) 24 .font(.callout)
24 25 .fontWeight(.semibold)
25 // Unwrap optional value 26
26 if let doubleValue = child.value as? Double { 27 Spacer()
27 Text("\(doubleValue)") 28 Text(unwrappedValue)
28 } else if let intValue = child.value as? Int { 29 .font(.callout)
29 Text("\(intValue)")
30 } else if let stringValue = child.value as? String {
31 Text("\(stringValue)")
32 } else {
33 Text("NaN")
34 } 30 }
35 } 31 }
36 } 32 }
37 } 33 }
38 .navigationTitle("Key statistics") 34 .navigationTitle("Key statistics")
45 } 41 }
46 } 42 }
47 } 43 }
48 } 44 }
49 } 45 }
46
47 /*
48 Unwrap optional Int, Double, String into String
49 */
50 private func unwrapAnyOptional(value: Any) -> String? {
51 if let value = value as? Int {
52 return "\(value)"
53 } else if let value = value as? Double {
54 return String(format: "%.3f", value)
55 } else {
56 return value as? String
57 }
58 }
50 } 59 }
51 60
52 61
53 struct StatsView_Previews: PreviewProvider { 62 struct StatsView_Previews: PreviewProvider {
54 static var previews: some View { 63 static var previews: some View {
55 StatsView(keyStats: 64 StatsView(keyStats:
56 KeyStatsModel(avg10Volume: 123, 65 KeyStatsModel(
57 avg30Volume: 123, 66 companyName: "Apple inc",
58 beta: 123.12, 67 employees: 123,
59 companyName: "Apple inc", 68 marketcap: 123,
60 day200MovingAvg: 123.4, 69 float: 123,
61 day30ChangePercent: 123.4, 70 sharesOutstanding: 123,
62 day50MovingAvg: 123.4, 71 beta: 123.12,
63 day5ChangePercent: 123.4, 72 peRatio: 123.4,
64 dividendYield: 123.4, 73 dividendYield: 123.4,
65 employees: 123, 74 ttmDividendRate: 123.4,
66 exDividendDate: "2020-01-01", 75 ttmEPS: 123.4,
67 float: 123, 76 avg10Volume: 123,
68 marketcap: 123, 77 avg30Volume: 123,
69 maxChangePercent: 123.4, 78 day50MovingAvg: 123.4,
70 month1ChangePercent: 123.4, 79 day200MovingAvg: 123.4,
71 month3ChangePercent: 123.4, 80 week52Change: 123.4,
72 month6ChangePercent: 123.4, 81 week52High: 123.4,
73 nextDividendDate: "2020-01-01", 82 week52Low: 123.4,
74 nextEarningsDate: "2020-01-01", 83 week52HighSplitAdjustOnly: 123.4,
75 peRatio: 123.4, 84 week52LowSplitAdjustOnly: 123.4,
76 sharesOutstanding: 123, 85 maxChangePercent: 123.4,
77 ttmDividendRate: 123.4, 86 ytdChangePercent: 123.4,
78 ttmEPS: 123.4, 87 day5ChangePercent: 123.4,
79 week52Change: 123.4, 88 day30ChangePercent: 123.4,
80 week52High: 123.4, 89 month1ChangePercent: 123.4,
81 week52HighSplitAdjustOnly: 123.4, 90 month3ChangePercent: 123.4,
82 week52Low: 123.4, 91 month6ChangePercent: 123.4,
83 week52LowSplitAdjustOnly: 123.4, 92 year1ChangePercent: 123.4,
84 year1ChangePercent: 123.4, 93 year2ChangePercent: 123.4,
85 year2ChangePercent: 123.4, 94 year5ChangePercent: 123.4,
86 year5ChangePercent: 123.4, 95 exDividendDate: "2020-01-01",
87 ytdChangePercent: 123.4 96 nextDividendDate: "2020-01-01",
97 nextEarningsDate: "2020-01-01"
88 ) 98 )
89 ) 99 )
90 } 100 }
91 } 101 }