| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- // Copyright 2020 Google LLC
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- import SwiftUI
- import WidgetKit
- struct Provider: AppIntentTimelineProvider {
- func placeholder(in context: Context) -> SimpleEntry {
- SimpleEntry(date: Date(), configuration: ConfigurationAppIntent())
- }
- func snapshot(for configuration: ConfigurationAppIntent,
- in context: Context) async -> SimpleEntry {
- SimpleEntry(date: Date(), configuration: configuration)
- }
- func timeline(for configuration: ConfigurationAppIntent,
- in context: Context) async -> Timeline<SimpleEntry> {
- var entries: [SimpleEntry] = []
- // Generate a timeline consisting of five entries an hour apart, starting from the current date.
- let currentDate = Date()
- for hourOffset in 0 ..< 5 {
- let entryDate = Calendar.current.date(
- byAdding: .hour,
- value: hourOffset,
- to: currentDate
- )!
- let entry = SimpleEntry(date: entryDate, configuration: configuration)
- entries.append(entry)
- }
- return Timeline(entries: entries, policy: .atEnd)
- }
- }
- struct SimpleEntry: TimelineEntry {
- let date: Date
- let configuration: ConfigurationAppIntent
- }
- struct SampleLiveActivityEntryView: View {
- var entry: Provider.Entry
- var body: some View {
- VStack {
- Text("Time:")
- Text(entry.date, style: .time)
- Text("Favorite Emoji:")
- Text(entry.configuration.favoriteEmoji)
- }
- }
- }
- struct SampleLiveActivity: Widget {
- let kind: String = "SampleLiveActivity"
- var body: some WidgetConfiguration {
- AppIntentConfiguration(
- kind: kind,
- intent: ConfigurationAppIntent.self,
- provider: Provider()
- ) { entry in
- SampleLiveActivityEntryView(entry: entry)
- .containerBackground(.fill.tertiary, for: .widget)
- }
- }
- }
- private extension ConfigurationAppIntent {
- static var smiley: ConfigurationAppIntent {
- let intent = ConfigurationAppIntent()
- intent.favoriteEmoji = "😀"
- return intent
- }
- static var starEyes: ConfigurationAppIntent {
- let intent = ConfigurationAppIntent()
- intent.favoriteEmoji = "🤩"
- return intent
- }
- }
- #Preview(as: .systemSmall) {
- SampleLiveActivity()
- } timeline: {
- SimpleEntry(date: .now, configuration: .smiley)
- SimpleEntry(date: .now, configuration: .starEyes)
- }
|