From 0f59b904efc151ce4eba094c584470be426ef1de Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Sat, 15 Sep 2018 11:16:05 -0700 Subject: [PATCH] Store feed.authors in ODB. Make Author Codable. --- Frameworks/Account/Feed.swift | 24 ++++++++++++++++-------- Frameworks/Articles/Author.swift | 31 ++++++++++++++++++++++++++++++- 2 files changed, 46 insertions(+), 9 deletions(-) diff --git a/Frameworks/Account/Feed.swift b/Frameworks/Account/Feed.swift index a84a4e971..5fe17b9f7 100644 --- a/Frameworks/Account/Feed.swift +++ b/Frameworks/Account/Feed.swift @@ -66,7 +66,22 @@ public final class Feed: DisplayNameProvider, UnreadCountProvider, Hashable { } public var name: String? - public var authors: Set? + public var authors: Set? { + get { + guard let authorsJSON = settingsTable.string(for: Key.authors) else { + return nil + } + return Author.authorsWithJSON(authorsJSON) + } + set { + if let authorsJSON = newValue?.json() { + settingsTable.setString(authorsJSON, for: Key.authors) + } + else { + settingsTable.setString(nil, for: Key.authors) + } + } + } public var editedName: String? { didSet { @@ -148,10 +163,6 @@ public final class Feed: DisplayNameProvider, UnreadCountProvider, Hashable { self.init(account: account, url: url, feedID: feedID) self.name = dictionary[Key.name] as? String self.editedName = dictionary[Key.editedName] as? String - - if let authorsDiskArray = dictionary[Key.authors] as? [[String: Any]] { - self.authors = Author.authorsWithDiskArray(authorsDiskArray) - } } public static func isFeedDictionary(_ d: [String: Any]) -> Bool { @@ -175,9 +186,6 @@ public final class Feed: DisplayNameProvider, UnreadCountProvider, Hashable { if let editedName = editedName { d[Key.editedName] = editedName } - if let authorsArray = authors?.diskArray() { - d[Key.authors] = authorsArray - } return d } diff --git a/Frameworks/Articles/Author.swift b/Frameworks/Articles/Author.swift index f75775b66..e32fe3ea7 100644 --- a/Frameworks/Articles/Author.swift +++ b/Frameworks/Articles/Author.swift @@ -8,7 +8,7 @@ import Foundation -public struct Author: Hashable { +public struct Author: Codable, Hashable { public let authorID: String // calculated public let name: String? @@ -73,6 +73,23 @@ public struct Author: Hashable { return d } + public static func authorsWithJSON(_ jsonString: String) -> Set? { + // This is JSON stored in the database, not the JSON Feed version of an author. + guard let data = jsonString.data(using: .utf8) else { + return nil + } + + let decoder = JSONDecoder() + do { + let authors = try decoder.decode([Author].self, from: data) + return Set(authors) + } + catch { + assertionFailure("JSON representation of Author array could not be decoded: \(jsonString) error: \(error)") + } + return nil + } + public static func authorsWithDiskArray(_ diskArray: [[String: Any]]) -> Set? { let authors = diskArray.compactMap { Author(dictionary: $0) } @@ -89,4 +106,16 @@ extension Set where Element == Author { } return self.map{ $0.dictionary } } + + public func json() -> String? { + let encoder = JSONEncoder() + do { + let jsonData = try encoder.encode(Array(self)) + return String(data: jsonData, encoding: .utf8) + } + catch { + assertionFailure("JSON representation of Author array could not be encoded: \(self) error: \(error)") + } + return nil + } }