54 lines
1.8 KiB
Swift
54 lines
1.8 KiB
Swift
//
|
|
// WidgetData.swift
|
|
// Reader
|
|
//
|
|
// Created by Shadowfacts on 6/19/22.
|
|
//
|
|
|
|
import Foundation
|
|
import Persistence
|
|
|
|
struct WidgetData: Codable {
|
|
let recentItems: [Item]
|
|
|
|
private static func url(for account: LocalData.Account) -> URL {
|
|
let groupContainerURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.net.shadowfacts.Reader")!
|
|
let appSupport = groupContainerURL
|
|
.appendingPathComponent("Library", isDirectory: true)
|
|
.appendingPathComponent("Application Support", isDirectory: true)
|
|
let widgetData = appSupport.appendingPathComponent("Widget Data", isDirectory: true)
|
|
return widgetData
|
|
.appendingPathComponent(account.persistenceKey)
|
|
.appendingPathExtension("plist")
|
|
}
|
|
|
|
static func load(account: LocalData.Account) -> WidgetData {
|
|
let url = url(for: account)
|
|
let decoder = PropertyListDecoder()
|
|
guard let data = try? Data(contentsOf: url),
|
|
let decoded = try? decoder.decode(WidgetData.self, from: data) else {
|
|
return WidgetData(recentItems: [])
|
|
}
|
|
return decoded
|
|
}
|
|
|
|
func save(account: LocalData.Account) {
|
|
let encoder = PropertyListEncoder()
|
|
let data = try! encoder.encode(self)
|
|
let url = WidgetData.url(for: account)
|
|
try! FileManager.default.createDirectory(at: url.deletingLastPathComponent(), withIntermediateDirectories: true)
|
|
try! data.write(to: url, options: .noFileProtection)
|
|
}
|
|
|
|
struct Item: Codable, Identifiable {
|
|
let id: String
|
|
let feedTitle: String?
|
|
let title: String?
|
|
let published: Date?
|
|
|
|
var launchURL: URL {
|
|
return URL(string: "reader://item/\(id)")!
|
|
}
|
|
}
|
|
}
|