StatRootView.swift 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. import Charts
  2. import SwiftDate
  3. import SwiftUI
  4. import Swinject
  5. extension Stat {
  6. struct RootView: BaseView {
  7. enum Constants {
  8. static let spacing: CGFloat = 16
  9. static let cornerRadius: CGFloat = 10
  10. static let backgroundOpacity = 0.1
  11. }
  12. let resolver: Resolver
  13. @Environment(\.colorScheme) var colorScheme
  14. @Environment(AppState.self) var appState
  15. @State var state = StateModel()
  16. @State private var selectedView: StateModel.StatisticViewType = .glucose
  17. var body: some View {
  18. VStack {
  19. Picker("View", selection: $selectedView) {
  20. ForEach(StateModel.StatisticViewType.allCases) { viewType in
  21. Text(viewType.title).tag(viewType)
  22. }
  23. }
  24. .pickerStyle(.segmented)
  25. .padding(.horizontal)
  26. ScrollView {
  27. VStack(spacing: Constants.spacing) {
  28. switch selectedView {
  29. case .glucose:
  30. glucoseView
  31. case .insulin:
  32. insulinView
  33. case .looping:
  34. loopingView
  35. case .meals:
  36. mealsView
  37. }
  38. }
  39. .padding()
  40. }
  41. }
  42. .background(appState.trioBackgroundColor(for: colorScheme))
  43. .onAppear(perform: configureView)
  44. .navigationBarTitleDisplayMode(.inline)
  45. .navigationTitle("Statistics")
  46. .toolbar {
  47. ToolbarItem(placement: .topBarLeading) {
  48. Button(action: state.hideModal) {
  49. Text("Close")
  50. .foregroundColor(.tabBar)
  51. }
  52. }
  53. }
  54. }
  55. // MARK: - Stats View
  56. @ViewBuilder var glucoseView: some View {
  57. HStack {
  58. Text("Chart Type")
  59. .font(.headline)
  60. Spacer()
  61. Picker("Glucose Chart Type", selection: $state.selectedGlucoseChartType) {
  62. ForEach(GlucoseChartType.allCases, id: \.self) { type in
  63. Text(type.rawValue)
  64. }
  65. }
  66. .pickerStyle(.menu)
  67. }.padding(.horizontal)
  68. Picker("Duration", selection: $state.selectedDurationForGlucoseStats) {
  69. ForEach(StateModel.StatsTimeInterval.allCases, id: \.self) { timeInterval in
  70. Text(timeInterval.rawValue)
  71. }
  72. }
  73. .pickerStyle(.segmented)
  74. if state.glucoseFromPersistence.isEmpty {
  75. ContentUnavailableView(
  76. "No Glucose Data",
  77. systemImage: "chart.bar.fill",
  78. description: Text("Glucose statistics will appear here once data is available.")
  79. )
  80. } else {
  81. timeInRangeCard
  82. glucoseStatsCard
  83. }
  84. }
  85. private var timeInRangeCard: some View {
  86. StatCard {
  87. VStack(spacing: Constants.spacing) {
  88. switch state.selectedGlucoseChartType {
  89. case .percentile:
  90. GlucosePercentileChart(
  91. selectedDuration: $state.selectedDurationForGlucoseStats,
  92. state: state,
  93. glucose: state.glucoseFromPersistence,
  94. highLimit: state.highLimit,
  95. lowLimit: state.lowLimit,
  96. units: state.units,
  97. hourlyStats: state.hourlyStats
  98. )
  99. case .distribution:
  100. GlucoseDistributionChart(
  101. selectedDuration: $state.selectedDurationForGlucoseStats,
  102. state: state,
  103. glucose: state.glucoseFromPersistence,
  104. highLimit: state.highLimit,
  105. lowLimit: state.lowLimit,
  106. units: state.units,
  107. glucoseRangeStats: state.glucoseRangeStats
  108. )
  109. }
  110. Divider()
  111. SectorChart(
  112. highLimit: state.highLimit,
  113. lowLimit: state.lowLimit,
  114. units: state.units,
  115. hbA1cDisplayUnit: state.hbA1cDisplayUnit,
  116. timeInRangeChartStyle: state.timeInRangeChartStyle,
  117. glucose: state.glucoseFromPersistence
  118. )
  119. }
  120. }
  121. }
  122. private var glucoseStatsCard: some View {
  123. StatCard {
  124. VStack(spacing: Constants.spacing) {
  125. BareStatisticsView.HbA1cView(
  126. highLimit: state.highLimit,
  127. lowLimit: state.lowLimit,
  128. units: state.units,
  129. hbA1cDisplayUnit: state.hbA1cDisplayUnit,
  130. glucose: state.glucoseFromPersistence
  131. )
  132. Divider()
  133. BareStatisticsView.BloodGlucoseView(
  134. highLimit: state.highLimit,
  135. lowLimit: state.lowLimit,
  136. units: state.units,
  137. hbA1cDisplayUnit: state.hbA1cDisplayUnit,
  138. glucose: state.glucoseFromPersistence
  139. )
  140. }
  141. }
  142. }
  143. @ViewBuilder var insulinView: some View {
  144. HStack {
  145. Text("Chart Type")
  146. .font(.headline)
  147. Spacer()
  148. Picker("Insulin Chart Type", selection: $state.selectedInsulinChartType) {
  149. ForEach(InsulinChartType.allCases, id: \.self) { type in
  150. Text(type.rawValue)
  151. }
  152. }.pickerStyle(.menu)
  153. }.padding(.horizontal)
  154. Picker("Duration", selection: $state.selectedDurationForInsulinStats) {
  155. ForEach(StateModel.StatsTimeInterval.allCases) { timeInterval in
  156. Text(timeInterval.rawValue).tag(timeInterval)
  157. }
  158. }
  159. .pickerStyle(.segmented)
  160. StatCard {
  161. switch state.selectedInsulinChartType {
  162. case .totalDailyDose:
  163. if state.tddStats.isEmpty {
  164. ContentUnavailableView(
  165. "No TDD Data",
  166. systemImage: "chart.bar.xaxis",
  167. description: Text("Total Daily Doses will appear here once data is available.")
  168. )
  169. } else {
  170. TDDChartView(
  171. selectedDuration: $state.selectedDurationForInsulinStats,
  172. tddStats: state.tddStats,
  173. calculateAverage: { start, end in
  174. await state.calculateAverageTDD(from: start, to: end)
  175. },
  176. calculateMedian: { start, end in
  177. await state.calculateMedianTDD(from: start, to: end)
  178. }
  179. )
  180. }
  181. case .bolusDistribution:
  182. var hasBolusData: Bool {
  183. state.bolusStats.contains { $0.manualBolus > 0 || $0.smb > 0 || $0.external > 0 }
  184. }
  185. if state.bolusStats.isEmpty || !hasBolusData {
  186. ContentUnavailableView(
  187. "No Bolus Data",
  188. systemImage: "cross.vial",
  189. description: Text("Bolus statistics will appear here once data is available.")
  190. )
  191. } else {
  192. BolusStatsView(
  193. selectedDuration: $state.selectedDurationForInsulinStats,
  194. bolusStats: state.bolusStats,
  195. calculateAverages: { start, end in
  196. await state.calculateAverageBolus(from: start, to: end)
  197. }
  198. )
  199. }
  200. }
  201. }
  202. }
  203. @ViewBuilder var loopingView: some View {
  204. HStack {
  205. Text("Chart Type")
  206. .font(.headline)
  207. Spacer()
  208. Picker("Looping Chart Type", selection: $state.selectedLoopingChartType) {
  209. ForEach(LoopingChartType.allCases, id: \.self) { type in
  210. Text(type.rawValue)
  211. }
  212. }.pickerStyle(.menu)
  213. }.padding(.horizontal)
  214. Picker("Duration", selection: $state.selectedDurationForLoopStats) {
  215. ForEach(StateModel.Duration.allCases, id: \.self) { duration in
  216. Text(duration.rawValue)
  217. }
  218. }
  219. .pickerStyle(.segmented)
  220. switch state.selectedLoopingChartType {
  221. case .loopingPerformance:
  222. if state.loopStatRecords.isEmpty {
  223. ContentUnavailableView(
  224. "No Loop Data",
  225. systemImage: "clock.arrow.2.circlepath",
  226. description: Text("Loop statistics will appear here once data is available.")
  227. )
  228. } else {
  229. loopsCard
  230. loopStats
  231. }
  232. case .trioUpTime:
  233. Text("Not yet implemented")
  234. case .cgmConnectionTrace:
  235. Text("Not yet implemented")
  236. }
  237. }
  238. private var loopsCard: some View {
  239. StatCard {
  240. VStack(spacing: Constants.spacing) {
  241. LoopStatsView(
  242. loopStatRecords: state.loopStatRecords,
  243. selectedDuration: state.selectedDurationForLoopStats,
  244. groupedStats: state.groupedLoopStats
  245. )
  246. }
  247. }
  248. }
  249. private var loopStats: some View {
  250. StatCard {
  251. VStack(spacing: Constants.spacing) {
  252. BareStatisticsView.LoopsView(
  253. highLimit: state.highLimit,
  254. lowLimit: state.lowLimit,
  255. units: state.units,
  256. hbA1cDisplayUnit: state.hbA1cDisplayUnit,
  257. loopStatRecords: state.loopStatRecords
  258. )
  259. }
  260. }
  261. }
  262. @ViewBuilder var mealsView: some View {
  263. HStack {
  264. Text("Chart Type")
  265. .font(.headline)
  266. Spacer()
  267. Picker("Meal Chart Type", selection: $state.selectedMealChartType) {
  268. ForEach(MealChartType.allCases, id: \.self) { type in
  269. Text(type.rawValue)
  270. }
  271. }.pickerStyle(.menu)
  272. }.padding(.horizontal)
  273. Picker("Duration", selection: $state.selectedDurationForMealStats) {
  274. ForEach(StateModel.StatsTimeInterval.allCases, id: \.self) { timeInterval in
  275. Text(timeInterval.rawValue)
  276. }
  277. }
  278. .pickerStyle(.segmented)
  279. StatCard {
  280. switch state.selectedMealChartType {
  281. case .totalMeals:
  282. // TODO: -
  283. var hasMealData: Bool {
  284. state.dailyMealStats.contains { $0.carbs > 0 || $0.fat > 0 || $0.protein > 0 }
  285. }
  286. // TODO: -
  287. if state.dailyMealStats.isEmpty || !hasMealData {
  288. ContentUnavailableView(
  289. "No Meal Data",
  290. systemImage: "fork.knife",
  291. description: Text("Meal statistics will appear here once data is available.")
  292. )
  293. } else {
  294. MealStatsView(
  295. selectedDuration: $state.selectedDurationForMealStats,
  296. mealStats: state.selectedDurationForMealStats == .Day ?
  297. state.hourlyMealStats : state.dailyMealStats,
  298. state: state
  299. )
  300. }
  301. case .mealToHypoHyperDistribution:
  302. Text("TODO: Meal to Hypoglycemia/Hyperglycemia Distribution")
  303. }
  304. }
  305. }
  306. }
  307. }
  308. // MARK: - Supporting Views
  309. struct StatCard<Content: View>: View {
  310. let content: Content
  311. init(@ViewBuilder content: () -> Content) {
  312. self.content = content()
  313. }
  314. var body: some View {
  315. content
  316. .padding()
  317. .background(
  318. RoundedRectangle(cornerRadius: Stat.RootView.Constants.cornerRadius)
  319. .fill(Color.secondary.opacity(Stat.RootView.Constants.backgroundOpacity))
  320. )
  321. }
  322. }