LiveActivityView.swift 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 FirebaseMessaging
  16. import SampleLiveActivityExtension
  17. import SwiftUI
  18. struct LiveActivityView: View {
  19. @State var activityTokenDict = [String: String]()
  20. var body: some View {
  21. VStack {
  22. Button("Refresh List") {
  23. Task {
  24. refreshAcitivtyList()
  25. }
  26. }.padding(10)
  27. Button("Cancel all running Activities") {
  28. cancelAllRunningActivities()
  29. }.padding(10)
  30. Button("Start Live Activity with push") {
  31. startLiveActivity(supportPush: true)
  32. }.padding(10)
  33. Button("Start Live Activity without push") {
  34. startLiveActivity(supportPush: false)
  35. }.padding(10)
  36. List {
  37. ForEach(activityTokenDict.keys.sorted(), id: \.self) { key in
  38. VStack {
  39. Text("ActivityId: " + key).frame(maxWidth: .infinity, alignment: .leading)
  40. Button("Copy") {
  41. UIPasteboard.general.string = activityTokenDict[key]!
  42. }.frame(alignment: .trailing)
  43. Text("Push Token: " + activityTokenDict[key]!)
  44. .frame(maxWidth: .infinity, alignment: .leading)
  45. }.padding(10)
  46. }
  47. }
  48. }.onAppear {
  49. refreshAcitivtyList()
  50. }
  51. }
  52. private func refreshAcitivtyList() {
  53. Task {
  54. activityTokenDict.removeAll()
  55. let ptsToken = Activity<SampleLiveActivityAttributes>.pushToStartToken
  56. if ptsToken != nil {
  57. let ptsTokenString = getFormatedToken(token: ptsToken!)
  58. activityTokenDict["PTS"] = ptsTokenString
  59. } else {
  60. activityTokenDict["PTS"] = "Not available yet.!"
  61. Task {
  62. for await ptsToken in Activity<SampleLiveActivityAttributes>
  63. .pushToStartTokenUpdates {
  64. let ptsTokenString = getFormatedToken(token: ptsToken)
  65. activityTokenDict["PTS"] = ptsTokenString
  66. refreshAcitivtyList()
  67. }
  68. }
  69. }
  70. let activities = Activity<SampleLiveActivityAttributes>.activities
  71. for activity in activities {
  72. if activity.pushToken != nil {
  73. let activityToken = getFormatedToken(token: activity.pushToken!)
  74. activityTokenDict[activity.id] = activityToken
  75. } else {
  76. activityTokenDict[activity.id] = "Not available yet!"
  77. }
  78. }
  79. }
  80. }
  81. func getFormatedToken(token: Data) -> String {
  82. return token.reduce("") {
  83. $0 + String(format: "%02x", $1)
  84. }
  85. }
  86. private func startLiveActivity(supportPush: Bool) {
  87. let lAttributes = SampleLiveActivityAttributes(lvInstanceNumber: Date()
  88. .timeIntervalSince1970)
  89. let initialState = SampleLiveActivityAttributes.ContentState(status: .started)
  90. let content = ActivityContent(state: initialState, staleDate: nil, relevanceScore: 1.0)
  91. if supportPush {
  92. let activity = try? Activity.request(
  93. attributes: lAttributes,
  94. content: content,
  95. pushType: .token
  96. )
  97. if activity != nil {
  98. Task {
  99. for await pushToken in activity!.pushTokenUpdates {
  100. let activityToken = getFormatedToken(token: pushToken)
  101. activityTokenDict[activity!.id] = activityToken
  102. refreshAcitivtyList()
  103. }
  104. }
  105. }
  106. } else {
  107. let activity = try? Activity.request(
  108. attributes: lAttributes,
  109. content: content,
  110. pushType: .none
  111. )
  112. }
  113. refreshAcitivtyList()
  114. }
  115. func cancelAllRunningActivities() {
  116. Task {
  117. for activity in Activity<SampleLiveActivityAttributes>.activities {
  118. let initialContentState = SampleLiveActivityAttributes
  119. .ContentState(status: .started)
  120. await activity.end(
  121. ActivityContent(state: initialContentState, staleDate: Date()),
  122. dismissalPolicy: .immediate
  123. )
  124. }
  125. refreshAcitivtyList()
  126. }
  127. }
  128. }
  129. #Preview {
  130. LiveActivityView()
  131. }