StatRootView.swift 5.9 KB

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