From 65073d7346f57e980282d82851ffbcbfae4a835d Mon Sep 17 00:00:00 2001 From: Stuart Breckenridge Date: Sun, 22 Nov 2020 17:22:38 +0800 Subject: [PATCH] TimelineProvider.swift Previews now use live data if it is available. --- Widget/TimelineProvider.swift | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/Widget/TimelineProvider.swift b/Widget/TimelineProvider.swift index 75cf023c7..8ca18dc5d 100644 --- a/Widget/TimelineProvider.swift +++ b/Widget/TimelineProvider.swift @@ -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) } }