Make progress reading/writing Account feeds and folders to disk.

This commit is contained in:
Brent Simmons 2017-09-27 13:29:05 -07:00
parent 55967f8731
commit 48543bcfd6
3 changed files with 117 additions and 7 deletions

View File

@ -33,7 +33,7 @@ public final class Account: DisplayNameProvider, Hashable {
let settingsFile: String
let dataFolder: String
let database: Database
var topLevelObjects = [AnyObject]()
var topLevelObjects = [Any]()
var feedIDDictionary = [String: Feed]()
var username: String?
var refreshInProgress = false
@ -56,6 +56,8 @@ public final class Account: DisplayNameProvider, Hashable {
let databaseFilePath = (dataFolder as NSString).appendingPathComponent("DB.sqlite3")
self.database = Database(databaseFilePath: databaseFilePath, accountID: accountID)
pullObjectsFromDisk()
}
// MARK: - API
@ -122,13 +124,68 @@ public final class Account: DisplayNameProvider, Hashable {
}
extension Account: PlistProvider {
// MARK: - Disk
extension Account {
public func plist() -> AnyObject? {
return nil // TODO
}
private struct Key {
static let children = "children"
}
func pullObjectsFromDisk() {
guard let d = NSDictionary(contentsOf: settingsFile) as? [String: Any] else {
return
}
guard let childrenArray = d[Key.children] as? [Any] else {
return
}
topLevelObjects = objects(with: childrenArray)
updateFeedIDDictionary()
}
func objects(with diskObjects: [[String: Any]]) -> [Any] {
return diskObjects.flatMap { object(with: $0) }
}
func object(with diskObject: Any) -> Any {
guard let d = diskObject as? [String: Any] else {
return nil
}
if diskObjectIsFeed(diskObject) {
return Feed(accountID: accountID, dictionary: diskObject)
}
return Folder(accountID: accountID, dictionary: diskObject)
}
private func diskObjectIsFeed(_ diskObject: [String: Any]) -> Bool {
return d[Feed.Key.url] != nil
}
}
// Mark: - FeedIDDictionary
private extension Account {
func updateFeedIDDictionary() {
var d = [String: Feed]()
for feed in flattenedFeeds() {
d[feed.feedID] = feed
}
feedIDDictionary = d
}
}
// MARK: - OPMLRepresentable
extension Account: OPMLRepresentable {
public func OPMLString(indentLevel: Int) -> String {

View File

@ -12,8 +12,9 @@ import Data
public final class Folder: DisplayNameProvider, UnreadCountProvider {
public let accountID: String
var childObjects = [AnyObject]()
var children = [Any]()
var name: String?
public var account: Account? {
get {
return accountWithID(accountID)
@ -22,7 +23,12 @@ public final class Folder: DisplayNameProvider, UnreadCountProvider {
// MARK: - DisplayNameProvider
public var nameForDisplay: String
public var nameForDisplay: String {
get {
return name ?? NSLocalizedString("Untitled ƒ", comment: "Folder name")
}
}
// MARK: - UnreadCountProvider
@ -41,6 +47,53 @@ public final class Folder: DisplayNameProvider, UnreadCountProvider {
self.accountID = accountID
self.nameForDisplay = nameForDisplay
}
// MARK: - Disk Dictionary
struct Key {
static let name = "name"
static let unreadCount = "unreadCount"
static let childrenKey = "children"
}
convenience public init?(account: Account, dictionary: [String: Any]) {
self.name = dictionary[Key.name]
if let childrenArray = dictionary[Key.childrenKey] {
self.childObjects = account.objects(with: childrenArray)
}
if let savedUnreadCount = dictionary[Key.unreadCount] as? Int {
self.unreadCount = savedUnreadCount
}
}
public var dictionary: [String: Any] {
get {
var d = [String: Any]()
if let name = name {
d[Key.name] = name
}
if unreadCount > 0 {
d[Key.unreadCount] = unreadCount
}
// TODO: children as dictionaries - use method in Account
let childObjects = children.flatMap { (child) -> [String: Any]? in
if let feed = child as? Feed {
}
}
return d
}
}
}
extension Folder: OPMLRepresentable {

View File

@ -52,7 +52,7 @@ public final class Feed: DisplayNameProvider, UnreadCountProvider, Hashable {
// MARK: - Disk Dictionary
private struct Key {
struct Key {
static let url = "url"
static let feedID = "feedID"
static let homePageURL = "homePageURL"
@ -106,7 +106,7 @@ public final class Feed: DisplayNameProvider, UnreadCountProvider, Hashable {
d[Key.unreadCount] = unreadCount
}
if let conditionalGetInfo = conditionalGetInfo {
d[Key.conditionalGetInfo] = conditionalGetInfo.dictionary
d[Key.conditionalGetInfo] = conditionalGetInfo.dOictionary
}
return d