Merge pull request #2606 from stuartbreckenridge/widget-improvements-ios
Widget improvements — iOS
This commit is contained in:
commit
1d44084404
@ -16,10 +16,8 @@ import Articles
|
|||||||
@available(iOS 14, *)
|
@available(iOS 14, *)
|
||||||
struct WidgetDataEncoder {
|
struct WidgetDataEncoder {
|
||||||
|
|
||||||
static let taskIdentifier = "com.ranchero.NetNewsWire.WidgetEncode"
|
|
||||||
|
|
||||||
static func encodeWidgetData() {
|
static func encodeWidgetData() {
|
||||||
os_log(.info, "Starting widget data encoding")
|
os_log(.debug, "Starting encoding widget data.")
|
||||||
do {
|
do {
|
||||||
// Unread Articles
|
// Unread Articles
|
||||||
let unreadArticles = try SmartFeedsController.shared.unreadFeed.fetchArticles().sorted(by: { $0.datePublished ?? .distantPast > $1.datePublished ?? .distantPast })
|
let unreadArticles = try SmartFeedsController.shared.unreadFeed.fetchArticles().sorted(by: { $0.datePublished ?? .distantPast > $1.datePublished ?? .distantPast })
|
||||||
@ -72,15 +70,18 @@ struct WidgetDataEncoder {
|
|||||||
lastUpdateTime: Date())
|
lastUpdateTime: Date())
|
||||||
|
|
||||||
let encodedData = try JSONEncoder().encode(latestData)
|
let encodedData = try JSONEncoder().encode(latestData)
|
||||||
|
os_log(.debug, "Finished encoding widget data.")
|
||||||
let appGroup = Bundle.main.object(forInfoDictionaryKey: "AppGroup") as! String
|
let appGroup = Bundle.main.object(forInfoDictionaryKey: "AppGroup") as! String
|
||||||
let containerURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: appGroup)
|
let containerURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: appGroup)
|
||||||
let dataURL = containerURL?.appendingPathComponent("widget-data.json")
|
let dataURL = containerURL?.appendingPathComponent("widget-data.json")
|
||||||
if FileManager.default.fileExists(atPath: dataURL!.path) {
|
if FileManager.default.fileExists(atPath: dataURL!.path) {
|
||||||
try FileManager.default.removeItem(at: dataURL!)
|
try FileManager.default.removeItem(at: dataURL!)
|
||||||
|
os_log(.debug, "Removed widget data from container.")
|
||||||
}
|
}
|
||||||
try encodedData.write(to: dataURL!)
|
if FileManager.default.createFile(atPath: dataURL!.path, contents: encodedData, attributes: nil) {
|
||||||
|
os_log(.debug, "Wrote widget data to container.")
|
||||||
WidgetCenter.shared.reloadAllTimelines()
|
WidgetCenter.shared.reloadAllTimelines()
|
||||||
os_log(.info, "Finished encoding widget data")
|
}
|
||||||
} catch {
|
} catch {
|
||||||
os_log(.error, "%@", error.localizedDescription)
|
os_log(.error, "%@", error.localizedDescription)
|
||||||
}
|
}
|
||||||
|
@ -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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user