LiveActivity.swift 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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("Updated: \(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])
  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. }
  81. }
  82. var body: some WidgetConfiguration {
  83. ActivityConfiguration(for: LiveActivityAttributes.self) { context in
  84. // Lock screen/banner UI goes here
  85. HStack(spacing: 2) {
  86. VStack {
  87. chart(context: context).frame(width: UIScreen.main.bounds.width / 1.8)
  88. }.padding(.vertical, 5).padding(.horizontal, 15)
  89. Divider().foregroundStyle(Color.white)
  90. VStack {
  91. ZStack {
  92. bobble(context: context)
  93. .scaleEffect(0.6)
  94. .clipped()
  95. VStack {
  96. // bgAndTrend(context: context).imageScale(.small).font(.title2)
  97. bgLabel(context: context).font(.title2).imageScale(.small)
  98. changeLabel(context: context).font(.callout)
  99. }
  100. }.padding(.trailing, 10).padding(.top, 5)
  101. updatedLabel(context: context).font(.caption).padding(.bottom).padding(.trailing, 5)
  102. }
  103. }
  104. .privacySensitive()
  105. .imageScale(.small)
  106. .padding(.all, 15)
  107. .background(Color.white.opacity(0.2))
  108. .foregroundColor(Color.white)
  109. .activityBackgroundTint(Color.black.opacity(0.7))
  110. .activitySystemActionForegroundColor(Color.black)
  111. } dynamicIsland: { context in
  112. DynamicIsland {
  113. // Expanded UI goes here. Compose the expanded UI through
  114. // various regions, like leading/trailing/center/bottom
  115. DynamicIslandExpandedRegion(.leading) {
  116. HStack(spacing: 3) {
  117. bgAndTrend(context: context)
  118. }.imageScale(.small).font(.title).padding(.leading, 5)
  119. }
  120. DynamicIslandExpandedRegion(.trailing) {
  121. changeLabel(context: context).font(.title).padding(.trailing, 5)
  122. }
  123. DynamicIslandExpandedRegion(.bottom) {
  124. chart(context: context)
  125. }
  126. DynamicIslandExpandedRegion(.center) {
  127. updatedLabel(context: context).font(.caption).foregroundStyle(Color.secondary)
  128. }
  129. } compactLeading: {
  130. HStack(spacing: 1) {
  131. bgAndTrend(context: context)
  132. }.bold().imageScale(.small).padding(.leading, 5)
  133. } compactTrailing: {
  134. changeLabel(context: context).padding(.trailing, 5)
  135. } minimal: {
  136. bgLabel(context: context).bold()
  137. }
  138. .widgetURL(URL(string: "freeaps-x://"))
  139. .keylineTint(Color.cyan.opacity(0.5))
  140. }
  141. }
  142. }
  143. // private extension LiveActivityAttributes {
  144. // static var preview: LiveActivityAttributes {
  145. // LiveActivityAttributes(startDate: Date())
  146. // }
  147. // }
  148. //
  149. // private extension LiveActivityAttributes.ContentState {
  150. // static var test: LiveActivityAttributes.ContentState {
  151. // LiveActivityAttributes.ContentState(bg: "100", trendSystemImage: "arrow.right", change: "+2", date: Date())
  152. // }
  153. // }
  154. //
  155. // #Preview("Notification", as: .content, using: LiveActivityAttributes.preview) {
  156. // LiveActivity()
  157. // } contentStates: {
  158. // LiveActivityAttributes.ContentState.test
  159. // }