LiveActivity.swift 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import ActivityKit
  2. import Charts
  3. import SwiftUI
  4. import WidgetKit
  5. struct LiveActivity: Widget {
  6. let dateFormatter: DateFormatter = {
  7. var f = DateFormatter()
  8. f.dateStyle = .none
  9. f.timeStyle = .short
  10. return f
  11. }()
  12. func changeLabel(context: ActivityViewContext<LiveActivityAttributes>) -> Text {
  13. if !context.isStale && !context.state.change.isEmpty {
  14. Text(context.state.change)
  15. } else {
  16. Text("--")
  17. }
  18. }
  19. func updatedLabel(context: ActivityViewContext<LiveActivityAttributes>) -> Text {
  20. Text(dateFormatter.string(from: context.state.date))
  21. }
  22. func bgLabel(context: ActivityViewContext<LiveActivityAttributes>) -> Text {
  23. if context.isStale {
  24. Text("--")
  25. } else {
  26. Text(context.state.bg)
  27. }
  28. }
  29. @ViewBuilder func trend(context: ActivityViewContext<LiveActivityAttributes>) -> some View {
  30. if context.isStale {
  31. Text("--")
  32. } else {
  33. if let trendSystemImage = context.state.trendSystemImage {
  34. Image(systemName: trendSystemImage)
  35. }
  36. }
  37. }
  38. @ViewBuilder func bgAndTrend(context: ActivityViewContext<LiveActivityAttributes>) -> some View {
  39. if context.isStale {
  40. Text("--")
  41. } else {
  42. HStack {
  43. Text(context.state.bg).fontWeight(.bold)
  44. if let trendSystemImage = context.state.trendSystemImage {
  45. Image(systemName: trendSystemImage)
  46. }
  47. }
  48. }
  49. }
  50. @ViewBuilder func chart(context: ActivityViewContext<LiveActivityAttributes>) -> some View {
  51. if context.isStale {
  52. Text("--")
  53. } else {
  54. Chart {
  55. ForEach(context.state.chart.indices, id: \.self) { index in
  56. LineMark(
  57. x: .value("Time", context.state.chartDate[index] ?? Date()),
  58. y: .value("Value", context.state.chart[index] ?? 0)
  59. ).foregroundStyle(Color.green.gradient).symbolSize(12)
  60. }
  61. }.chartPlotStyle { plotContent in
  62. plotContent.background(.gray.opacity(0.1))
  63. }
  64. .chartYAxis {
  65. AxisMarks(position: .leading)
  66. }.foregroundStyle(Color.white)
  67. .chartXAxis {
  68. AxisMarks(position: .automatic)
  69. }.foregroundStyle(Color.white)
  70. }
  71. }
  72. var body: some WidgetConfiguration {
  73. ActivityConfiguration(for: LiveActivityAttributes.self) { context in
  74. // Lock screen/banner UI goes here
  75. HStack(spacing: 2) {
  76. VStack {
  77. chart(context: context).frame(width: UIScreen.main.bounds.width / 1.7)
  78. }.padding()
  79. Divider()
  80. VStack {
  81. ZStack {
  82. Circle().fill(Color.green.opacity(0.7))
  83. .frame(width: UIScreen.main.bounds.width / 5, height: UIScreen.main.bounds.height / 5).clipped()
  84. VStack {
  85. bgAndTrend(context: context).imageScale(.small).font(.title2)
  86. changeLabel(context: context).font(.callout)
  87. }
  88. }.padding()
  89. updatedLabel(context: context).font(.caption)
  90. }
  91. }
  92. .privacySensitive()
  93. .imageScale(.small)
  94. .padding(.all, 15)
  95. .background(Color.white.opacity(0.2))
  96. .foregroundColor(Color.white)
  97. .activityBackgroundTint(Color.cyan.opacity(0.3))
  98. .activitySystemActionForegroundColor(Color.black)
  99. } dynamicIsland: { context in
  100. DynamicIsland {
  101. // Expanded UI goes here. Compose the expanded UI through
  102. // various regions, like leading/trailing/center/bottom
  103. DynamicIslandExpandedRegion(.leading) {
  104. HStack(spacing: 3) {
  105. bgAndTrend(context: context)
  106. }.imageScale(.small).font(.title).padding(.leading, 5)
  107. }
  108. DynamicIslandExpandedRegion(.trailing) {
  109. changeLabel(context: context).font(.title).padding(.trailing, 5)
  110. }
  111. DynamicIslandExpandedRegion(.bottom) {
  112. updatedLabel(context: context).font(.caption).foregroundStyle(Color.secondary)
  113. .padding(.bottom, 5)
  114. chart(context: context)
  115. }
  116. } compactLeading: {
  117. HStack(spacing: 1) {
  118. bgAndTrend(context: context)
  119. }.bold().imageScale(.small).padding(.leading, 5)
  120. } compactTrailing: {
  121. changeLabel(context: context).padding(.trailing, 5)
  122. } minimal: {
  123. bgLabel(context: context).bold()
  124. }
  125. .widgetURL(URL(string: "freeaps-x://"))
  126. .keylineTint(Color.cyan.opacity(0.5))
  127. }
  128. }
  129. }
  130. // private extension LiveActivityAttributes {
  131. // static var preview: LiveActivityAttributes {
  132. // LiveActivityAttributes(startDate: Date())
  133. // }
  134. // }
  135. //
  136. // private extension LiveActivityAttributes.ContentState {
  137. // static var test: LiveActivityAttributes.ContentState {
  138. // LiveActivityAttributes.ContentState(bg: "100", trendSystemImage: "arrow.right", change: "+2", date: Date())
  139. // }
  140. // }
  141. //
  142. // #Preview("Notification", as: .content, using: LiveActivityAttributes.preview) {
  143. // LiveActivity()
  144. // } contentStates: {
  145. // LiveActivityAttributes.ContentState.test
  146. // }