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
|
2018-07-28 12:16:14 -07:00
|
|
|
|
import Articles
|
2017-07-01 17:22:19 -07:00
|
|
|
|
|
2018-11-22 13:57:49 -08:00
|
|
|
|
public final class Feed: DisplayNameProvider, Renamable, UnreadCountProvider, Hashable {
|
2017-07-01 17:22:19 -07:00
|
|
|
|
|
2018-09-13 22:37:40 -07:00
|
|
|
|
public weak var account: Account?
|
2017-07-01 17:22:19 -07:00
|
|
|
|
public let url: String
|
2019-05-08 09:54:55 -05:00
|
|
|
|
|
|
|
|
|
public var feedID: String {
|
|
|
|
|
get {
|
|
|
|
|
return metadata.feedID
|
|
|
|
|
}
|
|
|
|
|
set {
|
|
|
|
|
metadata.feedID = newValue
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-09-02 12:14:04 -07:00
|
|
|
|
|
|
|
|
|
public var homePageURL: String? {
|
|
|
|
|
get {
|
2019-03-16 12:08:31 -07:00
|
|
|
|
return metadata.homePageURL
|
2018-09-02 12:14:04 -07:00
|
|
|
|
}
|
|
|
|
|
set {
|
|
|
|
|
if let url = newValue {
|
2019-03-16 12:08:31 -07:00
|
|
|
|
metadata.homePageURL = url.rs_normalizedURL()
|
2018-09-02 12:14:04 -07:00
|
|
|
|
}
|
|
|
|
|
else {
|
2019-03-16 12:08:31 -07:00
|
|
|
|
metadata.homePageURL = nil
|
2018-09-02 12:14:04 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-14 21:51:05 -07:00
|
|
|
|
public var iconURL: String? {
|
|
|
|
|
get {
|
2019-03-16 12:08:31 -07:00
|
|
|
|
return metadata.iconURL
|
2018-09-14 21:51:05 -07:00
|
|
|
|
}
|
|
|
|
|
set {
|
2019-03-16 12:08:31 -07:00
|
|
|
|
metadata.iconURL = newValue
|
2018-09-14 21:51:05 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public var faviconURL: String? {
|
|
|
|
|
get {
|
2019-03-16 12:08:31 -07:00
|
|
|
|
return metadata.faviconURL
|
2018-09-14 21:51:05 -07:00
|
|
|
|
}
|
|
|
|
|
set {
|
2019-03-16 12:08:31 -07:00
|
|
|
|
metadata.faviconURL = newValue
|
2018-09-14 21:51:05 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-15 11:45:01 -07:00
|
|
|
|
public var name: String? {
|
|
|
|
|
get {
|
2019-03-16 12:08:31 -07:00
|
|
|
|
return metadata.name
|
2018-09-15 11:45:01 -07:00
|
|
|
|
}
|
|
|
|
|
set {
|
|
|
|
|
let oldNameForDisplay = nameForDisplay
|
2019-03-16 12:08:31 -07:00
|
|
|
|
metadata.name = newValue
|
2019-06-05 12:53:30 +01:00
|
|
|
|
if oldNameForDisplay != newValue {
|
2018-09-15 11:45:01 -07:00
|
|
|
|
postDisplayNameDidChangeNotification()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-15 11:16:05 -07:00
|
|
|
|
public var authors: Set<Author>? {
|
|
|
|
|
get {
|
2019-03-16 12:08:31 -07:00
|
|
|
|
if let authorsArray = metadata.authors {
|
2019-03-13 23:41:43 -07:00
|
|
|
|
return Set(authorsArray)
|
2018-09-15 11:16:05 -07:00
|
|
|
|
}
|
2019-03-13 23:41:43 -07:00
|
|
|
|
return nil
|
2018-09-15 11:16:05 -07:00
|
|
|
|
}
|
|
|
|
|
set {
|
2019-03-13 23:41:43 -07:00
|
|
|
|
if let authorsSet = newValue {
|
2019-03-16 12:08:31 -07:00
|
|
|
|
metadata.authors = Array(authorsSet)
|
2018-09-15 11:16:05 -07:00
|
|
|
|
}
|
|
|
|
|
else {
|
2019-03-16 12:08:31 -07:00
|
|
|
|
metadata.authors = nil
|
2018-09-15 11:16:05 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-01-23 21:49:33 -08:00
|
|
|
|
|
|
|
|
|
public var editedName: String? {
|
2019-02-02 17:46:15 -08:00
|
|
|
|
// Don’t let editedName == ""
|
2018-09-15 11:39:33 -07:00
|
|
|
|
get {
|
2019-03-16 12:08:31 -07:00
|
|
|
|
guard let s = metadata.editedName, !s.isEmpty else {
|
2019-02-02 17:46:15 -08:00
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
return s
|
2018-09-15 11:39:33 -07:00
|
|
|
|
}
|
|
|
|
|
set {
|
|
|
|
|
if newValue != editedName {
|
2019-02-02 17:46:15 -08:00
|
|
|
|
if let valueToSet = newValue, !valueToSet.isEmpty {
|
2019-03-16 12:08:31 -07:00
|
|
|
|
metadata.editedName = valueToSet
|
2019-02-02 17:46:15 -08:00
|
|
|
|
}
|
|
|
|
|
else {
|
2019-03-16 12:08:31 -07:00
|
|
|
|
metadata.editedName = nil
|
2019-02-02 17:46:15 -08:00
|
|
|
|
}
|
2018-09-15 11:39:33 -07:00
|
|
|
|
postDisplayNameDidChangeNotification()
|
|
|
|
|
}
|
2018-01-23 21:49:33 -08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-14 19:33:47 -07:00
|
|
|
|
public var conditionalGetInfo: HTTPConditionalGetInfo? {
|
|
|
|
|
get {
|
2019-03-16 12:08:31 -07:00
|
|
|
|
return metadata.conditionalGetInfo
|
2018-09-14 19:33:47 -07:00
|
|
|
|
}
|
|
|
|
|
set {
|
2019-03-16 12:08:31 -07:00
|
|
|
|
metadata.conditionalGetInfo = newValue
|
2018-09-14 19:33:47 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-09-14 21:51:05 -07:00
|
|
|
|
|
2018-09-13 22:52:34 -07:00
|
|
|
|
public var contentHash: String? {
|
|
|
|
|
get {
|
2019-03-16 12:08:31 -07:00
|
|
|
|
return metadata.contentHash
|
2018-09-13 22:52:34 -07:00
|
|
|
|
}
|
|
|
|
|
set {
|
2019-03-16 12:08:31 -07:00
|
|
|
|
metadata.contentHash = newValue
|
2018-09-13 22:52:34 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-09-16 15:25:38 -07:00
|
|
|
|
|
2019-05-08 18:13:54 -05:00
|
|
|
|
public var subscriptionID: String? {
|
|
|
|
|
get {
|
|
|
|
|
return metadata.subscriptionID
|
|
|
|
|
}
|
|
|
|
|
set {
|
|
|
|
|
metadata.subscriptionID = newValue
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-07 18:34:45 -05:00
|
|
|
|
// Folder Name: Sync Service Relationship ID
|
|
|
|
|
public var folderRelationship: [String: String]? {
|
|
|
|
|
get {
|
|
|
|
|
return metadata.folderRelationship
|
|
|
|
|
}
|
|
|
|
|
set {
|
|
|
|
|
metadata.folderRelationship = newValue
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-16 15:25:38 -07:00
|
|
|
|
// MARK: - DisplayNameProvider
|
|
|
|
|
|
2017-07-01 17:22:19 -07:00
|
|
|
|
public var nameForDisplay: String {
|
2018-02-14 13:14:25 -08:00
|
|
|
|
if let s = editedName, !s.isEmpty {
|
|
|
|
|
return s
|
2017-07-01 17:22:19 -07:00
|
|
|
|
}
|
2018-02-14 13:14:25 -08:00
|
|
|
|
if let s = name, !s.isEmpty {
|
|
|
|
|
return s
|
|
|
|
|
}
|
|
|
|
|
return NSLocalizedString("Untitled", comment: "Feed name")
|
2017-07-01 17:22:19 -07:00
|
|
|
|
}
|
|
|
|
|
|
2018-11-22 13:57:49 -08:00
|
|
|
|
// MARK: - Renamable
|
|
|
|
|
|
2019-05-06 10:53:20 -05:00
|
|
|
|
public func rename(to newName: String, completion: @escaping (Result<Void, Error>) -> Void) {
|
2019-05-08 17:55:53 -05:00
|
|
|
|
guard let account = account else { return }
|
|
|
|
|
account.renameFeed(self, to: newName, completion: completion)
|
2018-11-22 13:57:49 -08:00
|
|
|
|
}
|
|
|
|
|
|
2017-09-16 15:25:38 -07:00
|
|
|
|
// MARK: - UnreadCountProvider
|
2017-07-03 10:29:44 -07:00
|
|
|
|
|
2018-09-14 22:06:03 -07:00
|
|
|
|
public var unreadCount: Int {
|
|
|
|
|
get {
|
|
|
|
|
return account?.unreadCount(for: self) ?? 0
|
|
|
|
|
}
|
|
|
|
|
set {
|
|
|
|
|
if unreadCount == newValue {
|
|
|
|
|
return
|
2017-09-16 15:25:38 -07:00
|
|
|
|
}
|
2018-09-14 22:06:03 -07:00
|
|
|
|
account?.setUnreadCount(newValue, for: self)
|
|
|
|
|
postUnreadCountDidChangeNotification()
|
2017-09-16 15:25:38 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-17 11:24:21 -07:00
|
|
|
|
// MARK: - Private
|
|
|
|
|
|
2018-09-14 22:15:22 -07:00
|
|
|
|
private let accountID: String // Used for hashing and equality; account may turn nil
|
2019-03-16 12:08:31 -07:00
|
|
|
|
private let metadata: FeedMetadata
|
2019-03-13 23:41:43 -07:00
|
|
|
|
|
2017-09-16 15:25:38 -07:00
|
|
|
|
// MARK: - Init
|
|
|
|
|
|
2019-05-08 09:54:55 -05:00
|
|
|
|
init(account: Account, url: String, metadata: FeedMetadata) {
|
2018-09-13 22:37:40 -07:00
|
|
|
|
self.account = account
|
2018-09-14 22:15:22 -07:00
|
|
|
|
self.accountID = account.accountID
|
2017-07-01 17:22:19 -07:00
|
|
|
|
self.url = url
|
2019-03-16 12:08:31 -07:00
|
|
|
|
self.metadata = metadata
|
2017-07-01 17:22:19 -07:00
|
|
|
|
}
|
|
|
|
|
|
2017-11-25 11:13:15 -08:00
|
|
|
|
// MARK: - Debug
|
|
|
|
|
|
|
|
|
|
public func debugDropConditionalGetInfo() {
|
|
|
|
|
conditionalGetInfo = nil
|
|
|
|
|
contentHash = nil
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-25 11:54:58 -07:00
|
|
|
|
// MARK: - Hashable
|
|
|
|
|
|
|
|
|
|
public func hash(into hasher: inout Hasher) {
|
|
|
|
|
hasher.combine(feedID)
|
2018-09-14 22:15:22 -07:00
|
|
|
|
hasher.combine(accountID)
|
2018-08-25 11:54:58 -07:00
|
|
|
|
}
|
|
|
|
|
|
2017-11-25 11:13:15 -08:00
|
|
|
|
// MARK: - Equatable
|
|
|
|
|
|
2017-07-01 17:22:19 -07:00
|
|
|
|
public class func ==(lhs: Feed, rhs: Feed) -> Bool {
|
2018-09-14 22:15:22 -07:00
|
|
|
|
return lhs.feedID == rhs.feedID && lhs.accountID == rhs.accountID
|
2017-07-01 17:22:19 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-16 15:25:38 -07:00
|
|
|
|
// MARK: - OPMLRepresentable
|
|
|
|
|
|
|
|
|
|
extension Feed: OPMLRepresentable {
|
|
|
|
|
|
|
|
|
|
public func OPMLString(indentLevel: Int) -> String {
|
2019-02-06 21:18:22 -08:00
|
|
|
|
// https://github.com/brentsimmons/NetNewsWire/issues/527
|
|
|
|
|
// Don’t 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()
|
|
|
|
|
|
2017-09-16 15:25:38 -07:00
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-28 12:16:14 -07:00
|
|
|
|
extension Set where Element == Feed {
|
|
|
|
|
|
|
|
|
|
func feedIDs() -> Set<String> {
|
|
|
|
|
return Set<String>(map { $0.feedID })
|
|
|
|
|
}
|
|
|
|
|
}
|