StatRootView.swift 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import Charts
  2. import CoreData
  3. import SwiftDate
  4. import SwiftUI
  5. import Swinject
  6. extension Stat {
  7. struct RootView: BaseView {
  8. let resolver: Resolver
  9. @State var state = StateModel()
  10. @Environment(\.colorScheme) var colorScheme
  11. @Environment(AppState.self) var appState
  12. @State var paddingAmount: CGFloat? = 10
  13. @State var headline: Color = .secondary
  14. @State var days: Double = 0
  15. @State var pointSize: CGFloat = 3
  16. @State var conversionFactor = 0.0555
  17. @ViewBuilder func stats() -> some View {
  18. ZStack {
  19. Color.gray.opacity(0.05).ignoresSafeArea(.all)
  20. let filter = DateFilter()
  21. switch state.selectedDuration {
  22. case .Today:
  23. StatsView(
  24. filter: filter.today,
  25. highLimit: state.highLimit,
  26. lowLimit: state.lowLimit,
  27. units: state.units,
  28. overrideUnit: state.overrideUnit
  29. )
  30. case .Day:
  31. StatsView(
  32. filter: filter.day,
  33. highLimit: state.highLimit,
  34. lowLimit: state.lowLimit,
  35. units: state.units,
  36. overrideUnit: state.overrideUnit
  37. )
  38. case .Week:
  39. StatsView(
  40. filter: filter.week,
  41. highLimit: state.highLimit,
  42. lowLimit: state.lowLimit,
  43. units: state.units,
  44. overrideUnit: state.overrideUnit
  45. )
  46. case .Month:
  47. StatsView(
  48. filter: filter.month,
  49. highLimit: state.highLimit,
  50. lowLimit: state.lowLimit,
  51. units: state.units,
  52. overrideUnit: state.overrideUnit
  53. )
  54. case .Total:
  55. StatsView(
  56. filter: filter.total,
  57. highLimit: state.highLimit,
  58. lowLimit: state.lowLimit,
  59. units: state.units,
  60. overrideUnit: state.overrideUnit
  61. )
  62. }
  63. }
  64. }
  65. @ViewBuilder func chart() -> some View {
  66. switch state.selectedDuration {
  67. case .Today:
  68. ChartsView(
  69. highLimit: state.highLimit,
  70. lowLimit: state.lowLimit,
  71. units: state.units,
  72. overrideUnit: state.overrideUnit,
  73. standing: state.layingChart,
  74. glucose: state.glucoseFromPersistence
  75. )
  76. case .Day:
  77. ChartsView(
  78. highLimit: state.highLimit,
  79. lowLimit: state.lowLimit,
  80. units: state.units,
  81. overrideUnit: state.overrideUnit,
  82. standing: state.layingChart,
  83. glucose: state.glucoseFromPersistence
  84. )
  85. case .Week:
  86. ChartsView(
  87. highLimit: state.highLimit,
  88. lowLimit: state.lowLimit,
  89. units: state.units,
  90. overrideUnit: state.overrideUnit,
  91. standing: state.layingChart,
  92. glucose: state.glucoseFromPersistence
  93. )
  94. case .Month:
  95. ChartsView(
  96. highLimit: state.highLimit,
  97. lowLimit: state.lowLimit,
  98. units: state.units,
  99. overrideUnit: state.overrideUnit,
  100. standing: state.layingChart,
  101. glucose: state.glucoseFromPersistence
  102. )
  103. case .Total:
  104. ChartsView(
  105. highLimit: state.highLimit,
  106. lowLimit: state.lowLimit,
  107. units: state.units,
  108. overrideUnit: state.overrideUnit,
  109. standing: state.layingChart,
  110. glucose: state.glucoseFromPersistence
  111. )
  112. }
  113. }
  114. var body: some View {
  115. VStack(alignment: .center) {
  116. chart().padding(.top, 20)
  117. Picker("Duration", selection: $state.selectedDuration) {
  118. ForEach(Stat.StateModel.Duration.allCases) { duration in
  119. Text(NSLocalizedString(duration.rawValue, comment: "")).tag(Optional(duration))
  120. }
  121. }.onChange(of: state.selectedDuration) { _, newValue in
  122. state.setupGlucoseArray(for: newValue)
  123. }
  124. .pickerStyle(.segmented).background(.cyan.opacity(0.2))
  125. stats()
  126. }.background(appState.trioBackgroundColor(for: colorScheme))
  127. .onAppear(perform: configureView)
  128. .navigationBarTitle("Statistics")
  129. .navigationBarTitleDisplayMode(.automatic)
  130. .toolbar {
  131. ToolbarItem(placement: .topBarLeading, content: {
  132. Button(
  133. action: { state.hideModal() },
  134. label: {
  135. HStack {
  136. Text("Close")
  137. }
  138. }
  139. )
  140. })
  141. }
  142. }
  143. }
  144. }