StatRootView.swift 5.6 KB

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