StatRootView.swift 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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.displayName).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(StateModel.GlucoseChartType.allCases, id: \.self) { type in
  63. Text(type.displayName)
  64. }
  65. }
  66. .pickerStyle(.menu)
  67. }.padding(.horizontal)
  68. Picker("Duration", selection: $state.selectedIntervalForGlucoseStats) {
  69. ForEach(StateModel.StatsTimeIntervalWithToday.allCases, id: \.self) { timeInterval in
  70. Text(timeInterval.displayName)
  71. }
  72. }
  73. .pickerStyle(.segmented)
  74. if state.glucoseFromPersistence.isEmpty {
  75. ContentUnavailableView(
  76. String(localized: "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. HStack {
  84. var hintText: String {
  85. switch state.selectedGlucoseChartType {
  86. case .percentile:
  87. String(localized: "Tap and hold the AGP graph or Time-in-Range ring to reveal more details.")
  88. case .distribution:
  89. String(localized: "Tap and hold the Time-in-Range ring to reveal more details.")
  90. }
  91. }
  92. Image(systemName: "hand.draw.fill")
  93. .foregroundStyle(Color.primary)
  94. .padding(.leading)
  95. Text(hintText)
  96. .foregroundStyle(Color.secondary)
  97. .padding(.trailing)
  98. }.font(.footnote)
  99. }
  100. }
  101. private var timeInRangeCard: some View {
  102. StatCard {
  103. VStack(spacing: Constants.spacing) {
  104. switch state.selectedGlucoseChartType {
  105. case .percentile:
  106. GlucosePercentileChart(
  107. glucose: state.glucoseFromPersistence,
  108. highLimit: state.highLimit,
  109. lowLimit: state.lowLimit,
  110. units: state.units,
  111. hourlyStats: state.hourlyStats,
  112. isToday: state.selectedIntervalForGlucoseStats == .today
  113. )
  114. case .distribution:
  115. GlucoseDistributionChart(
  116. glucose: state.glucoseFromPersistence,
  117. highLimit: state.highLimit,
  118. lowLimit: state.lowLimit,
  119. units: state.units,
  120. glucoseRangeStats: state.glucoseRangeStats
  121. )
  122. }
  123. }
  124. }
  125. }
  126. private var glucoseStatsCard: some View {
  127. StatCard {
  128. VStack(spacing: Constants.spacing) {
  129. GlucoseSectorChart(
  130. highLimit: state.highLimit,
  131. lowLimit: state.lowLimit,
  132. units: state.units,
  133. glucose: state.glucoseFromPersistence
  134. )
  135. Divider()
  136. GlucoseMetricsView(
  137. highLimit: state.highLimit,
  138. lowLimit: state.lowLimit,
  139. units: state.units,
  140. eA1cDisplayUnit: state.eA1cDisplayUnit,
  141. glucose: state.glucoseFromPersistence
  142. )
  143. }
  144. }
  145. }
  146. @ViewBuilder var insulinView: some View {
  147. HStack {
  148. Text("Chart Type")
  149. .font(.headline)
  150. Spacer()
  151. Picker("Insulin Chart Type", selection: $state.selectedInsulinChartType) {
  152. ForEach(StateModel.InsulinChartType.allCases, id: \.self) { type in
  153. Text(type.displayName)
  154. }
  155. }.pickerStyle(.menu)
  156. }.padding(.horizontal)
  157. Picker("Duration", selection: $state.selectedIntervalForInsulinStats) {
  158. ForEach(StateModel.StatsTimeInterval.allCases) { timeInterval in
  159. Text(timeInterval.displayName).tag(timeInterval)
  160. }
  161. }
  162. .pickerStyle(.segmented)
  163. StatCard {
  164. switch state.selectedInsulinChartType {
  165. case .totalDailyDose:
  166. if state.dailyTDDStats.isEmpty {
  167. ContentUnavailableView(
  168. String(localized: "No TDD Data"),
  169. systemImage: "chart.bar.xaxis",
  170. description: Text("Total Daily Doses will appear here once data is available.")
  171. )
  172. } else {
  173. TotalDailyDoseChart(
  174. selectedInterval: $state.selectedIntervalForInsulinStats,
  175. tddStats: state.selectedIntervalForInsulinStats == .day ?
  176. state.hourlyTDDStats : state.dailyTDDStats,
  177. state: state
  178. )
  179. }
  180. case .bolusDistribution:
  181. var hasBolusData: Bool {
  182. state.dailyBolusStats.contains { $0.manualBolus > 0 || $0.smb > 0 || $0.external > 0 }
  183. }
  184. if state.dailyBolusStats.isEmpty || !hasBolusData {
  185. ContentUnavailableView(
  186. String(localized: "No Bolus Data"),
  187. systemImage: "cross.vial",
  188. description: Text("Bolus statistics will appear here once data is available.")
  189. )
  190. } else {
  191. BolusStatsView(
  192. selectedInterval: $state.selectedIntervalForInsulinStats,
  193. bolusStats: state.selectedIntervalForInsulinStats == .day ?
  194. state.hourlyBolusStats : state.dailyBolusStats,
  195. state: state
  196. )
  197. }
  198. }
  199. }
  200. HStack {
  201. Image(systemName: "hand.draw.fill").foregroundStyle(Color.primary)
  202. VStack(alignment: .leading) {
  203. Text("Swipe the chart to scroll through time.")
  204. Text("Tap and hold a bar to reveal more details.")
  205. }.foregroundStyle(Color.secondary)
  206. }.font(.footnote)
  207. }
  208. @ViewBuilder var loopingView: some View {
  209. HStack {
  210. Text("Chart Type")
  211. .font(.headline)
  212. Spacer()
  213. Picker("Looping Chart Type", selection: $state.selectedLoopingChartType) {
  214. ForEach(StateModel.LoopingChartType.allCases, id: \.self) { type in
  215. Text(type.displayName)
  216. }
  217. }.pickerStyle(.menu)
  218. }.padding(.horizontal)
  219. Picker("Duration", selection: $state.selectedIntervalForLoopStats) {
  220. ForEach(StateModel.StatsTimeIntervalWithToday.allCases, id: \.self) { interval in
  221. Text(interval.displayName)
  222. }
  223. }
  224. .pickerStyle(.segmented)
  225. StatCard {
  226. switch state.selectedLoopingChartType {
  227. case .loopingPerformance:
  228. if state.loopStatRecords.isEmpty {
  229. ContentUnavailableView(
  230. String(localized: "No Loop Data"),
  231. systemImage: "clock.arrow.2.circlepath",
  232. description: Text("Loop statistics will appear here once data is available.")
  233. )
  234. } else {
  235. loopingChartView
  236. loopStats
  237. }
  238. case .trioUpTime:
  239. // TODO: Trio Up-Time Chart
  240. ContentUnavailableView(
  241. String(localized: "Coming soon."),
  242. systemImage: "hourglass",
  243. description: Text("Trio Up-Time Chart")
  244. )
  245. case .cgmConnectionTrace:
  246. // TODO: CGM Connection Trace Chart
  247. ContentUnavailableView(
  248. String(localized: "Coming soon."),
  249. systemImage: "hourglass",
  250. description: Text("CGM Connection Trace Chart")
  251. )
  252. }
  253. }
  254. }
  255. private var loopingChartView: some View {
  256. VStack(spacing: Constants.spacing) {
  257. LoopBarChartView(
  258. loopStatRecords: state.loopStatRecords,
  259. selectedInterval: state.selectedIntervalForLoopStats,
  260. statsData: state.loopStats
  261. )
  262. }
  263. }
  264. private var loopStats: some View {
  265. VStack(spacing: Constants.spacing) {
  266. LoopStatsView(
  267. statsData: state.loopStats
  268. )
  269. }
  270. }
  271. @ViewBuilder var mealsView: some View {
  272. HStack {
  273. Text("Chart Type")
  274. .font(.headline)
  275. Spacer()
  276. Picker("Meal Chart Type", selection: $state.selectedMealChartType) {
  277. ForEach(StateModel.MealChartType.allCases, id: \.self) { type in
  278. Text(type.displayName)
  279. }
  280. }.pickerStyle(.menu)
  281. }.padding(.horizontal)
  282. Picker("Duration", selection: $state.selectedIntervalForMealStats) {
  283. ForEach(StateModel.StatsTimeInterval.allCases, id: \.self) { timeInterval in
  284. Text(timeInterval.displayName)
  285. }
  286. }
  287. .pickerStyle(.segmented)
  288. StatCard {
  289. switch state.selectedMealChartType {
  290. case .totalMeals:
  291. var hasMealData: Bool {
  292. state.dailyMealStats.contains { $0.carbs > 0 || $0.fat > 0 || $0.protein > 0 }
  293. }
  294. if state.dailyMealStats.isEmpty || !hasMealData {
  295. ContentUnavailableView(
  296. String(localized: "No Meal Data"),
  297. systemImage: "fork.knife",
  298. description: Text("Meal statistics will appear here once data is available.")
  299. )
  300. } else {
  301. MealStatsView(
  302. selectedInterval: $state.selectedIntervalForMealStats,
  303. mealStats: state.selectedIntervalForMealStats == .day ?
  304. state.hourlyMealStats : state.dailyMealStats,
  305. state: state
  306. )
  307. }
  308. case .mealToHypoHyperDistribution:
  309. // TODO: Meal to Hypoglycemia/Hyperglycemia Distribution
  310. ContentUnavailableView(
  311. String(localized: "Coming soon."),
  312. systemImage: "hourglass",
  313. description: Text("Meal to Hypoglycemia/Hyperglycemia Distribution Chart")
  314. )
  315. }
  316. }
  317. HStack {
  318. Image(systemName: "hand.draw.fill").foregroundStyle(Color.primary)
  319. VStack(alignment: .leading) {
  320. Text("Swipe the chart to scroll through time.")
  321. Text("Tap and hold a bar to reveal more details.")
  322. }.foregroundStyle(Color.secondary)
  323. }.font(.footnote)
  324. }
  325. }
  326. }
  327. // MARK: - Supporting Views
  328. struct StatCard<Content: View>: View {
  329. let content: Content
  330. init(@ViewBuilder content: () -> Content) {
  331. self.content = content()
  332. }
  333. var body: some View {
  334. content
  335. .padding()
  336. .background(
  337. RoundedRectangle(cornerRadius: Stat.RootView.Constants.cornerRadius)
  338. .fill(Color.secondary.opacity(Stat.RootView.Constants.backgroundOpacity))
  339. )
  340. }
  341. }