TrioMainWatchView.swift 8.2 KB

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