SampleLiveActivityLiveActivity.swift 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Copyright 2020 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. import ActivityKit
  15. import SwiftUI
  16. import WidgetKit
  17. struct SampleLiveActivityAttributes: ActivityAttributes {
  18. public struct ContentState: Codable, Hashable {
  19. enum LVStatus: Float, Codable, Hashable {
  20. case started = 0
  21. case inProgress = 1
  22. case completed = 2
  23. var description: String {
  24. switch self {
  25. case .started:
  26. return "Your Live Activity is started!"
  27. case .inProgress:
  28. return "Your Live Activity is in progress!"
  29. case .completed:
  30. return "Your Live activity is completed!"
  31. }
  32. }
  33. }
  34. let status: LVStatus
  35. }
  36. // Fixed non-changing properties about your activity go here!
  37. var lvInstanceNumber: Double
  38. }
  39. struct SampleLiveActivityLiveActivity: Widget {
  40. var body: some WidgetConfiguration {
  41. ActivityConfiguration(for: SampleLiveActivityAttributes.self) { context in
  42. // Lock screen/banner UI goes here
  43. VStack {
  44. SampleLiveActivityView(context: context)
  45. }
  46. .activityBackgroundTint(Color.cyan)
  47. .activitySystemActionForegroundColor(Color.black)
  48. } dynamicIsland: { context in
  49. DynamicIsland {
  50. // Expanded UI goes here. Compose the expanded UI through
  51. // various regions, like leading/trailing/center/bottom
  52. DynamicIslandExpandedRegion(.leading) {
  53. Text("Leading")
  54. }
  55. DynamicIslandExpandedRegion(.trailing) {
  56. Text("Trailing")
  57. }
  58. DynamicIslandExpandedRegion(.bottom) {
  59. Text("Bottom")
  60. // more content
  61. }
  62. } compactLeading: {
  63. Text("L")
  64. } compactTrailing: {
  65. Text("T")
  66. } minimal: {
  67. Text("M")
  68. }
  69. .widgetURL(URL(string: "http://www.apple.com"))
  70. .keylineTint(Color.red)
  71. }
  72. }
  73. }
  74. private extension SampleLiveActivityAttributes {
  75. static var preview: SampleLiveActivityAttributes {
  76. SampleLiveActivityAttributes(lvInstanceNumber: 1)
  77. }
  78. }
  79. private extension SampleLiveActivityAttributes.ContentState {
  80. static var stateStarted: SampleLiveActivityAttributes.ContentState {
  81. SampleLiveActivityAttributes.ContentState(status: LVStatus.started)
  82. }
  83. static var stateInProgress: SampleLiveActivityAttributes.ContentState {
  84. SampleLiveActivityAttributes.ContentState(status: LVStatus.inProgress)
  85. }
  86. static var stateCompleted: SampleLiveActivityAttributes.ContentState {
  87. SampleLiveActivityAttributes.ContentState(status: LVStatus.completed)
  88. }
  89. }
  90. #Preview("Notification", as: .content, using: SampleLiveActivityAttributes.preview) {
  91. SampleLiveActivityLiveActivity()
  92. } contentStates: {
  93. SampleLiveActivityAttributes.ContentState.stateStarted
  94. SampleLiveActivityAttributes.ContentState.stateInProgress
  95. SampleLiveActivityAttributes.ContentState.stateCompleted
  96. }