LiveActivity.swift 18 KB

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