LiveActivityView.swift 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. .activityBackgroundTint(colorScheme == .light ? Color.white.opacity(0.43) : Color.black.opacity(0.43))
  99. } else {
  100. Group {
  101. if context.state.isInitialState {
  102. Text("Live Activity Expired. Open Trio to Refresh").minimumScaleFactor(0.01)
  103. } else {
  104. HStack(spacing: 3) {
  105. LiveActivityBGAndTrendView(context: context, size: .expanded, glucoseColor: glucoseColor).font(.title)
  106. Spacer()
  107. VStack(alignment: .trailing, spacing: 5) {
  108. LiveActivityGlucoseDeltaLabelView(
  109. context: context,
  110. glucoseColor: hasStaticColorScheme ? .primary : glucoseColor,
  111. isDetailed: false
  112. ).font(.title3)
  113. LiveActivityUpdatedLabelView(context: context, isDetailedLayout: false).font(.caption)
  114. .foregroundStyle(.primary.opacity(0.7))
  115. }
  116. }
  117. }
  118. }
  119. .privacySensitive()
  120. .padding(.all, 15)
  121. .foregroundStyle(Color.primary)
  122. .activityBackgroundTint(colorScheme == .light ? Color.white.opacity(0.43) : Color.black.opacity(0.43))
  123. }
  124. }
  125. }
  126. // Expanded, minimal, compact view components
  127. struct LiveActivityExpandedLeadingView: View {
  128. var context: ActivityViewContext<LiveActivityAttributes>
  129. var glucoseColor: Color
  130. var body: some View {
  131. LiveActivityBGAndTrendView(context: context, size: .expanded, glucoseColor: glucoseColor).font(.title2)
  132. .padding(.leading, 5)
  133. }
  134. }
  135. struct LiveActivityExpandedTrailingView: View {
  136. var context: ActivityViewContext<LiveActivityAttributes>
  137. var glucoseColor: Color
  138. var body: some View {
  139. LiveActivityGlucoseDeltaLabelView(context: context, glucoseColor: glucoseColor, isDetailed: false).font(.title2)
  140. .padding(.trailing, 5)
  141. }
  142. }
  143. struct LiveActivityExpandedBottomView: View {
  144. var context: ActivityViewContext<LiveActivityAttributes>
  145. var body: some View {
  146. if context.state.isInitialState {
  147. Text("Live Activity Expired. Open Trio to Refresh").minimumScaleFactor(0.01)
  148. } else if let detailedViewState = context.state.detailedViewState {
  149. LiveActivityChartView(context: context, additionalState: detailedViewState)
  150. }
  151. }
  152. }
  153. struct LiveActivityExpandedCenterView: View {
  154. var context: ActivityViewContext<LiveActivityAttributes>
  155. var body: some View {
  156. LiveActivityUpdatedLabelView(context: context, isDetailedLayout: false).font(Font.caption)
  157. .foregroundStyle(Color.secondary)
  158. }
  159. }
  160. struct LiveActivityCompactLeadingView: View {
  161. var context: ActivityViewContext<LiveActivityAttributes>
  162. var glucoseColor: Color
  163. var body: some View {
  164. LiveActivityBGAndTrendView(context: context, size: .compact, glucoseColor: glucoseColor).padding(.leading, 4)
  165. }
  166. }
  167. struct LiveActivityCompactTrailingView: View {
  168. var context: ActivityViewContext<LiveActivityAttributes>
  169. var glucoseColor: Color
  170. var body: some View {
  171. LiveActivityGlucoseDeltaLabelView(context: context, glucoseColor: glucoseColor, isDetailed: false).padding(.trailing, 4)
  172. }
  173. }
  174. struct LiveActivityMinimalView: View {
  175. var context: ActivityViewContext<LiveActivityAttributes>
  176. var glucoseColor: Color
  177. var body: some View {
  178. let (label, characterCount) = bgAndTrend(context: context, size: .minimal, glucoseColor: glucoseColor)
  179. let adjustedLabel = label.padding(.leading, 5).padding(.trailing, 2)
  180. if characterCount < 4 {
  181. adjustedLabel.fontWidth(.condensed)
  182. } else if characterCount < 5 {
  183. adjustedLabel.fontWidth(.compressed)
  184. } else {
  185. adjustedLabel.fontWidth(.compressed)
  186. }
  187. }
  188. }