SampleLiveActivity.swift 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 SwiftUI
  15. import WidgetKit
  16. struct Provider: AppIntentTimelineProvider {
  17. func placeholder(in context: Context) -> SimpleEntry {
  18. SimpleEntry(date: Date(), configuration: ConfigurationAppIntent())
  19. }
  20. func snapshot(for configuration: ConfigurationAppIntent,
  21. in context: Context) async -> SimpleEntry {
  22. SimpleEntry(date: Date(), configuration: configuration)
  23. }
  24. func timeline(for configuration: ConfigurationAppIntent,
  25. in context: Context) async -> Timeline<SimpleEntry> {
  26. var entries: [SimpleEntry] = []
  27. // Generate a timeline consisting of five entries an hour apart, starting from the current date.
  28. let currentDate = Date()
  29. for hourOffset in 0 ..< 5 {
  30. let entryDate = Calendar.current.date(
  31. byAdding: .hour,
  32. value: hourOffset,
  33. to: currentDate
  34. )!
  35. let entry = SimpleEntry(date: entryDate, configuration: configuration)
  36. entries.append(entry)
  37. }
  38. return Timeline(entries: entries, policy: .atEnd)
  39. }
  40. }
  41. struct SimpleEntry: TimelineEntry {
  42. let date: Date
  43. let configuration: ConfigurationAppIntent
  44. }
  45. struct SampleLiveActivityEntryView: View {
  46. var entry: Provider.Entry
  47. var body: some View {
  48. VStack {
  49. Text("Time:")
  50. Text(entry.date, style: .time)
  51. Text("Favorite Emoji:")
  52. Text(entry.configuration.favoriteEmoji)
  53. }
  54. }
  55. }
  56. struct SampleLiveActivity: Widget {
  57. let kind: String = "SampleLiveActivity"
  58. var body: some WidgetConfiguration {
  59. AppIntentConfiguration(
  60. kind: kind,
  61. intent: ConfigurationAppIntent.self,
  62. provider: Provider()
  63. ) { entry in
  64. SampleLiveActivityEntryView(entry: entry)
  65. .containerBackground(.fill.tertiary, for: .widget)
  66. }
  67. }
  68. }
  69. private extension ConfigurationAppIntent {
  70. static var smiley: ConfigurationAppIntent {
  71. let intent = ConfigurationAppIntent()
  72. intent.favoriteEmoji = "😀"
  73. return intent
  74. }
  75. static var starEyes: ConfigurationAppIntent {
  76. let intent = ConfigurationAppIntent()
  77. intent.favoriteEmoji = "🤩"
  78. return intent
  79. }
  80. }
  81. #Preview(as: .systemSmall) {
  82. SampleLiveActivity()
  83. } timeline: {
  84. SimpleEntry(date: .now, configuration: .smiley)
  85. SimpleEntry(date: .now, configuration: .starEyes)
  86. }