LiveActivity.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. import ActivityKit
  2. import Charts
  3. import SwiftUI
  4. import WidgetKit
  5. private enum Size {
  6. case minimal
  7. case compact
  8. case expanded
  9. }
  10. struct LiveActivity: Widget {
  11. private let dateFormatter: DateFormatter = {
  12. var f = DateFormatter()
  13. f.dateStyle = .none
  14. f.timeStyle = .short
  15. return f
  16. }()
  17. private var bolusFormatter: NumberFormatter {
  18. let formatter = NumberFormatter()
  19. formatter.numberStyle = .decimal
  20. formatter.maximumFractionDigits = 2
  21. formatter.decimalSeparator = "."
  22. return formatter
  23. }
  24. private var carbsFormatter: NumberFormatter {
  25. let formatter = NumberFormatter()
  26. formatter.numberStyle = .decimal
  27. formatter.maximumFractionDigits = 0
  28. return formatter
  29. }
  30. @ViewBuilder private func changeLabel(context: ActivityViewContext<LiveActivityAttributes>) -> some View {
  31. if !context.state.change.isEmpty {
  32. if context.isStale {
  33. Text(context.state.change).foregroundStyle(.primary.opacity(0.5))
  34. .strikethrough(pattern: .solid, color: .red.opacity(0.6))
  35. } else {
  36. Text(context.state.change)
  37. }
  38. } else {
  39. Text("--")
  40. }
  41. }
  42. @ViewBuilder func mealLabel(context: ActivityViewContext<LiveActivityAttributes>) -> some View {
  43. VStack(alignment: .leading, spacing: 1, content: {
  44. HStack {
  45. Text("COB: ").font(.caption)
  46. Text(
  47. (carbsFormatter.string(from: context.state.cob as NSNumber) ?? "--") +
  48. NSLocalizedString(" g", comment: "grams of carbs")
  49. ).font(.caption).fontWeight(.bold)
  50. }
  51. HStack {
  52. Text("IOB: ").font(.caption)
  53. Text(
  54. (bolusFormatter.string(from: context.state.iob as NSNumber) ?? "--") +
  55. NSLocalizedString(" U", comment: "Unit in number of units delivered (keep the space character!)")
  56. ).font(.caption).fontWeight(.bold)
  57. }
  58. })
  59. }
  60. @ViewBuilder func trend(context: ActivityViewContext<LiveActivityAttributes>) -> some View {
  61. if context.isStale {
  62. Text("--")
  63. } else {
  64. if let trendSystemImage = context.state.direction {
  65. Image(systemName: trendSystemImage)
  66. }
  67. }
  68. }
  69. private func updatedLabel(context: ActivityViewContext<LiveActivityAttributes>) -> Text {
  70. let text = Text("Updated: \(dateFormatter.string(from: context.state.date))")
  71. if context.isStale {
  72. if #available(iOSApplicationExtension 17.0, *) {
  73. return text.bold().foregroundStyle(.red)
  74. } else {
  75. return text.bold().foregroundColor(.red)
  76. }
  77. } else {
  78. return text
  79. }
  80. }
  81. private func bgLabel(context: ActivityViewContext<LiveActivityAttributes>) -> Text {
  82. Text(context.state.bg)
  83. .fontWeight(.bold)
  84. .strikethrough(context.isStale, pattern: .solid, color: .red.opacity(0.6))
  85. }
  86. private func bgAndTrend(context: ActivityViewContext<LiveActivityAttributes>, size: Size) -> (some View, Int) {
  87. var characters = 0
  88. let bgText = context.state.bg
  89. characters += bgText.count
  90. // narrow mode is for the minimal dynamic island view
  91. // there is not enough space to show all three arrow there
  92. // and everything has to be squeezed together to some degree
  93. // only display the first arrow character and make it red in case there were more characters
  94. var directionText: String?
  95. var warnColor: Color?
  96. if let direction = context.state.direction {
  97. if size == .compact {
  98. directionText = String(direction[direction.startIndex ... direction.startIndex])
  99. if direction.count > 1 {
  100. warnColor = Color.red
  101. }
  102. } else {
  103. directionText = direction
  104. }
  105. characters += directionText!.count
  106. }
  107. let spacing: CGFloat
  108. switch size {
  109. case .minimal: spacing = -1
  110. case .compact: spacing = 0
  111. case .expanded: spacing = 3
  112. }
  113. let stack = HStack(spacing: spacing) {
  114. Text(bgText)
  115. .strikethrough(context.isStale, pattern: .solid, color: .red.opacity(0.6))
  116. if let direction = directionText {
  117. let text = Text(direction)
  118. switch size {
  119. case .minimal:
  120. let scaledText = text.scaleEffect(x: 0.7, y: 0.7, anchor: .leading)
  121. if let warnColor {
  122. scaledText.foregroundStyle(warnColor)
  123. } else {
  124. scaledText
  125. }
  126. case .compact:
  127. text.scaleEffect(x: 0.8, y: 0.8, anchor: .leading).padding(.trailing, -3)
  128. case .expanded:
  129. text.scaleEffect(x: 0.7, y: 0.7, anchor: .leading).padding(.trailing, -5)
  130. }
  131. }
  132. }
  133. .foregroundStyle(
  134. context.state.lockScreenView == "Simple" ? (context.isStale ? Color.primary.opacity(0.5) : Color.primary) :
  135. (context.isStale ? Color.white.opacity(0.5) : Color.white)
  136. )
  137. return (stack, characters)
  138. }
  139. @ViewBuilder func chart(context: ActivityViewContext<LiveActivityAttributes>) -> some View {
  140. if context.isStale {
  141. Text("No data available")
  142. } else {
  143. Chart {
  144. ForEach(context.state.chart.indices, id: \.self) { index in
  145. let currentValue = context.state.chart[index]
  146. if currentValue > context.state.highGlucose {
  147. PointMark(
  148. x: .value("Time", context.state.chartDate[index] ?? Date()),
  149. y: .value("Value", currentValue)
  150. ).foregroundStyle(Color.orange.gradient).symbolSize(12)
  151. } else if currentValue < context.state.lowGlucose {
  152. PointMark(
  153. x: .value("Time", context.state.chartDate[index] ?? Date()),
  154. y: .value("Value", currentValue)
  155. ).foregroundStyle(Color.red.gradient).symbolSize(12)
  156. } else {
  157. PointMark(
  158. x: .value("Time", context.state.chartDate[index] ?? Date()),
  159. y: .value("Value", currentValue)
  160. ).foregroundStyle(Color.green.gradient).symbolSize(12)
  161. }
  162. }
  163. }.chartPlotStyle { plotContent in
  164. plotContent.background(.cyan.opacity(0.1))
  165. }
  166. .chartYAxis {
  167. AxisMarks(position: .leading) { _ in
  168. AxisValueLabel().foregroundStyle(Color.white)
  169. AxisGridLine(stroke: .init(lineWidth: 0.1, dash: [2, 3])).foregroundStyle(Color.white)
  170. }
  171. }
  172. .chartXAxis {
  173. AxisMarks(position: .automatic) { _ in
  174. AxisValueLabel(format: .dateTime.hour(.defaultDigits(amPM: .narrow)), anchor: .top)
  175. .foregroundStyle(Color.white)
  176. AxisGridLine(stroke: .init(lineWidth: 0.1, dash: [2, 3])).foregroundStyle(Color.white)
  177. }
  178. }
  179. }
  180. }
  181. var body: some WidgetConfiguration {
  182. ActivityConfiguration(for: LiveActivityAttributes.self) { context in
  183. // Lock screen/banner UI goes here
  184. if context.state.lockScreenView == "Simple" {
  185. HStack(spacing: 3) {
  186. bgAndTrend(context: context, size: .expanded).0.font(.title)
  187. Spacer()
  188. VStack(alignment: .trailing, spacing: 5) {
  189. changeLabel(context: context).font(.title3)
  190. updatedLabel(context: context).font(.caption).foregroundStyle(.primary.opacity(0.7))
  191. }
  192. }
  193. .privacySensitive()
  194. .padding(.all, 15)
  195. // Semantic BackgroundStyle and Color values work here. They adapt to the given interface style (light mode, dark mode)
  196. // Semantic UIColors do NOT (as of iOS 17.1.1). Like UIColor.systemBackgroundColor (it does not adapt to changes of the interface style)
  197. // The colorScheme environment varaible that is usually used to detect dark mode does NOT work here (it reports false values)
  198. .foregroundStyle(Color.primary)
  199. .background(BackgroundStyle.background.opacity(0.4))
  200. .activityBackgroundTint(Color.clear)
  201. } else {
  202. HStack(spacing: 2) {
  203. VStack {
  204. chart(context: context).frame(width: UIScreen.main.bounds.width / 1.8)
  205. }.padding(.all, 15)
  206. Divider().foregroundStyle(Color.white)
  207. VStack(alignment: .center) {
  208. Spacer()
  209. ZStack {
  210. VStack {
  211. bgAndTrend(context: context, size: .expanded).0.font(.largeTitle)
  212. changeLabel(context: context).font(.callout)
  213. }.frame(width: 130, height: 130)
  214. }.scaleEffect(0.85).offset(y: 30)
  215. mealLabel(context: context).padding(.bottom, 8)
  216. updatedLabel(context: context).font(.caption).padding(.bottom, 70)
  217. }
  218. }
  219. .privacySensitive()
  220. .imageScale(.small)
  221. .background(Color.white.opacity(0.2))
  222. .foregroundColor(Color.white)
  223. .activityBackgroundTint(Color.black.opacity(0.7))
  224. .activitySystemActionForegroundColor(Color.white)
  225. }
  226. } dynamicIsland: { context in
  227. DynamicIsland {
  228. // Expanded UI goes here. Compose the expanded UI through
  229. // various regions, like leading/trailing/center/bottom
  230. DynamicIslandExpandedRegion(.leading) {
  231. bgAndTrend(context: context, size: .expanded).0.font(.title2).padding(.leading, 5)
  232. }
  233. DynamicIslandExpandedRegion(.trailing) {
  234. changeLabel(context: context).font(.title2).padding(.trailing, 5)
  235. }
  236. DynamicIslandExpandedRegion(.bottom) {
  237. if context.state.lockScreenView == "Simple" {
  238. Group {
  239. updatedLabel(context: context).font(.caption).foregroundStyle(Color.secondary)
  240. }
  241. .frame(
  242. maxHeight: .infinity,
  243. alignment: .bottom
  244. )
  245. } else {
  246. chart(context: context)
  247. }
  248. }
  249. DynamicIslandExpandedRegion(.center) {
  250. if context.state.lockScreenView == "Detailed" {
  251. updatedLabel(context: context).font(.caption).foregroundStyle(Color.secondary)
  252. }
  253. }
  254. } compactLeading: {
  255. bgAndTrend(context: context, size: .compact).0.padding(.leading, 4)
  256. } compactTrailing: {
  257. changeLabel(context: context).padding(.trailing, 4)
  258. } minimal: {
  259. let (_label, characterCount) = bgAndTrend(context: context, size: .minimal)
  260. let label = _label.padding(.leading, 7).padding(.trailing, 3)
  261. if characterCount < 4 {
  262. label
  263. } else if characterCount < 5 {
  264. label.fontWidth(.condensed)
  265. } else {
  266. label.fontWidth(.compressed)
  267. }
  268. }
  269. .widgetURL(URL(string: "Trio://"))
  270. .keylineTint(Color.purple)
  271. .contentMargins(.horizontal, 0, for: .minimal)
  272. .contentMargins(.trailing, 0, for: .compactLeading)
  273. .contentMargins(.leading, 0, for: .compactTrailing)
  274. }
  275. }
  276. }