Skip to content

Instantly share code, notes, and snippets.

@dezinezync
Created September 25, 2024 03:42
Show Gist options
  • Save dezinezync/d9c7cd3d3a149aae4cc0f52370cf4033 to your computer and use it in GitHub Desktop.
Save dezinezync/d9c7cd3d3a149aae4cc0f52370cf4033 to your computer and use it in GitHub Desktop.
AppIntentTimelineProvider bug on macOS 15.0
//
// 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