StatRootView.swift 5.4 KB

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