LiveActivity.swift 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  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. enum GlucoseUnits: String, Equatable {
  11. case mgdL = "mg/dL"
  12. case mmolL = "mmol/L"
  13. static let exchangeRate: Decimal = 0.0555
  14. }
  15. func rounded(_ value: Decimal, scale: Int, roundingMode: NSDecimalNumber.RoundingMode) -> Decimal {
  16. var result = Decimal()
  17. var toRound = value
  18. NSDecimalRound(&result, &toRound, scale, roundingMode)
  19. return result
  20. }
  21. extension Int {
  22. var asMmolL: Decimal {
  23. rounded(Decimal(self) * GlucoseUnits.exchangeRate, scale: 1, roundingMode: .plain)
  24. }
  25. var formattedAsMmolL: String {
  26. NumberFormatter.glucoseFormatter.string(from: asMmolL as NSDecimalNumber) ?? "\(asMmolL)"
  27. }
  28. }
  29. extension Decimal {
  30. var asMmolL: Decimal {
  31. rounded(self * GlucoseUnits.exchangeRate, scale: 1, roundingMode: .plain)
  32. }
  33. var asMgdL: Decimal {
  34. rounded(self / GlucoseUnits.exchangeRate, scale: 0, roundingMode: .plain)
  35. }
  36. var formattedAsMmolL: String {
  37. NumberFormatter.glucoseFormatter.string(from: asMmolL as NSDecimalNumber) ?? "\(asMmolL)"
  38. }
  39. }
  40. extension NumberFormatter {
  41. static let glucoseFormatter: NumberFormatter = {
  42. let formatter = NumberFormatter()
  43. formatter.locale = Locale.current
  44. formatter.numberStyle = .decimal
  45. formatter.minimumFractionDigits = 1
  46. formatter.maximumFractionDigits = 1
  47. return formatter
  48. }()
  49. }
  50. extension Color {
  51. static let systemBackground = Color(UIColor.systemBackground)
  52. }
  53. struct LiveActivity: Widget {
  54. var body: some WidgetConfiguration {
  55. ActivityConfiguration(for: LiveActivityAttributes.self) { context in
  56. LiveActivityView(context: context)
  57. } dynamicIsland: { context in
  58. DynamicIsland {
  59. DynamicIslandExpandedRegion(.leading) {
  60. LiveActivityExpandedLeadingView(context: context)
  61. }
  62. DynamicIslandExpandedRegion(.trailing) {
  63. LiveActivityExpandedTrailingView(context: context)
  64. }
  65. DynamicIslandExpandedRegion(.bottom) {
  66. LiveActivityExpandedBottomView(context: context)
  67. }
  68. DynamicIslandExpandedRegion(.center) {
  69. LiveActivityExpandedCenterView(context: context)
  70. }
  71. } compactLeading: {
  72. LiveActivityCompactLeadingView(context: context)
  73. } compactTrailing: {
  74. LiveActivityCompactTrailingView(context: context)
  75. } minimal: {
  76. LiveActivityMinimalView(context: context).font(.system(size: 11))
  77. }
  78. }
  79. }
  80. }
  81. struct LiveActivityView: View {
  82. @Environment(\.colorScheme) var colorScheme
  83. var context: ActivityViewContext<LiveActivityAttributes>
  84. var body: some View {
  85. if let detailedViewState = context.state.detailedViewState {
  86. VStack {
  87. LiveActivityChartView(context: context, additionalState: detailedViewState)
  88. .frame(maxWidth: UIScreen.main.bounds.width * 0.9)
  89. .frame(height: 80)
  90. .overlay(alignment: .topTrailing) {
  91. if detailedViewState.isOverrideActive {
  92. HStack {
  93. Text("\(detailedViewState.overrideName)")
  94. .font(.footnote)
  95. .fontWeight(.bold)
  96. .foregroundStyle(.white)
  97. }
  98. .padding(6)
  99. .background {
  100. RoundedRectangle(cornerRadius: 10)
  101. .fill(Color.purple.opacity(colorScheme == .dark ? 0.6 : 0.8))
  102. }
  103. }
  104. }
  105. HStack {
  106. ForEach(Array(context.state.itemOrder.enumerated()), id: \.element) { index, item in
  107. switch item {
  108. case "currentGlucose":
  109. if context.state.showCurrentGlucose {
  110. VStack {
  111. LiveActivityBGLabelView(context: context, additionalState: detailedViewState)
  112. HStack {
  113. LiveActivityGlucoseDeltaLabelView(context: context)
  114. if !context.isStale, let direction = context.state.direction {
  115. Text(direction).font(.headline)
  116. }
  117. }
  118. }
  119. }
  120. case "iob":
  121. if context.state.showIOB {
  122. LiveActivityIOBLabelView(context: context, additionalState: detailedViewState)
  123. }
  124. case "cob":
  125. if context.state.showCOB {
  126. LiveActivityCOBLabelView(context: context, additionalState: detailedViewState)
  127. }
  128. case "updatedLabel":
  129. if context.state.showUpdatedLabel {
  130. LiveActivityUpdatedLabelView(context: context, isDetailedLayout: true)
  131. }
  132. default:
  133. EmptyView()
  134. }
  135. if index < context.state.itemOrder.count - 1 {
  136. Divider().foregroundStyle(.primary).fontWeight(.bold).frame(width: 10)
  137. }
  138. }
  139. }
  140. }
  141. .privacySensitive()
  142. .padding(.all, 14)
  143. .foregroundStyle(Color.primary)
  144. .activityBackgroundTint(colorScheme == .light ? Color.white.opacity(0.43) : Color.black.opacity(0.43))
  145. } else {
  146. HStack(spacing: 3) {
  147. LiveActivityBGAndTrendView(context: context, size: .expanded).font(.title)
  148. Spacer()
  149. VStack(alignment: .trailing, spacing: 5) {
  150. LiveActivityGlucoseDeltaLabelView(context: context).font(.title3)
  151. LiveActivityUpdatedLabelView(context: context, isDetailedLayout: false).font(.caption)
  152. .foregroundStyle(.primary.opacity(0.7))
  153. }
  154. }
  155. .privacySensitive()
  156. .padding(.all, 15)
  157. .foregroundStyle(Color.primary)
  158. .activityBackgroundTint(colorScheme == .light ? Color.white.opacity(0.43) : Color.black.opacity(0.43))
  159. }
  160. }
  161. }
  162. // Separate the smaller sections into reusable views
  163. struct LiveActivityBGAndTrendView: View {
  164. var context: ActivityViewContext<LiveActivityAttributes>
  165. fileprivate var size: Size
  166. var body: some View {
  167. let (view, _) = bgAndTrend(context: context, size: size)
  168. return view
  169. }
  170. }
  171. struct LiveActivityBGLabelView: View {
  172. var context: ActivityViewContext<LiveActivityAttributes>
  173. var additionalState: LiveActivityAttributes.ContentAdditionalState
  174. var body: some View {
  175. Text(context.state.bg)
  176. .fontWeight(.bold)
  177. .font(.title3)
  178. .strikethrough(context.isStale, pattern: .solid, color: .red.opacity(0.6))
  179. }
  180. }
  181. struct LiveActivityGlucoseDeltaLabelView: View {
  182. var context: ActivityViewContext<LiveActivityAttributes>
  183. var body: some View {
  184. if !context.state.change.isEmpty {
  185. Text(context.state.change).foregroundStyle(.primary)
  186. .strikethrough(context.isStale, pattern: .solid, color: .red.opacity(0.6))
  187. } else {
  188. Text("--")
  189. }
  190. }
  191. }
  192. struct LiveActivityIOBLabelView: View {
  193. var context: ActivityViewContext<LiveActivityAttributes>
  194. var additionalState: LiveActivityAttributes.ContentAdditionalState
  195. private var bolusFormatter: NumberFormatter {
  196. let formatter = NumberFormatter()
  197. formatter.numberStyle = .decimal
  198. formatter.maximumFractionDigits = 1
  199. formatter.decimalSeparator = "."
  200. return formatter
  201. }
  202. var body: some View {
  203. VStack(spacing: 2) {
  204. HStack {
  205. Text(
  206. bolusFormatter.string(from: additionalState.iob as NSNumber) ?? "--"
  207. ).fontWeight(.bold).font(.title3).strikethrough(context.isStale, pattern: .solid, color: .red.opacity(0.6))
  208. Text("U").foregroundStyle(.primary).font(.headline).fontWeight(.bold)
  209. }
  210. Text("IOB").font(.subheadline).foregroundStyle(.primary)
  211. }
  212. }
  213. }
  214. struct LiveActivityCOBLabelView: View {
  215. var context: ActivityViewContext<LiveActivityAttributes>
  216. var additionalState: LiveActivityAttributes.ContentAdditionalState
  217. var body: some View {
  218. VStack(spacing: 2) {
  219. HStack {
  220. Text(
  221. "\(additionalState.cob)"
  222. ).fontWeight(.bold).font(.title3).strikethrough(context.isStale, pattern: .solid, color: .red.opacity(0.6))
  223. Text("g").foregroundStyle(.primary).font(.headline).fontWeight(.bold)
  224. }
  225. Text("COB").font(.subheadline).foregroundStyle(.primary)
  226. }
  227. }
  228. }
  229. struct LiveActivityUpdatedLabelView: View {
  230. var context: ActivityViewContext<LiveActivityAttributes>
  231. var isDetailedLayout: Bool
  232. private var dateFormatter: DateFormatter {
  233. let formatter = DateFormatter()
  234. formatter.dateStyle = .none
  235. formatter.timeStyle = .short
  236. return formatter
  237. }
  238. var body: some View {
  239. if isDetailedLayout {
  240. let dateText = Text("\(dateFormatter.string(from: context.state.date))").font(.title3)
  241. .foregroundStyle(.primary)
  242. VStack {
  243. if context.isStale {
  244. if #available(iOSApplicationExtension 17.0, *) {
  245. dateText.bold().foregroundStyle(.red)
  246. } else {
  247. dateText.bold().foregroundColor(.red)
  248. }
  249. } else {
  250. if #available(iOSApplicationExtension 17.0, *) {
  251. dateText.bold().foregroundStyle(.primary)
  252. } else {
  253. dateText.bold().foregroundColor(.primary)
  254. }
  255. }
  256. Text("Updated").font(.subheadline).foregroundStyle(.primary)
  257. }
  258. } else {
  259. let dateText = Text("\(dateFormatter.string(from: context.state.date))").font(.subheadline)
  260. .foregroundStyle(.secondary)
  261. HStack {
  262. Text("Updated:").font(.subheadline).foregroundStyle(.secondary)
  263. if context.isStale {
  264. if #available(iOSApplicationExtension 17.0, *) {
  265. dateText.bold().foregroundStyle(.red)
  266. } else {
  267. dateText.bold().foregroundColor(.red)
  268. }
  269. } else {
  270. if #available(iOSApplicationExtension 17.0, *) {
  271. dateText.bold().foregroundStyle(.primary)
  272. } else {
  273. dateText.bold().foregroundColor(.primary)
  274. }
  275. }
  276. }
  277. }
  278. }
  279. }
  280. struct LiveActivityChartView: View {
  281. var context: ActivityViewContext<LiveActivityAttributes>
  282. var additionalState: LiveActivityAttributes.ContentAdditionalState
  283. var body: some View {
  284. // Determine scale
  285. let minValue = min(additionalState.chart.min() ?? 39, 39) as Decimal
  286. let maxValue = max(additionalState.chart.max() ?? 300, 300) as Decimal
  287. let yAxisRuleMarkMin = additionalState.unit == "mg/dL" ? additionalState.lowGlucose : additionalState.lowGlucose
  288. .asMmolL
  289. let yAxisRuleMarkMax = additionalState.unit == "mg/dL" ? additionalState.highGlucose : additionalState.highGlucose
  290. .asMmolL
  291. let target = additionalState.unit == "mg/dL" ? additionalState.target : additionalState.target.asMmolL
  292. let isOverrideActive = additionalState.isOverrideActive == true
  293. let calendar = Calendar.current
  294. let now = Date()
  295. let startDate = calendar.date(byAdding: .hour, value: -6, to: now) ?? now
  296. let endDate = isOverrideActive ? (calendar.date(byAdding: .hour, value: 2, to: now) ?? now) : now
  297. Chart {
  298. RuleMark(y: .value("Low", yAxisRuleMarkMin))
  299. .lineStyle(.init(lineWidth: 0.5, dash: [5]))
  300. RuleMark(y: .value("High", yAxisRuleMarkMax))
  301. .lineStyle(.init(lineWidth: 0.5, dash: [5]))
  302. RuleMark(y: .value("Target", target)).foregroundStyle(.green.gradient).lineStyle(.init(lineWidth: 1))
  303. if isOverrideActive {
  304. drawActiveOverrides()
  305. }
  306. drawChart(yAxisRuleMarkMin: yAxisRuleMarkMin, yAxisRuleMarkMax: yAxisRuleMarkMax)
  307. }
  308. .chartYAxis {
  309. AxisMarks(position: .trailing) { _ in
  310. AxisGridLine(stroke: .init(lineWidth: 0.2, dash: [2, 3])).foregroundStyle(Color.white)
  311. AxisValueLabel().foregroundStyle(.primary).font(.footnote)
  312. }
  313. }
  314. .chartYScale(domain: additionalState.unit == "mg/dL" ? minValue ... maxValue : minValue.asMmolL ... maxValue.asMmolL)
  315. .chartYAxis(.hidden)
  316. .chartPlotStyle { plotContent in
  317. plotContent
  318. .background(
  319. RoundedRectangle(cornerRadius: 12)
  320. .fill(Color.clear)
  321. )
  322. .clipShape(RoundedRectangle(cornerRadius: 12))
  323. }
  324. .chartXScale(domain: startDate ... endDate)
  325. .chartXAxis {
  326. AxisMarks(position: .automatic) { _ in
  327. AxisGridLine(stroke: .init(lineWidth: 0.2, dash: [2, 3])).foregroundStyle(Color.white)
  328. }
  329. }
  330. }
  331. private func drawActiveOverrides() -> some ChartContent {
  332. let start: Date = context.state.detailedViewState?.overrideDate ?? .distantPast
  333. let duration = context.state.detailedViewState?.overrideDuration ?? 0
  334. let durationAsTimeInterval = TimeInterval((duration as NSDecimalNumber).doubleValue * 60) // return seconds
  335. let end: Date = start.addingTimeInterval(durationAsTimeInterval)
  336. let target = context.state.detailedViewState?.overrideTarget ?? 0
  337. return RuleMark(
  338. xStart: .value("Start", start, unit: .second),
  339. xEnd: .value("End", end, unit: .second),
  340. y: .value("Value", target)
  341. )
  342. .foregroundStyle(Color.purple.opacity(0.6))
  343. .lineStyle(.init(lineWidth: 8))
  344. }
  345. private func drawChart(yAxisRuleMarkMin: Decimal, yAxisRuleMarkMax: Decimal) -> some ChartContent {
  346. ForEach(additionalState.chart.indices, id: \.self) { index in
  347. let currentValue = additionalState.chart[index]
  348. let displayValue = additionalState.unit == "mg/dL" ? currentValue : currentValue.asMmolL
  349. let chartDate = additionalState.chartDate[index] ?? Date()
  350. let pointMark = PointMark(
  351. x: .value("Time", chartDate),
  352. y: .value("Value", displayValue)
  353. ).symbolSize(15)
  354. if displayValue > yAxisRuleMarkMax {
  355. pointMark.foregroundStyle(Color.orange.gradient)
  356. } else if displayValue < yAxisRuleMarkMin {
  357. pointMark.foregroundStyle(Color.red.gradient)
  358. } else {
  359. pointMark.foregroundStyle(Color.green.gradient)
  360. }
  361. }
  362. }
  363. }
  364. // Expanded, minimal, compact view components
  365. struct LiveActivityExpandedLeadingView: View {
  366. var context: ActivityViewContext<LiveActivityAttributes>
  367. var body: some View {
  368. LiveActivityBGAndTrendView(context: context, size: .expanded).font(.title2).padding(.leading, 5)
  369. }
  370. }
  371. struct LiveActivityExpandedTrailingView: View {
  372. var context: ActivityViewContext<LiveActivityAttributes>
  373. var body: some View {
  374. LiveActivityGlucoseDeltaLabelView(context: context).font(.title2).padding(.trailing, 5)
  375. }
  376. }
  377. struct LiveActivityExpandedBottomView: View {
  378. var context: ActivityViewContext<LiveActivityAttributes>
  379. var body: some View {
  380. if context.state.isInitialState {
  381. Text("Live Activity Expired. Open Trio to Refresh")
  382. } else if let detailedViewState = context.state.detailedViewState {
  383. LiveActivityChartView(context: context, additionalState: detailedViewState)
  384. }
  385. }
  386. }
  387. struct LiveActivityExpandedCenterView: View {
  388. var context: ActivityViewContext<LiveActivityAttributes>
  389. var body: some View {
  390. LiveActivityUpdatedLabelView(context: context, isDetailedLayout: false).font(.caption).foregroundStyle(Color.secondary)
  391. }
  392. }
  393. struct LiveActivityCompactLeadingView: View {
  394. var context: ActivityViewContext<LiveActivityAttributes>
  395. var body: some View {
  396. LiveActivityBGAndTrendView(context: context, size: .compact).padding(.leading, 4)
  397. }
  398. }
  399. struct LiveActivityCompactTrailingView: View {
  400. var context: ActivityViewContext<LiveActivityAttributes>
  401. var body: some View {
  402. LiveActivityGlucoseDeltaLabelView(context: context).padding(.trailing, 4)
  403. }
  404. }
  405. struct LiveActivityMinimalView: View {
  406. var context: ActivityViewContext<LiveActivityAttributes>
  407. var body: some View {
  408. let (label, characterCount) = bgAndTrend(context: context, size: .minimal)
  409. let adjustedLabel = label.padding(.leading, 5).padding(.trailing, 2)
  410. if characterCount < 4 {
  411. adjustedLabel.fontWidth(.condensed)
  412. } else if characterCount < 5 {
  413. adjustedLabel.fontWidth(.compressed)
  414. } else {
  415. adjustedLabel.fontWidth(.compressed)
  416. }
  417. }
  418. }
  419. private func bgAndTrend(context: ActivityViewContext<LiveActivityAttributes>, size: Size) -> (some View, Int) {
  420. var characters = 0
  421. let bgText = context.state.bg
  422. characters += bgText.count
  423. // narrow mode is for the minimal dynamic island view
  424. // there is not enough space to show all three arrow there
  425. // and everything has to be squeezed together to some degree
  426. // only display the first arrow character and make it red in case there were more characters
  427. var directionText: String?
  428. var warnColor: Color?
  429. if let direction = context.state.direction {
  430. if size == .compact || size == .minimal {
  431. directionText = String(direction[direction.startIndex ... direction.startIndex])
  432. if direction.count > 1 {
  433. warnColor = Color.red
  434. }
  435. } else {
  436. directionText = direction
  437. }
  438. characters += directionText!.count
  439. }
  440. let spacing: CGFloat
  441. switch size {
  442. case .minimal: spacing = -1
  443. case .compact: spacing = 0
  444. case .expanded: spacing = 3
  445. }
  446. let stack = HStack(spacing: spacing) {
  447. Text(bgText)
  448. .strikethrough(context.isStale, pattern: .solid, color: .red.opacity(0.6))
  449. if let direction = directionText {
  450. let text = Text(direction)
  451. switch size {
  452. case .minimal:
  453. let scaledText = text.scaleEffect(x: 0.7, y: 0.7, anchor: .leading)
  454. if let warnColor {
  455. scaledText.foregroundStyle(warnColor)
  456. } else {
  457. scaledText
  458. }
  459. case .compact:
  460. text.scaleEffect(x: 0.8, y: 0.8, anchor: .leading).padding(.trailing, -3)
  461. case .expanded:
  462. text.scaleEffect(x: 0.7, y: 0.7, anchor: .leading).padding(.trailing, -5)
  463. }
  464. }
  465. }
  466. return (stack, characters)
  467. }