LiveActivityView.swift 9.7 KB

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