LiveActivityView.swift 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. //
  2. // LiveActivityView.swift
  3. // Trio
  4. //
  5. // Created by Cengiz Deniz on 17.10.24.
  6. //
  7. import ActivityKit
  8. import Foundation
  9. import SwiftUI
  10. import WidgetKit
  11. struct LiveActivityView: View {
  12. @Environment(\.colorScheme) var colorScheme
  13. var context: ActivityViewContext<LiveActivityAttributes>
  14. private var hasStaticColorScheme: Bool {
  15. context.state.glucoseColorScheme == "staticColor"
  16. }
  17. private var glucoseColor: Color {
  18. let state = context.state
  19. let isMgdL = state.unit == "mg/dL"
  20. // 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
  21. let hardCodedLow = isMgdL ? Decimal(55) : 55.asMmolL
  22. let hardCodedHigh = isMgdL ? Decimal(220) : 220.asMmolL
  23. return Color.getDynamicGlucoseColor(
  24. glucoseValue: Decimal(string: state.bg) ?? 100,
  25. highGlucoseColorValue: !hasStaticColorScheme ? hardCodedHigh : state.highGlucose,
  26. lowGlucoseColorValue: !hasStaticColorScheme ? hardCodedLow : state.lowGlucose,
  27. targetGlucose: isMgdL ? state.target : state.target.asMmolL,
  28. glucoseColorScheme: state.glucoseColorScheme
  29. )
  30. }
  31. var body: some View {
  32. if let detailedViewState = context.state.detailedViewState {
  33. VStack {
  34. LiveActivityChartView(context: context, additionalState: detailedViewState)
  35. .frame(maxWidth: UIScreen.main.bounds.width * 0.9)
  36. .frame(height: 80)
  37. .overlay(alignment: .topTrailing) {
  38. if detailedViewState.isOverrideActive {
  39. HStack {
  40. Text("\(detailedViewState.overrideName)")
  41. .font(.footnote)
  42. .fontWeight(.bold)
  43. .foregroundStyle(.white)
  44. }
  45. .padding(6)
  46. .background {
  47. RoundedRectangle(cornerRadius: 10)
  48. .fill(Color.purple.opacity(colorScheme == .dark ? 0.6 : 0.8))
  49. }
  50. }
  51. }
  52. HStack {
  53. if detailedViewState.widgetItems.contains(where: { $0 != .empty }) {
  54. ForEach(Array(detailedViewState.widgetItems.enumerated()), id: \.element) { index, widgetItem in
  55. switch widgetItem {
  56. case .currentGlucose:
  57. VStack {
  58. LiveActivityBGLabelView(context: context, additionalState: detailedViewState)
  59. HStack {
  60. LiveActivityGlucoseDeltaLabelView(
  61. context: context,
  62. glucoseColor: .primary
  63. )
  64. if !context.isStale, let direction = context.state.direction {
  65. Text(direction).font(.headline)
  66. }
  67. }
  68. }
  69. case .currentGlucoseLarge:
  70. LiveActivityBGLabelLargeView(
  71. context: context,
  72. additionalState: detailedViewState,
  73. glucoseColor: glucoseColor
  74. )
  75. case .iob:
  76. LiveActivityIOBLabelView(context: context, additionalState: detailedViewState)
  77. case .cob:
  78. LiveActivityCOBLabelView(context: context, additionalState: detailedViewState)
  79. case .updatedLabel:
  80. LiveActivityUpdatedLabelView(context: context, isDetailedLayout: true)
  81. case .totalDailyDose:
  82. LiveActivityTotalDailyDoseView(context: context, additionalState: detailedViewState)
  83. case .empty:
  84. Text("").frame(width: 50, height: 50)
  85. }
  86. /// Check if the next item is also non-empty to determine if a divider should be shown
  87. if index < detailedViewState.widgetItems.count - 1 {
  88. let currentItem = detailedViewState.widgetItems[index]
  89. let nextItem = detailedViewState.widgetItems[index + 1]
  90. if currentItem != .empty, nextItem != .empty {
  91. Divider()
  92. .foregroundStyle(.primary)
  93. .fontWeight(.bold)
  94. .frame(width: 10)
  95. }
  96. }
  97. }
  98. }
  99. }
  100. }
  101. .privacySensitive()
  102. .padding(.all, 14)
  103. .foregroundStyle(Color.primary)
  104. // Semantic BackgroundStyle and Color values work here. They adapt to the given interface style (light mode, dark mode)
  105. // Semantic UIColors do NOT (as of iOS 17.1.1). Like UIColor.systemBackgroundColor (it does not adapt to changes of the interface style)
  106. // The colorScheme environment variable does work here, but BackgroundStyle gives us this functionality for free
  107. .foregroundStyle(Color.primary)
  108. .background(BackgroundStyle.background.opacity(0.4))
  109. .activityBackgroundTint(Color.clear)
  110. } else {
  111. Group {
  112. if context.state.isInitialState {
  113. Text("Live Activity Expired. Open Trio to Refresh").minimumScaleFactor(0.01)
  114. } else {
  115. HStack(spacing: 3) {
  116. LiveActivityBGAndTrendView(context: context, size: .expanded, glucoseColor: glucoseColor).font(.title)
  117. Spacer()
  118. VStack(alignment: .trailing, spacing: 5) {
  119. LiveActivityGlucoseDeltaLabelView(
  120. context: context,
  121. glucoseColor: hasStaticColorScheme ? .primary : glucoseColor
  122. ).font(.title3)
  123. LiveActivityUpdatedLabelView(context: context, isDetailedLayout: false).font(.caption)
  124. .foregroundStyle(.primary.opacity(0.7))
  125. }
  126. }
  127. }
  128. }
  129. .privacySensitive()
  130. .padding(.all, 15)
  131. .foregroundStyle(Color.primary)
  132. /// Semantic BackgroundStyle and Color values work here. They adapt to the given interface style (light mode, dark mode)
  133. // Semantic UIColors do NOT (as of iOS 17.1.1). Like UIColor.systemBackgroundColor (it does not adapt to changes of the interface style)
  134. // The colorScheme environment variable does work here, but BackgroundStyle gives us this functionality for free
  135. .foregroundStyle(Color.primary)
  136. .background(BackgroundStyle.background.opacity(0.4))
  137. .activityBackgroundTint(Color.clear)
  138. }
  139. }
  140. }
  141. // Expanded, minimal, compact view components
  142. struct LiveActivityExpandedLeadingView: View {
  143. var context: ActivityViewContext<LiveActivityAttributes>
  144. var glucoseColor: Color
  145. var body: some View {
  146. LiveActivityBGAndTrendView(context: context, size: .expanded, glucoseColor: glucoseColor).font(.title2)
  147. .padding(.leading, 5)
  148. }
  149. }
  150. struct LiveActivityExpandedTrailingView: View {
  151. var context: ActivityViewContext<LiveActivityAttributes>
  152. var glucoseColor: Color
  153. var body: some View {
  154. LiveActivityGlucoseDeltaLabelView(context: context, glucoseColor: glucoseColor).font(.title2)
  155. .padding(.trailing, 5)
  156. }
  157. }
  158. struct LiveActivityExpandedBottomView: View {
  159. var context: ActivityViewContext<LiveActivityAttributes>
  160. var body: some View {
  161. if context.state.isInitialState {
  162. Text("Live Activity Expired. Open Trio to Refresh").minimumScaleFactor(0.01)
  163. } else if let detailedViewState = context.state.detailedViewState {
  164. LiveActivityChartView(context: context, additionalState: detailedViewState)
  165. }
  166. }
  167. }
  168. struct LiveActivityExpandedCenterView: View {
  169. var context: ActivityViewContext<LiveActivityAttributes>
  170. var body: some View {
  171. LiveActivityUpdatedLabelView(context: context, isDetailedLayout: false).font(Font.caption)
  172. .foregroundStyle(Color.secondary)
  173. }
  174. }
  175. struct LiveActivityCompactLeadingView: View {
  176. var context: ActivityViewContext<LiveActivityAttributes>
  177. var glucoseColor: Color
  178. var body: some View {
  179. LiveActivityBGAndTrendView(context: context, size: .compact, glucoseColor: glucoseColor).padding(.leading, 4)
  180. }
  181. }
  182. struct LiveActivityCompactTrailingView: View {
  183. var context: ActivityViewContext<LiveActivityAttributes>
  184. var glucoseColor: Color
  185. var body: some View {
  186. LiveActivityGlucoseDeltaLabelView(context: context, glucoseColor: glucoseColor).padding(.trailing, 4)
  187. }
  188. }
  189. struct LiveActivityMinimalView: View {
  190. var context: ActivityViewContext<LiveActivityAttributes>
  191. var glucoseColor: Color
  192. var body: some View {
  193. let (label, characterCount) = bgAndTrend(context: context, size: .minimal, glucoseColor: glucoseColor)
  194. let adjustedLabel = label.padding(.leading, 5).padding(.trailing, 2)
  195. if characterCount < 4 {
  196. adjustedLabel.fontWidth(.condensed)
  197. } else if characterCount < 5 {
  198. adjustedLabel.fontWidth(.compressed)
  199. } else {
  200. adjustedLabel.fontWidth(.compressed)
  201. }
  202. }
  203. }