LiveActivityView.swift 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. isDetailed: true
  64. )
  65. if !context.isStale, let direction = context.state.direction {
  66. Text(direction).font(.headline)
  67. }
  68. }
  69. }
  70. case .iob:
  71. LiveActivityIOBLabelView(context: context, additionalState: detailedViewState)
  72. case .cob:
  73. LiveActivityCOBLabelView(context: context, additionalState: detailedViewState)
  74. case .updatedLabel:
  75. LiveActivityUpdatedLabelView(context: context, isDetailedLayout: true)
  76. case .empty:
  77. Text("").frame(width: 50, height: 50)
  78. }
  79. /// Check if the next item is also non-empty to determine if a divider should be shown
  80. if index < detailedViewState.widgetItems.count - 1 {
  81. let currentItem = detailedViewState.widgetItems[index]
  82. let nextItem = detailedViewState.widgetItems[index + 1]
  83. if currentItem != .empty, nextItem != .empty {
  84. Divider()
  85. .foregroundStyle(.primary)
  86. .fontWeight(.bold)
  87. .frame(width: 10)
  88. }
  89. }
  90. }
  91. }
  92. }
  93. }
  94. .privacySensitive()
  95. .padding(.all, 14)
  96. .foregroundStyle(Color.primary)
  97. // Semantic BackgroundStyle and Color values work here. They adapt to the given interface style (light mode, dark mode)
  98. // Semantic UIColors do NOT (as of iOS 17.1.1). Like UIColor.systemBackgroundColor (it does not adapt to changes of the interface style)
  99. // The colorScheme environment variable does work here, but BackgroundStyle gives us this functionality for free
  100. .foregroundStyle(Color.primary)
  101. .background(BackgroundStyle.background.opacity(0.4))
  102. .activityBackgroundTint(Color.clear)
  103. } else {
  104. Group {
  105. if context.state.isInitialState {
  106. Text("Live Activity Expired. Open Trio to Refresh").minimumScaleFactor(0.01)
  107. } else {
  108. HStack(spacing: 3) {
  109. LiveActivityBGAndTrendView(context: context, size: .expanded, glucoseColor: glucoseColor).font(.title)
  110. Spacer()
  111. VStack(alignment: .trailing, spacing: 5) {
  112. LiveActivityGlucoseDeltaLabelView(
  113. context: context,
  114. glucoseColor: hasStaticColorScheme ? .primary : glucoseColor,
  115. isDetailed: false
  116. ).font(.title3)
  117. LiveActivityUpdatedLabelView(context: context, isDetailedLayout: false).font(.caption)
  118. .foregroundStyle(.primary.opacity(0.7))
  119. }
  120. }
  121. }
  122. }
  123. .privacySensitive()
  124. .padding(.all, 15)
  125. .foregroundStyle(Color.primary)
  126. /// Semantic BackgroundStyle and Color values work here. They adapt to the given interface style (light mode, dark mode)
  127. // Semantic UIColors do NOT (as of iOS 17.1.1). Like UIColor.systemBackgroundColor (it does not adapt to changes of the interface style)
  128. // The colorScheme environment variable does work here, but BackgroundStyle gives us this functionality for free
  129. .foregroundStyle(Color.primary)
  130. .background(BackgroundStyle.background.opacity(0.4))
  131. .activityBackgroundTint(Color.clear)
  132. }
  133. }
  134. }
  135. // Expanded, minimal, compact view components
  136. struct LiveActivityExpandedLeadingView: View {
  137. var context: ActivityViewContext<LiveActivityAttributes>
  138. var glucoseColor: Color
  139. var body: some View {
  140. LiveActivityBGAndTrendView(context: context, size: .expanded, glucoseColor: glucoseColor).font(.title2)
  141. .padding(.leading, 5)
  142. }
  143. }
  144. struct LiveActivityExpandedTrailingView: View {
  145. var context: ActivityViewContext<LiveActivityAttributes>
  146. var glucoseColor: Color
  147. var body: some View {
  148. LiveActivityGlucoseDeltaLabelView(context: context, glucoseColor: glucoseColor, isDetailed: false).font(.title2)
  149. .padding(.trailing, 5)
  150. }
  151. }
  152. struct LiveActivityExpandedBottomView: View {
  153. var context: ActivityViewContext<LiveActivityAttributes>
  154. var body: some View {
  155. if context.state.isInitialState {
  156. Text("Live Activity Expired. Open Trio to Refresh").minimumScaleFactor(0.01)
  157. } else if let detailedViewState = context.state.detailedViewState {
  158. LiveActivityChartView(context: context, additionalState: detailedViewState)
  159. }
  160. }
  161. }
  162. struct LiveActivityExpandedCenterView: View {
  163. var context: ActivityViewContext<LiveActivityAttributes>
  164. var body: some View {
  165. LiveActivityUpdatedLabelView(context: context, isDetailedLayout: false).font(Font.caption)
  166. .foregroundStyle(Color.secondary)
  167. }
  168. }
  169. struct LiveActivityCompactLeadingView: View {
  170. var context: ActivityViewContext<LiveActivityAttributes>
  171. var glucoseColor: Color
  172. var body: some View {
  173. LiveActivityBGAndTrendView(context: context, size: .compact, glucoseColor: glucoseColor).padding(.leading, 4)
  174. }
  175. }
  176. struct LiveActivityCompactTrailingView: View {
  177. var context: ActivityViewContext<LiveActivityAttributes>
  178. var glucoseColor: Color
  179. var body: some View {
  180. LiveActivityGlucoseDeltaLabelView(context: context, glucoseColor: glucoseColor, isDetailed: false).padding(.trailing, 4)
  181. }
  182. }
  183. struct LiveActivityMinimalView: View {
  184. var context: ActivityViewContext<LiveActivityAttributes>
  185. var glucoseColor: Color
  186. var body: some View {
  187. let (label, characterCount) = bgAndTrend(context: context, size: .minimal, glucoseColor: glucoseColor)
  188. let adjustedLabel = label.padding(.leading, 5).padding(.trailing, 2)
  189. if characterCount < 4 {
  190. adjustedLabel.fontWidth(.condensed)
  191. } else if characterCount < 5 {
  192. adjustedLabel.fontWidth(.compressed)
  193. } else {
  194. adjustedLabel.fontWidth(.compressed)
  195. }
  196. }
  197. }