Save feed.authors.

This commit is contained in:
Brent Simmons 2017-12-02 14:20:58 -08:00
parent b5d8233955
commit e71d763443
4 changed files with 69 additions and 2 deletions

View File

@ -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)
}

View File

@ -42,8 +42,60 @@ public struct Author: Hashable {
}
}
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<Author>? {
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 }
}
}

View File

@ -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<Author>?
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
}

View File

@ -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<ParsedAuthor>?) -> Set<Author>? {
public static func authorsWithParsedAuthors(_ parsedAuthors: Set<ParsedAuthor>?) -> Set<Author>? {
guard let parsedAuthors = parsedAuthors else {
return nil