LiveActivity.swift 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. import ActivityKit
  2. import Charts
  3. import Foundation
  4. import SwiftUI
  5. import WidgetKit
  6. private enum Size {
  7. case minimal
  8. case compact
  9. case expanded
  10. }
  11. enum GlucoseUnits: String, Equatable {
  12. case mgdL = "mg/dL"
  13. case mmolL = "mmol/L"
  14. static let exchangeRate: Decimal = 0.0555
  15. }
  16. func rounded(_ value: Decimal, scale: Int, roundingMode: NSDecimalNumber.RoundingMode) -> Decimal {
  17. var result = Decimal()
  18. var toRound = value
  19. NSDecimalRound(&result, &toRound, scale, roundingMode)
  20. return result
  21. }
  22. extension Int {
  23. var asMmolL: Decimal {
  24. rounded(Decimal(self) * GlucoseUnits.exchangeRate, scale: 1, roundingMode: .plain)
  25. }
  26. var formattedAsMmolL: String {
  27. NumberFormatter.glucoseFormatter.string(from: asMmolL as NSDecimalNumber) ?? "\(asMmolL)"
  28. }
  29. }
  30. extension Decimal {
  31. var asMmolL: Decimal {
  32. rounded(self * GlucoseUnits.exchangeRate, scale: 1, roundingMode: .plain)
  33. }
  34. var asMgdL: Decimal {
  35. rounded(self / GlucoseUnits.exchangeRate, scale: 0, roundingMode: .plain)
  36. }
  37. var formattedAsMmolL: String {
  38. NumberFormatter.glucoseFormatter.string(from: asMmolL as NSDecimalNumber) ?? "\(asMmolL)"
  39. }
  40. }
  41. extension NumberFormatter {
  42. static let glucoseFormatter: NumberFormatter = {
  43. let formatter = NumberFormatter()
  44. formatter.locale = Locale.current
  45. formatter.numberStyle = .decimal
  46. formatter.minimumFractionDigits = 1
  47. formatter.maximumFractionDigits = 1
  48. return formatter
  49. }()
  50. }
  51. struct LiveActivity: Widget {
  52. private let dateFormatter: DateFormatter = {
  53. var f = DateFormatter()
  54. f.dateStyle = .none
  55. f.timeStyle = .short
  56. return f
  57. }()
  58. private var bolusFormatter: NumberFormatter {
  59. let formatter = NumberFormatter()
  60. formatter.numberStyle = .decimal
  61. formatter.maximumFractionDigits = 2
  62. formatter.decimalSeparator = "."
  63. return formatter
  64. }
  65. private var carbsFormatter: NumberFormatter {
  66. let formatter = NumberFormatter()
  67. formatter.numberStyle = .decimal
  68. formatter.maximumFractionDigits = 0
  69. return formatter
  70. }
  71. @ViewBuilder private func changeLabel(context: ActivityViewContext<LiveActivityAttributes>) -> some View {
  72. if !context.state.change.isEmpty {
  73. Text(context.state.change).foregroundStyle(.primary.opacity(0.5)).font(.headline)
  74. .strikethrough(context.isStale, pattern: .solid, color: .red.opacity(0.6))
  75. } else {
  76. Text("--")
  77. }
  78. }
  79. @ViewBuilder func mealLabel(
  80. context: ActivityViewContext<LiveActivityAttributes>,
  81. additionalState: LiveActivityAttributes.ContentAdditionalState
  82. ) -> some View {
  83. HStack {
  84. VStack(alignment: .leading, spacing: 1, content: {
  85. HStack {
  86. Image(systemName: "fork.knife")
  87. .font(.title3)
  88. .foregroundColor(.yellow)
  89. }
  90. HStack {
  91. Image(systemName: "syringe.fill")
  92. .font(.title3)
  93. .foregroundColor(.blue)
  94. }
  95. })
  96. VStack(alignment: .trailing, spacing: 1, content: {
  97. HStack {
  98. Text(
  99. carbsFormatter.string(from: additionalState.cob as NSNumber) ?? "--"
  100. ).fontWeight(.bold).font(.headline).strikethrough(context.isStale, pattern: .solid, color: .red.opacity(0.6))
  101. Text(NSLocalizedString(" g", comment: "grams of carbs")).foregroundStyle(.secondary).font(.footnote)
  102. }
  103. HStack {
  104. Text(
  105. bolusFormatter.string(from: additionalState.iob as NSNumber) ?? "--"
  106. ).font(.headline).fontWeight(.bold).strikethrough(context.isStale, pattern: .solid, color: .red.opacity(0.6))
  107. Text(NSLocalizedString(" U", comment: "Unit in number of units delivered (keep the space character!)"))
  108. .foregroundStyle(.secondary).font(.footnote)
  109. }
  110. })
  111. VStack(alignment: .trailing, spacing: 1, content: {
  112. if additionalState.isOverrideActive {
  113. Image(systemName: "person.crop.circle.fill.badge.checkmark")
  114. .font(.title3)
  115. }
  116. })
  117. }
  118. }
  119. @ViewBuilder func trend(context: ActivityViewContext<LiveActivityAttributes>) -> some View {
  120. if context.isStale {
  121. Text("--")
  122. } else {
  123. if let trendSystemImage = context.state.direction {
  124. Image(systemName: trendSystemImage)
  125. }
  126. }
  127. }
  128. private func expiredLabel() -> some View {
  129. Text("Live Activity Expired. Open Trio to Refresh")
  130. .minimumScaleFactor(0.01)
  131. }
  132. private func updatedLabel(context: ActivityViewContext<LiveActivityAttributes>) -> Text {
  133. let text = Text("Updated: \(dateFormatter.string(from: context.state.date))")
  134. .font(.caption2)
  135. if context.isStale {
  136. // foregroundStyle is not available in <iOS 17 hence the check here
  137. if #available(iOSApplicationExtension 17.0, *) {
  138. return text.bold().foregroundStyle(.red)
  139. } else {
  140. return text.bold().foregroundColor(.red)
  141. }
  142. } else {
  143. if #available(iOSApplicationExtension 17.0, *) {
  144. return text.bold().foregroundStyle(.secondary)
  145. } else {
  146. return text.bold().foregroundColor(.red)
  147. }
  148. }
  149. }
  150. @ViewBuilder private func bgLabel(
  151. context: ActivityViewContext<LiveActivityAttributes>,
  152. additionalState: LiveActivityAttributes.ContentAdditionalState
  153. ) -> some View {
  154. HStack(alignment: .center) {
  155. Text(context.state.bg)
  156. .fontWeight(.bold)
  157. .font(.largeTitle)
  158. .strikethrough(context.isStale, pattern: .solid, color: .red.opacity(0.6))
  159. Text(additionalState.unit).foregroundStyle(.secondary).font(.subheadline).offset(x: -5, y: 5)
  160. }
  161. }
  162. private func bgAndTrend(
  163. context: ActivityViewContext<LiveActivityAttributes>,
  164. size: Size,
  165. dynamicColor _: Color
  166. ) -> (some View, Int) {
  167. var characters = 0
  168. let bgText = context.state.bg
  169. characters += bgText.count
  170. // narrow mode is for the minimal dynamic island view
  171. // there is not enough space to show all three arrow there
  172. // and everything has to be squeezed together to some degree
  173. // only display the first arrow character and make it red in case there were more characters
  174. var directionText: String?
  175. var warnColor: Color?
  176. if let direction = context.state.direction {
  177. if size == .compact {
  178. directionText = String(direction[direction.startIndex ... direction.startIndex])
  179. if direction.count > 1 {
  180. warnColor = Color.red
  181. }
  182. } else {
  183. directionText = direction
  184. }
  185. characters += directionText!.count
  186. }
  187. let spacing: CGFloat
  188. switch size {
  189. case .minimal: spacing = -1
  190. case .compact: spacing = 0
  191. case .expanded: spacing = 3
  192. }
  193. let stack = HStack(spacing: spacing) {
  194. Text(bgText)
  195. .strikethrough(context.isStale, pattern: .solid, color: .red.opacity(0.6))
  196. if let direction = directionText {
  197. let text = Text(direction)
  198. switch size {
  199. case .minimal:
  200. let scaledText = text.scaleEffect(x: 0.7, y: 0.7, anchor: .leading)
  201. if let warnColor {
  202. scaledText.foregroundStyle(warnColor)
  203. } else {
  204. scaledText
  205. }
  206. case .compact:
  207. text.scaleEffect(x: 0.8, y: 0.8, anchor: .leading).padding(.trailing, -3)
  208. case .expanded:
  209. text.scaleEffect(x: 0.7, y: 0.7, anchor: .leading).padding(.trailing, -5)
  210. }
  211. }
  212. }
  213. .foregroundStyle(context.isStale ? Color.primary.opacity(0.5) : Color.primary)
  214. return (stack, characters)
  215. }
  216. @ViewBuilder func trendArrow(
  217. context: ActivityViewContext<LiveActivityAttributes>,
  218. additionalState: LiveActivityAttributes.ContentAdditionalState
  219. ) -> some View {
  220. let gradient = LinearGradient(colors: [
  221. Color(red: 0.6235294118, green: 0.4235294118, blue: 0.9803921569),
  222. Color(red: 0.4862745098, green: 0.5450980392, blue: 0.9529411765),
  223. Color(red: 0.3411764706, green: 0.6666666667, blue: 0.9254901961),
  224. Color(red: 0.262745098, green: 0.7333333333, blue: 0.9137254902)
  225. ], startPoint: .leading, endPoint: .trailing)
  226. if !context.isStale {
  227. Image(systemName: "arrow.right")
  228. .font(.title)
  229. .rotationEffect(.degrees(additionalState.rotationDegrees))
  230. .foregroundStyle(gradient)
  231. }
  232. }
  233. @ViewBuilder func chart(
  234. context: ActivityViewContext<LiveActivityAttributes>,
  235. additionalState: LiveActivityAttributes.ContentAdditionalState
  236. ) -> some View {
  237. if context.isStale {
  238. Text("No data available")
  239. } else {
  240. // Determine scale
  241. let min = min(additionalState.chart.min() ?? 45, 40) - 20
  242. let max = max(additionalState.chart.max() ?? 270, 300) + 50
  243. let yAxisRuleMarkMin = additionalState.unit == "mg/dL" ? additionalState.lowGlucose : additionalState.lowGlucose
  244. .asMmolL
  245. let yAxisRuleMarkMax = additionalState.unit == "mg/dL" ? additionalState.highGlucose : additionalState.highGlucose
  246. .asMmolL
  247. Chart {
  248. RuleMark(y: .value("Low", yAxisRuleMarkMin))
  249. .lineStyle(.init(lineWidth: 0.5, dash: [5]))
  250. RuleMark(y: .value("High", yAxisRuleMarkMax))
  251. .lineStyle(.init(lineWidth: 0.5, dash: [5]))
  252. ForEach(additionalState.chart.indices, id: \.self) { index in
  253. let currentValue = additionalState.chart[index]
  254. let displayValue = additionalState.unit == "mg/dL" ? currentValue : currentValue.asMmolL
  255. let chartDate = additionalState.chartDate[index] ?? Date()
  256. let pointMark = PointMark(
  257. x: .value("Time", chartDate),
  258. y: .value("Value", displayValue)
  259. ).symbolSize(15)
  260. let color = getDynamicGlucoseColor(
  261. glucoseValue: currentValue,
  262. highGlucoseColorValue: additionalState.highGlucose,
  263. lowGlucoseColorValue: additionalState.lowGlucose,
  264. targetGlucose: 90,
  265. dynamicGlucoseColor: additionalState.dynamicGlucoseColor,
  266. offset: additionalState.unit == "mg/dL" ? 20 : 20.asMmolL
  267. )
  268. pointMark.foregroundStyle(color)
  269. }
  270. }
  271. .chartYAxis {
  272. AxisMarks(position: .trailing) { _ in
  273. AxisGridLine(stroke: .init(lineWidth: 0.2, dash: [2, 3])).foregroundStyle(Color.white)
  274. AxisValueLabel().foregroundStyle(.secondary).font(.footnote)
  275. }
  276. }
  277. .chartYScale(domain: additionalState.unit == "mg/dL" ? min ... max : min.asMmolL ... max.asMmolL)
  278. .chartXAxis {
  279. AxisMarks(position: .automatic) { _ in
  280. AxisGridLine(stroke: .init(lineWidth: 0.2, dash: [2, 3])).foregroundStyle(Color.white)
  281. }
  282. }
  283. }
  284. }
  285. @ViewBuilder func content(context: ActivityViewContext<LiveActivityAttributes>) -> some View {
  286. if let detailedViewState = context.state.detailedViewState {
  287. HStack(spacing: 12) {
  288. chart(context: context, additionalState: detailedViewState).frame(maxWidth: UIScreen.main.bounds.width / 1.8)
  289. VStack(alignment: .leading) {
  290. Spacer()
  291. bgLabel(context: context, additionalState: detailedViewState)
  292. HStack {
  293. changeLabel(context: context)
  294. trendArrow(context: context, additionalState: detailedViewState)
  295. }
  296. mealLabel(context: context, additionalState: detailedViewState).padding(.bottom, 8)
  297. updatedLabel(context: context).padding(.bottom, 10)
  298. }
  299. }
  300. .privacySensitive()
  301. .padding(.all, 14)
  302. .imageScale(.small)
  303. .foregroundColor(Color.white)
  304. .activityBackgroundTint(Color.black.opacity(0.8))
  305. } else {
  306. Group {
  307. let glucoseColor = getDynamicGlucoseColor(
  308. glucoseValue: Decimal(string: context.state.bg) ?? 100,
  309. highGlucoseColorValue: context.state.detailedViewState?.highGlucose ?? 180,
  310. lowGlucoseColorValue: context.state.detailedViewState?.lowGlucose ?? 70,
  311. targetGlucose: 90,
  312. dynamicGlucoseColor: context.state.detailedViewState?.dynamicGlucoseColor ?? false,
  313. offset: context.state.detailedViewState?.unit == "mg/dL" ? 20 : 20.asMmolL
  314. )
  315. if context.state.isInitialState {
  316. // add vertical and horizontal spacers around the label to ensure that the live activity view gets filled completely
  317. HStack {
  318. Spacer()
  319. VStack {
  320. Spacer()
  321. expiredLabel()
  322. Spacer()
  323. }
  324. Spacer()
  325. }
  326. } else {
  327. HStack(spacing: 3) {
  328. bgAndTrend(context: context, size: .expanded, dynamicColor: glucoseColor).0.font(.title)
  329. Spacer()
  330. VStack(alignment: .trailing, spacing: 5) {
  331. changeLabel(context: context).font(.title3)
  332. updatedLabel(context: context).font(.caption).foregroundStyle(.primary.opacity(0.7))
  333. }
  334. }
  335. }
  336. }
  337. .privacySensitive()
  338. .padding(.all, 15)
  339. // Semantic BackgroundStyle and Color values work here. They adapt to the given interface style (light mode, dark mode)
  340. // Semantic UIColors do NOT (as of iOS 17.1.1). Like UIColor.systemBackgroundColor (it does not adapt to changes of the interface style)
  341. // The colorScheme environment varaible that is usually used to detect dark mode does NOT work here (it reports false values)
  342. .foregroundStyle(Color.primary)
  343. .background(BackgroundStyle.background.opacity(0.4))
  344. .activityBackgroundTint(Color.clear)
  345. }
  346. }
  347. func dynamicIsland(context: ActivityViewContext<LiveActivityAttributes>) -> DynamicIsland {
  348. let glucoseValueForColor = context.state.bg
  349. let highGlucose = context.state.detailedViewState?.highGlucose ?? 180
  350. let lowGlucose = context.state.detailedViewState?.lowGlucose ?? 70
  351. let dynamicGlucoseColor = context.state.detailedViewState?.dynamicGlucoseColor ?? false
  352. let glucoseColor = getDynamicGlucoseColor(
  353. glucoseValue: Decimal(string: glucoseValueForColor) ?? 100,
  354. highGlucoseColorValue: highGlucose,
  355. lowGlucoseColorValue: lowGlucose,
  356. targetGlucose: 90,
  357. dynamicGlucoseColor: dynamicGlucoseColor,
  358. offset: context.state.detailedViewState?.unit == "mg/dL" ? 20 : 20.asMmolL
  359. )
  360. return DynamicIsland {
  361. DynamicIslandExpandedRegion(.leading) {
  362. bgAndTrend(context: context, size: .expanded, dynamicColor: glucoseColor).0.font(.title2).padding(.leading, 5)
  363. }
  364. DynamicIslandExpandedRegion(.trailing) {
  365. changeLabel(context: context).font(.title2).padding(.trailing, 5)
  366. }
  367. DynamicIslandExpandedRegion(.bottom) {
  368. if context.state.isInitialState {
  369. expiredLabel()
  370. } else if let detailedViewState = context.state.detailedViewState {
  371. chart(context: context, additionalState: detailedViewState)
  372. } else {
  373. Group {
  374. updatedLabel(context: context).font(.caption).foregroundStyle(Color.secondary)
  375. }
  376. .frame(
  377. maxHeight: .infinity,
  378. alignment: .bottom
  379. )
  380. }
  381. }
  382. DynamicIslandExpandedRegion(.center) {
  383. if context.state.detailedViewState != nil {
  384. updatedLabel(context: context).font(.caption).foregroundStyle(Color.secondary)
  385. }
  386. }
  387. } compactLeading: {
  388. bgAndTrend(context: context, size: .compact, dynamicColor: glucoseColor).0.padding(.leading, 4)
  389. } compactTrailing: {
  390. changeLabel(context: context).padding(.trailing, 4)
  391. } minimal: {
  392. let (_label, characterCount) = bgAndTrend(context: context, size: .minimal, dynamicColor: glucoseColor)
  393. let label = _label.padding(.leading, 7).padding(.trailing, 3)
  394. if characterCount < 4 {
  395. label
  396. } else if characterCount < 5 {
  397. label.fontWidth(.condensed)
  398. } else {
  399. label.fontWidth(.compressed)
  400. }
  401. }
  402. .widgetURL(URL(string: "Trio://"))
  403. .keylineTint(Color.purple)
  404. .contentMargins(.horizontal, 0, for: .minimal)
  405. .contentMargins(.trailing, 0, for: .compactLeading)
  406. .contentMargins(.leading, 0, for: .compactTrailing)
  407. }
  408. var body: some WidgetConfiguration {
  409. ActivityConfiguration(for: LiveActivityAttributes.self, content: self.content, dynamicIsland: self.dynamicIsland)
  410. }
  411. }
  412. private extension LiveActivityAttributes {
  413. static var preview: LiveActivityAttributes {
  414. LiveActivityAttributes(startDate: Date())
  415. }
  416. }
  417. private extension LiveActivityAttributes.ContentState {
  418. // 0 is the widest digit. Use this to get an upper bound on text width.
  419. // Use mmol/l notation with decimal point as well for the same reason, it uses up to 4 characters, while mg/dl uses up to 3
  420. static var testWide: LiveActivityAttributes.ContentState {
  421. LiveActivityAttributes.ContentState(
  422. bg: 00.0.description,
  423. direction: "→",
  424. change: "+0.0",
  425. date: Date(),
  426. detailedViewState: nil,
  427. isInitialState: false
  428. )
  429. }
  430. static var testVeryWide: LiveActivityAttributes.ContentState {
  431. LiveActivityAttributes.ContentState(
  432. bg: "00.0",
  433. direction: "↑↑",
  434. change: "+0.0",
  435. date: Date(),
  436. detailedViewState: nil,
  437. isInitialState: false
  438. )
  439. }
  440. static var testSuperWide: LiveActivityAttributes.ContentState {
  441. LiveActivityAttributes.ContentState(
  442. bg: "00.0",
  443. direction: "↑↑↑",
  444. change: "+0.0",
  445. date: Date(),
  446. detailedViewState: nil,
  447. isInitialState: false
  448. )
  449. }
  450. // 2 characters for BG, 1 character for change is the minimum that will be shown
  451. static var testNarrow: LiveActivityAttributes.ContentState {
  452. LiveActivityAttributes.ContentState(
  453. bg: "00",
  454. direction: "↑",
  455. change: "+0",
  456. date: Date(),
  457. detailedViewState: nil,
  458. isInitialState: false
  459. )
  460. }
  461. static var testMedium: LiveActivityAttributes.ContentState {
  462. LiveActivityAttributes.ContentState(
  463. bg: "000",
  464. direction: "↗︎",
  465. change: "+00",
  466. date: Date(),
  467. detailedViewState: nil,
  468. isInitialState: false
  469. )
  470. }
  471. static var testExpired: LiveActivityAttributes.ContentState {
  472. LiveActivityAttributes.ContentState(
  473. bg: "--",
  474. direction: nil,
  475. change: "--",
  476. date: Date().addingTimeInterval(-60 * 60),
  477. detailedViewState: nil,
  478. isInitialState: true
  479. )
  480. }
  481. }
  482. @available(iOS 17.0, iOSApplicationExtension 17.0, *)
  483. #Preview("Notification", as: .content, using: LiveActivityAttributes.preview) {
  484. LiveActivity()
  485. } contentStates: {
  486. LiveActivityAttributes.ContentState.testSuperWide
  487. LiveActivityAttributes.ContentState.testVeryWide
  488. LiveActivityAttributes.ContentState.testWide
  489. LiveActivityAttributes.ContentState.testMedium
  490. LiveActivityAttributes.ContentState.testNarrow
  491. LiveActivityAttributes.ContentState.testExpired
  492. }