On startup, import from AccountData.plist one last time, then rename it to AccountData-old.plist. On subsequent startups, import using Subscriptions.opml.

This commit is contained in:
Brent Simmons 2018-09-16 12:42:46 -07:00
parent 823d83c942
commit 4010b4cb2a
3 changed files with 129 additions and 81 deletions

View File

@ -574,6 +574,12 @@ private extension Account {
func pullObjectsFromDisk() { func pullObjectsFromDisk() {
// 9/16/2018: Turning a corner  we used to store data in a plist file,
// but now were switching over to OPML. Read the plist file one last time,
// then rename it so we never read from it again.
if FileManager.default.fileExists(atPath: settingsFile) {
// Old code for reading in plist file.
let settingsFileURL = URL(fileURLWithPath: settingsFile) let settingsFileURL = URL(fileURLWithPath: settingsFile)
guard let d = NSDictionary(contentsOf: settingsFileURL) as? [String: Any] else { guard let d = NSDictionary(contentsOf: settingsFileURL) as? [String: Any] else {
return return
@ -586,42 +592,84 @@ private extension Account {
let userInfo = d[Key.userInfo] as? NSDictionary let userInfo = d[Key.userInfo] as? NSDictionary
delegate.update(account: self, withUserInfo: userInfo) delegate.update(account: self, withUserInfo: userInfo)
// Rename plist file so we dont see it next time.
let renamedFilePath = (dataFolder as NSString).appendingPathComponent("AccountData-old.plist")
do {
try FileManager.default.moveItem(atPath: settingsFile, toPath: renamedFilePath)
}
catch {}
dirty = true // Ensure OPML file will be written soon.
return
} }
func diskDictionary() -> NSDictionary { importOPMLFile(path: opmlFilePath)
let diskObjects = children.compactMap { (object) -> [String: Any]? in
if let folder = object as? Folder {
return folder.dictionary
}
else if let feed = object as? Feed {
return feed.dictionary
}
return nil
} }
var d = [String: Any]() func importOPMLFile(path: String) {
d[Key.children] = diskObjects as NSArray let opmlFileURL = URL(fileURLWithPath: path)
var fileData: Data?
if let userInfo = delegate.userInfo(for: self) { do {
d[Key.userInfo] = userInfo fileData = try Data(contentsOf: opmlFileURL)
} catch {
NSApplication.shared.presentError(error)
return
}
guard let opmlData = fileData else {
return
} }
return d as NSDictionary let parserData = ParserData(url: opmlFileURL.absoluteString, data: opmlData)
var opmlDocument: RSOPMLDocument?
do {
opmlDocument = try RSOPMLParser.parseOPML(with: parserData)
} catch {
NSApplication.shared.presentError(error)
return
} }
guard let parsedOPML = opmlDocument else {
return
}
importOPML(parsedOPML)
}
// func diskDictionary() -> NSDictionary {
//
// let diskObjects = children.compactMap { (object) -> [String: Any]? in
//
// if let folder = object as? Folder {
// return folder.dictionary
// }
// else if let feed = object as? Feed {
// return feed.dictionary
// }
// return nil
// }
//
// var d = [String: Any]()
// d[Key.children] = diskObjects as NSArray
//
// if let userInfo = delegate.userInfo(for: self) {
// d[Key.userInfo] = userInfo
// }
//
// return d as NSDictionary
// }
func saveToDisk() { func saveToDisk() {
dirty = false dirty = false
let d = diskDictionary() // let d = diskDictionary()
do { // do {
try RSPlist.write(d, filePath: settingsFile) // try RSPlist.write(d, filePath: settingsFile)
} // }
catch let error as NSError { // catch let error as NSError {
NSApplication.shared.presentError(error) // NSApplication.shared.presentError(error)
} // }
let opmlDocumentString = opmlDocument() let opmlDocumentString = opmlDocument()
do { do {

View File

@ -188,25 +188,25 @@ public final class Feed: DisplayNameProvider, UnreadCountProvider, Hashable {
return d[Key.url] != nil return d[Key.url] != nil
} }
public var dictionary: [String: Any] { // public var dictionary: [String: Any] {
var d = [String: Any]() // var d = [String: Any]()
//
d[Key.url] = url // d[Key.url] = url
//
// feedID is not repeated when its the same as url // // feedID is not repeated when its the same as url
if (feedID != url) { // if (feedID != url) {
d[Key.feedID] = feedID // d[Key.feedID] = feedID
} // }
//
if let name = name { // if let name = name {
d[Key.name] = name // d[Key.name] = name
} // }
if let editedName = editedName { // if let editedName = editedName {
d[Key.editedName] = editedName // d[Key.editedName] = editedName
} // }
//
return d // return d
} // }
// MARK: - Debug // MARK: - Debug

View File

@ -73,35 +73,35 @@ public final class Folder: DisplayNameProvider, Container, UnreadCountProvider,
} }
} }
var dictionary: [String: Any] { // var dictionary: [String: Any] {
//
var d = [String: Any]() // var d = [String: Any]()
guard let account = account else { // guard let account = account else {
return d // return d
} // }
//
if let name = name { // if let name = name {
d[Key.name] = name // d[Key.name] = name
} // }
//
let childObjects = children.compactMap { (child) -> [String: Any]? in // let childObjects = children.compactMap { (child) -> [String: Any]? in
//
if let feed = child as? Feed { // if let feed = child as? Feed {
return feed.dictionary // return feed.dictionary
} // }
if let folder = child as? Folder, account.supportsSubFolders { // if let folder = child as? Folder, account.supportsSubFolders {
return folder.dictionary // return folder.dictionary
} // }
assertionFailure("Expected a feed or a folder."); // assertionFailure("Expected a feed or a folder.");
return nil // return nil
} // }
//
if !childObjects.isEmpty { // if !childObjects.isEmpty {
d[Key.children] = childObjects // d[Key.children] = childObjects
} // }
//
return d // return d
} // }
// MARK: Feeds // MARK: Feeds