LiveActivity.swift 13 KB

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