StatRootView.swift 5.6 KB

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