mirror of
https://github.com/Ranchero-Software/NetNewsWire.git
synced 2025-02-06 05:43:32 +01:00
Add the ability to observe OPML file changes
This commit is contained in:
parent
83476baa5b
commit
7de08e992e
@ -167,9 +167,7 @@ public final class Account: DisplayNameProvider, UnreadCountProvider, Container,
|
|||||||
static let saveQueue = CoalescingQueue(name: "Account Save Queue", interval: 1.0)
|
static let saveQueue = CoalescingQueue(name: "Account Save Queue", interval: 1.0)
|
||||||
|
|
||||||
private var unreadCounts = [String: Int]() // [feedID: Int]
|
private var unreadCounts = [String: Int]() // [feedID: Int]
|
||||||
private lazy var opmlFile: OPMLFile = {
|
private lazy var opmlFile: OPMLFile = OPMLFile(filename: (dataFolder as NSString).appendingPathComponent("Subscriptions.opml"), account: self)
|
||||||
OPMLFile(filename: (dataFolder as NSString).appendingPathComponent("Subscriptions.opml"), account: self)
|
|
||||||
}()
|
|
||||||
|
|
||||||
private var _flattenedFeeds = Set<Feed>()
|
private var _flattenedFeeds = Set<Feed>()
|
||||||
private var flattenedFeedsNeedUpdate = true
|
private var flattenedFeedsNeedUpdate = true
|
||||||
|
@ -13,7 +13,6 @@ import RSParser
|
|||||||
|
|
||||||
final class OPMLFile: NSObject, NSFilePresenter {
|
final class OPMLFile: NSObject, NSFilePresenter {
|
||||||
|
|
||||||
|
|
||||||
private var log = OSLog(subsystem: Bundle.main.bundleIdentifier!, category: "account")
|
private var log = OSLog(subsystem: Bundle.main.bundleIdentifier!, category: "account")
|
||||||
|
|
||||||
private let filename: String
|
private let filename: String
|
||||||
@ -21,7 +20,7 @@ final class OPMLFile: NSObject, NSFilePresenter {
|
|||||||
private let operationQueue: OperationQueue
|
private let operationQueue: OperationQueue
|
||||||
|
|
||||||
var presentedItemURL: URL? {
|
var presentedItemURL: URL? {
|
||||||
return URL(string: filename)
|
return URL(fileURLWithPath: filename)
|
||||||
}
|
}
|
||||||
|
|
||||||
var presentedItemOperationQueue: OperationQueue {
|
var presentedItemOperationQueue: OperationQueue {
|
||||||
@ -33,43 +32,26 @@ final class OPMLFile: NSObject, NSFilePresenter {
|
|||||||
self.account = account
|
self.account = account
|
||||||
operationQueue = OperationQueue()
|
operationQueue = OperationQueue()
|
||||||
operationQueue.maxConcurrentOperationCount = 1
|
operationQueue.maxConcurrentOperationCount = 1
|
||||||
|
|
||||||
|
super.init()
|
||||||
|
|
||||||
|
NSFileCoordinator.addFilePresenter(self)
|
||||||
|
}
|
||||||
|
|
||||||
|
func presentedItemDidChange() {
|
||||||
|
DispatchQueue.main.async {
|
||||||
|
self.reload()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func load() {
|
func load() {
|
||||||
let opmlFileURL = URL(fileURLWithPath: filename)
|
guard let opmlItems = parsedOPMLItems() else { return }
|
||||||
var fileData: Data?
|
|
||||||
do {
|
|
||||||
fileData = try Data(contentsOf: opmlFileURL)
|
|
||||||
} catch {
|
|
||||||
// Commented out because it’s not an error on first run.
|
|
||||||
// TODO: make it so we know if it’s first run or not.
|
|
||||||
//NSApplication.shared.presentError(error)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
guard let opmlData = fileData else {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
let parserData = ParserData(url: opmlFileURL.absoluteString, data: opmlData)
|
|
||||||
var opmlDocument: RSOPMLDocument?
|
|
||||||
|
|
||||||
do {
|
|
||||||
opmlDocument = try RSOPMLParser.parseOPML(with: parserData)
|
|
||||||
} catch {
|
|
||||||
os_log(.error, log: log, "OPML Import failed: %@.", error.localizedDescription)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
guard let parsedOPML = opmlDocument, let children = parsedOPML.children else {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
BatchUpdate.shared.perform {
|
BatchUpdate.shared.perform {
|
||||||
account.loadOPMLItems(children, parentFolder: nil)
|
account.loadOPMLItems(opmlItems, parentFolder: nil)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func save() {
|
func save() {
|
||||||
|
|
||||||
let opmlDocumentString = opmlDocument()
|
let opmlDocumentString = opmlDocument()
|
||||||
do {
|
do {
|
||||||
let url = URL(fileURLWithPath: filename)
|
let url = URL(fileURLWithPath: filename)
|
||||||
@ -83,6 +65,44 @@ final class OPMLFile: NSObject, NSFilePresenter {
|
|||||||
|
|
||||||
private extension OPMLFile {
|
private extension OPMLFile {
|
||||||
|
|
||||||
|
func reload() {
|
||||||
|
guard let opmlItems = parsedOPMLItems() else { return }
|
||||||
|
BatchUpdate.shared.perform {
|
||||||
|
account.topLevelFeeds.removeAll()
|
||||||
|
account.loadOPMLItems(opmlItems, parentFolder: nil)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func parsedOPMLItems() -> [RSOPMLItem]? {
|
||||||
|
|
||||||
|
let opmlFileURL = URL(fileURLWithPath: filename)
|
||||||
|
var fileData: Data?
|
||||||
|
do {
|
||||||
|
fileData = try Data(contentsOf: opmlFileURL)
|
||||||
|
} catch {
|
||||||
|
// Commented out because it’s not an error on first run.
|
||||||
|
// TODO: make it so we know if it’s first run or not.
|
||||||
|
//NSApplication.shared.presentError(error)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
guard let opmlData = fileData else {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
let parserData = ParserData(url: opmlFileURL.absoluteString, data: opmlData)
|
||||||
|
var opmlDocument: RSOPMLDocument?
|
||||||
|
|
||||||
|
do {
|
||||||
|
opmlDocument = try RSOPMLParser.parseOPML(with: parserData)
|
||||||
|
} catch {
|
||||||
|
os_log(.error, log: log, "OPML Import failed: %@.", error.localizedDescription)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return opmlDocument?.children
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
func opmlDocument() -> String {
|
func opmlDocument() -> String {
|
||||||
let escapedTitle = account.nameForDisplay.rs_stringByEscapingSpecialXMLCharacters()
|
let escapedTitle = account.nameForDisplay.rs_stringByEscapingSpecialXMLCharacters()
|
||||||
let openingText =
|
let openingText =
|
||||||
|
Loading…
x
Reference in New Issue
Block a user