LiveActivityView.swift 10 KB

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