Skip to content

Instantly share code, notes, and snippets.

@velotiotech
Created July 3, 2024 11:28
Show Gist options
  • Save velotiotech/2f542e9678aeebc34ee4516f9fa4aa74 to your computer and use it in GitHub Desktop.
Save velotiotech/2f542e9678aeebc34ee4516f9fa4aa74 to your computer and use it in GitHub Desktop.
struct Provider: TimelineProvider {
// Provides a placeholder entry while the widget is loading.
func placeholder(in context: Context) -> DayEntry {
DayEntry(date: Date(), configuration: ConfigurationIntent())
}
// Provides a snapshot of the widget's current state.
func getSnapshot(in context: Context, completion: @escaping (DayEntry) -> ()) {
let entry = DayEntry(date: Date(), configuration: ConfigurationIntent())
completion(entry)
}
// Provides a timeline of entries for the widget.
func getTimeline(in context: Context, completion: @escaping (Timeline<DayEntry>) -> ()) {
var entries: [DayEntry] = []
// Generate a timeline consisting of seven entries an day apart, starting from the current date.
let currentDate = Date()
for dayOffset in 0 ..< 7 {
let entryDate = Calendar.current.date(byAdding: .day, value: dayOffset, to: currentDate)!
let startOfDate = Calendar.current.startOfDay(for: entryDate)
let entry = DayEntry(date: startOfDate, configuration: ConfigurationIntent())
entries.append(entry)
let timeline = Timeline(entries: entries, policy: .atEnd)
completion(timeline)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment