TrioMainWatchView.swift 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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 5 min, so 1 loop cycle
  26. return secondsSinceUpdate > 5 * 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
  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. if state.showSyncingAnimation {
  62. Image(systemName: "iphone.radiowaves.left.and.right")
  63. .symbolRenderingMode(.palette)
  64. .foregroundStyle(Color.primary, Color.tabBar, Color.clear)
  65. .symbolEffect(
  66. .variableColor.iterative,
  67. options: .repeating,
  68. value: state.showSyncingAnimation
  69. )
  70. .position(
  71. x: 20,
  72. y: (WKInterfaceDevice.current().screenBounds.height / 4) -
  73. 7 // Font .body == 14, so half of default size for the SF Symbol image
  74. )
  75. }
  76. }.tag(0)
  77. // Page 2: Glucose chart
  78. GlucoseChartView(glucoseValues: state.glucoseValues)
  79. .tag(1)
  80. }
  81. .background(trioBackgroundColor)
  82. .tabViewStyle(.verticalPage)
  83. .digitalCrownRotation($currentPage.doubleBinding(), from: 0, through: 1, by: 1)
  84. .onChange(of: state.trend) { _, newTrend in
  85. withAnimation {
  86. updateRotation(for: newTrend)
  87. }
  88. }
  89. .toolbar {
  90. ToolbarItem(placement: .topBarLeading) {
  91. HStack {
  92. Image(systemName: "syringe.fill")
  93. .foregroundStyle(Color.insulin)
  94. Text(isWatchStateDated || isSessionUnreachable ? "--" : state.iob ?? "--")
  95. .foregroundStyle(isWatchStateDated ? Color.secondary : Color.white)
  96. }.font(.caption2)
  97. }
  98. ToolbarItem(placement: .topBarTrailing) {
  99. HStack {
  100. Text(isWatchStateDated || isSessionUnreachable ? "--" : state.cob ?? "--")
  101. .foregroundStyle(isWatchStateDated || isSessionUnreachable ? Color.secondary : Color.white)
  102. Image(systemName: "fork.knife")
  103. .foregroundStyle(Color.orange)
  104. }.font(.caption2)
  105. }
  106. ToolbarItemGroup(placement: .bottomBar) {
  107. Button {
  108. showingOverrideSheet = true
  109. } label: {
  110. Image(systemName: "clock.arrow.2.circlepath")
  111. .foregroundStyle(Color.primary, isOverrideActive ? Color.primary : Color.purple)
  112. }.tint(isOverrideActive ? Color.purple : nil)
  113. Button {
  114. showingTreatmentMenuSheet = true
  115. } label: {
  116. Image(systemName: "plus")
  117. .foregroundStyle(Color.bgDarkerDarkBlue)
  118. }
  119. .controlSize(.large)
  120. .buttonStyle(WatchOSButtonStyle(deviceType: state.deviceType))
  121. Button {
  122. showingTempTargetSheet = true
  123. } label: {
  124. Image(systemName: "target")
  125. .foregroundStyle(isTempTargetActive ? Color.primary : Color.loopGreen.opacity(0.75))
  126. }.tint(isTempTargetActive ? Color.loopGreen.opacity(0.75) : nil)
  127. }
  128. }
  129. .fullScreenCover(isPresented: $showingTreatmentMenuSheet) {
  130. TreatmentMenuView(deviceType: state.deviceType, selectedTreatment: $selectedTreatment) {
  131. handleTreatmentSelection()
  132. }
  133. .onAppear {
  134. // reset the conditional navigation flag when opening
  135. continueToBolus = false
  136. }
  137. }
  138. .sheet(isPresented: $showingOverrideSheet) {
  139. OverridePresetsView(
  140. state: state,
  141. overridePresets: state.overridePresets
  142. ) {
  143. showingOverrideSheet = false
  144. navigationPath.append(NavigationDestinations.acknowledgmentPending)
  145. }
  146. }
  147. .sheet(isPresented: $showingTempTargetSheet) {
  148. TempTargetPresetsView(
  149. state: state,
  150. tempTargetPresets: state.tempTargetPresets
  151. ) {
  152. showingTempTargetSheet = false
  153. navigationPath.append(NavigationDestinations.acknowledgmentPending)
  154. }
  155. }
  156. .navigationDestination(for: NavigationDestinations.self) { destination in
  157. switch destination {
  158. case .acknowledgmentPending:
  159. AcknowledgementPendingView(
  160. navigationPath: $navigationPath,
  161. state: state,
  162. shouldNavigateToRoot: $state.shouldNavigateToRoot
  163. )
  164. case .carbsInput:
  165. CarbsInputView(
  166. navigationPath: $navigationPath,
  167. state: state,
  168. continueToBolus: continueToBolus
  169. )
  170. case .bolusInput:
  171. BolusInputView(
  172. navigationPath: $navigationPath,
  173. state: state
  174. )
  175. case .bolusConfirm:
  176. BolusConfirmationView(
  177. navigationPath: $navigationPath,
  178. state: state,
  179. bolusAmount: $state.bolusAmount,
  180. confirmationProgress: $state.confirmationProgress
  181. )
  182. }
  183. }
  184. .onChange(of: navigationPath) { _, newPath in
  185. if newPath.isEmpty {
  186. // Reset conditional view navigation when returning to root view
  187. continueToBolus = false
  188. }
  189. }
  190. }
  191. .ignoresSafeArea()
  192. .blur(radius: state.showBolusProgressOverlay ? 3 : 0)
  193. .overlay {
  194. if state.showBolusProgressOverlay {
  195. BolusProgressOverlay(state: state) {
  196. state.shouldNavigateToRoot = false
  197. navigationPath.append(NavigationDestinations.acknowledgmentPending)
  198. }.transition(.opacity)
  199. }
  200. }
  201. }
  202. private func updateRotation(for trend: String?) {
  203. switch trend {
  204. case "DoubleUp",
  205. "SingleUp":
  206. rotationDegrees = -90
  207. case "FortyFiveUp":
  208. rotationDegrees = -45
  209. case "Flat":
  210. rotationDegrees = 0
  211. case "FortyFiveDown":
  212. rotationDegrees = 45
  213. case "DoubleDown",
  214. "SingleDown":
  215. rotationDegrees = 90
  216. default:
  217. rotationDegrees = 0
  218. }
  219. }
  220. private func handleTreatmentSelection() {
  221. showingTreatmentMenuSheet = false // Dismiss the sheet
  222. guard let treatment = selectedTreatment else { return }
  223. switch treatment {
  224. case .meal:
  225. navigationPath.append(NavigationDestinations.carbsInput)
  226. case .bolus:
  227. // Reset carbs amount when directly going to bolus input
  228. state.carbsAmount = 0
  229. navigationPath.append(NavigationDestinations.bolusInput)
  230. case .mealBolusCombo:
  231. continueToBolus = true // Explicitely set subsequent view navigation
  232. navigationPath.append(NavigationDestinations.carbsInput)
  233. }
  234. }
  235. }
  236. #Preview {
  237. TrioMainWatchView()
  238. }