TrioMainWatchView.swift 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. import Charts
  2. import SwiftUI
  3. struct TrioMainWatchView: View {
  4. @State private var state = WatchState()
  5. // misc
  6. @State private var currentPage: Int = 0
  7. @State private var rotationDegrees: Double = 0.0
  8. @State private var showingTempTargetSheet = false
  9. // view visbility
  10. @State private var showingTreatmentMenuSheet: Bool = false
  11. @State private var showingOverrideSheet: Bool = false
  12. // navigation flag for meal bolus combo
  13. @State private var continueToBolus = false
  14. // @State private var navigationPath: [NavigationDestinations] = []
  15. @State private var navigationPath = NavigationPath()
  16. // treatments
  17. @State private var selectedTreatment: TreatmentOption?
  18. // Active adjustment indicator
  19. private func isAdjustmentActive<T>(for presets: [T], predicate: (T) -> Bool) -> Bool {
  20. let sortedPresets = presets.sorted { predicate($0) && !predicate($1) }
  21. return !sortedPresets.isEmpty && sortedPresets.first(where: predicate) != nil
  22. }
  23. private var isTempTargetActive: Bool {
  24. isAdjustmentActive(for: state.tempTargetPresets) { $0.isEnabled }
  25. }
  26. private var isOverrideActive: Bool {
  27. isAdjustmentActive(for: state.overridePresets) { $0.isEnabled }
  28. }
  29. private var trioBackgroundColor = LinearGradient(
  30. gradient: Gradient(colors: [Color.bgDarkBlue, Color.bgDarkerDarkBlue]),
  31. startPoint: .top,
  32. endPoint: .bottom
  33. )
  34. var body: some View {
  35. NavigationStack(path: $navigationPath) {
  36. TabView(selection: $currentPage) {
  37. // Page 1: Current glucose trend in "BG bobble"
  38. GlucoseTrendView(state: state, rotationDegrees: rotationDegrees)
  39. .tag(0)
  40. // Page 2: Glucose chart
  41. GlucoseChartView(glucoseValues: state.glucoseValues)
  42. .tag(1)
  43. }
  44. .background(trioBackgroundColor)
  45. .tabViewStyle(.verticalPage)
  46. .digitalCrownRotation($currentPage.doubleBinding(), from: 0, through: 1, by: 1)
  47. .onChange(of: state.trend) { _, newTrend in
  48. withAnimation {
  49. updateRotation(for: newTrend)
  50. }
  51. }
  52. .onAppear {
  53. state.confirmationProgress = 0 // reset auth progress
  54. }
  55. .toolbar {
  56. ToolbarItem(placement: .topBarLeading) {
  57. HStack {
  58. Image(systemName: "syringe.fill")
  59. .foregroundStyle(.blue)
  60. Text(state.iob ?? "--")
  61. .foregroundStyle(.white)
  62. }.font(.caption)
  63. }
  64. ToolbarItem(placement: .topBarTrailing) {
  65. HStack {
  66. Text(state.cob ?? "--")
  67. .foregroundStyle(.white)
  68. Image(systemName: "fork.knife")
  69. .foregroundStyle(.orange)
  70. }.font(.caption)
  71. }
  72. ToolbarItemGroup(placement: .bottomBar) {
  73. Button {
  74. showingOverrideSheet = true
  75. } label: {
  76. Image(systemName: "clock.arrow.2.circlepath")
  77. .foregroundStyle(Color.primary, isOverrideActive ? Color.primary : Color.purple)
  78. }.tint(isOverrideActive ? Color.purple : nil)
  79. Button {
  80. showingTreatmentMenuSheet = true
  81. } label: {
  82. Image(systemName: "plus")
  83. .foregroundStyle(Color.bgDarkerDarkBlue)
  84. }
  85. .controlSize(.large)
  86. .buttonStyle(WatchOSButtonStyle())
  87. Button {
  88. showingTempTargetSheet = true
  89. } label: {
  90. Image(systemName: "target")
  91. .foregroundStyle(isTempTargetActive ? Color.primary : Color.green.opacity(0.75))
  92. }.tint(isTempTargetActive ? Color.green.opacity(0.75) : nil)
  93. }
  94. }
  95. .fullScreenCover(isPresented: $showingTreatmentMenuSheet) {
  96. TreatmentMenuView(selectedTreatment: $selectedTreatment) {
  97. handleTreatmentSelection()
  98. }
  99. .onAppear {
  100. // reset the conditional navigation flag when opening
  101. continueToBolus = false
  102. }
  103. }
  104. .sheet(isPresented: $showingOverrideSheet) {
  105. OverridePresetsView(
  106. overridePresets: state.overridePresets,
  107. state: state
  108. )
  109. }
  110. .sheet(isPresented: $showingTempTargetSheet) {
  111. TempTargetPresetsView(
  112. tempTargetPresets: state.tempTargetPresets,
  113. state: state
  114. )
  115. }
  116. .navigationDestination(for: NavigationDestinations.self) { destination in
  117. switch destination {
  118. case .acknowledgmentPending:
  119. AcknowledgementPendingView(
  120. navigationPath: $navigationPath,
  121. state: state
  122. )
  123. case .carbsInput:
  124. CarbsInputView(
  125. navigationPath: $navigationPath,
  126. state: state,
  127. continueToBolus: continueToBolus
  128. )
  129. case .bolusInput:
  130. BolusInputView(
  131. navigationPath: $navigationPath,
  132. state: state
  133. )
  134. case .bolusConfirm:
  135. BolusConfirmationView(
  136. navigationPath: $navigationPath,
  137. state: state,
  138. bolusAmount: $state.bolusAmount,
  139. confirmationProgress: $state.confirmationProgress
  140. )
  141. }
  142. }
  143. .onChange(of: navigationPath) { _, newPath in
  144. if newPath.isEmpty {
  145. // Reset conditional view navigation when returning to root view
  146. continueToBolus = false
  147. }
  148. }
  149. }
  150. .blur(radius: state.showBolusProgressOverlay ? 3 : 0)
  151. .overlay {
  152. if state.showBolusProgressOverlay {
  153. BolusProgressOverlay(state: state)
  154. .transition(.opacity)
  155. }
  156. }
  157. }
  158. private func updateRotation(for trend: String?) {
  159. switch trend {
  160. case "DoubleUp",
  161. "SingleUp":
  162. rotationDegrees = -90
  163. case "FortyFiveUp":
  164. rotationDegrees = -45
  165. case "Flat":
  166. rotationDegrees = 0
  167. case "FortyFiveDown":
  168. rotationDegrees = 45
  169. case "DoubleDown",
  170. "SingleDown":
  171. rotationDegrees = 90
  172. default:
  173. rotationDegrees = 0
  174. }
  175. }
  176. private func handleTreatmentSelection() {
  177. showingTreatmentMenuSheet = false // Dismiss the sheet
  178. guard let treatment = selectedTreatment else { return }
  179. switch treatment {
  180. case .meal:
  181. navigationPath.append(NavigationDestinations.carbsInput)
  182. case .bolus:
  183. navigationPath.append(NavigationDestinations.bolusInput)
  184. case .mealBolusCombo:
  185. continueToBolus = true // Explicitely set subsequent view navigation
  186. navigationPath.append(NavigationDestinations.carbsInput)
  187. }
  188. }
  189. }
  190. #Preview {
  191. TrioMainWatchView()
  192. }