LiveActivity.swift 16 KB

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