comparison LazyBear/Views/Company/Helpers/KeyStatsList.swift @ 440:01fa77358b82

Fixes #47
author Dennis Concepción Martín <dennisconcepcionmartin@gmail.com>
date Sun, 20 Jun 2021 16:58:36 +0200
parents
children ffbb1dbab531
comparison
equal deleted inserted replaced
439:aa1f4b614b2b 440:01fa77358b82
1 //
2 // KeyStatsList.swift
3 // LazyBear
4 //
5 // Created by Dennis Concepción Martín on 20/6/21.
6 //
7
8 import SwiftUI
9
10 struct KeyStatsList: View {
11 var keyStats: KeyStatsModel
12
13 let displayWords: DisplayWordsModel = parseJSON("DisplayWords.json")
14 @Environment(\.presentationMode) var keyStatsListPresentation
15
16 var body: some View {
17 NavigationView {
18 Form {
19 let mirror = Mirror(reflecting: keyStats)
20 ForEach(Array(mirror.children), id: \.label) { child in /// Iterate over each variable within the class
21 if let unwrappedValue = unwrapAnyOptional(value: child.value) {
22 let label = String(child.label!)
23 HStack {
24 Text("\(displayWords.keyStats[label]!):")
25 .font(.callout)
26 .fontWeight(.semibold)
27 .lineLimit(1)
28
29 Spacer()
30 Text(unwrappedValue)
31 .font(.callout)
32 .lineLimit(1)
33 }
34 }
35 }
36 }
37 .navigationTitle("Key Stats")
38 .navigationBarTitleDisplayMode(.inline)
39 .toolbar {
40 ToolbarItem(placement: .navigationBarLeading) {
41 Button(action: { keyStatsListPresentation.wrappedValue.dismiss() }) {
42 Image(systemName: "multiply")
43 }
44
45 }
46 }
47 }
48 }
49
50 /*
51 Unwrap optional Int, Double, String into String
52 */
53 private func unwrapAnyOptional(value: Any) -> String? {
54 if let value = value as? Int {
55 return "\(value)"
56 } else if let value = value as? Double {
57 return String(format: "%.3f", value)
58 } else {
59 return value as? String
60 }
61 }
62 }
63
64 struct KeyStatsList_Previews: PreviewProvider {
65 static var previews: some View {
66 KeyStatsList(keyStats:
67 KeyStatsModel(
68 companyName: "Apple inc",
69 employees: 123,
70 marketcap: 123,
71 float: 123,
72 sharesOutstanding: 123,
73 beta: 123.12,
74 peRatio: 123.4,
75 dividendYield: 123.4,
76 ttmDividendRate: 123.4,
77 ttmEPS: 123.4,
78 avg10Volume: 123,
79 avg30Volume: 123,
80 day50MovingAvg: 123.4,
81 day200MovingAvg: 123.4,
82 week52Change: 123.4,
83 week52High: 123.4,
84 week52Low: 123.4,
85 week52HighSplitAdjustOnly: 123.4,
86 week52LowSplitAdjustOnly: 123.4,
87 maxChangePercent: 123.4,
88 ytdChangePercent: 123.4,
89 day5ChangePercent: 123.4,
90 day30ChangePercent: 123.4,
91 month1ChangePercent: 123.4,
92 month3ChangePercent: 123.4,
93 month6ChangePercent: 123.4,
94 year1ChangePercent: 123.4,
95 year2ChangePercent: 123.4,
96 year5ChangePercent: 123.4,
97 exDividendDate: "2020-01-01",
98 nextDividendDate: "2020-01-01",
99 nextEarningsDate: "2020-01-01"
100 )
101 )
102 }
103 }