LiveActivity.swift 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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. HStack(spacing: 3) {
  219. if context.state.isInitialState {
  220. expiredLabel()
  221. } else {
  222. bgAndTrend(context: context, size: .expanded).0.font(.title)
  223. Spacer()
  224. VStack(alignment: .trailing, spacing: 5) {
  225. changeLabel(context: context).font(.title3)
  226. updatedLabel(context: context).font(.caption).foregroundStyle(.primary.opacity(0.7))
  227. }
  228. }
  229. }
  230. .privacySensitive()
  231. .padding(.all, 15)
  232. // Semantic BackgroundStyle and Color values work here. They adapt to the given interface style (light mode, dark mode)
  233. // Semantic UIColors do NOT (as of iOS 17.1.1). Like UIColor.systemBackgroundColor (it does not adapt to changes of the interface style)
  234. // The colorScheme environment varaible that is usually used to detect dark mode does NOT work here (it reports false values)
  235. .foregroundStyle(Color.primary)
  236. .background(BackgroundStyle.background.opacity(0.4))
  237. .activityBackgroundTint(Color.clear)
  238. }
  239. }
  240. func dynamicIsland(context: ActivityViewContext<LiveActivityAttributes>) -> DynamicIsland {
  241. DynamicIsland {
  242. // Expanded UI goes here. Compose the expanded UI through
  243. // various regions, like leading/trailing/center/bottom
  244. DynamicIslandExpandedRegion(.leading) {
  245. bgAndTrend(context: context, size: .expanded).0.font(.title2).padding(.leading, 5)
  246. }
  247. DynamicIslandExpandedRegion(.trailing) {
  248. changeLabel(context: context).font(.title2).padding(.trailing, 5)
  249. }
  250. DynamicIslandExpandedRegion(.bottom) {
  251. if context.state.isInitialState {
  252. expiredLabel()
  253. } else if let detailedViewState = context.state.detailedViewState {
  254. chart(context: context, additionalState: detailedViewState)
  255. } else {
  256. Group {
  257. updatedLabel(context: context).font(.caption).foregroundStyle(Color.secondary)
  258. }
  259. .frame(
  260. maxHeight: .infinity,
  261. alignment: .bottom
  262. )
  263. }
  264. }
  265. DynamicIslandExpandedRegion(.center) {
  266. if context.state.detailedViewState != nil {
  267. updatedLabel(context: context).font(.caption).foregroundStyle(Color.secondary)
  268. }
  269. }
  270. } compactLeading: {
  271. bgAndTrend(context: context, size: .compact).0.padding(.leading, 4)
  272. } compactTrailing: {
  273. changeLabel(context: context).padding(.trailing, 4)
  274. } minimal: {
  275. let (_label, characterCount) = bgAndTrend(context: context, size: .minimal)
  276. let label = _label.padding(.leading, 7).padding(.trailing, 3)
  277. if characterCount < 4 {
  278. label
  279. } else if characterCount < 5 {
  280. label.fontWidth(.condensed)
  281. } else {
  282. label.fontWidth(.compressed)
  283. }
  284. }
  285. .widgetURL(URL(string: "Trio://"))
  286. .keylineTint(Color.purple)
  287. .contentMargins(.horizontal, 0, for: .minimal)
  288. .contentMargins(.trailing, 0, for: .compactLeading)
  289. .contentMargins(.leading, 0, for: .compactTrailing)
  290. }
  291. var body: some WidgetConfiguration {
  292. ActivityConfiguration(for: LiveActivityAttributes.self, content: self.content, dynamicIsland: self.dynamicIsland)
  293. }
  294. }
  295. private extension LiveActivityAttributes {
  296. static var preview: LiveActivityAttributes {
  297. LiveActivityAttributes(startDate: Date())
  298. }
  299. }
  300. private extension LiveActivityAttributes.ContentState {
  301. // 0 is the widest digit. Use this to get an upper bound on text width.
  302. // 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
  303. static var testWide: LiveActivityAttributes.ContentState {
  304. LiveActivityAttributes.ContentState(
  305. bg: "00.0",
  306. direction: "→",
  307. change: "+0.0",
  308. date: Date(),
  309. detailedViewState: nil,
  310. isInitialState: false
  311. )
  312. }
  313. static var testVeryWide: LiveActivityAttributes.ContentState {
  314. LiveActivityAttributes.ContentState(
  315. bg: "00.0",
  316. direction: "↑↑",
  317. change: "+0.0",
  318. date: Date(),
  319. detailedViewState: nil,
  320. isInitialState: false
  321. )
  322. }
  323. static var testSuperWide: LiveActivityAttributes.ContentState {
  324. LiveActivityAttributes.ContentState(
  325. bg: "00.0",
  326. direction: "↑↑↑",
  327. change: "+0.0",
  328. date: Date(),
  329. detailedViewState: nil,
  330. isInitialState: false
  331. )
  332. }
  333. // 2 characters for BG, 1 character for change is the minimum that will be shown
  334. static var testNarrow: LiveActivityAttributes.ContentState {
  335. LiveActivityAttributes.ContentState(
  336. bg: "00",
  337. direction: "↑",
  338. change: "+0",
  339. date: Date(),
  340. detailedViewState: nil,
  341. isInitialState: false
  342. )
  343. }
  344. static var testMedium: LiveActivityAttributes.ContentState {
  345. LiveActivityAttributes.ContentState(
  346. bg: "000",
  347. direction: "↗︎",
  348. change: "+00",
  349. date: Date(),
  350. detailedViewState: nil,
  351. isInitialState: false
  352. )
  353. }
  354. static var testExpired: LiveActivityAttributes.ContentState {
  355. LiveActivityAttributes.ContentState(
  356. bg: "--",
  357. direction: nil,
  358. change: "--",
  359. date: Date().addingTimeInterval(-60 * 60),
  360. detailedViewState: nil,
  361. isInitialState: true
  362. )
  363. }
  364. }
  365. @available(iOS 17.0, iOSApplicationExtension 17.0, *)
  366. #Preview("Notification", as: .content, using: LiveActivityAttributes.preview) {
  367. LiveActivity()
  368. } contentStates: {
  369. LiveActivityAttributes.ContentState.testSuperWide
  370. LiveActivityAttributes.ContentState.testVeryWide
  371. LiveActivityAttributes.ContentState.testWide
  372. LiveActivityAttributes.ContentState.testMedium
  373. LiveActivityAttributes.ContentState.testNarrow
  374. LiveActivityAttributes.ContentState.testExpired
  375. }