LiveActivity.swift 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  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 = 1
  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. HStack(spacing: -5) {
  73. if !context.state.change.isEmpty {
  74. Text(context.state.change).foregroundStyle(.primary).font(.subheadline)
  75. .strikethrough(context.isStale, pattern: .solid, color: .red.opacity(0.6))
  76. } else {
  77. Text("--")
  78. }
  79. }
  80. }
  81. @ViewBuilder func cobLabel(
  82. context: ActivityViewContext<LiveActivityAttributes>,
  83. additionalState: LiveActivityAttributes.ContentAdditionalState
  84. ) -> some View {
  85. VStack(spacing: 2) {
  86. // Image(systemName: "fork.knife")
  87. // .font(.title3)
  88. // .foregroundColor(.yellow)
  89. HStack {
  90. Text(
  91. carbsFormatter.string(from: additionalState.cob as NSNumber) ?? "--"
  92. ).fontWeight(.bold).font(.title3).strikethrough(context.isStale, pattern: .solid, color: .red.opacity(0.6))
  93. Text(NSLocalizedString("g", comment: "grams of carbs")).foregroundStyle(.primary).font(.headline)
  94. .fontWeight(.bold)
  95. }
  96. Text("COB").font(.subheadline).foregroundStyle(.primary)
  97. }
  98. }
  99. @ViewBuilder func iobLabel(
  100. context: ActivityViewContext<LiveActivityAttributes>,
  101. additionalState: LiveActivityAttributes.ContentAdditionalState
  102. ) -> some View {
  103. VStack(spacing: 2) {
  104. // Image(systemName: "syringe.fill")
  105. // .font(.title3)
  106. // .foregroundColor(.blue)
  107. HStack {
  108. Text(
  109. bolusFormatter.string(from: additionalState.iob as NSNumber) ?? "--"
  110. ).font(.title3).fontWeight(.bold).strikethrough(context.isStale, pattern: .solid, color: .red.opacity(0.6))
  111. Text(NSLocalizedString("U", comment: "Unit in number of units delivered (keep the space character!)"))
  112. .foregroundStyle(.primary).font(.headline).fontWeight(.bold)
  113. }
  114. Text("IOB").font(.subheadline).foregroundStyle(.primary)
  115. }
  116. }
  117. @ViewBuilder func mealLabel(
  118. context: ActivityViewContext<LiveActivityAttributes>,
  119. additionalState: LiveActivityAttributes.ContentAdditionalState
  120. ) -> some View {
  121. HStack {
  122. VStack(alignment: .leading, spacing: 0, content: {
  123. HStack {
  124. Image(systemName: "fork.knife")
  125. .font(.title3)
  126. .foregroundColor(.yellow)
  127. }
  128. HStack {
  129. Image(systemName: "syringe.fill")
  130. .font(.title3)
  131. .foregroundColor(.blue)
  132. }
  133. })
  134. VStack(alignment: .trailing, spacing: 0, content: {
  135. HStack {
  136. Text(
  137. carbsFormatter.string(from: additionalState.cob as NSNumber) ?? "--"
  138. ).fontWeight(.bold).font(.title3).strikethrough(context.isStale, pattern: .solid, color: .red.opacity(0.6))
  139. Text(NSLocalizedString(" g", comment: "grams of carbs")).foregroundStyle(.primary).font(.headline)
  140. }
  141. HStack {
  142. Text(
  143. bolusFormatter.string(from: additionalState.iob as NSNumber) ?? "--"
  144. ).font(.title3).fontWeight(.bold).strikethrough(context.isStale, pattern: .solid, color: .red.opacity(0.6))
  145. Text(NSLocalizedString(" U", comment: "Unit in number of units delivered (keep the space character!)"))
  146. .foregroundStyle(.primary).font(.headline)
  147. }
  148. })
  149. VStack(alignment: .trailing, spacing: 1, content: {
  150. if additionalState.isOverrideActive {
  151. Image(systemName: "person.crop.circle.fill.badge.checkmark")
  152. .font(.title3)
  153. }
  154. })
  155. }
  156. }
  157. @ViewBuilder func trend(context: ActivityViewContext<LiveActivityAttributes>) -> some View {
  158. if context.isStale {
  159. Text("--")
  160. } else {
  161. if let trendSystemImage = context.state.direction {
  162. Image(systemName: trendSystemImage)
  163. }
  164. }
  165. }
  166. private func expiredLabel() -> some View {
  167. Text("Live Activity Expired. Open Trio to Refresh")
  168. .minimumScaleFactor(0.01)
  169. }
  170. @ViewBuilder private func updatedLabel(context: ActivityViewContext<LiveActivityAttributes>) -> some View {
  171. VStack {
  172. let dateText = Text("\(dateFormatter.string(from: context.state.date))").font(.title3).foregroundStyle(.primary)
  173. if context.isStale {
  174. if #available(iOSApplicationExtension 17.0, *) {
  175. dateText.bold().foregroundStyle(.red)
  176. } else {
  177. dateText.bold().foregroundColor(.red)
  178. }
  179. } else {
  180. if #available(iOSApplicationExtension 17.0, *) {
  181. dateText.bold().foregroundStyle(.primary)
  182. } else {
  183. dateText.bold().foregroundColor(.primary)
  184. }
  185. }
  186. Text("Updated").font(.subheadline).foregroundStyle(.primary)
  187. }
  188. }
  189. @ViewBuilder private func bgLabel(
  190. context: ActivityViewContext<LiveActivityAttributes>,
  191. additionalState _: LiveActivityAttributes.ContentAdditionalState
  192. ) -> some View {
  193. HStack(alignment: .center) {
  194. Text(context.state.bg)
  195. .fontWeight(.bold)
  196. .font(.title3)
  197. .strikethrough(context.isStale, pattern: .solid, color: .red.opacity(0.6))
  198. // Text(additionalState.unit).foregroundStyle(.primary).font(.footnote)
  199. }
  200. }
  201. private func bgAndTrend(context: ActivityViewContext<LiveActivityAttributes>, size: Size) -> (some View, Int) {
  202. var characters = 0
  203. let bgText = context.state.bg
  204. characters += bgText.count
  205. // narrow mode is for the minimal dynamic island view
  206. // there is not enough space to show all three arrow there
  207. // and everything has to be squeezed together to some degree
  208. // only display the first arrow character and make it red in case there were more characters
  209. var directionText: String?
  210. var warnColor: Color?
  211. if let direction = context.state.direction {
  212. if size == .compact {
  213. directionText = String(direction[direction.startIndex ... direction.startIndex])
  214. if direction.count > 1 {
  215. warnColor = Color.red
  216. }
  217. } else {
  218. directionText = direction
  219. }
  220. characters += directionText!.count
  221. }
  222. let spacing: CGFloat
  223. switch size {
  224. case .minimal: spacing = -1
  225. case .compact: spacing = 0
  226. case .expanded: spacing = 3
  227. }
  228. let stack = HStack(spacing: spacing) {
  229. Text(bgText)
  230. .strikethrough(context.isStale, pattern: .solid, color: .red.opacity(0.6))
  231. if let direction = directionText {
  232. let text = Text(direction)
  233. switch size {
  234. case .minimal:
  235. let scaledText = text.scaleEffect(x: 0.7, y: 0.7, anchor: .leading)
  236. if let warnColor {
  237. scaledText.foregroundStyle(warnColor)
  238. } else {
  239. scaledText
  240. }
  241. case .compact:
  242. text.scaleEffect(x: 0.8, y: 0.8, anchor: .leading).padding(.trailing, -3)
  243. case .expanded:
  244. text.scaleEffect(x: 0.7, y: 0.7, anchor: .leading).padding(.trailing, -5)
  245. }
  246. }
  247. }
  248. .foregroundStyle(context.isStale ? Color.primary.opacity(0.5) : Color.primary)
  249. return (stack, characters)
  250. }
  251. @ViewBuilder func trendArrow(
  252. context: ActivityViewContext<LiveActivityAttributes>,
  253. additionalState: LiveActivityAttributes.ContentAdditionalState
  254. ) -> some View {
  255. let gradient = LinearGradient(colors: [
  256. Color(red: 0.6235294118, green: 0.4235294118, blue: 0.9803921569),
  257. Color(red: 0.4862745098, green: 0.5450980392, blue: 0.9529411765),
  258. Color(red: 0.3411764706, green: 0.6666666667, blue: 0.9254901961),
  259. Color(red: 0.262745098, green: 0.7333333333, blue: 0.9137254902)
  260. ], startPoint: .leading, endPoint: .trailing)
  261. if !context.isStale {
  262. Image(systemName: "arrowshape.right.circle")
  263. .font(.headline)
  264. .rotationEffect(.degrees(additionalState.rotationDegrees))
  265. // .foregroundStyle(gradient)
  266. }
  267. }
  268. @ViewBuilder func chart(
  269. context: ActivityViewContext<LiveActivityAttributes>,
  270. additionalState: LiveActivityAttributes.ContentAdditionalState
  271. ) -> some View {
  272. if context.isStale {
  273. Text("No data available")
  274. } else {
  275. // Determine scale
  276. let min = min(additionalState.chart.min() ?? 45, 40) - 20
  277. let max = max(additionalState.chart.max() ?? 270, 300) + 50
  278. let yAxisRuleMarkMin = additionalState.unit == "mg/dL" ? additionalState.lowGlucose : additionalState.lowGlucose
  279. .asMmolL
  280. let yAxisRuleMarkMax = additionalState.unit == "mg/dL" ? additionalState.highGlucose : additionalState.highGlucose
  281. .asMmolL
  282. let target = additionalState.unit == "mg/dL" ? additionalState.target : additionalState.target.asMmolL
  283. Chart {
  284. RuleMark(y: .value("Low", yAxisRuleMarkMin))
  285. .lineStyle(.init(lineWidth: 0.5, dash: [5]))
  286. RuleMark(y: .value("High", yAxisRuleMarkMax))
  287. .lineStyle(.init(lineWidth: 0.5, dash: [5]))
  288. RuleMark(y: .value("Target", target)).foregroundStyle(.green.gradient).lineStyle(.init(lineWidth: 1))
  289. ForEach(additionalState.chart.indices, id: \.self) { index in
  290. let currentValue = additionalState.chart[index]
  291. let displayValue = additionalState.unit == "mg/dL" ? currentValue : currentValue.asMmolL
  292. let chartDate = additionalState.chartDate[index] ?? Date()
  293. let pointMark = PointMark(
  294. x: .value("Time", chartDate),
  295. y: .value("Value", displayValue)
  296. ).symbolSize(15)
  297. if displayValue > yAxisRuleMarkMax {
  298. pointMark.foregroundStyle(Color.orange.gradient)
  299. } else if displayValue < yAxisRuleMarkMin {
  300. pointMark.foregroundStyle(Color.red.gradient)
  301. } else {
  302. pointMark.foregroundStyle(Color.green.gradient)
  303. }
  304. }
  305. }
  306. .chartYAxis {
  307. AxisMarks(position: .trailing) { _ in
  308. AxisGridLine(stroke: .init(lineWidth: 0.2, dash: [2, 3])).foregroundStyle(Color.white)
  309. AxisValueLabel().foregroundStyle(.primary).font(.footnote)
  310. }
  311. }
  312. // .chartYScale(domain: additionalState.unit == "mg/dL" ? min ... max : min.asMmolL ... max.asMmolL)
  313. .chartYAxis(.hidden)
  314. .chartPlotStyle { plotContent in
  315. plotContent
  316. .background(
  317. RoundedRectangle(cornerRadius: 12)
  318. .fill(Color.cyan.opacity(0.15))
  319. )
  320. .clipShape(RoundedRectangle(cornerRadius: 12))
  321. }
  322. .chartXAxis {
  323. AxisMarks(position: .automatic) { _ in
  324. AxisGridLine(stroke: .init(lineWidth: 0.2, dash: [2, 3])).foregroundStyle(Color.white)
  325. }
  326. }
  327. }
  328. }
  329. @ViewBuilder func content(context: ActivityViewContext<LiveActivityAttributes>) -> some View {
  330. if let detailedViewState = context.state.detailedViewState {
  331. VStack(content: {
  332. chart(context: context, additionalState: detailedViewState)
  333. .frame(maxWidth: UIScreen.main.bounds.width * 0.9)
  334. .frame(height: 80)
  335. HStack {
  336. ForEach(context.state.itemOrder, id: \.self) { item in
  337. switch item {
  338. case "currentGlucose":
  339. if context.state.showCurrentGlucose {
  340. VStack {
  341. bgLabel(context: context, additionalState: detailedViewState)
  342. HStack {
  343. changeLabel(context: context)
  344. }
  345. }
  346. }
  347. case "iob":
  348. if context.state.showIOB {
  349. iobLabel(context: context, additionalState: detailedViewState)
  350. }
  351. case "cob":
  352. if context.state.showCOB {
  353. cobLabel(context: context, additionalState: detailedViewState)
  354. }
  355. case "updatedLabel":
  356. if context.state.showUpdatedLabel {
  357. updatedLabel(context: context)
  358. }
  359. default:
  360. EmptyView()
  361. }
  362. Divider().foregroundStyle(.primary).fontWeight(.bold).frame(width: 10)
  363. }
  364. }
  365. // HStack {
  366. // if context.state.showCurrentGlucose {
  367. // VStack {
  368. // bgLabel(context: context, additionalState: detailedViewState)
  369. // HStack {
  370. // changeLabel(context: context)
  371. // }
  372. // }
  373. // Divider().foregroundStyle(.primary).fontWeight(.bold).frame(width: 10)
  374. // }
  375. //
  376. // if context.state.showIOB {
  377. // iobLabel(context: context, additionalState: detailedViewState)
  378. // Divider().foregroundStyle(.primary).fontWeight(.bold).frame(width: 10)
  379. // }
  380. //
  381. // if context.state.showCOB {
  382. // cobLabel(context: context, additionalState: detailedViewState)
  383. // Divider().foregroundStyle(.primary).fontWeight(.bold).frame(width: 10)
  384. // }
  385. //
  386. // if context.state.showUpdatedLabel {
  387. // updatedLabel(context: context)
  388. // }
  389. // }
  390. })
  391. .privacySensitive()
  392. .padding(.all, 14)
  393. .imageScale(.small)
  394. .foregroundStyle(Color.primary)
  395. .activityBackgroundTint(Color.clear)
  396. } else {
  397. Group {
  398. if context.state.isInitialState {
  399. // add vertical and horizontal spacers around the label to ensure that the live activity view gets filled completely
  400. HStack {
  401. Spacer()
  402. VStack {
  403. Spacer()
  404. expiredLabel()
  405. Spacer()
  406. }
  407. Spacer()
  408. }
  409. } else {
  410. HStack(spacing: 3) {
  411. bgAndTrend(context: context, size: .expanded).0.font(.title)
  412. Spacer()
  413. VStack(alignment: .trailing, spacing: 5) {
  414. changeLabel(context: context).font(.title3)
  415. updatedLabel(context: context).font(.caption).foregroundStyle(.primary.opacity(0.7))
  416. }
  417. }
  418. }
  419. }
  420. .privacySensitive()
  421. .padding(.all, 15)
  422. // Semantic BackgroundStyle and Color values work here. They adapt to the given interface style (light mode, dark mode)
  423. // Semantic UIColors do NOT (as of iOS 17.1.1). Like UIColor.systemBackgroundColor (it does not adapt to changes of the interface style)
  424. // The colorScheme environment varaible that is usually used to detect dark mode does NOT work here (it reports false values)
  425. .foregroundStyle(Color.primary)
  426. .background(BackgroundStyle.background.opacity(0.4))
  427. .activityBackgroundTint(Color.clear)
  428. }
  429. }
  430. func dynamicIsland(context: ActivityViewContext<LiveActivityAttributes>) -> DynamicIsland {
  431. DynamicIsland {
  432. DynamicIslandExpandedRegion(.leading) {
  433. bgAndTrend(context: context, size: .expanded).0.font(.title2).padding(.leading, 5)
  434. }
  435. DynamicIslandExpandedRegion(.trailing) {
  436. changeLabel(context: context).font(.title2).padding(.trailing, 5)
  437. }
  438. DynamicIslandExpandedRegion(.bottom) {
  439. if context.state.isInitialState {
  440. expiredLabel()
  441. } else if let detailedViewState = context.state.detailedViewState {
  442. chart(context: context, additionalState: detailedViewState)
  443. } else {
  444. Group {
  445. updatedLabel(context: context).font(.caption).foregroundStyle(Color.secondary)
  446. }
  447. .frame(
  448. maxHeight: .infinity,
  449. alignment: .bottom
  450. )
  451. }
  452. }
  453. DynamicIslandExpandedRegion(.center) {
  454. if context.state.detailedViewState != nil {
  455. updatedLabel(context: context).font(.caption).foregroundStyle(Color.secondary)
  456. }
  457. }
  458. } compactLeading: {
  459. bgAndTrend(context: context, size: .compact).0.padding(.leading, 4)
  460. } compactTrailing: {
  461. changeLabel(context: context).padding(.trailing, 4)
  462. } minimal: {
  463. let (_label, characterCount) = bgAndTrend(context: context, size: .minimal)
  464. let label = _label.padding(.leading, 7).padding(.trailing, 3)
  465. if characterCount < 4 {
  466. label
  467. } else if characterCount < 5 {
  468. label.fontWidth(.condensed)
  469. } else {
  470. label.fontWidth(.compressed)
  471. }
  472. }
  473. .widgetURL(URL(string: "Trio://"))
  474. .keylineTint(Color.purple)
  475. .contentMargins(.horizontal, 0, for: .minimal)
  476. .contentMargins(.trailing, 0, for: .compactLeading)
  477. .contentMargins(.leading, 0, for: .compactTrailing)
  478. }
  479. var body: some WidgetConfiguration {
  480. ActivityConfiguration(for: LiveActivityAttributes.self, content: self.content, dynamicIsland: self.dynamicIsland)
  481. }
  482. }
  483. private extension LiveActivityAttributes {
  484. static var preview: LiveActivityAttributes {
  485. LiveActivityAttributes(startDate: Date())
  486. }
  487. }
  488. private extension LiveActivityAttributes.ContentState {
  489. // 0 is the widest digit. Use this to get an upper bound on text width.
  490. // 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
  491. static var testWide: LiveActivityAttributes.ContentState {
  492. LiveActivityAttributes.ContentState(
  493. bg: "00.0",
  494. direction: "→",
  495. change: "+0.0",
  496. date: Date(),
  497. detailedViewState: nil,
  498. showCOB: true,
  499. showIOB: true,
  500. showCurrentGlucose: true,
  501. showUpdatedLabel: true,
  502. itemOrder: ["currentGlucose", "iob", "cob", "updatedLabel"],
  503. isInitialState: false
  504. )
  505. }
  506. static var testVeryWide: LiveActivityAttributes.ContentState {
  507. LiveActivityAttributes.ContentState(
  508. bg: "00.0",
  509. direction: "↑↑",
  510. change: "+0.0",
  511. date: Date(),
  512. detailedViewState: nil,
  513. showCOB: true,
  514. showIOB: true,
  515. showCurrentGlucose: true,
  516. showUpdatedLabel: true,
  517. itemOrder: ["currentGlucose", "iob", "cob", "updatedLabel"],
  518. isInitialState: false
  519. )
  520. }
  521. static var testSuperWide: LiveActivityAttributes.ContentState {
  522. LiveActivityAttributes.ContentState(
  523. bg: "00.0",
  524. direction: "↑↑↑",
  525. change: "+0.0",
  526. date: Date(),
  527. detailedViewState: nil,
  528. showCOB: true,
  529. showIOB: true,
  530. showCurrentGlucose: true,
  531. showUpdatedLabel: true,
  532. itemOrder: ["currentGlucose", "iob", "cob", "updatedLabel"],
  533. isInitialState: false
  534. )
  535. }
  536. // 2 characters for BG, 1 character for change is the minimum that will be shown
  537. static var testNarrow: LiveActivityAttributes.ContentState {
  538. LiveActivityAttributes.ContentState(
  539. bg: "00",
  540. direction: "↑",
  541. change: "+0",
  542. date: Date(),
  543. detailedViewState: nil,
  544. showCOB: true,
  545. showIOB: true,
  546. showCurrentGlucose: true,
  547. showUpdatedLabel: true,
  548. itemOrder: ["currentGlucose", "iob", "cob", "updatedLabel"],
  549. isInitialState: false
  550. )
  551. }
  552. static var testMedium: LiveActivityAttributes.ContentState {
  553. LiveActivityAttributes.ContentState(
  554. bg: "000",
  555. direction: "↗︎",
  556. change: "+00",
  557. date: Date(),
  558. detailedViewState: nil,
  559. showCOB: true,
  560. showIOB: true,
  561. showCurrentGlucose: true,
  562. showUpdatedLabel: true,
  563. itemOrder: ["currentGlucose", "iob", "cob", "updatedLabel"],
  564. isInitialState: false
  565. )
  566. }
  567. static var testExpired: LiveActivityAttributes.ContentState {
  568. LiveActivityAttributes.ContentState(
  569. bg: "--",
  570. direction: nil,
  571. change: "--",
  572. date: Date().addingTimeInterval(-60 * 60),
  573. detailedViewState: nil,
  574. showCOB: true,
  575. showIOB: true,
  576. showCurrentGlucose: true,
  577. showUpdatedLabel: true,
  578. itemOrder: ["currentGlucose", "iob", "cob", "updatedLabel"],
  579. isInitialState: true
  580. )
  581. }
  582. }
  583. @available(iOS 17.0, iOSApplicationExtension 17.0, *)
  584. #Preview("Notification", as: .content, using: LiveActivityAttributes.preview) {
  585. LiveActivity()
  586. } contentStates: {
  587. LiveActivityAttributes.ContentState.testSuperWide
  588. LiveActivityAttributes.ContentState.testVeryWide
  589. LiveActivityAttributes.ContentState.testWide
  590. LiveActivityAttributes.ContentState.testMedium
  591. LiveActivityAttributes.ContentState.testNarrow
  592. LiveActivityAttributes.ContentState.testExpired
  593. }