Created
September 25, 2024 03:42
-
-
Save dezinezync/d9c7cd3d3a149aae4cc0f52370cf4033 to your computer and use it in GitHub Desktop.
AppIntentTimelineProvider bug on macOS 15.0
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Widgets.swift | |
// Widgets | |
// | |
// Created by Nikhil Nigade on 25/09/24. | |
// | |
import WidgetKit | |
import SwiftUI | |
import AppIntents | |
struct Provider: AppIntentTimelineProvider { | |
typealias Entry = SimpleEntry | |
typealias Intent = WidgetsConfig | |
func placeholder(in context: Context) -> SimpleEntry { | |
Entry(date: Date(), emoji: "π") | |
} | |
func snapshot(for configuration: WidgetsConfig, in context: Context) async -> Entry { | |
let entry = Entry(date: Date(), emoji: "π") | |
return entry | |
} | |
func timeline(for configuration: Intent, in context: Context) async -> 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 = Entry(date: entryDate, emoji: "π") | |
entries.append(entry) | |
} | |
let timeline = Timeline(entries: entries, policy: .atEnd) | |
return timeline | |
} | |
} | |
struct SimpleEntry: TimelineEntry { | |
let date: Date | |
let emoji: String | |
} | |
struct WidgetsEntryView : View { | |
var entry: Provider.Entry | |
var body: some View { | |
VStack { | |
HStack { | |
Text("Time:") | |
Text(entry.date, style: .time) | |
} | |
Text("Emoji:") | |
Text(entry.emoji) | |
} | |
} | |
} | |
struct Widgets: Widget { | |
let kind: String = "Widgets" | |
var body: some WidgetConfiguration { | |
AppIntentConfiguration( | |
kind: kind, | |
intent: WidgetsConfig.self, | |
provider: Provider(), | |
content: { entry in | |
WidgetsEntryView(entry: entry) | |
.containerBackground(.fill.tertiary, for: .widget) | |
} | |
) | |
.configurationDisplayName("My Widget") | |
.description("This is an example widget.") | |
} | |
} | |
struct WidgetsConfig: WidgetConfigurationIntent { | |
nonisolated(unsafe) static var title = LocalizedStringResource(stringLiteral: "Select Config") | |
nonisolated(unsafe) static var description = IntentDescription("View all some emojis") | |
@Parameter(title: "Emoji") | |
var showEmoji: Bool? | |
init(showEmoji: Bool) { | |
self.showEmoji = showEmoji | |
} | |
init() { | |
self.showEmoji = true | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment