LiveActivity.swift 25 KB

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