LiveActivityView.swift 10 KB

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