2023-06-22 13:01:22 -06:00
|
|
|
//
|
|
|
|
// Wonderous_Widget.swift
|
|
|
|
// Wonderous Widget
|
|
|
|
//
|
2023-10-11 13:40:26 -06:00
|
|
|
// Created by Shawn on 2023-10-06.
|
2023-06-22 13:01:22 -06:00
|
|
|
//
|
|
|
|
|
|
|
|
import WidgetKit
|
|
|
|
import SwiftUI
|
2023-10-11 13:40:26 -06:00
|
|
|
import Intents
|
2023-06-22 13:01:22 -06:00
|
|
|
|
2023-10-11 13:40:26 -06:00
|
|
|
struct Provider: IntentTimelineProvider {
|
2023-06-22 13:01:22 -06:00
|
|
|
func placeholder(in context: Context) -> SimpleEntry {
|
2023-10-11 13:40:26 -06:00
|
|
|
SimpleEntry(date: Date(), configuration: ConfigurationIntent())
|
2023-06-22 13:01:22 -06:00
|
|
|
}
|
|
|
|
|
2023-10-11 13:40:26 -06:00
|
|
|
func getSnapshot(for configuration: ConfigurationIntent, in context: Context, completion: @escaping (SimpleEntry) -> ()) {
|
|
|
|
let entry = SimpleEntry(date: Date(), configuration: configuration)
|
2023-06-22 13:01:22 -06:00
|
|
|
completion(entry)
|
|
|
|
}
|
|
|
|
|
2023-10-11 13:40:26 -06:00
|
|
|
func getTimeline(for configuration: ConfigurationIntent, in context: Context, completion: @escaping (Timeline<Entry>) -> ()) {
|
2023-06-22 13:01:22 -06:00
|
|
|
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)!
|
2023-10-11 13:40:26 -06:00
|
|
|
let entry = SimpleEntry(date: entryDate, configuration: configuration)
|
2023-06-22 13:01:22 -06:00
|
|
|
entries.append(entry)
|
|
|
|
}
|
|
|
|
|
|
|
|
let timeline = Timeline(entries: entries, policy: .atEnd)
|
|
|
|
completion(timeline)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct SimpleEntry: TimelineEntry {
|
|
|
|
let date: Date
|
2023-10-11 13:40:26 -06:00
|
|
|
let configuration: ConfigurationIntent
|
2023-06-22 13:01:22 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
struct Wonderous_WidgetEntryView : View {
|
|
|
|
var entry: Provider.Entry
|
|
|
|
|
|
|
|
var body: some View {
|
|
|
|
Text(entry.date, style: .time)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Wonderous_Widget: Widget {
|
|
|
|
let kind: String = "Wonderous_Widget"
|
|
|
|
|
|
|
|
var body: some WidgetConfiguration {
|
2023-10-11 13:40:26 -06:00
|
|
|
IntentConfiguration(kind: kind, intent: ConfigurationIntent.self, provider: Provider()) { entry in
|
2023-06-22 13:01:22 -06:00
|
|
|
Wonderous_WidgetEntryView(entry: entry)
|
|
|
|
}
|
|
|
|
.configurationDisplayName("My Widget")
|
|
|
|
.description("This is an example widget.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Wonderous_Widget_Previews: PreviewProvider {
|
|
|
|
static var previews: some View {
|
2023-10-11 13:40:26 -06:00
|
|
|
Wonderous_WidgetEntryView(entry: SimpleEntry(date: Date(), configuration: ConfigurationIntent()))
|
2023-06-22 13:01:22 -06:00
|
|
|
.previewContext(WidgetPreviewContext(family: .systemSmall))
|
|
|
|
}
|
|
|
|
}
|