parent
ed27943a65
commit
42580684c1
@ -1,69 +0,0 @@ |
||||
// |
||||
// MacWidget.swift |
||||
// MacWidget |
||||
// |
||||
// Created by Alan Francis on 11/06/2023. |
||||
// |
||||
|
||||
import WidgetKit |
||||
import SwiftUI |
||||
import Intents |
||||
|
||||
struct Provider: IntentTimelineProvider { |
||||
func placeholder(in context: Context) -> SimpleEntry { |
||||
SimpleEntry(date: Date(), configuration: ConfigurationIntent()) |
||||
} |
||||
|
||||
func getSnapshot(for configuration: ConfigurationIntent, in context: Context, completion: @escaping (SimpleEntry) -> ()) { |
||||
let entry = SimpleEntry(date: Date(), configuration: configuration) |
||||
completion(entry) |
||||
} |
||||
|
||||
func getTimeline(for configuration: ConfigurationIntent, in context: Context, completion: @escaping (Timeline<Entry>) -> ()) { |
||||
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) |
||||
} |
||||
|
||||
let timeline = Timeline(entries: entries, policy: .atEnd) |
||||
completion(timeline) |
||||
} |
||||
} |
||||
|
||||
struct SimpleEntry: TimelineEntry { |
||||
let date: Date |
||||
let configuration: ConfigurationIntent |
||||
} |
||||
|
||||
struct MacWidgetEntryView : View { |
||||
var entry: Provider.Entry |
||||
|
||||
var body: some View { |
||||
Text(entry.date, style: .time) |
||||
} |
||||
} |
||||
|
||||
@main |
||||
struct MacWidget: Widget { |
||||
let kind: String = "MacWidget" |
||||
|
||||
var body: some WidgetConfiguration { |
||||
IntentConfiguration(kind: kind, intent: ConfigurationIntent.self, provider: Provider()) { entry in |
||||
MacWidgetEntryView(entry: entry) |
||||
} |
||||
.configurationDisplayName("My Widget") |
||||
.description("This is an example widget.") |
||||
} |
||||
} |
||||
|
||||
struct MacWidget_Previews: PreviewProvider { |
||||
static var previews: some View { |
||||
MacWidgetEntryView(entry: SimpleEntry(date: Date(), configuration: ConfigurationIntent())) |
||||
.previewContext(WidgetPreviewContext(family: .systemSmall)) |
||||
} |
||||
} |
@ -1,68 +0,0 @@ |
||||
// |
||||
// PhoneWidget.swift |
||||
// PhoneWidget |
||||
// |
||||
// Created by Alan Francis on 11/06/2023. |
||||
// |
||||
|
||||
import WidgetKit |
||||
import SwiftUI |
||||
import Intents |
||||
|
||||
struct Provider: IntentTimelineProvider { |
||||
func placeholder(in context: Context) -> SimpleEntry { |
||||
SimpleEntry(date: Date(), configuration: ConfigurationIntent()) |
||||
} |
||||
|
||||
func getSnapshot(for configuration: ConfigurationIntent, in context: Context, completion: @escaping (SimpleEntry) -> ()) { |
||||
let entry = SimpleEntry(date: Date(), configuration: configuration) |
||||
completion(entry) |
||||
} |
||||
|
||||
func getTimeline(for configuration: ConfigurationIntent, in context: Context, completion: @escaping (Timeline<Entry>) -> ()) { |
||||
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) |
||||
} |
||||
|
||||
let timeline = Timeline(entries: entries, policy: .atEnd) |
||||
completion(timeline) |
||||
} |
||||
} |
||||
|
||||
struct SimpleEntry: TimelineEntry { |
||||
let date: Date |
||||
let configuration: ConfigurationIntent |
||||
} |
||||
|
||||
struct PhoneWidgetEntryView : View { |
||||
var entry: Provider.Entry |
||||
|
||||
var body: some View { |
||||
Text(entry.date, style: .time) |
||||
} |
||||
} |
||||
|
||||
struct PhoneWidget: Widget { |
||||
let kind: String = "PhoneWidget" |
||||
|
||||
var body: some WidgetConfiguration { |
||||
IntentConfiguration(kind: kind, intent: ConfigurationIntent.self, provider: Provider()) { entry in |
||||
PhoneWidgetEntryView(entry: entry) |
||||
} |
||||
.configurationDisplayName("My Widget") |
||||
.description("This is an example widget.") |
||||
} |
||||
} |
||||
|
||||
struct PhoneWidget_Previews: PreviewProvider { |
||||
static var previews: some View { |
||||
PhoneWidgetEntryView(entry: SimpleEntry(date: Date(), configuration: ConfigurationIntent())) |
||||
.previewContext(WidgetPreviewContext(family: .systemSmall)) |
||||
} |
||||
} |
@ -1,16 +0,0 @@ |
||||
// |
||||
// PhoneWidgetBundle.swift |
||||
// PhoneWidget |
||||
// |
||||
// Created by Alan Francis on 11/06/2023. |
||||
// |
||||
|
||||
import WidgetKit |
||||
import SwiftUI |
||||
|
||||
@main |
||||
struct PhoneWidgetBundle: WidgetBundle { |
||||
var body: some Widget { |
||||
PhoneWidget() |
||||
} |
||||
} |
Loading…
Reference in new issue