LiveActivity.swift 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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).fontWeight(.bold)
  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 bobble(context: ActivityViewContext<LiveActivityAttributes>) -> some View {
  51. @State var angularGradient = AngularGradient(colors: [
  52. Color(red: 0.7215686275, green: 0.3411764706, blue: 1),
  53. Color(red: 0.6235294118, green: 0.4235294118, blue: 0.9803921569),
  54. Color(red: 0.4862745098, green: 0.5450980392, blue: 0.9529411765),
  55. Color(red: 0.3411764706, green: 0.6666666667, blue: 0.9254901961),
  56. Color(red: 0.262745098, green: 0.7333333333, blue: 0.9137254902),
  57. Color(red: 0.7215686275, green: 0.3411764706, blue: 1)
  58. ], center: .center, startAngle: .degrees(270), endAngle: .degrees(-90))
  59. let triangleColor = Color(red: 0.262745098, green: 0.7333333333, blue: 0.9137254902)
  60. WidgetBobble(gradient: angularGradient, color: triangleColor)
  61. .rotationEffect(.degrees(context.state.rotationDegrees))
  62. }
  63. @ViewBuilder func chart(context: ActivityViewContext<LiveActivityAttributes>) -> some View {
  64. if context.isStale {
  65. Text("No data available")
  66. } else {
  67. Chart {
  68. ForEach(context.state.chart.indices, id: \.self) { index in
  69. LineMark(
  70. x: .value("Time", context.state.chartDate[index] ?? Date()),
  71. y: .value("Value", context.state.chart[index] ?? 0)
  72. ).foregroundStyle(Color.green.gradient).symbolSize(12)
  73. }
  74. }.chartPlotStyle { plotContent in
  75. plotContent.background(.cyan.opacity(0.1))
  76. }
  77. .chartYAxis {
  78. AxisMarks(position: .leading)
  79. }
  80. .chartXAxis {
  81. AxisMarks(position: .automatic)
  82. }
  83. }
  84. }
  85. var body: some WidgetConfiguration {
  86. ActivityConfiguration(for: LiveActivityAttributes.self) { context in
  87. // Lock screen/banner UI goes here
  88. HStack(spacing: 2) {
  89. VStack {
  90. chart(context: context).frame(width: UIScreen.main.bounds.width / 1.7)
  91. }.padding(.vertical, 5).padding(.horizontal, 15)
  92. Divider()
  93. VStack {
  94. ZStack {
  95. bobble(context: context)
  96. .scaleEffect(0.6)
  97. .clipped()
  98. VStack {
  99. // bgAndTrend(context: context).imageScale(.small).font(.title2)
  100. bgLabel(context: context).font(.title2).imageScale(.small)
  101. changeLabel(context: context).font(.callout)
  102. }
  103. }.padding(.trailing, 5).padding(.top, 5)
  104. updatedLabel(context: context).font(.caption).padding(.bottom)
  105. }
  106. }
  107. .privacySensitive()
  108. .imageScale(.small)
  109. .padding(.all, 15)
  110. .background(Color.white.opacity(0.2))
  111. .foregroundColor(Color.white)
  112. .activityBackgroundTint(Color.black.opacity(0.7))
  113. .activitySystemActionForegroundColor(Color.black)
  114. } dynamicIsland: { context in
  115. DynamicIsland {
  116. // Expanded UI goes here. Compose the expanded UI through
  117. // various regions, like leading/trailing/center/bottom
  118. DynamicIslandExpandedRegion(.leading) {
  119. HStack(spacing: 3) {
  120. bgAndTrend(context: context)
  121. }.imageScale(.small).font(.title).padding(.leading, 5)
  122. }
  123. DynamicIslandExpandedRegion(.trailing) {
  124. changeLabel(context: context).font(.title).padding(.trailing, 5)
  125. }
  126. DynamicIslandExpandedRegion(.bottom) {
  127. updatedLabel(context: context).font(.caption).foregroundStyle(Color.secondary)
  128. .padding(.bottom, 5)
  129. chart(context: context).frame(height: 70)
  130. }
  131. } compactLeading: {
  132. HStack(spacing: 1) {
  133. bgAndTrend(context: context)
  134. }.bold().imageScale(.small).padding(.leading, 5)
  135. } compactTrailing: {
  136. changeLabel(context: context).padding(.trailing, 5)
  137. } minimal: {
  138. bgLabel(context: context).bold()
  139. }
  140. .widgetURL(URL(string: "freeaps-x://"))
  141. .keylineTint(Color.cyan.opacity(0.5))
  142. }
  143. }
  144. }
  145. // private extension LiveActivityAttributes {
  146. // static var preview: LiveActivityAttributes {
  147. // LiveActivityAttributes(startDate: Date())
  148. // }
  149. // }
  150. //
  151. // private extension LiveActivityAttributes.ContentState {
  152. // static var test: LiveActivityAttributes.ContentState {
  153. // LiveActivityAttributes.ContentState(bg: "100", trendSystemImage: "arrow.right", change: "+2", date: Date())
  154. // }
  155. // }
  156. //
  157. // #Preview("Notification", as: .content, using: LiveActivityAttributes.preview) {
  158. // LiveActivity()
  159. // } contentStates: {
  160. // LiveActivityAttributes.ContentState.test
  161. // }