StatRootView.swift 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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. @State private var isGlucoseDaySelected: Bool = false
  18. private var intervalOptions: [Stat.StateModel.StatsTimeIntervalWithToday] {
  19. state.selectedGlucoseChartType == .percentileByDay || state.selectedGlucoseChartType == .distributionByDay
  20. ? [.week, .month, .total] : Stat.StateModel.StatsTimeIntervalWithToday.allCases
  21. }
  22. var body: some View {
  23. VStack {
  24. Picker("View", selection: $selectedView) {
  25. ForEach(StateModel.StatisticViewType.allCases) { viewType in
  26. Text(viewType.displayName).tag(viewType)
  27. }
  28. }
  29. .pickerStyle(.segmented)
  30. .padding(.horizontal)
  31. ScrollView {
  32. VStack(spacing: Constants.spacing) {
  33. switch selectedView {
  34. case .glucose:
  35. glucoseView
  36. case .insulin:
  37. insulinView
  38. case .looping:
  39. loopingView
  40. case .meals:
  41. mealsView
  42. }
  43. }
  44. .padding()
  45. }
  46. }
  47. .background(appState.trioBackgroundColor(for: colorScheme))
  48. .onAppear(perform: configureView)
  49. .navigationBarTitleDisplayMode(.inline)
  50. .navigationTitle("Statistics")
  51. .toolbar {
  52. ToolbarItem(placement: .topBarLeading) {
  53. Button(action: state.hideModal) {
  54. Text("Close")
  55. .foregroundColor(.tabBar)
  56. }
  57. }
  58. }
  59. }
  60. // MARK: - Stats View
  61. @ViewBuilder var glucoseView: some View {
  62. HStack {
  63. Text("Chart Type")
  64. .font(.headline)
  65. Spacer()
  66. Picker("Glucose Chart Type", selection: $state.selectedGlucoseChartType) {
  67. ForEach(StateModel.GlucoseChartType.allCases, id: \.self) { type in
  68. Text(type.displayName)
  69. }
  70. }
  71. .pickerStyle(.menu)
  72. .onChange(of: state.selectedGlucoseChartType) { _, newValue in
  73. // If switching to daily chart and day/today is selected, switch to week
  74. if newValue == .percentileByDay || newValue == .distributionByDay,
  75. state.selectedIntervalForGlucoseStats == .day || state.selectedIntervalForGlucoseStats == .today
  76. {
  77. state.selectedIntervalForGlucoseStats = .week
  78. }
  79. }
  80. }.padding(.horizontal)
  81. Picker("Duration", selection: $state.selectedIntervalForGlucoseStats) {
  82. ForEach(intervalOptions, id: \.self) { timeInterval in
  83. Text(timeInterval.displayName)
  84. }
  85. }
  86. .pickerStyle(.segmented)
  87. if state.glucoseFromPersistence.isEmpty {
  88. ContentUnavailableView(
  89. String(localized: "No Glucose Data"),
  90. systemImage: "chart.bar.fill",
  91. description: Text("Glucose statistics will appear here once data is available.")
  92. )
  93. } else {
  94. timeInRangeCard
  95. if !isGlucoseDaySelected && state.selectedGlucoseChartType != .percentileByDay && state
  96. .selectedGlucoseChartType != .distributionByDay
  97. {
  98. glucoseStatsCard
  99. }
  100. HStack {
  101. var hintText: String {
  102. switch state.selectedGlucoseChartType {
  103. case .percentileByTime:
  104. String(localized: "Tap and hold the AGP graph or Time-in-Range ring to reveal more details.")
  105. case .distributionByTime:
  106. String(localized: "Tap and hold the Time-in-Range ring to reveal more details.")
  107. case .percentileByDay:
  108. String(
  109. localized: "Tap a percentile or tap and hold a bar to reveal more details. Swipe to scroll through time."
  110. )
  111. case .distributionByDay:
  112. String(
  113. localized: "Tap and hold a bar in the chart to reveal more details. Swipe to scroll through time."
  114. )
  115. }
  116. }
  117. Image(systemName: "hand.draw.fill")
  118. .foregroundStyle(Color.primary)
  119. .padding(.leading)
  120. Text(hintText)
  121. .foregroundStyle(Color.secondary)
  122. .padding(.trailing)
  123. }.font(.footnote)
  124. }
  125. }
  126. private var timeInRangeCard: some View {
  127. StatCard {
  128. VStack(spacing: Constants.spacing) {
  129. switch state.selectedGlucoseChartType {
  130. case .distributionByDay,
  131. .percentileByDay:
  132. let interval: Stat.StateModel.StatsTimeInterval = {
  133. switch state.selectedIntervalForGlucoseStats {
  134. case .month,
  135. .total:
  136. return Stat.StateModel.StatsTimeInterval(
  137. rawValue: state.selectedIntervalForGlucoseStats.rawValue
  138. )!
  139. default:
  140. return .week
  141. }
  142. }()
  143. if state.selectedGlucoseChartType == .percentileByDay {
  144. GlucoseDailyPercentileChart(
  145. glucose: state.glucoseFromPersistence,
  146. highLimit: state.highLimit,
  147. units: state.units,
  148. timeInRangeType: state.timeInRangeType,
  149. selectedInterval: interval,
  150. isDaySelected: $isGlucoseDaySelected
  151. ).environment(state)
  152. } else { // if state.selectedGlucoseChartType == .distributionByDay
  153. GlucoseDailyDistributionChart(
  154. glucose: state.glucoseReadings,
  155. highLimit: state.highLimit,
  156. units: state.units,
  157. timeInRangeType: state.timeInRangeType,
  158. selectedInterval: interval,
  159. eA1cDisplayUnit: state.eA1cDisplayUnit,
  160. isDaySelected: $isGlucoseDaySelected
  161. ).environment(state)
  162. }
  163. case .percentileByTime:
  164. GlucosePercentileChart(
  165. glucose: state.glucoseFromPersistence,
  166. highLimit: state.highLimit,
  167. timeInRangeType: state.timeInRangeType,
  168. units: state.units,
  169. hourlyStats: state.hourlyStats,
  170. isToday: state.selectedIntervalForGlucoseStats == .today
  171. )
  172. case .distributionByTime:
  173. GlucoseDistributionChart(
  174. glucose: state.glucoseReadings,
  175. highLimit: state.highLimit,
  176. lowLimit: state.lowLimit,
  177. units: state.units,
  178. glucoseRangeStats: state.glucoseRangeStats,
  179. timeInRangeType: state.timeInRangeType
  180. )
  181. }
  182. }
  183. }
  184. }
  185. private var glucoseStatsCard: some View {
  186. StatCard {
  187. VStack(spacing: Constants.spacing) {
  188. GlucoseSectorChart(
  189. highLimit: state.highLimit,
  190. units: state.units,
  191. glucose: state.glucoseFromPersistence,
  192. timeInRangeType: state.timeInRangeType,
  193. showChart: true
  194. )
  195. Divider()
  196. GlucoseMetricsView(
  197. units: state.units,
  198. eA1cDisplayUnit: state.eA1cDisplayUnit,
  199. glucose: state.glucoseFromPersistence
  200. )
  201. }
  202. }
  203. }
  204. @ViewBuilder var insulinView: some View {
  205. HStack {
  206. Text("Chart Type")
  207. .font(.headline)
  208. Spacer()
  209. Picker("Insulin Chart Type", selection: $state.selectedInsulinChartType) {
  210. ForEach(StateModel.InsulinChartType.allCases, id: \.self) { type in
  211. Text(type.displayName)
  212. }
  213. }.pickerStyle(.menu)
  214. }.padding(.horizontal)
  215. Picker("Duration", selection: $state.selectedIntervalForInsulinStats) {
  216. ForEach(StateModel.StatsTimeInterval.allCases) { timeInterval in
  217. Text(timeInterval.displayName).tag(timeInterval)
  218. }
  219. }
  220. .pickerStyle(.segmented)
  221. StatCard {
  222. switch state.selectedInsulinChartType {
  223. case .totalDailyDose:
  224. if state.dailyTDDStats.isEmpty {
  225. ContentUnavailableView(
  226. String(localized: "No TDD Data"),
  227. systemImage: "chart.bar.xaxis",
  228. description: Text("Total Daily Doses will appear here once data is available.")
  229. )
  230. } else {
  231. TotalDailyDoseChart(
  232. selectedInterval: $state.selectedIntervalForInsulinStats,
  233. tddStats: state.selectedIntervalForInsulinStats == .day ?
  234. state.hourlyTDDStats : state.dailyTDDStats,
  235. state: state
  236. )
  237. }
  238. case .bolusDistribution:
  239. var hasBolusData: Bool {
  240. state.dailyBolusStats.contains { $0.manualBolus > 0 || $0.smb > 0 || $0.external > 0 }
  241. }
  242. if state.dailyBolusStats.isEmpty || !hasBolusData {
  243. ContentUnavailableView(
  244. String(localized: "No Bolus Data"),
  245. systemImage: "cross.vial",
  246. description: Text("Bolus statistics will appear here once data is available.")
  247. )
  248. } else {
  249. BolusStatsView(
  250. selectedInterval: $state.selectedIntervalForInsulinStats,
  251. bolusStats: state.selectedIntervalForInsulinStats == .day ?
  252. state.hourlyBolusStats : state.dailyBolusStats,
  253. state: state
  254. )
  255. }
  256. }
  257. }
  258. HStack {
  259. Image(systemName: "hand.draw.fill").foregroundStyle(Color.primary)
  260. VStack(alignment: .leading) {
  261. Text("Swipe the chart to scroll through time.")
  262. Text("Tap and hold a bar to reveal more details.")
  263. }.foregroundStyle(Color.secondary)
  264. }.font(.footnote)
  265. }
  266. @ViewBuilder var loopingView: some View {
  267. HStack {
  268. Text("Chart Type")
  269. .font(.headline)
  270. Spacer()
  271. Picker("Looping Chart Type", selection: $state.selectedLoopingChartType) {
  272. ForEach(StateModel.LoopingChartType.allCases, id: \.self) { type in
  273. Text(type.displayName)
  274. }
  275. }.pickerStyle(.menu)
  276. }.padding(.horizontal)
  277. Picker("Duration", selection: $state.selectedIntervalForLoopStats) {
  278. ForEach(StateModel.StatsTimeIntervalWithToday.allCases, id: \.self) { interval in
  279. Text(interval.displayName)
  280. }
  281. }
  282. .pickerStyle(.segmented)
  283. StatCard {
  284. switch state.selectedLoopingChartType {
  285. case .loopingPerformance:
  286. if state.loopStatRecords.isEmpty {
  287. ContentUnavailableView(
  288. String(localized: "No Loop Data"),
  289. systemImage: "clock.arrow.2.circlepath",
  290. description: Text("Loop statistics will appear here once data is available.")
  291. )
  292. } else {
  293. loopingChartView
  294. loopStats
  295. }
  296. case .cgmConnectionTrace,
  297. .trioUpTime:
  298. // TODO: Trio Up-Time Chart & CGM Connection Trace Chart
  299. ContentUnavailableView(
  300. String(localized: "Coming soon."),
  301. systemImage: "hourglass",
  302. description: Text(state.selectedLoopingChartType.displayName)
  303. )
  304. }
  305. }
  306. }
  307. private var loopingChartView: some View {
  308. VStack(spacing: Constants.spacing) {
  309. LoopBarChartView(
  310. loopStatRecords: state.loopStatRecords,
  311. selectedInterval: state.selectedIntervalForLoopStats,
  312. statsData: state.loopStats
  313. )
  314. }
  315. }
  316. private var loopStats: some View {
  317. VStack(spacing: Constants.spacing) {
  318. LoopStatsView(
  319. statsData: state.loopStats
  320. )
  321. }
  322. }
  323. @ViewBuilder var mealsView: some View {
  324. HStack {
  325. Text("Chart Type")
  326. .font(.headline)
  327. Spacer()
  328. Picker("Meal Chart Type", selection: $state.selectedMealChartType) {
  329. ForEach(StateModel.MealChartType.allCases, id: \.self) { type in
  330. Text(type.displayName)
  331. }
  332. }.pickerStyle(.menu)
  333. }.padding(.horizontal)
  334. Picker("Duration", selection: $state.selectedIntervalForMealStats) {
  335. ForEach(StateModel.StatsTimeInterval.allCases, id: \.self) { timeInterval in
  336. Text(timeInterval.displayName)
  337. }
  338. }
  339. .pickerStyle(.segmented)
  340. StatCard {
  341. switch state.selectedMealChartType {
  342. case .totalMeals:
  343. var hasMealData: Bool {
  344. state.dailyMealStats.contains { $0.carbs > 0 || $0.fat > 0 || $0.protein > 0 }
  345. }
  346. if state.dailyMealStats.isEmpty || !hasMealData {
  347. ContentUnavailableView(
  348. String(localized: "No Meal Data"),
  349. systemImage: "fork.knife",
  350. description: Text("Meal statistics will appear here once data is available.")
  351. )
  352. } else {
  353. MealStatsView(
  354. selectedInterval: $state.selectedIntervalForMealStats,
  355. mealStats: state.selectedIntervalForMealStats == .day ?
  356. state.hourlyMealStats : state.dailyMealStats,
  357. state: state
  358. )
  359. }
  360. case .mealToHypoHyperDistribution:
  361. // TODO: Meal to Hypoglycemia/Hyperglycemia Distribution
  362. ContentUnavailableView(
  363. String(localized: "Coming soon."),
  364. systemImage: "hourglass",
  365. description: Text(state.selectedMealChartType.displayName)
  366. )
  367. }
  368. }
  369. HStack {
  370. Image(systemName: "hand.draw.fill").foregroundStyle(Color.primary)
  371. VStack(alignment: .leading) {
  372. Text("Swipe the chart to scroll through time.")
  373. Text("Tap and hold a bar to reveal more details.")
  374. }.foregroundStyle(Color.secondary)
  375. }.font(.footnote)
  376. }
  377. }
  378. }
  379. // MARK: - Supporting Views
  380. struct StatCard<Content: View>: View {
  381. let content: Content
  382. init(@ViewBuilder content: () -> Content) {
  383. self.content = content()
  384. }
  385. var body: some View {
  386. content
  387. .padding()
  388. .background(
  389. RoundedRectangle(cornerRadius: Stat.RootView.Constants.cornerRadius)
  390. .fill(Color.secondary.opacity(Stat.RootView.Constants.backgroundOpacity))
  391. )
  392. }
  393. }