StatRootView.swift 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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.Duration.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. glucose: state.glucoseFromPersistence,
  92. highLimit: state.highLimit,
  93. lowLimit: state.lowLimit,
  94. units: state.units,
  95. hourlyStats: state.hourlyStats,
  96. isToday: state.selectedDurationForGlucoseStats == .Today
  97. )
  98. case .distribution:
  99. GlucoseDistributionChart(
  100. glucose: state.glucoseFromPersistence,
  101. highLimit: state.highLimit,
  102. lowLimit: state.lowLimit,
  103. units: state.units,
  104. glucoseRangeStats: state.glucoseRangeStats
  105. )
  106. }
  107. Divider()
  108. SectorChart(
  109. highLimit: state.highLimit,
  110. lowLimit: state.lowLimit,
  111. units: state.units,
  112. hbA1cDisplayUnit: state.hbA1cDisplayUnit,
  113. timeInRangeChartStyle: state.timeInRangeChartStyle,
  114. glucose: state.glucoseFromPersistence
  115. )
  116. }
  117. }
  118. }
  119. private var glucoseStatsCard: some View {
  120. StatCard {
  121. VStack(spacing: Constants.spacing) {
  122. BareStatisticsView.HbA1cView(
  123. highLimit: state.highLimit,
  124. lowLimit: state.lowLimit,
  125. units: state.units,
  126. hbA1cDisplayUnit: state.hbA1cDisplayUnit,
  127. glucose: state.glucoseFromPersistence
  128. )
  129. Divider()
  130. BareStatisticsView.BloodGlucoseView(
  131. highLimit: state.highLimit,
  132. lowLimit: state.lowLimit,
  133. units: state.units,
  134. hbA1cDisplayUnit: state.hbA1cDisplayUnit,
  135. glucose: state.glucoseFromPersistence
  136. )
  137. }
  138. }
  139. }
  140. @ViewBuilder var insulinView: some View {
  141. HStack {
  142. Text("Chart Type")
  143. .font(.headline)
  144. Spacer()
  145. Picker("Insulin Chart Type", selection: $state.selectedInsulinChartType) {
  146. ForEach(InsulinChartType.allCases, id: \.self) { type in
  147. Text(type.rawValue)
  148. }
  149. }.pickerStyle(.menu)
  150. }.padding(.horizontal)
  151. Picker("Duration", selection: $state.selectedDurationForInsulinStats) {
  152. ForEach(StateModel.StatsTimeInterval.allCases) { timeInterval in
  153. Text(timeInterval.rawValue).tag(timeInterval)
  154. }
  155. }
  156. .pickerStyle(.segmented)
  157. StatCard {
  158. switch state.selectedInsulinChartType {
  159. case .totalDailyDose:
  160. if state.dailyTDDStats.isEmpty {
  161. ContentUnavailableView(
  162. "No TDD Data",
  163. systemImage: "chart.bar.xaxis",
  164. description: Text("Total Daily Doses will appear here once data is available.")
  165. )
  166. } else {
  167. TDDChartView(
  168. selectedDuration: $state.selectedDurationForInsulinStats,
  169. tddStats: state.selectedDurationForInsulinStats == .Day ?
  170. state.hourlyTDDStats : state.dailyTDDStats,
  171. state: state
  172. )
  173. }
  174. case .bolusDistribution:
  175. // TODO: -
  176. var hasBolusData: Bool {
  177. state.dailyBolusStats.contains { $0.manualBolus > 0 || $0.smb > 0 || $0.external > 0 }
  178. }
  179. // TODO: -
  180. if state.dailyBolusStats.isEmpty || !hasBolusData {
  181. ContentUnavailableView(
  182. "No Bolus Data",
  183. systemImage: "cross.vial",
  184. description: Text("Bolus statistics will appear here once data is available.")
  185. )
  186. } else {
  187. BolusStatsView(
  188. selectedDuration: $state.selectedDurationForInsulinStats,
  189. bolusStats: state.selectedDurationForInsulinStats == .Day ?
  190. state.hourlyBolusStats : state.dailyBolusStats,
  191. state: state
  192. )
  193. }
  194. }
  195. }
  196. }
  197. @ViewBuilder var loopingView: some View {
  198. HStack {
  199. Text("Chart Type")
  200. .font(.headline)
  201. Spacer()
  202. Picker("Looping Chart Type", selection: $state.selectedLoopingChartType) {
  203. ForEach(LoopingChartType.allCases, id: \.self) { type in
  204. Text(type.rawValue)
  205. }
  206. }.pickerStyle(.menu)
  207. }.padding(.horizontal)
  208. Picker("Duration", selection: $state.selectedDurationForLoopStats) {
  209. ForEach(StateModel.Duration.allCases, id: \.self) { duration in
  210. Text(duration.rawValue)
  211. }
  212. }
  213. .pickerStyle(.segmented)
  214. switch state.selectedLoopingChartType {
  215. case .loopingPerformance:
  216. if state.loopStatRecords.isEmpty {
  217. ContentUnavailableView(
  218. "No Loop Data",
  219. systemImage: "clock.arrow.2.circlepath",
  220. description: Text("Loop statistics will appear here once data is available.")
  221. )
  222. } else {
  223. loopsCard
  224. loopStats
  225. }
  226. case .trioUpTime:
  227. Text("Not yet implemented")
  228. case .cgmConnectionTrace:
  229. Text("Not yet implemented")
  230. }
  231. }
  232. private var loopsCard: some View {
  233. StatCard {
  234. VStack(spacing: Constants.spacing) {
  235. LoopStatsView(
  236. loopStatRecords: state.loopStatRecords,
  237. selectedDuration: state.selectedDurationForLoopStats,
  238. groupedStats: state.groupedLoopStats
  239. )
  240. }
  241. }
  242. }
  243. private var loopStats: some View {
  244. StatCard {
  245. VStack(spacing: Constants.spacing) {
  246. BareStatisticsView.LoopsView(
  247. highLimit: state.highLimit,
  248. lowLimit: state.lowLimit,
  249. units: state.units,
  250. hbA1cDisplayUnit: state.hbA1cDisplayUnit,
  251. loopStatRecords: state.loopStatRecords
  252. )
  253. }
  254. }
  255. }
  256. @ViewBuilder var mealsView: some View {
  257. HStack {
  258. Text("Chart Type")
  259. .font(.headline)
  260. Spacer()
  261. Picker("Meal Chart Type", selection: $state.selectedMealChartType) {
  262. ForEach(MealChartType.allCases, id: \.self) { type in
  263. Text(type.rawValue)
  264. }
  265. }.pickerStyle(.menu)
  266. }.padding(.horizontal)
  267. Picker("Duration", selection: $state.selectedDurationForMealStats) {
  268. ForEach(StateModel.StatsTimeInterval.allCases, id: \.self) { timeInterval in
  269. Text(timeInterval.rawValue)
  270. }
  271. }
  272. .pickerStyle(.segmented)
  273. StatCard {
  274. switch state.selectedMealChartType {
  275. case .totalMeals:
  276. // TODO: -
  277. var hasMealData: Bool {
  278. state.dailyMealStats.contains { $0.carbs > 0 || $0.fat > 0 || $0.protein > 0 }
  279. }
  280. // TODO: -
  281. if state.dailyMealStats.isEmpty || !hasMealData {
  282. ContentUnavailableView(
  283. "No Meal Data",
  284. systemImage: "fork.knife",
  285. description: Text("Meal statistics will appear here once data is available.")
  286. )
  287. } else {
  288. MealStatsView(
  289. selectedDuration: $state.selectedDurationForMealStats,
  290. mealStats: state.selectedDurationForMealStats == .Day ?
  291. state.hourlyMealStats : state.dailyMealStats,
  292. state: state
  293. )
  294. }
  295. case .mealToHypoHyperDistribution:
  296. Text("TODO: Meal to Hypoglycemia/Hyperglycemia Distribution")
  297. }
  298. }
  299. }
  300. }
  301. }
  302. // MARK: - Supporting Views
  303. struct StatCard<Content: View>: View {
  304. let content: Content
  305. init(@ViewBuilder content: () -> Content) {
  306. self.content = content()
  307. }
  308. var body: some View {
  309. content
  310. .padding()
  311. .background(
  312. RoundedRectangle(cornerRadius: Stat.RootView.Constants.cornerRadius)
  313. .fill(Color.secondary.opacity(Stat.RootView.Constants.backgroundOpacity))
  314. )
  315. }
  316. }