NetNewsWire/Shared/Data/ArticleUtilities.swift

140 lines
2.9 KiB
Swift
Raw Normal View History

2017-05-27 19:43:27 +02:00
//
// ArticleUtilities.swift
2018-08-29 07:18:24 +02:00
// NetNewsWire
2017-05-27 19:43:27 +02:00
//
// Created by Brent Simmons on 7/25/15.
// Copyright © 2015 Ranchero Software, LLC. All rights reserved.
//
import Foundation
import RSCore
import Articles
2017-09-17 21:34:10 +02:00
import Account
2017-05-27 19:43:27 +02:00
// These handle multiple accounts.
@discardableResult
func markArticles(_ articles: Set<Article>, statusKey: ArticleStatus.Key, flag: Bool) -> Set<Article>? {
2017-05-27 19:43:27 +02:00
2017-09-17 21:54:08 +02:00
let d: [String: Set<Article>] = accountAndArticlesDictionary(articles)
var updatedArticles = Set<Article>()
for (accountID, accountArticles) in d {
2017-05-27 19:43:27 +02:00
guard let account = AccountManager.shared.existingAccount(with: accountID) else {
continue
2017-05-27 19:43:27 +02:00
}
if let accountUpdatedArticles = account.markArticles(accountArticles, statusKey: statusKey, flag: flag) {
updatedArticles.formUnion(accountUpdatedArticles)
}
2017-05-27 19:43:27 +02:00
}
return updatedArticles
2017-05-27 19:43:27 +02:00
}
2017-09-17 21:54:08 +02:00
private func accountAndArticlesDictionary(_ articles: Set<Article>) -> [String: Set<Article>] {
2017-05-27 19:43:27 +02:00
let d = Dictionary(grouping: articles, by: { $0.accountID })
return d.mapValues{ Set($0) }
2017-05-27 19:43:27 +02:00
}
2017-09-17 21:54:08 +02:00
extension Article {
2017-05-27 19:43:27 +02:00
2017-09-18 02:03:58 +02:00
var feed: Feed? {
return account?.existingFeed(withFeedID: feedID)
2017-09-18 02:03:58 +02:00
}
2017-09-18 01:30:45 +02:00
var preferredLink: String? {
if let url = url, !url.isEmpty {
return url
}
if let externalURL = externalURL, !externalURL.isEmpty {
return externalURL
}
return nil
2017-09-18 01:30:45 +02:00
}
var body: String? {
return contentHTML ?? contentText ?? summary
2017-05-27 19:43:27 +02:00
}
2017-09-18 02:03:58 +02:00
var logicalDatePublished: Date {
return datePublished ?? dateModified ?? status.dateArrived
2017-09-18 02:03:58 +02:00
}
func iconImage() -> IconImage? {
if let authors = authors, authors.count == 1, let author = authors.first {
if let image = appDelegate.authorAvatarDownloader.image(for: author) {
return image
}
}
if let authors = feed?.authors, authors.count == 1, let author = authors.first {
if let image = appDelegate.authorAvatarDownloader.image(for: author) {
return image
}
}
guard let feed = feed else {
return nil
}
let feedIconImage = appDelegate.feedIconDownloader.icon(for: feed)
if feedIconImage != nil {
return feedIconImage
}
if let faviconImage = appDelegate.faviconDownloader.faviconAsIcon(for: feed) {
return faviconImage
}
return FaviconGenerator.favicon(feed)
}
2017-05-27 19:43:27 +02:00
}
// MARK: Path
2019-10-03 16:53:21 +02:00
struct ArticlePathKey {
static let accountID = "accountID"
static let accountName = "accountName"
static let feedID = "feedID"
static let articleID = "articleID"
}
extension Article {
2019-10-03 16:53:21 +02:00
public var pathUserInfo: [AnyHashable : Any] {
2019-10-03 16:53:21 +02:00
return [
ArticlePathKey.accountID: accountID,
ArticlePathKey.accountName: account?.nameForDisplay ?? "",
ArticlePathKey.feedID: feedID,
ArticlePathKey.articleID: articleID
2019-10-03 16:53:21 +02:00
]
}
}
// MARK: SortableArticle
extension Article: SortableArticle {
var sortableName: String {
return feed?.name ?? ""
}
var sortableDate: Date {
return logicalDatePublished
}
var sortableArticleID: String {
return articleID
}
var sortableFeedID: String {
return feedID
}
}