TimelineProvider.swift

Previews now use live data if it is available.
This commit is contained in:
Stuart Breckenridge 2020-11-22 17:22:38 +08:00
parent 186be331d6
commit 65073d7346
No known key found for this signature in database
GPG Key ID: 1F11FD62007DC331

View File

@ -12,14 +12,23 @@ import SwiftUI
struct Provider: TimelineProvider {
func placeholder(in context: Context) -> WidgetTimelineEntry {
WidgetTimelineEntry(date: Date(), widgetData: WidgetDataDecoder.sampleData())
do {
let data = try WidgetDataDecoder.decodeWidgetData()
return WidgetTimelineEntry(date: Date(), widgetData: data)
} catch {
return WidgetTimelineEntry(date: Date(), widgetData: WidgetDataDecoder.sampleData())
}
}
func getSnapshot(in context: Context, completion: @escaping (WidgetTimelineEntry) -> Void) {
if context.isPreview {
let entry = WidgetTimelineEntry(date: Date(),
widgetData: WidgetDataDecoder.sampleData())
completion(entry)
do {
let data = try WidgetDataDecoder.decodeWidgetData()
completion(WidgetTimelineEntry(date: Date(), widgetData: data))
} catch {
completion(WidgetTimelineEntry(date: Date(),
widgetData: WidgetDataDecoder.sampleData()))
}
} else {
do {
let widgetData = try WidgetDataDecoder.decodeWidgetData()
@ -27,7 +36,7 @@ struct Provider: TimelineProvider {
completion(entry)
} catch {
let entry = WidgetTimelineEntry(date: Date(),
widgetData: WidgetData(currentUnreadCount: 41, currentTodayCount: 40, currentStarredCount: 12, unreadArticles: [], starredArticles: [], todayArticles: [], lastUpdateTime: Date()) )
widgetData: WidgetDataDecoder.sampleData())
completion(entry)
}
}