LiveActivity.swift 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. import ActivityKit
  2. import SwiftUI
  3. import WidgetKit
  4. struct LiveActivity: Widget {
  5. var body: some WidgetConfiguration {
  6. let configuration = ActivityConfiguration(for: LiveActivityAttributes.self) { context in
  7. LiveActivityView(context: context)
  8. .addIsWatchOS()
  9. } dynamicIsland: { context in
  10. let hasStaticColorScheme = context.state.glucoseColorScheme == "staticColor"
  11. var glucoseColor: Color {
  12. let state = context.state
  13. let isMgdL = state.unit == "mg/dL"
  14. // TODO: workaround for now: set low value to 55, to have dynamic color shades between 55 and user-set low (approx. 70); same for high glucose
  15. let hardCodedLow = isMgdL ? Decimal(55) : 55.asMmolL
  16. let hardCodedHigh = isMgdL ? Decimal(220) : 220.asMmolL
  17. return Color.getDynamicGlucoseColor(
  18. glucoseValue: Decimal(string: state.bg) ?? 100,
  19. highGlucoseColorValue: !hasStaticColorScheme
  20. ? hardCodedHigh : state.highGlucose,
  21. lowGlucoseColorValue: !hasStaticColorScheme
  22. ? hardCodedLow : state.lowGlucose,
  23. targetGlucose: isMgdL ? state.target : state.target.asMmolL,
  24. glucoseColorScheme: state.glucoseColorScheme
  25. )
  26. }
  27. return DynamicIsland {
  28. DynamicIslandExpandedRegion(.leading) {
  29. LiveActivityExpandedLeadingView(
  30. context: context,
  31. glucoseColor: glucoseColor
  32. )
  33. }
  34. DynamicIslandExpandedRegion(.trailing) {
  35. LiveActivityExpandedTrailingView(
  36. context: context,
  37. glucoseColor: hasStaticColorScheme ? .primary : glucoseColor
  38. )
  39. }
  40. DynamicIslandExpandedRegion(.bottom) {
  41. LiveActivityExpandedBottomView(context: context)
  42. }
  43. DynamicIslandExpandedRegion(.center) {
  44. LiveActivityExpandedCenterView(context: context)
  45. }
  46. } compactLeading: {
  47. LiveActivityCompactLeadingView(
  48. context: context,
  49. glucoseColor: glucoseColor
  50. )
  51. } compactTrailing: {
  52. LiveActivityCompactTrailingView(
  53. context: context,
  54. glucoseColor: hasStaticColorScheme ? .primary : glucoseColor
  55. )
  56. } minimal: {
  57. LiveActivityMinimalView(
  58. context: context,
  59. glucoseColor: glucoseColor
  60. )
  61. }
  62. .widgetURL(URL(string: "Trio://"))
  63. .keylineTint(glucoseColor)
  64. .contentMargins(.horizontal, 0, for: .minimal)
  65. .contentMargins(.trailing, 0, for: .compactLeading)
  66. .contentMargins(.leading, 0, for: .compactTrailing)
  67. }
  68. if #available(iOS 18.0, *) {
  69. return configuration.supplementalActivityFamilies([.small])
  70. } else {
  71. return configuration
  72. }
  73. }
  74. }
  75. // Mock structure to replace GlucoseData
  76. struct MockGlucoseData {
  77. var glucose: Int
  78. var date: Date
  79. var direction: String? // You can refine this based on your expected data
  80. }
  81. private extension LiveActivityAttributes {
  82. static var preview: LiveActivityAttributes {
  83. LiveActivityAttributes(startDate: Date())
  84. }
  85. }
  86. private extension LiveActivityAttributes.ContentState {
  87. static var chartData: [MockGlucoseData] = [
  88. MockGlucoseData(
  89. glucose: 120,
  90. date: Date().addingTimeInterval(-600),
  91. direction: "flat"
  92. ),
  93. MockGlucoseData(
  94. glucose: 125,
  95. date: Date().addingTimeInterval(-300),
  96. direction: "flat"
  97. ),
  98. MockGlucoseData(glucose: 130, date: Date(), direction: "flat")
  99. ]
  100. static var detailedViewState =
  101. LiveActivityAttributes.ContentAdditionalState(
  102. chart: chartData.map {
  103. LiveActivityAttributes.ChartItem(value: Decimal($0.glucose), date: $0.date)
  104. },
  105. rotationDegrees: 0,
  106. cob: 20,
  107. iob: 1.5,
  108. tdd: 43.21,
  109. isOverrideActive: false,
  110. overrideName: "Exercise",
  111. overrideDate: Date().addingTimeInterval(-3600),
  112. overrideDuration: 120,
  113. overrideTarget: 150,
  114. isTempTargetActive: false,
  115. tempTargetName: "Temp Target",
  116. tempTargetDate: Date().addingTimeInterval(-1800),
  117. tempTargetDuration: 60,
  118. tempTargetTarget: 120,
  119. widgetItems: LiveActivityAttributes.LiveActivityItem.defaultItems,
  120. minForecast: [],
  121. maxForecast: []
  122. )
  123. // 0 is the widest digit. Use this to get an upper bound on text width.
  124. // 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
  125. static var testWide: LiveActivityAttributes.ContentState {
  126. LiveActivityAttributes.ContentState(
  127. unit: "mg/dL",
  128. bg: "00.0",
  129. direction: "→",
  130. change: "+0.0",
  131. date: Date(),
  132. highGlucose: 180,
  133. lowGlucose: 70,
  134. target: 100,
  135. glucoseColorScheme: "staticColor",
  136. useDetailedViewIOS: false,
  137. useDetailedViewWatchOS: false,
  138. detailedViewState: detailedViewState,
  139. isInitialState: false
  140. )
  141. }
  142. static var testVeryWide: LiveActivityAttributes.ContentState {
  143. LiveActivityAttributes.ContentState(
  144. unit: "mg/dL",
  145. bg: "00.0",
  146. direction: "↑↑",
  147. change: "+0.0",
  148. date: Date(),
  149. highGlucose: 180,
  150. lowGlucose: 70,
  151. target: 100,
  152. glucoseColorScheme: "staticColor",
  153. useDetailedViewIOS: false,
  154. useDetailedViewWatchOS: false,
  155. detailedViewState: detailedViewState,
  156. isInitialState: false
  157. )
  158. }
  159. static var testSuperWide: LiveActivityAttributes.ContentState {
  160. LiveActivityAttributes.ContentState(
  161. unit: "mg/dL",
  162. bg: "00.0",
  163. direction: "↑↑↑",
  164. change: "+0.0",
  165. date: Date(),
  166. highGlucose: 180,
  167. lowGlucose: 70,
  168. target: 100,
  169. glucoseColorScheme: "staticColor",
  170. useDetailedViewIOS: false,
  171. useDetailedViewWatchOS: false,
  172. detailedViewState: detailedViewState,
  173. isInitialState: false
  174. )
  175. }
  176. // 2 characters for BG, 1 character for change is the minimum that will be shown
  177. static var testNarrow: LiveActivityAttributes.ContentState {
  178. LiveActivityAttributes.ContentState(
  179. unit: "mg/dL",
  180. bg: "00",
  181. direction: "↑",
  182. change: "+0",
  183. date: Date(),
  184. highGlucose: 180,
  185. lowGlucose: 70,
  186. target: 100,
  187. glucoseColorScheme: "staticColor",
  188. useDetailedViewIOS: false,
  189. useDetailedViewWatchOS: false,
  190. detailedViewState: detailedViewState,
  191. isInitialState: false
  192. )
  193. }
  194. static var testMedium: LiveActivityAttributes.ContentState {
  195. LiveActivityAttributes.ContentState(
  196. unit: "mg/dL",
  197. bg: "000",
  198. direction: "↗︎",
  199. change: "+00",
  200. date: Date(),
  201. highGlucose: 180,
  202. lowGlucose: 70,
  203. target: 100,
  204. glucoseColorScheme: "staticColor",
  205. useDetailedViewIOS: false,
  206. useDetailedViewWatchOS: false,
  207. detailedViewState: detailedViewState,
  208. isInitialState: false
  209. )
  210. }
  211. static var testExpired: LiveActivityAttributes.ContentState {
  212. LiveActivityAttributes.ContentState(
  213. unit: "mg/dL",
  214. bg: "--",
  215. direction: nil,
  216. change: "--",
  217. date: Date().addingTimeInterval(-60 * 60),
  218. highGlucose: 180,
  219. lowGlucose: 70,
  220. target: 100,
  221. glucoseColorScheme: "staticColor",
  222. useDetailedViewIOS: false,
  223. useDetailedViewWatchOS: false,
  224. detailedViewState: detailedViewState,
  225. isInitialState: false
  226. )
  227. }
  228. static var testWideDetailed: LiveActivityAttributes.ContentState
  229. {
  230. LiveActivityAttributes.ContentState(
  231. unit: "mg/dL",
  232. bg: "00.0",
  233. direction: "→",
  234. change: "+0.0",
  235. date: Date(),
  236. highGlucose: 180,
  237. lowGlucose: 70,
  238. target: 100,
  239. glucoseColorScheme: "staticColor",
  240. useDetailedViewIOS: true,
  241. useDetailedViewWatchOS: true,
  242. detailedViewState: detailedViewState,
  243. isInitialState: false
  244. )
  245. }
  246. static var testVeryWideDetailed:
  247. LiveActivityAttributes.ContentState
  248. {
  249. LiveActivityAttributes.ContentState(
  250. unit: "mg/dL",
  251. bg: "00.0",
  252. direction: "↑↑",
  253. change: "+0.0",
  254. date: Date(),
  255. highGlucose: 180,
  256. lowGlucose: 70,
  257. target: 100,
  258. glucoseColorScheme: "staticColor",
  259. useDetailedViewIOS: true,
  260. useDetailedViewWatchOS: true,
  261. detailedViewState: detailedViewState,
  262. isInitialState: false
  263. )
  264. }
  265. static var testSuperWideDetailed:
  266. LiveActivityAttributes.ContentState
  267. {
  268. LiveActivityAttributes.ContentState(
  269. unit: "mg/dL",
  270. bg: "00.0",
  271. direction: "↑↑↑",
  272. change: "+0.0",
  273. date: Date(),
  274. highGlucose: 180,
  275. lowGlucose: 70,
  276. target: 100,
  277. glucoseColorScheme: "staticColor",
  278. useDetailedViewIOS: true,
  279. useDetailedViewWatchOS: true,
  280. detailedViewState: detailedViewState,
  281. isInitialState: false
  282. )
  283. }
  284. // 2 characters for BG, 1 character for change is the minimum that will be shown
  285. static var testNarrowDetailed:
  286. LiveActivityAttributes.ContentState
  287. {
  288. LiveActivityAttributes.ContentState(
  289. unit: "mg/dL",
  290. bg: "00",
  291. direction: "↑",
  292. change: "+0",
  293. date: Date(),
  294. highGlucose: 180,
  295. lowGlucose: 70,
  296. target: 100,
  297. glucoseColorScheme: "staticColor",
  298. useDetailedViewIOS: true,
  299. useDetailedViewWatchOS: true,
  300. detailedViewState: detailedViewState,
  301. isInitialState: false
  302. )
  303. }
  304. static var testMediumDetailed:
  305. LiveActivityAttributes.ContentState
  306. {
  307. LiveActivityAttributes.ContentState(
  308. unit: "mg/dL",
  309. bg: "000",
  310. direction: "↗︎",
  311. change: "+00",
  312. date: Date(),
  313. highGlucose: 180,
  314. lowGlucose: 70,
  315. target: 100,
  316. glucoseColorScheme: "staticColor",
  317. useDetailedViewIOS: true,
  318. useDetailedViewWatchOS: true,
  319. detailedViewState: detailedViewState,
  320. isInitialState: false
  321. )
  322. }
  323. static var testExpiredDetailed:
  324. LiveActivityAttributes.ContentState
  325. {
  326. LiveActivityAttributes.ContentState(
  327. unit: "mg/dL",
  328. bg: "--",
  329. direction: nil,
  330. change: "--",
  331. date: Date().addingTimeInterval(-60 * 60),
  332. highGlucose: 180,
  333. lowGlucose: 70,
  334. target: 100,
  335. glucoseColorScheme: "staticColor",
  336. useDetailedViewIOS: true,
  337. useDetailedViewWatchOS: true,
  338. detailedViewState: detailedViewState,
  339. isInitialState: false
  340. )
  341. }
  342. }
  343. @available(iOS 17.0, iOSApplicationExtension 17.0, *)
  344. #Preview("Simple", as: .content, using: LiveActivityAttributes.preview) {
  345. LiveActivity()
  346. } contentStates: {
  347. LiveActivityAttributes.ContentState.testSuperWide
  348. LiveActivityAttributes.ContentState.testVeryWide
  349. LiveActivityAttributes.ContentState.testWide
  350. LiveActivityAttributes.ContentState.testMedium
  351. LiveActivityAttributes.ContentState.testNarrow
  352. LiveActivityAttributes.ContentState.testExpired
  353. }
  354. @available(iOS 17.0, iOSApplicationExtension 17.0, *)
  355. #Preview("Detailed", as: .content, using: LiveActivityAttributes.preview) {
  356. LiveActivity()
  357. } contentStates: {
  358. LiveActivityAttributes.ContentState.testSuperWideDetailed
  359. LiveActivityAttributes.ContentState.testVeryWideDetailed
  360. LiveActivityAttributes.ContentState.testWideDetailed
  361. LiveActivityAttributes.ContentState.testMediumDetailed
  362. LiveActivityAttributes.ContentState.testNarrowDetailed
  363. LiveActivityAttributes.ContentState.testExpiredDetailed
  364. }