LiveActivityView.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. import ActivityKit
  2. import Foundation
  3. import SwiftUI
  4. import WidgetKit
  5. struct LiveActivityView: View {
  6. @Environment(\.colorScheme) var colorScheme
  7. @Environment(\.isWatchOS) var isWatchOS
  8. var context: ActivityViewContext<LiveActivityAttributes>
  9. private var hasStaticColorScheme: Bool {
  10. context.state.glucoseColorScheme == "staticColor"
  11. }
  12. private var glucoseColor: Color {
  13. let state = context.state
  14. let isMgdL = state.unit == "mg/dL"
  15. // TODO: workaround for now: set low value to 55, to have dynamic color shades between 55 and user-set low (approx. 70); same for high glucose
  16. let hardCodedLow = isMgdL ? Decimal(55) : 55.asMmolL
  17. let hardCodedHigh = isMgdL ? Decimal(220) : 220.asMmolL
  18. return Color.getDynamicGlucoseColor(
  19. glucoseValue: Decimal(string: state.bg) ?? 100,
  20. highGlucoseColorValue: !hasStaticColorScheme ? hardCodedHigh :
  21. (isMgdL ? state.highGlucose : state.highGlucose.asMmolL),
  22. lowGlucoseColorValue: !hasStaticColorScheme ? hardCodedLow : (isMgdL ? state.lowGlucose : state.lowGlucose.asMmolL),
  23. targetGlucose: isMgdL ? state.target : state.target.asMmolL,
  24. glucoseColorScheme: state.glucoseColorScheme
  25. )
  26. }
  27. var body: some View {
  28. if isWatchOS, context.state.useDetailedViewWatchOS {
  29. VStack {
  30. LiveActivityBGLabelWatchView(context: context, glucoseColor: glucoseColor)
  31. LiveActivityChartView(context: context, additionalState: context.state.detailedViewState)
  32. .frame(maxWidth: UIScreen.main.bounds.width * 0.9)
  33. }
  34. .addLiveActivityModifiers(isWatchOS: true)
  35. } else if isWatchOS {
  36. HStack {
  37. LiveActivityBGLabelLargeView(
  38. context: context,
  39. glucoseColor: glucoseColor
  40. )
  41. Spacer()
  42. VStack {
  43. LiveActivityGlucoseDeltaLabelView(
  44. context: context,
  45. glucoseColor: .primary
  46. )
  47. LiveActivityUpdatedLabelView(context: context, isDetailedLayout: false)
  48. }
  49. }
  50. .addLiveActivityModifiers(isWatchOS: true)
  51. } else if context.state.useDetailedViewIOS {
  52. VStack {
  53. LiveActivityChartView(context: context, additionalState: context.state.detailedViewState)
  54. .frame(maxWidth: UIScreen.main.bounds.width * 0.9)
  55. .frame(height: 80)
  56. .overlay(alignment: .topTrailing) {
  57. HStack(spacing: 4) {
  58. if context.state.detailedViewState.isOverrideActive {
  59. Text(context.state.detailedViewState.overrideName)
  60. .font(.footnote)
  61. .fontWeight(.bold)
  62. .foregroundStyle(.white)
  63. .padding(6)
  64. .background {
  65. RoundedRectangle(cornerRadius: 10)
  66. .fill(Color.purple.opacity(colorScheme == .dark ? 0.6 : 0.8))
  67. }
  68. }
  69. if context.state.detailedViewState.isTempTargetActive {
  70. Text(context.state.detailedViewState.tempTargetName)
  71. .font(.footnote)
  72. .fontWeight(.bold)
  73. .foregroundStyle(.white)
  74. .padding(6)
  75. .background {
  76. RoundedRectangle(cornerRadius: 10)
  77. .fill(Color("LoopGreen").opacity(colorScheme == .dark ? 0.6 : 0.8))
  78. }
  79. }
  80. }
  81. }
  82. HStack {
  83. if context.state.detailedViewState.widgetItems.contains(where: { $0 != .empty }) {
  84. ForEach(
  85. Array(context.state.detailedViewState.widgetItems.enumerated()),
  86. id: \.element
  87. ) { index, widgetItem in
  88. switch widgetItem {
  89. case .currentGlucose:
  90. VStack {
  91. LiveActivityBGLabelView(
  92. context: context,
  93. additionalState: context.state.detailedViewState
  94. )
  95. HStack {
  96. LiveActivityGlucoseDeltaLabelView(
  97. context: context,
  98. glucoseColor: .primary
  99. )
  100. if !context.isStale, let direction = context.state.direction {
  101. Text(direction).font(.headline)
  102. }
  103. }
  104. }
  105. case .currentGlucoseLarge:
  106. LiveActivityBGLabelLargeView(
  107. context: context,
  108. glucoseColor: glucoseColor
  109. )
  110. case .iob:
  111. LiveActivityIOBLabelView(context: context, additionalState: context.state.detailedViewState)
  112. case .cob:
  113. LiveActivityCOBLabelView(context: context, additionalState: context.state.detailedViewState)
  114. case .updatedLabel:
  115. LiveActivityUpdatedLabelView(context: context, isDetailedLayout: true)
  116. case .totalDailyDose:
  117. LiveActivityTotalDailyDoseView(
  118. context: context,
  119. additionalState: context.state.detailedViewState
  120. )
  121. case .empty:
  122. Text("").frame(width: 50, height: 50)
  123. }
  124. /// Check if the next item is also non-empty to determine if a divider should be shown
  125. if index < context.state.detailedViewState.widgetItems.count - 1 {
  126. let currentItem = context.state.detailedViewState.widgetItems[index]
  127. let nextItem = context.state.detailedViewState.widgetItems[index + 1]
  128. if currentItem != .empty, nextItem != .empty {
  129. Divider()
  130. .foregroundStyle(.primary)
  131. .fontWeight(.bold)
  132. .frame(width: 10)
  133. }
  134. }
  135. }
  136. }
  137. }
  138. }
  139. .addLiveActivityModifiers(isWatchOS: false)
  140. } else {
  141. Group {
  142. if context.state.isInitialState {
  143. Text("Live Activity Expired. Open Trio to Refresh").minimumScaleFactor(0.01)
  144. } else {
  145. HStack(spacing: 3) {
  146. LiveActivityBGAndTrendView(context: context, size: .expanded, glucoseColor: glucoseColor).font(.title)
  147. Spacer()
  148. VStack(alignment: .trailing, spacing: 5) {
  149. LiveActivityGlucoseDeltaLabelView(
  150. context: context,
  151. glucoseColor: hasStaticColorScheme ? .primary : glucoseColor
  152. ).font(.title3)
  153. LiveActivityUpdatedLabelView(context: context, isDetailedLayout: false)
  154. .font(.caption)
  155. .foregroundStyle(.primary.opacity(0.7))
  156. }
  157. }
  158. }
  159. }
  160. .addLiveActivityModifiers(isWatchOS: false)
  161. }
  162. }
  163. }
  164. // Expanded, minimal, compact view components
  165. struct LiveActivityExpandedLeadingView: View {
  166. var context: ActivityViewContext<LiveActivityAttributes>
  167. var glucoseColor: Color
  168. var body: some View {
  169. LiveActivityBGAndTrendView(context: context, size: .expanded, glucoseColor: glucoseColor).font(.title2)
  170. .padding(.leading, 5)
  171. }
  172. }
  173. struct LiveActivityExpandedTrailingView: View {
  174. var context: ActivityViewContext<LiveActivityAttributes>
  175. var glucoseColor: Color
  176. var body: some View {
  177. LiveActivityGlucoseDeltaLabelView(context: context, glucoseColor: glucoseColor).font(.title2)
  178. .padding(.trailing, 5)
  179. }
  180. }
  181. struct LiveActivityExpandedBottomView: View {
  182. var context: ActivityViewContext<LiveActivityAttributes>
  183. var body: some View {
  184. if context.state.isInitialState {
  185. Text("Live Activity Expired. Open Trio to Refresh").minimumScaleFactor(0.01)
  186. } else if context.state.useDetailedViewIOS {
  187. LiveActivityChartView(context: context, additionalState: context.state.detailedViewState)
  188. .addIsWatchOS()
  189. }
  190. }
  191. }
  192. struct LiveActivityExpandedCenterView: View {
  193. var context: ActivityViewContext<LiveActivityAttributes>
  194. var body: some View {
  195. LiveActivityUpdatedLabelView(context: context, isDetailedLayout: false)
  196. .font(Font.caption)
  197. .foregroundStyle(Color.secondary)
  198. .addIsWatchOS()
  199. }
  200. }
  201. struct LiveActivityCompactLeadingView: View {
  202. var context: ActivityViewContext<LiveActivityAttributes>
  203. var glucoseColor: Color
  204. var body: some View {
  205. LiveActivityBGAndTrendView(context: context, size: .compact, glucoseColor: glucoseColor).padding(.leading, 4)
  206. }
  207. }
  208. struct LiveActivityCompactTrailingView: View {
  209. var context: ActivityViewContext<LiveActivityAttributes>
  210. var glucoseColor: Color
  211. var body: some View {
  212. LiveActivityGlucoseDeltaLabelView(context: context, glucoseColor: glucoseColor).padding(.trailing, 4)
  213. }
  214. }
  215. struct LiveActivityMinimalView: View {
  216. var context: ActivityViewContext<LiveActivityAttributes>
  217. var glucoseColor: Color
  218. var body: some View {
  219. let (label, characterCount) = bgAndTrend(context: context, size: .minimal, glucoseColor: glucoseColor)
  220. let adjustedLabel = label.padding(.leading, 5).padding(.trailing, 2)
  221. if characterCount < 4 {
  222. adjustedLabel.fontWidth(.condensed)
  223. } else if characterCount < 5 {
  224. adjustedLabel.fontWidth(.compressed)
  225. } else {
  226. adjustedLabel.fontWidth(.compressed)
  227. }
  228. }
  229. }