LiveActivityView.swift 10 KB

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