TrioMainWatchView.swift 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. // reset input amounts
  54. state.bolusAmount = 0
  55. state.carbsAmount = 0
  56. // reset auth progress
  57. state.confirmationProgress = 0
  58. }
  59. .toolbar {
  60. ToolbarItem(placement: .topBarLeading) {
  61. HStack {
  62. Image(systemName: "syringe.fill")
  63. .foregroundStyle(Color.insulin)
  64. Text(state.iob ?? "--")
  65. .foregroundStyle(.white)
  66. }.font(.caption)
  67. }
  68. ToolbarItem(placement: .topBarTrailing) {
  69. HStack {
  70. Text(state.cob ?? "--")
  71. .foregroundStyle(.white)
  72. Image(systemName: "fork.knife")
  73. .foregroundStyle(Color.orange)
  74. }.font(.caption)
  75. }
  76. ToolbarItemGroup(placement: .bottomBar) {
  77. Button {
  78. showingOverrideSheet = true
  79. } label: {
  80. Image(systemName: "clock.arrow.2.circlepath")
  81. .foregroundStyle(Color.primary, isOverrideActive ? Color.primary : Color.purple)
  82. }.tint(isOverrideActive ? Color.purple : nil)
  83. Button {
  84. showingTreatmentMenuSheet = true
  85. } label: {
  86. Image(systemName: "plus")
  87. .foregroundStyle(Color.bgDarkerDarkBlue)
  88. }
  89. .controlSize(.large)
  90. .buttonStyle(WatchOSButtonStyle())
  91. Button {
  92. showingTempTargetSheet = true
  93. } label: {
  94. Image(systemName: "target")
  95. .foregroundStyle(isTempTargetActive ? Color.primary : Color.loopGreen.opacity(0.75))
  96. }.tint(isTempTargetActive ? Color.loopGreen.opacity(0.75) : nil)
  97. }
  98. }
  99. .fullScreenCover(isPresented: $showingTreatmentMenuSheet) {
  100. TreatmentMenuView(selectedTreatment: $selectedTreatment) {
  101. handleTreatmentSelection()
  102. }
  103. .onAppear {
  104. // reset the conditional navigation flag when opening
  105. continueToBolus = false
  106. }
  107. }
  108. .sheet(isPresented: $showingOverrideSheet) {
  109. OverridePresetsView(
  110. state: state,
  111. overridePresets: state.overridePresets
  112. ) {
  113. showingOverrideSheet = false
  114. navigationPath.append(NavigationDestinations.acknowledgmentPending)
  115. }
  116. }
  117. .sheet(isPresented: $showingTempTargetSheet) {
  118. TempTargetPresetsView(
  119. state: state,
  120. tempTargetPresets: state.tempTargetPresets
  121. ) {
  122. showingTempTargetSheet = false
  123. navigationPath.append(NavigationDestinations.acknowledgmentPending)
  124. }
  125. }
  126. .navigationDestination(for: NavigationDestinations.self) { destination in
  127. switch destination {
  128. case .acknowledgmentPending:
  129. AcknowledgementPendingView(
  130. navigationPath: $navigationPath,
  131. state: state,
  132. shouldNavigateToRoot: $state.shouldNavigateToRoot
  133. )
  134. case .carbsInput:
  135. CarbsInputView(
  136. navigationPath: $navigationPath,
  137. state: state,
  138. continueToBolus: continueToBolus
  139. )
  140. case .bolusInput:
  141. BolusInputView(
  142. navigationPath: $navigationPath,
  143. state: state
  144. )
  145. case .bolusConfirm:
  146. BolusConfirmationView(
  147. navigationPath: $navigationPath,
  148. state: state,
  149. bolusAmount: $state.bolusAmount,
  150. confirmationProgress: $state.confirmationProgress
  151. )
  152. }
  153. }
  154. .onChange(of: navigationPath) { _, newPath in
  155. if newPath.isEmpty {
  156. // Reset conditional view navigation when returning to root view
  157. continueToBolus = false
  158. }
  159. }
  160. }
  161. .blur(radius: state.showBolusProgressOverlay ? 3 : 0)
  162. .overlay {
  163. if state.showBolusProgressOverlay {
  164. BolusProgressOverlay(state: state) {
  165. state.shouldNavigateToRoot = false
  166. navigationPath.append(NavigationDestinations.acknowledgmentPending)
  167. }.transition(.opacity)
  168. }
  169. }
  170. }
  171. private func updateRotation(for trend: String?) {
  172. switch trend {
  173. case "DoubleUp",
  174. "SingleUp":
  175. rotationDegrees = -90
  176. case "FortyFiveUp":
  177. rotationDegrees = -45
  178. case "Flat":
  179. rotationDegrees = 0
  180. case "FortyFiveDown":
  181. rotationDegrees = 45
  182. case "DoubleDown",
  183. "SingleDown":
  184. rotationDegrees = 90
  185. default:
  186. rotationDegrees = 0
  187. }
  188. }
  189. private func handleTreatmentSelection() {
  190. showingTreatmentMenuSheet = false // Dismiss the sheet
  191. guard let treatment = selectedTreatment else { return }
  192. switch treatment {
  193. case .meal:
  194. navigationPath.append(NavigationDestinations.carbsInput)
  195. case .bolus:
  196. navigationPath.append(NavigationDestinations.bolusInput)
  197. case .mealBolusCombo:
  198. continueToBolus = true // Explicitely set subsequent view navigation
  199. navigationPath.append(NavigationDestinations.carbsInput)
  200. }
  201. }
  202. }
  203. #Preview {
  204. TrioMainWatchView()
  205. }