LiveActivity.swift 24 KB

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