63 lines
1.7 KiB
Swift
63 lines
1.7 KiB
Swift
import WidgetKit
|
|
import SwiftUI
|
|
|
|
struct Provider: TimelineProvider {
|
|
func placeholder(in context: Context) -> SimpleEntry {
|
|
SimpleEntry(date: Date())
|
|
}
|
|
|
|
func getSnapshot(in context: Context, completion: @escaping (SimpleEntry) -> Void) {
|
|
let entry = SimpleEntry(date: Date())
|
|
completion(entry)
|
|
}
|
|
|
|
func getTimeline(in context: Context, completion: @escaping (Timeline<SimpleEntry>) -> Void) {
|
|
var entries: [SimpleEntry] = []
|
|
let currentDate = Date()
|
|
for hourOffset in 0 ..< 5 {
|
|
let entryDate = Calendar.current.date(byAdding: .hour, value: hourOffset, to: currentDate)!
|
|
let entry = SimpleEntry(date: entryDate)
|
|
entries.append(entry)
|
|
}
|
|
let timeline = Timeline(entries: entries, policy: .atEnd)
|
|
completion(timeline)
|
|
}
|
|
}
|
|
|
|
struct SimpleEntry: TimelineEntry {
|
|
let date: Date
|
|
}
|
|
|
|
struct PlatformWidgetEntryView: View {
|
|
var entry: Provider.Entry
|
|
|
|
var body: some View {
|
|
VStack {
|
|
Text("Platform")
|
|
.font(.headline)
|
|
Text(entry.date, style: .time)
|
|
.font(.caption)
|
|
}
|
|
}
|
|
}
|
|
|
|
struct PlatformWidget: Widget {
|
|
let kind: String = "PlatformWidget"
|
|
|
|
var body: some WidgetConfiguration {
|
|
StaticConfiguration(kind: kind, provider: Provider()) { entry in
|
|
PlatformWidgetEntryView(entry: entry)
|
|
.containerBackground(.fill.tertiary, for: .widget)
|
|
}
|
|
.configurationDisplayName("Platform")
|
|
.description("Platform widget.")
|
|
.supportedFamilies([.systemSmall, .systemMedium])
|
|
}
|
|
}
|
|
|
|
#Preview(as: .systemSmall) {
|
|
PlatformWidget()
|
|
} timeline: {
|
|
SimpleEntry(date: .now)
|
|
}
|