LiveActivity.swift 20 KB

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