// // LaunchWidget.swift // LaunchWidget // // Created by Laurent Morvillier on 25/01/2023. // import WidgetKit import SwiftUI import Intents struct Provider: IntentTimelineProvider { func placeholder(in context: Context) -> SimpleEntry { SimpleEntry(id: "", name: "Tea", duration: 4 * 60.0, date: Date(), configuration: ConfigurationIntent()) } func getSnapshot(for configuration: ConfigurationIntent, in context: Context, completion: @escaping (SimpleEntry) -> ()) { let entry = SimpleEntry(id: "", name: "Tea", duration: 4 * 60.0, date: Date(), configuration: configuration) completion(entry) } func getTimeline(for configuration: ConfigurationIntent, in context: Context, completion: @escaping (Timeline) -> ()) { 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(id: "", name: "Tea", duration: 4 * 60.0, date: entryDate, configuration: configuration) entries.append(entry) } let timeline = Timeline(entries: entries, policy: .atEnd) completion(timeline) } } struct SimpleEntry: TimelineEntry { let id: String let name: String? let duration: Double let date: Date let configuration: ConfigurationIntent } struct CountdownWidgetView: View { let id: String let name: String? let duration: Double var body: some View { CountdownView(name: name, duration: duration).widgetURL(URL(string: id)) } } struct LaunchWidgetEntryView : View { @Environment(\.widgetFamily) var family: WidgetFamily var entry: Provider.Entry @ViewBuilder var body: some View { switch family { case .systemSmall: CountdownWidgetView(id: entry.id, name: entry.name, duration: entry.duration) // case .systemMedium: GameStatusWithLastTurnResult(gameStatus) // case .systemLarge: GameStatusWithStatistics(gameStatus) // case .systemExtraLarge: GameStatusWithStatisticsExtraLarge(gameStatus) // case .accessoryCircular: HealthLevelCircular(selectedCharacter) // case .accessoryRectangular: HealthLevelRectangular(selectedCharacter) // case .accessoryInline: HealthLevelInline(selectedCharacter) default: CountdownWidgetView(id: entry.id, name: entry.name, duration: entry.duration) } } } struct LaunchWidget: Widget { let kind: String = "com.staxriver.launch-widget" var body: some WidgetConfiguration { IntentConfiguration(kind: kind, intent: ConfigurationIntent.self, provider: Provider()) { entry in LaunchWidgetEntryView(entry: entry) } .configurationDisplayName("Launch Widget") .description("Select and launch your countdowns") .supportedFamilies([.systemSmall, .accessoryCircular]) } } struct LaunchWidget_Previews: PreviewProvider { static var previews: some View { LaunchWidgetEntryView(entry: SimpleEntry(id: "", name: "Tea", duration: 3 * 60.0, date: Date(), configuration: ConfigurationIntent())) .previewContext(WidgetPreviewContext(family: .systemSmall)) } }