2017-07-02 02:22:19 +02:00
|
|
|
//
|
|
|
|
// Article.swift
|
2017-07-29 21:29:05 +02:00
|
|
|
// Data
|
2017-07-02 02:22:19 +02:00
|
|
|
//
|
|
|
|
// Created by Brent Simmons on 7/1/17.
|
|
|
|
// Copyright © 2017 Ranchero Software, LLC. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
2019-07-06 05:06:31 +02:00
|
|
|
public typealias ArticleSetBlock = (Set<Article>) -> Void
|
|
|
|
|
2017-09-05 02:10:02 +02:00
|
|
|
public struct Article: Hashable {
|
2017-07-02 02:22:19 +02:00
|
|
|
|
2017-09-05 02:10:02 +02:00
|
|
|
public let articleID: String // Unique database ID (possibly sync service ID)
|
|
|
|
public let accountID: String
|
2017-07-12 21:55:48 +02:00
|
|
|
public let feedID: String // Likely a URL, but not necessarily
|
|
|
|
public let uniqueID: String // Unique per feed (RSS guid, for example)
|
2017-09-05 02:10:02 +02:00
|
|
|
public let title: String?
|
|
|
|
public let contentHTML: String?
|
|
|
|
public let contentText: String?
|
|
|
|
public let url: String?
|
|
|
|
public let externalURL: String?
|
|
|
|
public let summary: String?
|
|
|
|
public let imageURL: String?
|
|
|
|
public let bannerImageURL: String?
|
|
|
|
public let datePublished: Date?
|
|
|
|
public let dateModified: Date?
|
|
|
|
public let authors: Set<Author>?
|
|
|
|
public let attachments: Set<Attachment>?
|
2017-09-18 21:59:42 +02:00
|
|
|
public let status: ArticleStatus
|
2018-12-28 06:07:34 +01:00
|
|
|
|
2017-12-19 03:20:13 +01:00
|
|
|
public init(accountID: String, articleID: String?, feedID: String, uniqueID: String, title: String?, contentHTML: String?, contentText: String?, url: String?, externalURL: String?, summary: String?, imageURL: String?, bannerImageURL: String?, datePublished: Date?, dateModified: Date?, authors: Set<Author>?, attachments: Set<Attachment>?, status: ArticleStatus) {
|
2017-07-11 06:47:27 +02:00
|
|
|
|
2017-09-05 02:10:02 +02:00
|
|
|
self.accountID = accountID
|
2017-07-12 21:55:48 +02:00
|
|
|
self.feedID = feedID
|
|
|
|
self.uniqueID = uniqueID
|
2017-07-02 02:22:19 +02:00
|
|
|
self.title = title
|
|
|
|
self.contentHTML = contentHTML
|
|
|
|
self.contentText = contentText
|
|
|
|
self.url = url
|
|
|
|
self.externalURL = externalURL
|
|
|
|
self.summary = summary
|
|
|
|
self.imageURL = imageURL
|
|
|
|
self.bannerImageURL = bannerImageURL
|
|
|
|
self.datePublished = datePublished
|
|
|
|
self.dateModified = dateModified
|
|
|
|
self.authors = authors
|
|
|
|
self.attachments = attachments
|
2017-09-18 21:59:42 +02:00
|
|
|
self.status = status
|
|
|
|
|
2017-08-21 06:23:17 +02:00
|
|
|
if let articleID = articleID {
|
|
|
|
self.articleID = articleID
|
2017-07-17 04:36:38 +02:00
|
|
|
}
|
|
|
|
else {
|
2017-09-19 07:00:35 +02:00
|
|
|
self.articleID = Article.calculatedArticleID(feedID: feedID, uniqueID: uniqueID)
|
2017-07-17 04:36:38 +02:00
|
|
|
}
|
2017-07-02 02:22:19 +02:00
|
|
|
}
|
|
|
|
|
2017-09-19 07:00:35 +02:00
|
|
|
public static func calculatedArticleID(feedID: String, uniqueID: String) -> String {
|
|
|
|
return databaseIDWithString("\(feedID) \(uniqueID)")
|
|
|
|
}
|
2018-12-28 06:07:34 +01:00
|
|
|
|
|
|
|
// MARK: - Hashable
|
|
|
|
|
|
|
|
public func hash(into hasher: inout Hasher) {
|
|
|
|
hasher.combine(articleID)
|
|
|
|
}
|
2017-07-02 02:22:19 +02:00
|
|
|
}
|
|
|
|
|
2017-10-09 06:06:25 +02:00
|
|
|
public extension Set where Element == Article {
|
|
|
|
|
2019-02-12 16:04:18 +01:00
|
|
|
func articleIDs() -> Set<String> {
|
2017-10-09 06:06:25 +02:00
|
|
|
return Set<String>(map { $0.articleID })
|
|
|
|
}
|
2018-02-10 22:21:43 +01:00
|
|
|
|
2019-02-12 16:04:18 +01:00
|
|
|
func unreadArticles() -> Set<Article> {
|
2018-02-10 22:21:43 +01:00
|
|
|
let articles = self.filter { !$0.status.read }
|
|
|
|
return Set(articles)
|
|
|
|
}
|
2017-10-09 06:06:25 +02:00
|
|
|
}
|
2017-09-05 02:10:02 +02:00
|
|
|
|
2017-10-09 06:06:25 +02:00
|
|
|
public extension Array where Element == Article {
|
|
|
|
|
2019-02-12 16:04:18 +01:00
|
|
|
func articleIDs() -> [String] {
|
2017-10-09 06:06:25 +02:00
|
|
|
return map { $0.articleID }
|
|
|
|
}
|
|
|
|
}
|