2017-07-02 02:22:19 +02: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 21:08:50 +02:00
|
|
|
import RSWeb
|
2017-07-02 02:22:19 +02:00
|
|
|
|
2017-09-26 22:26:28 +02:00
|
|
|
public final class Feed: DisplayNameProvider, UnreadCountProvider, Hashable {
|
2017-07-02 02:22:19 +02:00
|
|
|
|
2017-09-08 05:51:51 +02:00
|
|
|
public let accountID: String
|
2017-07-02 02:22:19 +02:00
|
|
|
public let url: String
|
|
|
|
public let feedID: String
|
|
|
|
public var homePageURL: String?
|
|
|
|
public var name: String?
|
|
|
|
public var editedName: String?
|
2017-09-17 21:08:50 +02:00
|
|
|
public var conditionalGetInfo: HTTPConditionalGetInfo?
|
|
|
|
public var contentHash: String?
|
2017-07-03 19:29:44 +02:00
|
|
|
public let hashValue: Int
|
2017-09-17 00:25:38 +02:00
|
|
|
|
|
|
|
// MARK: - DisplayNameProvider
|
|
|
|
|
2017-07-02 02:22:19 +02:00
|
|
|
public var nameForDisplay: String {
|
|
|
|
get {
|
|
|
|
return (editedName ?? name) ?? NSLocalizedString("Untitled", comment: "Feed name")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-17 00:25:38 +02:00
|
|
|
// MARK: - UnreadCountProvider
|
2017-07-03 19:29:44 +02:00
|
|
|
|
2017-09-17 00:25:38 +02:00
|
|
|
public var unreadCount = 0 {
|
|
|
|
didSet {
|
|
|
|
if unreadCount != oldValue {
|
|
|
|
postUnreadCountDidChangeNotification()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Init
|
|
|
|
|
2017-09-08 05:51:51 +02:00
|
|
|
public init(accountID: String, url: String, feedID: String) {
|
2017-07-02 02:22:19 +02:00
|
|
|
|
2017-09-08 05:51:51 +02:00
|
|
|
self.accountID = accountID
|
2017-07-02 02:22:19 +02:00
|
|
|
self.url = url
|
|
|
|
self.feedID = feedID
|
2017-09-08 05:51:51 +02:00
|
|
|
self.hashValue = accountID.hashValue ^ url.hashValue ^ feedID.hashValue
|
2017-07-02 02:22:19 +02:00
|
|
|
}
|
|
|
|
|
2017-09-26 22:26:28 +02:00
|
|
|
// MARK: - Disk Dictionary
|
|
|
|
|
2017-09-27 22:29:05 +02:00
|
|
|
struct Key {
|
2017-09-26 22:26:28 +02:00
|
|
|
static let url = "url"
|
|
|
|
static let feedID = "feedID"
|
|
|
|
static let homePageURL = "homePageURL"
|
|
|
|
static let name = "name"
|
|
|
|
static let editedName = "editedName"
|
|
|
|
static let conditionalGetInfo = "conditionalGetInfo"
|
|
|
|
static let contentHash = "contentHash"
|
|
|
|
static let unreadCount = "unreadCount"
|
|
|
|
}
|
|
|
|
|
|
|
|
convenience public init?(accountID: String, dictionary: [String: Any]) {
|
|
|
|
|
|
|
|
guard let url = dictionary[Key.url] as? String, let feedID = dictionary[Key.feedID] as? String else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
self.init(accountID: accountID, 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
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public var dictionary: [String: Any] {
|
|
|
|
get {
|
|
|
|
var d = [String: Any]()
|
|
|
|
|
2017-09-26 22:32:02 +02:00
|
|
|
d[Key.url] = 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 contentHash = contentHash {
|
|
|
|
d[Key.contentHash] = contentHash
|
|
|
|
}
|
|
|
|
if unreadCount > 0 {
|
|
|
|
d[Key.unreadCount] = unreadCount
|
|
|
|
}
|
|
|
|
if let conditionalGetInfo = conditionalGetInfo {
|
2017-09-27 22:29:05 +02:00
|
|
|
d[Key.conditionalGetInfo] = conditionalGetInfo.dOictionary
|
2017-09-26 22:32:02 +02:00
|
|
|
}
|
|
|
|
|
2017-09-26 22:26:28 +02:00
|
|
|
return d
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-02 02:22:19 +02:00
|
|
|
public class func ==(lhs: Feed, rhs: Feed) -> Bool {
|
|
|
|
|
|
|
|
return lhs === rhs
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-17 00:25:38 +02: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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|