TrioMainWatchView.swift 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. import Charts
  2. import SwiftUI
  3. import WatchKit
  4. struct TrioMainWatchView: View {
  5. @State private var state = WatchState()
  6. // misc
  7. @State private var currentPage: Int = 0
  8. @State private var rotationDegrees: Double = 0.0
  9. @State private var showingTempTargetSheet = false
  10. // view visbility
  11. @State private var showingTreatmentMenuSheet: Bool = false
  12. @State private var showingOverrideSheet: Bool = false
  13. // navigation flag for meal bolus combo
  14. @State private var continueToBolus = false
  15. @State private var navigationPath = NavigationPath()
  16. // treatments
  17. @State private var selectedTreatment: TreatmentOption?
  18. var isWatchStateDated: Bool {
  19. // If `lastWatchStateUpdate` is nil, treat as "dated"
  20. guard let lastUpdateTimestamp = state.lastWatchStateUpdate else {
  21. return true
  22. }
  23. let now = Date().timeIntervalSince1970
  24. let secondsSinceUpdate = now - lastUpdateTimestamp
  25. // Return true if last update older than 15 min
  26. return secondsSinceUpdate > 15 * 60
  27. }
  28. var isSessionUnreachable: Bool {
  29. guard let session = state.session else {
  30. return true // No session at all => unreachable
  31. }
  32. // Return true if not .activated OR not reachable
  33. return session.activationState != .activated || !session.isReachable
  34. }
  35. // Active adjustment indicator
  36. private func isAdjustmentActive<T>(for presets: [T], predicate: (T) -> Bool) -> Bool {
  37. let sortedPresets = presets.sorted { predicate($0) && !predicate($1) }
  38. return !sortedPresets.isEmpty && sortedPresets.first(where: predicate) != nil
  39. }
  40. private var isTempTargetActive: Bool {
  41. isAdjustmentActive(for: state.tempTargetPresets) { $0.isEnabled }
  42. }
  43. private var isOverrideActive: Bool {
  44. isAdjustmentActive(for: state.overridePresets) { $0.isEnabled }
  45. }
  46. private var trioBackgroundColor = LinearGradient(
  47. gradient: Gradient(colors: [Color.bgDarkBlue, Color.bgDarkerDarkBlue]),
  48. startPoint: .top,
  49. endPoint: .bottom
  50. )
  51. var body: some View {
  52. NavigationStack(path: $navigationPath) {
  53. TabView(selection: $currentPage) {
  54. // Page 1: Current glucose trend in "BG bobble"
  55. ZStack {
  56. GlucoseTrendView(
  57. state: state,
  58. rotationDegrees: rotationDegrees,
  59. isWatchStateDated: isWatchStateDated || isSessionUnreachable
  60. )
  61. .tag(0)
  62. if state.showSyncingAnimation {
  63. Image(systemName: "iphone.radiowaves.left.and.right")
  64. .symbolRenderingMode(.palette)
  65. .foregroundStyle(Color.primary, Color.tabBar, Color.clear)
  66. .symbolEffect(
  67. .variableColor.iterative,
  68. options: .repeating,
  69. value: state.showSyncingAnimation
  70. )
  71. .position(
  72. x: 20,
  73. y: (WKInterfaceDevice.current().screenBounds.height / 4) -
  74. 7 // Font .body == 14, so half of default size for the SF Symbol image
  75. )
  76. }
  77. }
  78. // Page 2: Glucose chart
  79. GlucoseChartView(glucoseValues: state.glucoseValues)
  80. .tag(1)
  81. }
  82. .background(trioBackgroundColor)
  83. .tabViewStyle(.verticalPage)
  84. .digitalCrownRotation($currentPage.doubleBinding(), from: 0, through: 1, by: 1)
  85. .onChange(of: state.trend) { _, newTrend in
  86. withAnimation {
  87. updateRotation(for: newTrend)
  88. }
  89. }
  90. .toolbar {
  91. ToolbarItem(placement: .topBarLeading) {
  92. HStack {
  93. Image(systemName: "syringe.fill")
  94. .foregroundStyle(Color.insulin)
  95. Text(isWatchStateDated || isSessionUnreachable ? "--" : state.iob ?? "--")
  96. .foregroundStyle(isWatchStateDated ? Color.secondary : Color.white)
  97. }.font(.caption2)
  98. }
  99. ToolbarItem(placement: .topBarTrailing) {
  100. HStack {
  101. Text(isWatchStateDated || isSessionUnreachable ? "--" : state.cob ?? "--")
  102. .foregroundStyle(isWatchStateDated || isSessionUnreachable ? Color.secondary : Color.white)
  103. Image(systemName: "fork.knife")
  104. .foregroundStyle(Color.orange)
  105. }.font(.caption2)
  106. }
  107. ToolbarItemGroup(placement: .bottomBar) {
  108. Button {
  109. showingOverrideSheet = true
  110. } label: {
  111. Image(systemName: "clock.arrow.2.circlepath")
  112. .foregroundStyle(Color.primary, isOverrideActive ? Color.primary : Color.purple)
  113. }.tint(isOverrideActive ? Color.purple : nil)
  114. Button {
  115. showingTreatmentMenuSheet = true
  116. } label: {
  117. Image(systemName: "plus")
  118. .foregroundStyle(Color.bgDarkerDarkBlue)
  119. }
  120. .controlSize(.large)
  121. .buttonStyle(WatchOSButtonStyle(deviceType: state.deviceType))
  122. Button {
  123. showingTempTargetSheet = true
  124. } label: {
  125. Image(systemName: "target")
  126. .foregroundStyle(isTempTargetActive ? Color.primary : Color.loopGreen.opacity(0.75))
  127. }.tint(isTempTargetActive ? Color.loopGreen.opacity(0.75) : nil)
  128. }
  129. }
  130. .fullScreenCover(isPresented: $showingTreatmentMenuSheet) {
  131. TreatmentMenuView(deviceType: state.deviceType, selectedTreatment: $selectedTreatment) {
  132. handleTreatmentSelection()
  133. }
  134. .onAppear {
  135. // reset the conditional navigation flag when opening
  136. continueToBolus = false
  137. }
  138. }
  139. .sheet(isPresented: $showingOverrideSheet) {
  140. OverridePresetsView(
  141. state: state,
  142. overridePresets: state.overridePresets
  143. ) {
  144. showingOverrideSheet = false
  145. navigationPath.append(NavigationDestinations.acknowledgmentPending)
  146. }
  147. }
  148. .sheet(isPresented: $showingTempTargetSheet) {
  149. TempTargetPresetsView(
  150. state: state,
  151. tempTargetPresets: state.tempTargetPresets
  152. ) {
  153. showingTempTargetSheet = false
  154. navigationPath.append(NavigationDestinations.acknowledgmentPending)
  155. }
  156. }
  157. .navigationDestination(for: NavigationDestinations.self) { destination in
  158. switch destination {
  159. case .acknowledgmentPending:
  160. AcknowledgementPendingView(
  161. navigationPath: $navigationPath,
  162. state: state,
  163. shouldNavigateToRoot: $state.shouldNavigateToRoot
  164. )
  165. case .carbsInput:
  166. CarbsInputView(
  167. navigationPath: $navigationPath,
  168. state: state,
  169. continueToBolus: continueToBolus
  170. )
  171. case .bolusInput:
  172. BolusInputView(
  173. navigationPath: $navigationPath,
  174. state: state
  175. )
  176. case .bolusConfirm:
  177. BolusConfirmationView(
  178. navigationPath: $navigationPath,
  179. state: state,
  180. bolusAmount: $state.bolusAmount,
  181. confirmationProgress: $state.confirmationProgress
  182. )
  183. }
  184. }
  185. .onChange(of: navigationPath) { _, newPath in
  186. if newPath.isEmpty {
  187. // Reset conditional view navigation when returning to root view
  188. continueToBolus = false
  189. }
  190. }
  191. }
  192. .ignoresSafeArea()
  193. .blur(radius: state.showBolusProgressOverlay ? 3 : 0)
  194. .overlay {
  195. if state.showBolusProgressOverlay {
  196. BolusProgressOverlay(state: state) {
  197. state.shouldNavigateToRoot = false
  198. navigationPath.append(NavigationDestinations.acknowledgmentPending)
  199. }.transition(.opacity)
  200. }
  201. }
  202. }
  203. private func updateRotation(for trend: String?) {
  204. switch trend {
  205. case "DoubleUp",
  206. "SingleUp":
  207. rotationDegrees = -90
  208. case "FortyFiveUp":
  209. rotationDegrees = -45
  210. case "Flat":
  211. rotationDegrees = 0
  212. case "FortyFiveDown":
  213. rotationDegrees = 45
  214. case "DoubleDown",
  215. "SingleDown":
  216. rotationDegrees = 90
  217. default:
  218. rotationDegrees = 0
  219. }
  220. }
  221. private func handleTreatmentSelection() {
  222. showingTreatmentMenuSheet = false // Dismiss the sheet
  223. guard let treatment = selectedTreatment else { return }
  224. switch treatment {
  225. case .meal:
  226. navigationPath.append(NavigationDestinations.carbsInput)
  227. case .bolus:
  228. // Reset carbs amount when directly going to bolus input
  229. state.carbsAmount = 0
  230. navigationPath.append(NavigationDestinations.bolusInput)
  231. case .mealBolusCombo:
  232. continueToBolus = true // Explicitely set subsequent view navigation
  233. navigationPath.append(NavigationDestinations.carbsInput)
  234. }
  235. }
  236. }
  237. #Preview {
  238. TrioMainWatchView()
  239. }