// // Feed.swift // DataModel // // Created by Brent Simmons on 7/1/17. // Copyright © 2017 Ranchero Software, LLC. All rights reserved. // import Foundation import RSCore import RSWeb import Articles import RSDatabase public final class Feed: DisplayNameProvider, UnreadCountProvider, Hashable { private struct Key { static let url = "url" static let feedID = "feedID" static let homePageURL = "homePageURL" static let iconURL = "iconURL" static let faviconURL = "faviconURL" static let name = "name" static let editedName = "editedName" static let authors = "authors" static let conditionalGetInfo = "conditionalGetInfo" static let conditionalGetLastModified = "lastModified" static let conditionalGetEtag = "etag" static let contentHash = "contentHash" } public weak var account: Account? public let url: String public let feedID: String private var _homePageURL: String? public var homePageURL: String? { get { return _homePageURL } set { if let url = newValue { _homePageURL = url.rs_normalizedURL() } else { _homePageURL = nil } } } public var iconURL: String? { get { return settingsTable.string(for: Key.iconURL) } set { settingsTable.setString(newValue, for: Key.iconURL) } } public var faviconURL: String? { get { return settingsTable.string(for: Key.faviconURL) } set { settingsTable.setString(newValue, for: Key.faviconURL) } } public var name: String? public var authors: Set? public var editedName: String? { didSet { postDisplayNameDidChangeNotification() } } public var conditionalGetInfo: HTTPConditionalGetInfo? { get { let lastModified = settingsTable.string(for: Key.conditionalGetLastModified) let etag = settingsTable.string(for: Key.conditionalGetEtag) return HTTPConditionalGetInfo(lastModified: lastModified, etag: etag) } set { settingsTable.setString(newValue?.lastModified, for: Key.conditionalGetLastModified) settingsTable.setString(newValue?.etag, for: Key.conditionalGetEtag) } } public var contentHash: String? { get { return settingsTable.string(for: Key.contentHash) } set { settingsTable.setString(newValue, for: Key.contentHash) } } // MARK: - DisplayNameProvider public var nameForDisplay: String { if let s = editedName, !s.isEmpty { return s } if let s = name, !s.isEmpty { return s } return NSLocalizedString("Untitled", comment: "Feed name") } // MARK: - UnreadCountProvider public var unreadCount: Int { get { return account?.unreadCount(for: self) ?? 0 } set { if unreadCount == newValue { return } account?.setUnreadCount(newValue, for: self) postUnreadCountDidChangeNotification() } } private let settingsTable: ODBRawValueTable private let accountID: String // Used for hashing and equality; account may turn nil // MARK: - Init public init(account: Account, url: String, feedID: String) { self.account = account self.accountID = account.accountID self.url = url self.feedID = feedID self.settingsTable = account.settingsTableForFeed(feedID: feedID)! } // MARK: - Disk Dictionary convenience public init?(account: Account, dictionary: [String: Any]) { guard let url = dictionary[Key.url] as? String else { return nil } let feedID = dictionary[Key.feedID] as? String ?? url self.init(account: account, url: url, feedID: feedID) self.homePageURL = dictionary[Key.homePageURL] as? String 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 { return d[Key.url] != nil } public var dictionary: [String: Any] { var d = [String: Any]() d[Key.url] = url // feedID is not repeated when it’s the same as url if (feedID != url) { d[Key.feedID] = feedID } if let homePageURL = homePageURL { d[Key.homePageURL] = homePageURL } if let name = name { d[Key.name] = name } if let editedName = editedName { d[Key.editedName] = editedName } if let authorsArray = authors?.diskArray() { d[Key.authors] = authorsArray } return d } // MARK: - Debug public func debugDropConditionalGetInfo() { conditionalGetInfo = nil contentHash = nil } // MARK: - Hashable public func hash(into hasher: inout Hasher) { hasher.combine(feedID) hasher.combine(accountID) } // MARK: - Equatable public class func ==(lhs: Feed, rhs: Feed) -> Bool { return lhs.feedID == rhs.feedID && lhs.accountID == rhs.accountID } } // MARK: - OPMLRepresentable extension Feed: OPMLRepresentable { public func OPMLString(indentLevel: Int) -> String { let escapedName = nameForDisplay.rs_stringByEscapingSpecialXMLCharacters() var escapedHomePageURL = "" if let homePageURL = homePageURL { escapedHomePageURL = homePageURL.rs_stringByEscapingSpecialXMLCharacters() } let escapedFeedURL = url.rs_stringByEscapingSpecialXMLCharacters() var s = "\n" s = s.rs_string(byPrependingNumberOfTabs: indentLevel) return s } } extension Set where Element == Feed { func feedIDs() -> Set { return Set(map { $0.feedID }) } }