NetNewsWire/Frameworks/Account/Feed.swift

269 lines
5.6 KiB
Swift
Raw Normal View History

//
// Feed.swift
// NetNewsWire
//
// 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
import Articles
public final class Feed: DisplayNameProvider, Renamable, UnreadCountProvider, Hashable {
public weak var account: Account?
public let url: String
public var feedID: String {
get {
return metadata.feedID
}
set {
metadata.feedID = newValue
}
}
public var homePageURL: String? {
get {
return metadata.homePageURL
}
set {
if let url = newValue {
metadata.homePageURL = url.rs_normalizedURL()
}
else {
metadata.homePageURL = nil
}
}
}
// Note: this is available only if the icon URL was available in the feed.
// The icon URL is a JSON-Feed-only feature.
// Otherwise we find an icon URL via other means, but we dont store it
// as part of feed metadata.
2018-09-15 06:51:05 +02:00
public var iconURL: String? {
get {
return metadata.iconURL
2018-09-15 06:51:05 +02:00
}
set {
metadata.iconURL = newValue
2018-09-15 06:51:05 +02:00
}
}
// Note: this is available only if the favicon URL was available in the feed.
// The favicon URL is a JSON-Feed-only feature.
// Otherwise we find a favicon URL via other means, but we dont store it
// as part of feed metadata.
2018-09-15 06:51:05 +02:00
public var faviconURL: String? {
get {
return metadata.faviconURL
2018-09-15 06:51:05 +02:00
}
set {
metadata.faviconURL = newValue
2018-09-15 06:51:05 +02:00
}
}
public var name: String?
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 20:39:33 +02:00
get {
guard let s = metadata.editedName, !s.isEmpty else {
return nil
}
return s
2018-09-15 20:39:33 +02:00
}
set {
if newValue != editedName {
if let valueToSet = newValue, !valueToSet.isEmpty {
metadata.editedName = valueToSet
}
else {
metadata.editedName = nil
}
2018-09-15 20:39:33 +02:00
postDisplayNameDidChangeNotification()
}
}
}
2018-09-15 04:33:47 +02:00
public var conditionalGetInfo: HTTPConditionalGetInfo? {
get {
return metadata.conditionalGetInfo
2018-09-15 04:33:47 +02:00
}
set {
metadata.conditionalGetInfo = newValue
2018-09-15 04:33:47 +02:00
}
}
2018-09-15 06:51:05 +02:00
public var contentHash: String? {
get {
return metadata.contentHash
}
set {
metadata.contentHash = newValue
}
}
public var isNotifyAboutNewArticles: Bool? {
get {
return metadata.isNotifyAboutNewArticles
}
set {
metadata.isNotifyAboutNewArticles = newValue
}
}
public var isArticleExtractorAlwaysOn: Bool? {
get {
return metadata.isArticleExtractorAlwaysOn
}
set {
metadata.isArticleExtractorAlwaysOn = newValue
}
}
public var subscriptionID: String? {
get {
return metadata.subscriptionID
}
set {
metadata.subscriptionID = newValue
}
}
// Folder Name: Sync Service Relationship ID
public var folderRelationship: [String: String]? {
get {
return metadata.folderRelationship
}
set {
metadata.folderRelationship = 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
2019-05-06 17:53:20 +02:00
public func rename(to newName: String, completion: @escaping (Result<Void, Error>) -> Void) {
2019-05-09 00:55:53 +02:00
guard let account = account else { return }
account.renameFeed(self, to: newName, completion: completion)
}
// MARK: - UnreadCountProvider
public var unreadCount: Int {
get {
return account?.unreadCount(for: self) ?? 0
}
set {
if unreadCount == newValue {
return
}
account?.setUnreadCount(newValue, for: self)
postUnreadCountDidChangeNotification()
}
}
var metadata: FeedMetadata
// MARK: - Private
private let accountID: String // Used for hashing and equality; account may turn nil
// MARK: - Init
init(account: Account, url: String, metadata: FeedMetadata) {
self.account = account
self.accountID = account.accountID
self.url = url
self.metadata = metadata
}
// 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, strictConformance: Bool) -> 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 })
}
}