Merge pull request #2606 from stuartbreckenridge/widget-improvements-ios

Widget improvements — iOS
This commit is contained in:
Maurice Parker 2020-11-22 15:28:58 -06:00 committed by GitHub
commit 1d44084404
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 11 deletions

View File

@ -16,10 +16,8 @@ import Articles
@available(iOS 14, *)
struct WidgetDataEncoder {
static let taskIdentifier = "com.ranchero.NetNewsWire.WidgetEncode"
static func encodeWidgetData() {
os_log(.info, "Starting widget data encoding")
os_log(.debug, "Starting encoding widget data.")
do {
// Unread Articles
let unreadArticles = try SmartFeedsController.shared.unreadFeed.fetchArticles().sorted(by: { $0.datePublished ?? .distantPast > $1.datePublished ?? .distantPast })
@ -72,15 +70,18 @@ struct WidgetDataEncoder {
lastUpdateTime: Date())
let encodedData = try JSONEncoder().encode(latestData)
os_log(.debug, "Finished encoding widget data.")
let appGroup = Bundle.main.object(forInfoDictionaryKey: "AppGroup") as! String
let containerURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: appGroup)
let dataURL = containerURL?.appendingPathComponent("widget-data.json")
if FileManager.default.fileExists(atPath: dataURL!.path) {
try FileManager.default.removeItem(at: dataURL!)
os_log(.debug, "Removed widget data from container.")
}
if FileManager.default.createFile(atPath: dataURL!.path, contents: encodedData, attributes: nil) {
os_log(.debug, "Wrote widget data to container.")
WidgetCenter.shared.reloadAllTimelines()
}
try encodedData.write(to: dataURL!)
WidgetCenter.shared.reloadAllTimelines()
os_log(.info, "Finished encoding widget data")
} catch {
os_log(.error, "%@", error.localizedDescription)
}

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)
}
}