272 lines
5.8 KiB
Swift
Raw Normal View History

//
// 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
import Articles
import RSDatabase
public final class Feed: DisplayNameProvider, Renamable, 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"
2018-09-14 19:33:47 -07:00
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
public var homePageURL: String? {
get {
return metadata?.homePageURL
}
set {
if let url = newValue {
metadata?.homePageURL = url.rs_normalizedURL()
}
else {
metadata?.homePageURL = nil
}
}
}
2018-09-14 21:51:05 -07:00
public var iconURL: String? {
get {
return metadata?.iconURL
2018-09-14 21:51:05 -07:00
}
set {
metadata?.iconURL = newValue
2018-09-14 21:51:05 -07:00
}
}
public var faviconURL: String? {
get {
return metadata?.faviconURL
2018-09-14 21:51:05 -07:00
}
set {
metadata?.faviconURL = newValue
2018-09-14 21:51:05 -07:00
}
}
public var name: String? {
get {
return settingsTable.string(for: Key.name)
}
set {
let oldNameForDisplay = nameForDisplay
settingsTable.setString(newValue, for: Key.name)
if oldNameForDisplay != nameForDisplay {
postDisplayNameDidChangeNotification()
}
}
}
public var authors: Set<Author>? {
get {
if let authorsArray = metadata?.authors {
return Set(authorsArray)
}
return nil
}
set {
if let authorsSet = newValue {
metadata?.authors = Array(authorsSet)
}
else {
metadata?.authors = nil
}
}
}
public var editedName: String? {
// Dont let editedName == ""
2018-09-15 11:39:33 -07:00
get {
guard let s = settingsTable.string(for: Key.editedName), !s.isEmpty else {
return nil
}
return s
2018-09-15 11:39:33 -07:00
}
set {
if newValue != editedName {
if let valueToSet = newValue, !valueToSet.isEmpty {
settingsTable.setString(valueToSet, for: Key.editedName)
}
else {
settingsTable.setString(nil, for: Key.editedName)
}
2018-09-15 11:39:33 -07:00
postDisplayNameDidChangeNotification()
}
}
}
2018-09-14 19:33:47 -07:00
public var conditionalGetInfo: HTTPConditionalGetInfo? {
get {
return metadata?.conditionalGetInfo
2018-09-14 19:33:47 -07:00
}
set {
metadata?.conditionalGetInfo = newValue
2018-09-14 19:33:47 -07:00
}
}
2018-09-14 21:51:05 -07:00
public var contentHash: String? {
get {
return metadata?.contentHash
}
set {
metadata?.contentHash = newValue
}
}
// 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: - Renamable
public func rename(to newName: String) {
editedName = newName
}
// 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
private var _metadata: FeedMetadata?
private var metadata: FeedMetadata? {
if let cachedMetadata = _metadata {
return cachedMetadata
}
_metadata = account?.metadata(for: self)
return _metadata
}
// 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)!
}
2017-09-26 13:26:28 -07:00
// MARK: - Disk Dictionary
convenience public init?(account: Account, dictionary: [String: Any]) {
2017-09-26 13:26:28 -07:00
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
self.init(account: account, url: url, feedID: feedID)
2017-09-26 13:26:28 -07:00
self.editedName = dictionary[Key.editedName] as? String
self.name = dictionary[Key.name] as? String
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
}
// 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 {
// https://github.com/brentsimmons/NetNewsWire/issues/527
// Dont use nameForDisplay because that can result in a feed name "Untitled" written to disk,
// which NetNewsWire may take later to be the actual name.
var nameToUse = editedName
if nameToUse == nil {
nameToUse = name
}
if nameToUse == nil {
nameToUse = ""
}
let escapedName = nameToUse!.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
}
}
extension Set where Element == Feed {
func feedIDs() -> Set<String> {
return Set<String>(map { $0.feedID })
}
}