LiveActivity.swift 13 KB

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