LiveActivity.swift 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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. forecastLines: [],
  123. forecastDisplayType: "cone"
  124. )
  125. // 0 is the widest digit. Use this to get an upper bound on text width.
  126. // 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
  127. static var testWide: LiveActivityAttributes.ContentState {
  128. LiveActivityAttributes.ContentState(
  129. unit: "mg/dL",
  130. bg: "00.0",
  131. direction: "→",
  132. change: "+0.0",
  133. date: Date(),
  134. highGlucose: 180,
  135. lowGlucose: 70,
  136. target: 100,
  137. glucoseColorScheme: "staticColor",
  138. useDetailedViewIOS: false,
  139. useDetailedViewWatchOS: false,
  140. detailedViewState: detailedViewState,
  141. isInitialState: false
  142. )
  143. }
  144. static var testVeryWide: LiveActivityAttributes.ContentState {
  145. LiveActivityAttributes.ContentState(
  146. unit: "mg/dL",
  147. bg: "00.0",
  148. direction: "↑↑",
  149. change: "+0.0",
  150. date: Date(),
  151. highGlucose: 180,
  152. lowGlucose: 70,
  153. target: 100,
  154. glucoseColorScheme: "staticColor",
  155. useDetailedViewIOS: false,
  156. useDetailedViewWatchOS: false,
  157. detailedViewState: detailedViewState,
  158. isInitialState: false
  159. )
  160. }
  161. static var testSuperWide: LiveActivityAttributes.ContentState {
  162. LiveActivityAttributes.ContentState(
  163. unit: "mg/dL",
  164. bg: "00.0",
  165. direction: "↑↑↑",
  166. change: "+0.0",
  167. date: Date(),
  168. highGlucose: 180,
  169. lowGlucose: 70,
  170. target: 100,
  171. glucoseColorScheme: "staticColor",
  172. useDetailedViewIOS: false,
  173. useDetailedViewWatchOS: false,
  174. detailedViewState: detailedViewState,
  175. isInitialState: false
  176. )
  177. }
  178. // 2 characters for BG, 1 character for change is the minimum that will be shown
  179. static var testNarrow: LiveActivityAttributes.ContentState {
  180. LiveActivityAttributes.ContentState(
  181. unit: "mg/dL",
  182. bg: "00",
  183. direction: "↑",
  184. change: "+0",
  185. date: Date(),
  186. highGlucose: 180,
  187. lowGlucose: 70,
  188. target: 100,
  189. glucoseColorScheme: "staticColor",
  190. useDetailedViewIOS: false,
  191. useDetailedViewWatchOS: false,
  192. detailedViewState: detailedViewState,
  193. isInitialState: false
  194. )
  195. }
  196. static var testMedium: LiveActivityAttributes.ContentState {
  197. LiveActivityAttributes.ContentState(
  198. unit: "mg/dL",
  199. bg: "000",
  200. direction: "↗︎",
  201. change: "+00",
  202. date: Date(),
  203. highGlucose: 180,
  204. lowGlucose: 70,
  205. target: 100,
  206. glucoseColorScheme: "staticColor",
  207. useDetailedViewIOS: false,
  208. useDetailedViewWatchOS: false,
  209. detailedViewState: detailedViewState,
  210. isInitialState: false
  211. )
  212. }
  213. static var testExpired: LiveActivityAttributes.ContentState {
  214. LiveActivityAttributes.ContentState(
  215. unit: "mg/dL",
  216. bg: "--",
  217. direction: nil,
  218. change: "--",
  219. date: Date().addingTimeInterval(-60 * 60),
  220. highGlucose: 180,
  221. lowGlucose: 70,
  222. target: 100,
  223. glucoseColorScheme: "staticColor",
  224. useDetailedViewIOS: false,
  225. useDetailedViewWatchOS: false,
  226. detailedViewState: detailedViewState,
  227. isInitialState: false
  228. )
  229. }
  230. static var testWideDetailed: LiveActivityAttributes.ContentState
  231. {
  232. LiveActivityAttributes.ContentState(
  233. unit: "mg/dL",
  234. bg: "00.0",
  235. direction: "→",
  236. change: "+0.0",
  237. date: Date(),
  238. highGlucose: 180,
  239. lowGlucose: 70,
  240. target: 100,
  241. glucoseColorScheme: "staticColor",
  242. useDetailedViewIOS: true,
  243. useDetailedViewWatchOS: true,
  244. detailedViewState: detailedViewState,
  245. isInitialState: false
  246. )
  247. }
  248. static var testVeryWideDetailed:
  249. LiveActivityAttributes.ContentState
  250. {
  251. LiveActivityAttributes.ContentState(
  252. unit: "mg/dL",
  253. bg: "00.0",
  254. direction: "↑↑",
  255. change: "+0.0",
  256. date: Date(),
  257. highGlucose: 180,
  258. lowGlucose: 70,
  259. target: 100,
  260. glucoseColorScheme: "staticColor",
  261. useDetailedViewIOS: true,
  262. useDetailedViewWatchOS: true,
  263. detailedViewState: detailedViewState,
  264. isInitialState: false
  265. )
  266. }
  267. static var testSuperWideDetailed:
  268. LiveActivityAttributes.ContentState
  269. {
  270. LiveActivityAttributes.ContentState(
  271. unit: "mg/dL",
  272. bg: "00.0",
  273. direction: "↑↑↑",
  274. change: "+0.0",
  275. date: Date(),
  276. highGlucose: 180,
  277. lowGlucose: 70,
  278. target: 100,
  279. glucoseColorScheme: "staticColor",
  280. useDetailedViewIOS: true,
  281. useDetailedViewWatchOS: true,
  282. detailedViewState: detailedViewState,
  283. isInitialState: false
  284. )
  285. }
  286. // 2 characters for BG, 1 character for change is the minimum that will be shown
  287. static var testNarrowDetailed:
  288. LiveActivityAttributes.ContentState
  289. {
  290. LiveActivityAttributes.ContentState(
  291. unit: "mg/dL",
  292. bg: "00",
  293. direction: "↑",
  294. change: "+0",
  295. date: Date(),
  296. highGlucose: 180,
  297. lowGlucose: 70,
  298. target: 100,
  299. glucoseColorScheme: "staticColor",
  300. useDetailedViewIOS: true,
  301. useDetailedViewWatchOS: true,
  302. detailedViewState: detailedViewState,
  303. isInitialState: false
  304. )
  305. }
  306. static var testMediumDetailed:
  307. LiveActivityAttributes.ContentState
  308. {
  309. LiveActivityAttributes.ContentState(
  310. unit: "mg/dL",
  311. bg: "000",
  312. direction: "↗︎",
  313. change: "+00",
  314. date: Date(),
  315. highGlucose: 180,
  316. lowGlucose: 70,
  317. target: 100,
  318. glucoseColorScheme: "staticColor",
  319. useDetailedViewIOS: true,
  320. useDetailedViewWatchOS: true,
  321. detailedViewState: detailedViewState,
  322. isInitialState: false
  323. )
  324. }
  325. static var testExpiredDetailed:
  326. LiveActivityAttributes.ContentState
  327. {
  328. LiveActivityAttributes.ContentState(
  329. unit: "mg/dL",
  330. bg: "--",
  331. direction: nil,
  332. change: "--",
  333. date: Date().addingTimeInterval(-60 * 60),
  334. highGlucose: 180,
  335. lowGlucose: 70,
  336. target: 100,
  337. glucoseColorScheme: "staticColor",
  338. useDetailedViewIOS: true,
  339. useDetailedViewWatchOS: true,
  340. detailedViewState: detailedViewState,
  341. isInitialState: false
  342. )
  343. }
  344. }
  345. @available(iOS 17.0, iOSApplicationExtension 17.0, *)
  346. #Preview("Simple", as: .content, using: LiveActivityAttributes.preview) {
  347. LiveActivity()
  348. } contentStates: {
  349. LiveActivityAttributes.ContentState.testSuperWide
  350. LiveActivityAttributes.ContentState.testVeryWide
  351. LiveActivityAttributes.ContentState.testWide
  352. LiveActivityAttributes.ContentState.testMedium
  353. LiveActivityAttributes.ContentState.testNarrow
  354. LiveActivityAttributes.ContentState.testExpired
  355. }
  356. @available(iOS 17.0, iOSApplicationExtension 17.0, *)
  357. #Preview("Detailed", as: .content, using: LiveActivityAttributes.preview) {
  358. LiveActivity()
  359. } contentStates: {
  360. LiveActivityAttributes.ContentState.testSuperWideDetailed
  361. LiveActivityAttributes.ContentState.testVeryWideDetailed
  362. LiveActivityAttributes.ContentState.testWideDetailed
  363. LiveActivityAttributes.ContentState.testMediumDetailed
  364. LiveActivityAttributes.ContentState.testNarrowDetailed
  365. LiveActivityAttributes.ContentState.testExpiredDetailed
  366. }