StatRootView.swift 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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.dailyTDDStats.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.selectedDurationForInsulinStats == .Day ?
  173. state.hourlyTDDStats : state.dailyTDDStats,
  174. state: state
  175. )
  176. }
  177. case .bolusDistribution:
  178. // TODO: -
  179. var hasBolusData: Bool {
  180. state.dailyBolusStats.contains { $0.manualBolus > 0 || $0.smb > 0 || $0.external > 0 }
  181. }
  182. // TODO: -
  183. if state.dailyBolusStats.isEmpty || !hasBolusData {
  184. ContentUnavailableView(
  185. "No Bolus Data",
  186. systemImage: "cross.vial",
  187. description: Text("Bolus statistics will appear here once data is available.")
  188. )
  189. } else {
  190. BolusStatsView(
  191. selectedDuration: $state.selectedDurationForInsulinStats,
  192. bolusStats: state.selectedDurationForInsulinStats == .Day ?
  193. state.hourlyBolusStats : state.dailyBolusStats,
  194. state: state
  195. )
  196. }
  197. }
  198. }
  199. }
  200. @ViewBuilder var loopingView: some View {
  201. HStack {
  202. Text("Chart Type")
  203. .font(.headline)
  204. Spacer()
  205. Picker("Looping Chart Type", selection: $state.selectedLoopingChartType) {
  206. ForEach(LoopingChartType.allCases, id: \.self) { type in
  207. Text(type.rawValue)
  208. }
  209. }.pickerStyle(.menu)
  210. }.padding(.horizontal)
  211. Picker("Duration", selection: $state.selectedDurationForLoopStats) {
  212. ForEach(StateModel.Duration.allCases, id: \.self) { duration in
  213. Text(duration.rawValue)
  214. }
  215. }
  216. .pickerStyle(.segmented)
  217. switch state.selectedLoopingChartType {
  218. case .loopingPerformance:
  219. if state.loopStatRecords.isEmpty {
  220. ContentUnavailableView(
  221. "No Loop Data",
  222. systemImage: "clock.arrow.2.circlepath",
  223. description: Text("Loop statistics will appear here once data is available.")
  224. )
  225. } else {
  226. loopsCard
  227. loopStats
  228. }
  229. case .trioUpTime:
  230. Text("Not yet implemented")
  231. case .cgmConnectionTrace:
  232. Text("Not yet implemented")
  233. }
  234. }
  235. private var loopsCard: some View {
  236. StatCard {
  237. VStack(spacing: Constants.spacing) {
  238. LoopStatsView(
  239. loopStatRecords: state.loopStatRecords,
  240. selectedDuration: state.selectedDurationForLoopStats,
  241. groupedStats: state.groupedLoopStats
  242. )
  243. }
  244. }
  245. }
  246. private var loopStats: some View {
  247. StatCard {
  248. VStack(spacing: Constants.spacing) {
  249. BareStatisticsView.LoopsView(
  250. highLimit: state.highLimit,
  251. lowLimit: state.lowLimit,
  252. units: state.units,
  253. hbA1cDisplayUnit: state.hbA1cDisplayUnit,
  254. loopStatRecords: state.loopStatRecords
  255. )
  256. }
  257. }
  258. }
  259. @ViewBuilder var mealsView: some View {
  260. HStack {
  261. Text("Chart Type")
  262. .font(.headline)
  263. Spacer()
  264. Picker("Meal Chart Type", selection: $state.selectedMealChartType) {
  265. ForEach(MealChartType.allCases, id: \.self) { type in
  266. Text(type.rawValue)
  267. }
  268. }.pickerStyle(.menu)
  269. }.padding(.horizontal)
  270. Picker("Duration", selection: $state.selectedDurationForMealStats) {
  271. ForEach(StateModel.StatsTimeInterval.allCases, id: \.self) { timeInterval in
  272. Text(timeInterval.rawValue)
  273. }
  274. }
  275. .pickerStyle(.segmented)
  276. StatCard {
  277. switch state.selectedMealChartType {
  278. case .totalMeals:
  279. // TODO: -
  280. var hasMealData: Bool {
  281. state.dailyMealStats.contains { $0.carbs > 0 || $0.fat > 0 || $0.protein > 0 }
  282. }
  283. // TODO: -
  284. if state.dailyMealStats.isEmpty || !hasMealData {
  285. ContentUnavailableView(
  286. "No Meal Data",
  287. systemImage: "fork.knife",
  288. description: Text("Meal statistics will appear here once data is available.")
  289. )
  290. } else {
  291. MealStatsView(
  292. selectedDuration: $state.selectedDurationForMealStats,
  293. mealStats: state.selectedDurationForMealStats == .Day ?
  294. state.hourlyMealStats : state.dailyMealStats,
  295. state: state
  296. )
  297. }
  298. case .mealToHypoHyperDistribution:
  299. Text("TODO: Meal to Hypoglycemia/Hyperglycemia Distribution")
  300. }
  301. }
  302. }
  303. }
  304. }
  305. // MARK: - Supporting Views
  306. struct StatCard<Content: View>: View {
  307. let content: Content
  308. init(@ViewBuilder content: () -> Content) {
  309. self.content = content()
  310. }
  311. var body: some View {
  312. content
  313. .padding()
  314. .background(
  315. RoundedRectangle(cornerRadius: Stat.RootView.Constants.cornerRadius)
  316. .fill(Color.secondary.opacity(Stat.RootView.Constants.backgroundOpacity))
  317. )
  318. }
  319. }