LiveActivityView.swift 10 KB

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