LiveActivity.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. import ActivityKit
  2. import SwiftUI
  3. import WidgetKit
  4. struct LiveActivity: Widget {
  5. var body: some WidgetConfiguration {
  6. ActivityConfiguration(for: LiveActivityAttributes.self) { context in
  7. LiveActivityView(context: context)
  8. } dynamicIsland: { context in
  9. let hasStaticColorScheme = context.state.glucoseColorScheme == "staticColor"
  10. var glucoseColor: Color {
  11. let state = context.state
  12. let isMgdL = state.unit == "mg/dL"
  13. // 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
  14. let hardCodedLow = isMgdL ? Decimal(55) : 55.asMmolL
  15. let hardCodedHigh = isMgdL ? Decimal(220) : 220.asMmolL
  16. return Color.getDynamicGlucoseColor(
  17. glucoseValue: Decimal(string: state.bg) ?? 100,
  18. highGlucoseColorValue: !hasStaticColorScheme ? hardCodedHigh : state.highGlucose,
  19. lowGlucoseColorValue: !hasStaticColorScheme ? hardCodedLow : state.lowGlucose,
  20. targetGlucose: isMgdL ? state.target : state.target.asMmolL,
  21. glucoseColorScheme: state.glucoseColorScheme
  22. )
  23. }
  24. return DynamicIsland {
  25. DynamicIslandExpandedRegion(.leading) {
  26. LiveActivityExpandedLeadingView(context: context, glucoseColor: glucoseColor)
  27. }
  28. DynamicIslandExpandedRegion(.trailing) {
  29. LiveActivityExpandedTrailingView(
  30. context: context,
  31. glucoseColor: hasStaticColorScheme ? .primary : glucoseColor
  32. )
  33. }
  34. DynamicIslandExpandedRegion(.bottom) {
  35. LiveActivityExpandedBottomView(context: context)
  36. }
  37. DynamicIslandExpandedRegion(.center) {
  38. LiveActivityExpandedCenterView(context: context)
  39. }
  40. } compactLeading: {
  41. LiveActivityCompactLeadingView(context: context, glucoseColor: glucoseColor)
  42. } compactTrailing: {
  43. LiveActivityCompactTrailingView(context: context, glucoseColor: hasStaticColorScheme ? .primary : glucoseColor)
  44. } minimal: {
  45. LiveActivityMinimalView(context: context, glucoseColor: glucoseColor)
  46. }
  47. .widgetURL(URL(string: "Trio://"))
  48. .keylineTint(glucoseColor)
  49. .contentMargins(.horizontal, 0, for: .minimal)
  50. .contentMargins(.trailing, 0, for: .compactLeading)
  51. .contentMargins(.leading, 0, for: .compactTrailing)
  52. }
  53. }
  54. }
  55. // Mock structure to replace GlucoseData
  56. struct MockGlucoseData {
  57. var glucose: Int
  58. var date: Date
  59. var direction: String? // You can refine this based on your expected data
  60. }
  61. private extension LiveActivityAttributes {
  62. static var preview: LiveActivityAttributes {
  63. LiveActivityAttributes(startDate: Date())
  64. }
  65. }
  66. private extension LiveActivityAttributes.ContentState {
  67. static var chartData: [MockGlucoseData] = [
  68. MockGlucoseData(glucose: 120, date: Date().addingTimeInterval(-600), direction: "flat"),
  69. MockGlucoseData(glucose: 125, date: Date().addingTimeInterval(-300), direction: "flat"),
  70. MockGlucoseData(glucose: 130, date: Date(), direction: "flat")
  71. ]
  72. static var detailedViewState = LiveActivityAttributes.ContentAdditionalState(
  73. chart: chartData.map { Decimal($0.glucose) },
  74. chartDate: chartData.map(\.date),
  75. rotationDegrees: 0,
  76. cob: 20,
  77. iob: 1.5,
  78. tdd: 43.21,
  79. isOverrideActive: false,
  80. overrideName: "Exercise",
  81. overrideDate: Date().addingTimeInterval(-3600),
  82. overrideDuration: 120,
  83. overrideTarget: 150,
  84. widgetItems: LiveActivityAttributes.LiveActivityItem.defaultItems
  85. )
  86. // 0 is the widest digit. Use this to get an upper bound on text width.
  87. // 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
  88. static var testWide: LiveActivityAttributes.ContentState {
  89. LiveActivityAttributes.ContentState(
  90. unit: "mg/dL",
  91. bg: "00.0",
  92. direction: "→",
  93. change: "+0.0",
  94. date: Date(),
  95. highGlucose: 180,
  96. lowGlucose: 70,
  97. target: 100,
  98. glucoseColorScheme: "staticColor",
  99. detailedViewState: nil,
  100. isInitialState: false
  101. )
  102. }
  103. static var testVeryWide: LiveActivityAttributes.ContentState {
  104. LiveActivityAttributes.ContentState(
  105. unit: "mg/dL",
  106. bg: "00.0",
  107. direction: "↑↑",
  108. change: "+0.0",
  109. date: Date(),
  110. highGlucose: 180,
  111. lowGlucose: 70,
  112. target: 100,
  113. glucoseColorScheme: "staticColor",
  114. detailedViewState: nil,
  115. isInitialState: false
  116. )
  117. }
  118. static var testSuperWide: 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. detailedViewState: nil,
  130. isInitialState: false
  131. )
  132. }
  133. // 2 characters for BG, 1 character for change is the minimum that will be shown
  134. static var testNarrow: LiveActivityAttributes.ContentState {
  135. LiveActivityAttributes.ContentState(
  136. unit: "mg/dL",
  137. bg: "00",
  138. direction: "↑",
  139. change: "+0",
  140. date: Date(),
  141. highGlucose: 180,
  142. lowGlucose: 70,
  143. target: 100,
  144. glucoseColorScheme: "staticColor",
  145. detailedViewState: nil,
  146. isInitialState: false
  147. )
  148. }
  149. static var testMedium: LiveActivityAttributes.ContentState {
  150. LiveActivityAttributes.ContentState(
  151. unit: "mg/dL",
  152. bg: "000",
  153. direction: "↗︎",
  154. change: "+00",
  155. date: Date(),
  156. highGlucose: 180,
  157. lowGlucose: 70,
  158. target: 100,
  159. glucoseColorScheme: "staticColor",
  160. detailedViewState: nil,
  161. isInitialState: false
  162. )
  163. }
  164. static var testExpired: LiveActivityAttributes.ContentState {
  165. LiveActivityAttributes.ContentState(
  166. unit: "mg/dL",
  167. bg: "--",
  168. direction: nil,
  169. change: "--",
  170. date: Date().addingTimeInterval(-60 * 60),
  171. highGlucose: 180,
  172. lowGlucose: 70,
  173. target: 100,
  174. glucoseColorScheme: "staticColor",
  175. detailedViewState: nil,
  176. isInitialState: false
  177. )
  178. }
  179. static var testWideDetailed: LiveActivityAttributes.ContentState {
  180. LiveActivityAttributes.ContentState(
  181. unit: "mg/dL",
  182. bg: "00.0",
  183. direction: "→",
  184. change: "+0.0",
  185. date: Date(),
  186. highGlucose: 180,
  187. lowGlucose: 70,
  188. target: 100,
  189. glucoseColorScheme: "staticColor",
  190. detailedViewState: detailedViewState,
  191. isInitialState: false
  192. )
  193. }
  194. static var testVeryWideDetailed: LiveActivityAttributes.ContentState {
  195. LiveActivityAttributes.ContentState(
  196. unit: "mg/dL",
  197. bg: "00.0",
  198. direction: "↑↑",
  199. change: "+0.0",
  200. date: Date(),
  201. highGlucose: 180,
  202. lowGlucose: 70,
  203. target: 100,
  204. glucoseColorScheme: "staticColor",
  205. detailedViewState: detailedViewState,
  206. isInitialState: false
  207. )
  208. }
  209. static var testSuperWideDetailed: LiveActivityAttributes.ContentState {
  210. LiveActivityAttributes.ContentState(
  211. unit: "mg/dL",
  212. bg: "00.0",
  213. direction: "↑↑↑",
  214. change: "+0.0",
  215. date: Date(),
  216. highGlucose: 180,
  217. lowGlucose: 70,
  218. target: 100,
  219. glucoseColorScheme: "staticColor",
  220. detailedViewState: detailedViewState,
  221. isInitialState: false
  222. )
  223. }
  224. // 2 characters for BG, 1 character for change is the minimum that will be shown
  225. static var testNarrowDetailed: LiveActivityAttributes.ContentState {
  226. LiveActivityAttributes.ContentState(
  227. unit: "mg/dL",
  228. bg: "00",
  229. direction: "↑",
  230. change: "+0",
  231. date: Date(),
  232. highGlucose: 180,
  233. lowGlucose: 70,
  234. target: 100,
  235. glucoseColorScheme: "staticColor",
  236. detailedViewState: detailedViewState,
  237. isInitialState: false
  238. )
  239. }
  240. static var testMediumDetailed: LiveActivityAttributes.ContentState {
  241. LiveActivityAttributes.ContentState(
  242. unit: "mg/dL",
  243. bg: "000",
  244. direction: "↗︎",
  245. change: "+00",
  246. date: Date(),
  247. highGlucose: 180,
  248. lowGlucose: 70,
  249. target: 100,
  250. glucoseColorScheme: "staticColor",
  251. detailedViewState: detailedViewState,
  252. isInitialState: false
  253. )
  254. }
  255. static var testExpiredDetailed: LiveActivityAttributes.ContentState {
  256. LiveActivityAttributes.ContentState(
  257. unit: "mg/dL",
  258. bg: "--",
  259. direction: nil,
  260. change: "--",
  261. date: Date().addingTimeInterval(-60 * 60),
  262. highGlucose: 180,
  263. lowGlucose: 70,
  264. target: 100,
  265. glucoseColorScheme: "staticColor",
  266. detailedViewState: detailedViewState,
  267. isInitialState: false
  268. )
  269. }
  270. }
  271. @available(iOS 17.0, iOSApplicationExtension 17.0, *)
  272. #Preview("Simple", as: .content, using: LiveActivityAttributes.preview) {
  273. LiveActivity()
  274. } contentStates: {
  275. LiveActivityAttributes.ContentState.testSuperWide
  276. LiveActivityAttributes.ContentState.testVeryWide
  277. LiveActivityAttributes.ContentState.testWide
  278. LiveActivityAttributes.ContentState.testMedium
  279. LiveActivityAttributes.ContentState.testNarrow
  280. LiveActivityAttributes.ContentState.testExpired
  281. }
  282. @available(iOS 17.0, iOSApplicationExtension 17.0, *)
  283. #Preview("Detailed", as: .content, using: LiveActivityAttributes.preview) {
  284. LiveActivity()
  285. } contentStates: {
  286. LiveActivityAttributes.ContentState.testSuperWideDetailed
  287. LiveActivityAttributes.ContentState.testVeryWideDetailed
  288. LiveActivityAttributes.ContentState.testWideDetailed
  289. LiveActivityAttributes.ContentState.testMediumDetailed
  290. LiveActivityAttributes.ContentState.testNarrowDetailed
  291. LiveActivityAttributes.ContentState.testExpiredDetailed
  292. }