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,54 +574,102 @@ private extension Account {
func pullObjectsFromDisk() { func pullObjectsFromDisk() {
let settingsFileURL = URL(fileURLWithPath: settingsFile) // 9/16/2018: Turning a corner  we used to store data in a plist file,
guard let d = NSDictionary(contentsOf: settingsFileURL) as? [String: Any] else { // 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)
guard let d = NSDictionary(contentsOf: settingsFileURL) as? [String: Any] else {
return
}
guard let childrenArray = d[Key.children] as? [[String: Any]] else {
return
}
children = objects(with: childrenArray)
rebuildFeedDictionaries()
let userInfo = d[Key.userInfo] as? NSDictionary
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 return
} }
guard let childrenArray = d[Key.children] as? [[String: Any]] else {
importOPMLFile(path: opmlFilePath)
}
func importOPMLFile(path: String) {
let opmlFileURL = URL(fileURLWithPath: path)
var fileData: Data?
do {
fileData = try Data(contentsOf: opmlFileURL)
} catch {
NSApplication.shared.presentError(error)
return return
} }
children = objects(with: childrenArray) guard let opmlData = fileData else {
rebuildFeedDictionaries() return
let userInfo = d[Key.userInfo] as? NSDictionary
delegate.update(account: self, withUserInfo: userInfo)
}
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]() let parserData = ParserData(url: opmlFileURL.absoluteString, data: opmlData)
d[Key.children] = diskObjects as NSArray var opmlDocument: RSOPMLDocument?
if let userInfo = delegate.userInfo(for: self) { do {
d[Key.userInfo] = userInfo opmlDocument = try RSOPMLParser.parseOPML(with: parserData)
} catch {
NSApplication.shared.presentError(error)
return
}
guard let parsedOPML = opmlDocument else {
return
} }
return d as NSDictionary 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,36 +73,36 @@ 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
//
// if let feed = child as? Feed {
// return feed.dictionary
// }
// if let folder = child as? Folder, account.supportsSubFolders {
// return folder.dictionary
// }
// assertionFailure("Expected a feed or a folder.");
// return nil
// }
//
// if !childObjects.isEmpty {
// d[Key.children] = childObjects
// }
//
// return d
// }
let childObjects = children.compactMap { (child) -> [String: Any]? in
if let feed = child as? Feed {
return feed.dictionary
}
if let folder = child as? Folder, account.supportsSubFolders {
return folder.dictionary
}
assertionFailure("Expected a feed or a folder.");
return nil
}
if !childObjects.isEmpty {
d[Key.children] = childObjects
}
return d
}
// MARK: Feeds // MARK: Feeds
func addFeed(_ feed: Feed) -> Bool { func addFeed(_ feed: Feed) -> Bool {