diff --git a/Frameworks/Account/DataExtensions.swift b/Frameworks/Account/DataExtensions.swift index 60a4f460c..9e565a73a 100644 --- a/Frameworks/Account/DataExtensions.swift +++ b/Frameworks/Account/DataExtensions.swift @@ -53,6 +53,12 @@ public extension Feed { didChangeAtLeastOneSetting = true } + let updatedAuthors = Author.authorsWithParsedAuthors(parsedFeed.authors) + if authors != updatedAuthors { + authors = updatedAuthors + didChangeAtLeastOneSetting = true + } + if didChangeAtLeastOneSetting { NotificationCenter.default.post(name: .FeedSettingDidChange, object: self) } diff --git a/Frameworks/Data/Author.swift b/Frameworks/Data/Author.swift index 67fb137a3..d75ddb1a7 100644 --- a/Frameworks/Data/Author.swift +++ b/Frameworks/Data/Author.swift @@ -41,9 +41,61 @@ public struct Author: Hashable { self.authorID = databaseIDWithString(s) } } - + + public struct Key { + static let authorID = "authorID" + static let name = "name" + static let url = "url" + static let avatarURL = "avatarURL" + static let emailAddress = "emailAddress" + } + + public init?(dictionary: [String: Any]) { + + self.init(authorID: dictionary[Key.authorID] as? String, name: dictionary[Key.name] as? String, url: dictionary[Key.url] as? String, avatarURL: dictionary[Key.avatarURL] as? String, emailAddress: dictionary[Key.emailAddress] as? String) + } + + public var dictionary: [String: Any] { + + var d = [String: Any]() + + d[Key.authorID] = authorID + + if let name = name { + d[Key.name] = name + } + if let url = url { + d[Key.url] = url + } + if let avatarURL = avatarURL { + d[Key.avatarURL] = avatarURL + } + if let emailAddress = emailAddress { + d[Key.emailAddress] = emailAddress + } + + return d + } + public static func ==(lhs: Author, rhs: Author) -> Bool { return lhs.hashValue == rhs.hashValue && lhs.authorID == rhs.authorID } + + static func authorsWithDiskArray(_ diskArray: [[String: Any]]) -> Set? { + + let authors = diskArray.flatMap { Author(dictionary: $0) } + return authors.isEmpty ? nil : Set(authors) + } +} + +extension Set where Element == Author { + + func diskArray() -> [[String: Any]]? { + + if self.isEmpty { + return nil + } + return self.map{ $0.dictionary } + } } diff --git a/Frameworks/Data/Feed.swift b/Frameworks/Data/Feed.swift index ae344858f..750d01c6a 100644 --- a/Frameworks/Data/Feed.swift +++ b/Frameworks/Data/Feed.swift @@ -19,6 +19,7 @@ public final class Feed: DisplayNameProvider, UnreadCountProvider, Hashable { public var iconURL: String? public var faviconURL: String? public var name: String? + public var authors: Set? public var editedName: String? public var conditionalGetInfo: HTTPConditionalGetInfo? public var contentHash: String? @@ -62,6 +63,7 @@ public final class Feed: DisplayNameProvider, UnreadCountProvider, Hashable { static let faviconURL = "faviconURL" static let name = "name" static let editedName = "editedName" + static let authors = "authors" static let conditionalGetInfo = "conditionalGetInfo" static let contentHash = "contentHash" static let unreadCount = "unreadCount" @@ -89,6 +91,10 @@ public final class Feed: DisplayNameProvider, UnreadCountProvider, Hashable { if let savedUnreadCount = dictionary[Key.unreadCount] as? Int { self.unreadCount = savedUnreadCount } + + if let authorsDiskArray = dictionary[Key.authors] as? [[String: Any]] { + self.authors = Author.authorsWithDiskArray(authorsDiskArray) + } } public static func isFeedDictionary(_ d: [String: Any]) -> Bool { @@ -122,6 +128,9 @@ public final class Feed: DisplayNameProvider, UnreadCountProvider, Hashable { if let editedName = editedName { d[Key.editedName] = editedName } + if let authorsArray = authors?.diskArray() { + d[Key.authors] = authorsArray + } if let contentHash = contentHash { d[Key.contentHash] = contentHash } diff --git a/Frameworks/Database/Extensions/Author+Database.swift b/Frameworks/Database/Extensions/Author+Database.swift index fd3bb89e6..984aa9d6d 100644 --- a/Frameworks/Database/Extensions/Author+Database.swift +++ b/Frameworks/Database/Extensions/Author+Database.swift @@ -31,7 +31,7 @@ extension Author { self.init(authorID: nil, name: parsedAuthor.name, url: parsedAuthor.url, avatarURL: parsedAuthor.avatarURL, emailAddress: parsedAuthor.emailAddress) } - static func authorsWithParsedAuthors(_ parsedAuthors: Set?) -> Set? { + public static func authorsWithParsedAuthors(_ parsedAuthors: Set?) -> Set? { guard let parsedAuthors = parsedAuthors else { return nil