StatRootView.swift 13 KB

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