LiveActivity.swift 5.8 KB

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