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