2017-07-01 17:22:19 -07:00
|
|
|
|
//
|
|
|
|
|
// Feed.swift
|
|
|
|
|
// DataModel
|
|
|
|
|
//
|
|
|
|
|
// Created by Brent Simmons on 7/1/17.
|
|
|
|
|
// Copyright © 2017 Ranchero Software, LLC. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
import RSCore
|
2017-09-17 12:08:50 -07:00
|
|
|
|
import RSWeb
|
2017-07-01 17:22:19 -07:00
|
|
|
|
|
2017-09-26 13:26:28 -07:00
|
|
|
|
public final class Feed: DisplayNameProvider, UnreadCountProvider, Hashable {
|
2017-07-01 17:22:19 -07:00
|
|
|
|
|
2017-09-07 20:51:51 -07:00
|
|
|
|
public let accountID: String
|
2017-07-01 17:22:19 -07:00
|
|
|
|
public let url: String
|
|
|
|
|
public let feedID: String
|
|
|
|
|
public var homePageURL: String?
|
2017-11-19 22:37:59 -08:00
|
|
|
|
public var iconURL: String?
|
2017-11-19 21:24:19 -08:00
|
|
|
|
public var faviconURL: String?
|
2017-07-01 17:22:19 -07:00
|
|
|
|
public var name: String?
|
2017-12-02 14:20:58 -08:00
|
|
|
|
public var authors: Set<Author>?
|
2018-01-23 21:49:33 -08:00
|
|
|
|
|
|
|
|
|
public var editedName: String? {
|
|
|
|
|
didSet {
|
|
|
|
|
postDisplayNameDidChangeNotification()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-17 12:08:50 -07:00
|
|
|
|
public var conditionalGetInfo: HTTPConditionalGetInfo?
|
|
|
|
|
public var contentHash: String?
|
2017-07-03 10:29:44 -07:00
|
|
|
|
public let hashValue: Int
|
2017-09-16 15:25:38 -07:00
|
|
|
|
|
|
|
|
|
// MARK: - DisplayNameProvider
|
|
|
|
|
|
2017-07-01 17:22:19 -07:00
|
|
|
|
public var nameForDisplay: String {
|
|
|
|
|
get {
|
2018-01-23 21:49:33 -08:00
|
|
|
|
if let s = editedName, !s.isEmpty {
|
|
|
|
|
return s
|
|
|
|
|
}
|
|
|
|
|
if let s = name, !s.isEmpty {
|
|
|
|
|
return s
|
|
|
|
|
}
|
|
|
|
|
return NSLocalizedString("Untitled", comment: "Feed name")
|
2017-07-01 17:22:19 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-16 15:25:38 -07:00
|
|
|
|
// MARK: - UnreadCountProvider
|
2017-07-03 10:29:44 -07:00
|
|
|
|
|
2017-09-16 15:25:38 -07:00
|
|
|
|
public var unreadCount = 0 {
|
|
|
|
|
didSet {
|
|
|
|
|
if unreadCount != oldValue {
|
|
|
|
|
postUnreadCountDidChangeNotification()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MARK: - Init
|
|
|
|
|
|
2017-09-07 20:51:51 -07:00
|
|
|
|
public init(accountID: String, url: String, feedID: String) {
|
2017-07-01 17:22:19 -07:00
|
|
|
|
|
2017-09-07 20:51:51 -07:00
|
|
|
|
self.accountID = accountID
|
2017-07-01 17:22:19 -07:00
|
|
|
|
self.url = url
|
|
|
|
|
self.feedID = feedID
|
2017-10-05 21:08:27 -07:00
|
|
|
|
self.hashValue = feedID.hashValue
|
2017-07-01 17:22:19 -07:00
|
|
|
|
}
|
|
|
|
|
|
2017-09-26 13:26:28 -07:00
|
|
|
|
// MARK: - Disk Dictionary
|
|
|
|
|
|
2017-09-28 13:16:47 -07:00
|
|
|
|
private struct Key {
|
2017-09-26 13:26:28 -07:00
|
|
|
|
static let url = "url"
|
|
|
|
|
static let feedID = "feedID"
|
|
|
|
|
static let homePageURL = "homePageURL"
|
2017-11-19 22:37:59 -08:00
|
|
|
|
static let iconURL = "iconURL"
|
2017-11-19 21:24:19 -08:00
|
|
|
|
static let faviconURL = "faviconURL"
|
2017-09-26 13:26:28 -07:00
|
|
|
|
static let name = "name"
|
|
|
|
|
static let editedName = "editedName"
|
2017-12-02 14:20:58 -08:00
|
|
|
|
static let authors = "authors"
|
2017-09-26 13:26:28 -07:00
|
|
|
|
static let conditionalGetInfo = "conditionalGetInfo"
|
|
|
|
|
static let contentHash = "contentHash"
|
|
|
|
|
static let unreadCount = "unreadCount"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
convenience public init?(accountID: String, dictionary: [String: Any]) {
|
|
|
|
|
|
2017-10-05 21:08:27 -07:00
|
|
|
|
guard let url = dictionary[Key.url] as? String else {
|
2017-09-26 13:26:28 -07:00
|
|
|
|
return nil
|
|
|
|
|
}
|
2017-10-05 21:08:27 -07:00
|
|
|
|
let feedID = dictionary[Key.feedID] as? String ?? url
|
|
|
|
|
|
2017-09-26 13:26:28 -07:00
|
|
|
|
self.init(accountID: accountID, url: url, feedID: feedID)
|
|
|
|
|
self.homePageURL = dictionary[Key.homePageURL] as? String
|
2017-11-19 22:37:59 -08:00
|
|
|
|
self.iconURL = dictionary[Key.iconURL] as? String
|
2017-11-19 21:24:19 -08:00
|
|
|
|
self.faviconURL = dictionary[Key.faviconURL] as? String
|
2017-09-26 13:26:28 -07:00
|
|
|
|
self.name = dictionary[Key.name] as? String
|
|
|
|
|
self.editedName = dictionary[Key.editedName] as? String
|
|
|
|
|
self.contentHash = dictionary[Key.contentHash] as? String
|
|
|
|
|
|
|
|
|
|
if let conditionalGetInfoDictionary = dictionary[Key.conditionalGetInfo] as? [String: String] {
|
|
|
|
|
self.conditionalGetInfo = HTTPConditionalGetInfo(dictionary: conditionalGetInfoDictionary)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if let savedUnreadCount = dictionary[Key.unreadCount] as? Int {
|
|
|
|
|
self.unreadCount = savedUnreadCount
|
|
|
|
|
}
|
2017-12-02 14:20:58 -08:00
|
|
|
|
|
|
|
|
|
if let authorsDiskArray = dictionary[Key.authors] as? [[String: Any]] {
|
|
|
|
|
self.authors = Author.authorsWithDiskArray(authorsDiskArray)
|
|
|
|
|
}
|
2017-09-26 13:26:28 -07:00
|
|
|
|
}
|
|
|
|
|
|
2017-09-28 13:16:47 -07:00
|
|
|
|
public static func isFeedDictionary(_ d: [String: Any]) -> Bool {
|
|
|
|
|
|
|
|
|
|
return d[Key.url] != nil
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-26 13:26:28 -07:00
|
|
|
|
public var dictionary: [String: Any] {
|
|
|
|
|
get {
|
|
|
|
|
var d = [String: Any]()
|
|
|
|
|
|
2017-09-26 13:32:02 -07:00
|
|
|
|
d[Key.url] = url
|
2017-10-05 21:08:27 -07:00
|
|
|
|
|
|
|
|
|
// feedID is not repeated when it’s the same as url
|
|
|
|
|
if (feedID != url) {
|
|
|
|
|
d[Key.feedID] = feedID
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-26 13:32:02 -07:00
|
|
|
|
if let homePageURL = homePageURL {
|
|
|
|
|
d[Key.homePageURL] = homePageURL
|
|
|
|
|
}
|
2017-11-19 22:37:59 -08:00
|
|
|
|
if let iconURL = iconURL {
|
|
|
|
|
d[Key.iconURL] = iconURL
|
|
|
|
|
}
|
2017-11-19 21:24:19 -08:00
|
|
|
|
if let faviconURL = faviconURL {
|
|
|
|
|
d[Key.faviconURL] = faviconURL
|
|
|
|
|
}
|
2017-09-26 13:32:02 -07:00
|
|
|
|
if let name = name {
|
|
|
|
|
d[Key.name] = name
|
|
|
|
|
}
|
|
|
|
|
if let editedName = editedName {
|
|
|
|
|
d[Key.editedName] = editedName
|
|
|
|
|
}
|
2017-12-02 14:20:58 -08:00
|
|
|
|
if let authorsArray = authors?.diskArray() {
|
|
|
|
|
d[Key.authors] = authorsArray
|
|
|
|
|
}
|
2017-09-26 13:32:02 -07:00
|
|
|
|
if let contentHash = contentHash {
|
|
|
|
|
d[Key.contentHash] = contentHash
|
|
|
|
|
}
|
|
|
|
|
if unreadCount > 0 {
|
|
|
|
|
d[Key.unreadCount] = unreadCount
|
|
|
|
|
}
|
|
|
|
|
if let conditionalGetInfo = conditionalGetInfo {
|
2017-09-28 13:16:47 -07:00
|
|
|
|
d[Key.conditionalGetInfo] = conditionalGetInfo.dictionary
|
2017-09-26 13:32:02 -07:00
|
|
|
|
}
|
|
|
|
|
|
2017-09-26 13:26:28 -07:00
|
|
|
|
return d
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-25 11:13:15 -08:00
|
|
|
|
// MARK: - Debug
|
|
|
|
|
|
|
|
|
|
public func debugDropConditionalGetInfo() {
|
|
|
|
|
|
|
|
|
|
conditionalGetInfo = nil
|
|
|
|
|
contentHash = nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MARK: - Equatable
|
|
|
|
|
|
2017-07-01 17:22:19 -07:00
|
|
|
|
public class func ==(lhs: Feed, rhs: Feed) -> Bool {
|
|
|
|
|
|
|
|
|
|
return lhs === rhs
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-16 15:25:38 -07:00
|
|
|
|
// 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 = "<outline text=\"\(escapedName)\" title=\"\(escapedName)\" description=\"\" type=\"rss\" version=\"RSS\" htmlUrl=\"\(escapedHomePageURL)\" xmlUrl=\"\(escapedFeedURL)\"/>\n"
|
|
|
|
|
s = s.rs_string(byPrependingNumberOfTabs: indentLevel)
|
|
|
|
|
|
|
|
|
|
return s
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|