Sesame-iOS/Sesame-Widget/Sesame_Widget.swift
2023-12-20 19:00:20 +01:00

74 lines
1.9 KiB
Swift

import WidgetKit
import SwiftUI
import SFSafeSymbols
struct Provider: TimelineProvider {
func placeholder(in context: Context) -> SimpleEntry {
SimpleEntry(date: Date())
}
func getSnapshot(in context: Context, completion: @escaping (SimpleEntry) -> ()) {
let entry = SimpleEntry(date: Date())
completion(entry)
}
func getTimeline(in context: Context, completion: @escaping (Timeline<Entry>) -> ()) {
let entries = [Entry(date: .now), Entry(date: .distantFuture)]
completion(Timeline(entries: entries, policy: .atEnd))
}
}
struct SimpleEntry: TimelineEntry {
let date: Date
}
struct Sesame_WidgetEntryView : View {
@Environment(\.widgetRenderingMode)
var widgetRenderingMode
@Environment(\.widgetFamily)
var widgetFamily
var entry: Provider.Entry
var body: some View {
switch widgetRenderingMode {
default:
image.unredacted()
}
}
private var image: some View {
Image(systemSymbol: .lock)
.resizable()
.aspectRatio(contentMode: .fit)
.padding()
.fontWeight(.thin)
.widgetURL(URL(string: "sesame:///open")!)
.containerBackground(.green, for: .widget)
.widgetAccentable()
}
}
@main
struct Sesame_Widget: Widget {
let kind: String = "SesameOpen"
var body: some WidgetConfiguration {
StaticConfiguration(kind: kind, provider: Provider()) { entry in
Sesame_WidgetEntryView(entry: entry)
}
.configurationDisplayName("Open")
.description("This widget can be used to unlock the door with a single tap.")
}
}
struct Sesame_Widget_Previews: PreviewProvider {
static var previews: some View {
Sesame_WidgetEntryView(entry: SimpleEntry(date: Date()))
.previewContext(WidgetPreviewContext(family: .accessoryRectangular))
}
}