mirror of
https://github.com/Ranchero-Software/NetNewsWire
synced 2025-08-12 06:26:36 +00:00
Widget Updates
• Can now read data written by main app • Has preview capability in widget gallery • Still to solve using ORGANIZATION_IDENTIFIER
This commit is contained in:
40
Multiplatform/Shared/Widget Data/WidgetDataDecoder.swift
Normal file
40
Multiplatform/Shared/Widget Data/WidgetDataDecoder.swift
Normal file
@@ -0,0 +1,40 @@
|
||||
//
|
||||
// WidgetDataDecoder.swift
|
||||
// NetNewsWire
|
||||
//
|
||||
// Created by Stuart Breckenridge on 11/7/20.
|
||||
// Copyright © 2020 Ranchero Software. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
struct WidgetDataDecoder {
|
||||
|
||||
static func decodeWidgetData() throws -> WidgetData {
|
||||
let appGroup = Bundle.main.object(forInfoDictionaryKey: "AppGroup") as! String
|
||||
print(appGroup)
|
||||
let containerURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: appGroup)
|
||||
let dataURL = containerURL?.appendingPathComponent("widget-data.json")
|
||||
print("decoder path: \(dataURL!.path)")
|
||||
|
||||
if FileManager.default.fileExists(atPath: dataURL!.path) {
|
||||
let decodedWidgetData = try JSONDecoder().decode(WidgetData.self, from: Data(contentsOf: dataURL!))
|
||||
return decodedWidgetData
|
||||
} else {
|
||||
print("No data at location")
|
||||
return WidgetData(currentUnreadCount: 0, currentTodayCount: 0, latestArticles: [], lastUpdateTime: Date())
|
||||
}
|
||||
}
|
||||
|
||||
static func sampleData() -> WidgetData {
|
||||
let pathToSample = Bundle.main.url(forResource: "widget-data-sample", withExtension: "json")
|
||||
do {
|
||||
let data = try Data(contentsOf: pathToSample!)
|
||||
let decoded = try JSONDecoder().decode(WidgetData.self, from: data)
|
||||
return decoded
|
||||
} catch {
|
||||
return WidgetData(currentUnreadCount: 12, currentTodayCount: 12, latestArticles: [], lastUpdateTime: Date())
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user