LiveActivity.swift 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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(context: ActivityViewContext<LiveActivityAttributes>) -> some View {
  45. HStack {
  46. VStack(alignment: .leading, spacing: 1, content: {
  47. HStack {
  48. Image(systemName: "fork.knife")
  49. .font(.title3)
  50. .foregroundColor(.yellow)
  51. }
  52. HStack {
  53. Image(systemName: "syringe.fill")
  54. .font(.title3)
  55. .foregroundColor(.blue)
  56. }
  57. })
  58. VStack(alignment: .trailing, spacing: 1, content: {
  59. HStack {
  60. if context.isStale {
  61. Text(
  62. carbsFormatter.string(from: context.state.cob as NSNumber) ?? "--"
  63. ).fontWeight(.bold).font(.headline).strikethrough(pattern: .solid, color: .red.opacity(0.6))
  64. .font(.callout)
  65. Text(NSLocalizedString(" g", comment: "grams of carbs")).foregroundStyle(.secondary).font(.footnote)
  66. } else {
  67. Text(
  68. carbsFormatter.string(from: context.state.cob as NSNumber) ?? "--"
  69. ).fontWeight(.bold).font(.headline)
  70. Text(NSLocalizedString(" g", comment: "grams of carbs")).foregroundStyle(.secondary).font(.footnote)
  71. }
  72. }
  73. HStack {
  74. if context.isStale {
  75. Text(
  76. bolusFormatter.string(from: context.state.iob as NSNumber) ?? "--"
  77. ).font(.headline).fontWeight(.bold).strikethrough(pattern: .solid, color: .red.opacity(0.6))
  78. .font(.callout)
  79. Text(NSLocalizedString(" U", comment: "Unit in number of units delivered (keep the space character!)"))
  80. .foregroundStyle(.secondary).font(.footnote)
  81. } else {
  82. Text(
  83. bolusFormatter.string(from: context.state.iob as NSNumber) ?? "--"
  84. ).font(.headline).fontWeight(.bold)
  85. Text(NSLocalizedString(" U", comment: "Unit in number of units delivered (keep the space character!)"))
  86. .foregroundStyle(.secondary).font(.footnote)
  87. }
  88. }
  89. })
  90. VStack(alignment: .trailing, spacing: 1, content: {
  91. if context.state.isOverrideActive {
  92. if !context.isStale {
  93. Image(systemName: "person.crop.circle.fill.badge.checkmark")
  94. .font(.title3)
  95. } else {
  96. Image(systemName: "person.crop.circle.fill.badge.checkmark")
  97. .font(.title3)
  98. .strikethrough(pattern: .solid, color: .red.opacity(0.6))
  99. }
  100. }
  101. })
  102. }
  103. }
  104. @ViewBuilder func trend(context: ActivityViewContext<LiveActivityAttributes>) -> some View {
  105. if context.isStale {
  106. Text("--")
  107. } else {
  108. if let trendSystemImage = context.state.direction {
  109. Image(systemName: trendSystemImage)
  110. }
  111. }
  112. }
  113. private func updatedLabel(context: ActivityViewContext<LiveActivityAttributes>) -> Text {
  114. let text = Text("Updated: \(dateFormatter.string(from: context.state.date))")
  115. .font(.caption2)
  116. if context.isStale {
  117. // foregroundStyle is not available in <iOS 17 hence the check here
  118. if #available(iOSApplicationExtension 17.0, *) {
  119. return text.bold().foregroundStyle(.red)
  120. } else {
  121. return text.bold().foregroundColor(.red)
  122. }
  123. } else {
  124. if #available(iOSApplicationExtension 17.0, *) {
  125. return text.bold().foregroundStyle(.secondary)
  126. } else {
  127. return text.bold().foregroundColor(.red)
  128. }
  129. }
  130. }
  131. @ViewBuilder private func bgLabel(context: ActivityViewContext<LiveActivityAttributes>) -> some View {
  132. HStack(alignment: .center) {
  133. Text(context.state.bg)
  134. .fontWeight(.bold)
  135. .font(.largeTitle)
  136. .strikethrough(context.isStale, pattern: .solid, color: .red.opacity(0.6))
  137. Text(context.state.unit).foregroundStyle(.secondary).font(.subheadline).offset(x: -5, y: 5)
  138. }
  139. }
  140. private func bgAndTrend(context: ActivityViewContext<LiveActivityAttributes>, size: Size) -> (some View, Int) {
  141. var characters = 0
  142. let bgText = context.state.bg
  143. characters += bgText.count
  144. // narrow mode is for the minimal dynamic island view
  145. // there is not enough space to show all three arrow there
  146. // and everything has to be squeezed together to some degree
  147. // only display the first arrow character and make it red in case there were more characters
  148. var directionText: String?
  149. var warnColor: Color?
  150. if let direction = context.state.direction {
  151. if size == .compact {
  152. directionText = String(direction[direction.startIndex ... direction.startIndex])
  153. if direction.count > 1 {
  154. warnColor = Color.red
  155. }
  156. } else {
  157. directionText = direction
  158. }
  159. characters += directionText!.count
  160. }
  161. let spacing: CGFloat
  162. switch size {
  163. case .minimal: spacing = -1
  164. case .compact: spacing = 0
  165. case .expanded: spacing = 3
  166. }
  167. let stack = HStack(spacing: spacing) {
  168. Text(bgText)
  169. .strikethrough(context.isStale, pattern: .solid, color: .red.opacity(0.6))
  170. if let direction = directionText {
  171. let text = Text(direction)
  172. switch size {
  173. case .minimal:
  174. let scaledText = text.scaleEffect(x: 0.7, y: 0.7, anchor: .leading)
  175. if let warnColor {
  176. scaledText.foregroundStyle(warnColor)
  177. } else {
  178. scaledText
  179. }
  180. case .compact:
  181. text.scaleEffect(x: 0.8, y: 0.8, anchor: .leading).padding(.trailing, -3)
  182. case .expanded:
  183. text.scaleEffect(x: 0.7, y: 0.7, anchor: .leading).padding(.trailing, -5)
  184. }
  185. }
  186. }
  187. .foregroundStyle(context.isStale ? Color.primary.opacity(0.5) : Color.primary)
  188. return (stack, characters)
  189. }
  190. @ViewBuilder func bobble(context: ActivityViewContext<LiveActivityAttributes>) -> some View {
  191. let gradient = LinearGradient(colors: [
  192. Color(red: 0.6235294118, green: 0.4235294118, blue: 0.9803921569),
  193. Color(red: 0.4862745098, green: 0.5450980392, blue: 0.9529411765),
  194. Color(red: 0.3411764706, green: 0.6666666667, blue: 0.9254901961),
  195. Color(red: 0.262745098, green: 0.7333333333, blue: 0.9137254902)
  196. ], startPoint: .leading, endPoint: .trailing)
  197. if !context.isStale {
  198. Image(systemName: "arrow.right")
  199. .font(.title)
  200. .rotationEffect(.degrees(context.state.rotationDegrees))
  201. .foregroundStyle(gradient)
  202. }
  203. }
  204. @ViewBuilder func chart(context: ActivityViewContext<LiveActivityAttributes>) -> some View {
  205. if context.isStale {
  206. Text("No data available")
  207. } else {
  208. // determine scale
  209. let min = (context.state.chart.min() ?? 40 * (context.state.unit == " mmol/L" ? 0.0555 : 1)) - 20 *
  210. (context.state.unit == " mmol/L" ? 0.0555 : 1)
  211. let max = (context.state.chart.max() ?? 270 * (context.state.unit == " mmol/L" ? 0.0555 : 1)) + 50 *
  212. (context.state.unit == " mmol/L" ? 0.0555 : 1)
  213. Chart {
  214. RuleMark(y: .value("high", context.state.highGlucose))
  215. .lineStyle(.init(lineWidth: 0.5, dash: [5]))
  216. RuleMark(y: .value("low", context.state.lowGlucose))
  217. .lineStyle(.init(lineWidth: 0.5, dash: [5]))
  218. ForEach(context.state.chart.indices, id: \.self) { index in
  219. let currentValue = context.state.chart[index]
  220. if currentValue > context.state.highGlucose {
  221. PointMark(
  222. x: .value("Time", context.state.chartDate[index] ?? Date()),
  223. y: .value("Value", currentValue)
  224. ).foregroundStyle(Color.orange.gradient).symbolSize(15)
  225. } else if currentValue < context.state.lowGlucose {
  226. PointMark(
  227. x: .value("Time", context.state.chartDate[index] ?? Date()),
  228. y: .value("Value", currentValue)
  229. ).foregroundStyle(Color.red.gradient).symbolSize(15)
  230. } else {
  231. PointMark(
  232. x: .value("Time", context.state.chartDate[index] ?? Date()),
  233. y: .value("Value", currentValue)
  234. ).foregroundStyle(Color.green.gradient).symbolSize(15)
  235. }
  236. }
  237. }
  238. .chartYAxis {
  239. AxisMarks(position: .trailing) { _ in
  240. AxisGridLine(stroke: .init(lineWidth: 0.2, dash: [2, 3])).foregroundStyle(Color.white)
  241. AxisValueLabel().foregroundStyle(.secondary).font(.footnote)
  242. }
  243. }
  244. .chartYScale(domain: min ... max)
  245. .chartXAxis {
  246. AxisMarks(position: .automatic) { _ in
  247. AxisGridLine(stroke: .init(lineWidth: 0.2, dash: [2, 3])).foregroundStyle(Color.white)
  248. }
  249. }
  250. }
  251. }
  252. var body: some WidgetConfiguration {
  253. ActivityConfiguration(for: LiveActivityAttributes.self) { context in
  254. // Lock screen/banner UI goes here
  255. if context.state.lockScreenView == "Simple" {
  256. HStack(spacing: 3) {
  257. bgAndTrend(context: context, size: .expanded).0.font(.title)
  258. Spacer()
  259. VStack(alignment: .trailing, spacing: 5) {
  260. changeLabel(context: context).font(.title3)
  261. updatedLabel(context: context).font(.caption).foregroundStyle(.primary.opacity(0.7))
  262. }
  263. }
  264. .privacySensitive()
  265. .padding(.all, 15)
  266. // Semantic BackgroundStyle and Color values work here. They adapt to the given interface style (light mode, dark mode)
  267. // Semantic UIColors do NOT (as of iOS 17.1.1). Like UIColor.systemBackgroundColor (it does not adapt to changes of the interface style)
  268. // The colorScheme environment varaible that is usually used to detect dark mode does NOT work here (it reports false values)
  269. .foregroundStyle(Color.primary)
  270. .background(BackgroundStyle.background.opacity(0.4))
  271. .activityBackgroundTint(Color.clear)
  272. } else {
  273. HStack(spacing: 12) {
  274. chart(context: context).frame(maxWidth: UIScreen.main.bounds.width / 1.8)
  275. VStack(alignment: .leading) {
  276. Spacer()
  277. bgLabel(context: context)
  278. HStack {
  279. changeLabel(context: context)
  280. bobble(context: context)
  281. }
  282. mealLabel(context: context).padding(.bottom, 8)
  283. updatedLabel(context: context).padding(.bottom, 10)
  284. }
  285. }
  286. .privacySensitive()
  287. .padding(.all, 14)
  288. .imageScale(.small)
  289. .foregroundColor(Color.white)
  290. .activityBackgroundTint(Color.black.opacity(0.8))
  291. }
  292. } dynamicIsland: { context in
  293. DynamicIsland {
  294. // Expanded UI goes here. Compose the expanded UI through
  295. // various regions, like leading/trailing/center/bottom
  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.lockScreenView == "Simple" {
  304. Group {
  305. updatedLabel(context: context).font(.caption).foregroundStyle(Color.secondary)
  306. }
  307. .frame(
  308. maxHeight: .infinity,
  309. alignment: .bottom
  310. )
  311. } else {
  312. chart(context: context)
  313. }
  314. }
  315. DynamicIslandExpandedRegion(.center) {
  316. if context.state.lockScreenView == "Detailed" {
  317. updatedLabel(context: context).font(.caption).foregroundStyle(Color.secondary)
  318. }
  319. }
  320. } compactLeading: {
  321. bgAndTrend(context: context, size: .compact).0.padding(.leading, 4)
  322. } compactTrailing: {
  323. changeLabel(context: context).padding(.trailing, 4)
  324. } minimal: {
  325. let (_label, characterCount) = bgAndTrend(context: context, size: .minimal)
  326. let label = _label.padding(.leading, 7).padding(.trailing, 3)
  327. if characterCount < 4 {
  328. label
  329. } else if characterCount < 5 {
  330. label.fontWidth(.condensed)
  331. } else {
  332. label.fontWidth(.compressed)
  333. }
  334. }
  335. .widgetURL(URL(string: "freeaps-x://"))
  336. .keylineTint(Color.purple)
  337. .contentMargins(.horizontal, 0, for: .minimal)
  338. .contentMargins(.trailing, 0, for: .compactLeading)
  339. .contentMargins(.leading, 0, for: .compactTrailing)
  340. }
  341. }
  342. }