comparison LazyBear/Views/Company/Helpers/KeyStatsHelper.swift @ 448:f71761f166f2

Handle when data is empty
author Dennis Concepción Martín <dennisconcepcionmartin@gmail.com>
date Wed, 23 Jun 2021 11:47:14 +0200
parents 8621ba6fd457
children
comparison
equal deleted inserted replaced
447:8621ba6fd457 448:f71761f166f2
6 // 6 //
7 7
8 import SwiftUI 8 import SwiftUI
9 9
10 struct KeyStatsHelper: View { 10 struct KeyStatsHelper: View {
11 var keyStats: KeyStatsModel? 11 var keyStats: KeyStatsModel
12 let displayWords: DisplayWordsModel = parseJSON("DisplayWords.json") 12 let displayWords: DisplayWordsModel = parseJSON("DisplayWords.json")
13 @Environment(\.colorScheme) private var colorScheme
14 13
15 var body: some View { 14 var body: some View {
16 if let keyStats = keyStats { 15 ScrollView(.horizontal, showsIndicators: false) {
17 ScrollView(.horizontal, showsIndicators: false) { 16 HStack(spacing: 20) {
18 HStack(spacing: 20) { 17
19 18 let mirror = Mirror(reflecting: keyStats)
20 let mirror = Mirror(reflecting: keyStats) 19 ForEach(Array(mirror.children), id: \.label) { child in /// Iterate over each variable within the class
21 ForEach(Array(mirror.children), id: \.label) { child in /// Iterate over each variable within the class 20 if let unwrappedValue = unwrapAnyOptional(value: child.value) {
22 21 if unwrappedValue != "0.000" && unwrappedValue != "0" && !unwrappedValue.isEmpty {
23 if let unwrappedValue = unwrapAnyOptional(value: child.value) {
24 let label = String(child.label!) 22 let label = String(child.label!)
25 23
26 NavigationLink(destination: KeyStatsList(keyStats: keyStats) 24 NavigationLink(destination: KeyStatsList(keyStats: keyStats)
27 .navigationTitle("Key Stats") 25 .navigationTitle("Key Stats")
28 ) { 26 ) {
29 Capsule() 27 KeyStatsRow(label: displayWords.keyStats[label]!)
30 .frame(width: 250, height: 40)
31 .foregroundColor(Color("customSecondaryBackground"))
32 .if(colorScheme == .light) { content in
33 content.shadow(color: Color(.systemGray).opacity(0.25), radius: 10, x: 0.0, y: 0.0)
34 }
35 .overlay(
36 HStack {
37 Text("\(displayWords.keyStats[label]!):")
38 .font(.callout)
39 .fontWeight(.semibold)
40 .lineLimit(1)
41
42 Spacer()
43 Text(unwrappedValue)
44 .font(.callout)
45 .lineLimit(1)
46 }
47 .padding()
48 )
49 } 28 }
50 .buttonStyle(PlainButtonStyle()) 29 .buttonStyle(PlainButtonStyle())
51 } 30 }
52 } 31 }
53 } 32 }
54 .frame(height: 70)
55 .padding(.horizontal)
56 } 33 }
57 } 34 .frame(height: 70)
58 } 35 .padding(.horizontal)
59
60 /*
61 Unwrap optional Int, Double, String into String
62 */
63 private func unwrapAnyOptional(value: Any) -> String? {
64 if let value = value as? Int {
65 return "\(value)"
66 } else if let value = value as? Double {
67 return String(format: "%.3f", value)
68 } else {
69 return value as? String
70 } 36 }
71 } 37 }
72 } 38 }
73 39
74 struct KeyStatsHelper_Previews: PreviewProvider { 40 struct KeyStatsHelper_Previews: PreviewProvider {