NetNewsWire/Frameworks/Account/ArticleFetcher.swift

85 lines
2.3 KiB
Swift
Raw Normal View History

//
// ArticleFetcher.swift
// NetNewsWire
//
// Created by Brent Simmons on 2/4/18.
// Copyright © 2018 Ranchero Software, LLC. All rights reserved.
//
import Foundation
import Articles
public protocol ArticleFetcher {
func fetchArticles() -> Set<Article>
2019-12-15 02:01:34 +01:00
func fetchArticlesAsync(_ completion: @escaping ArticleSetBlock)
func fetchUnreadArticles() -> Set<Article>
2019-12-15 02:01:34 +01:00
func fetchUnreadArticlesAsync(_ completion: @escaping ArticleSetBlock)
}
extension WebFeed: ArticleFetcher {
public func fetchArticles() throws -> Set<Article> {
return try account?.fetchArticles(.webFeed(self)) ?? Set<Article>()
}
2019-12-15 02:01:34 +01:00
public func fetchArticlesAsync(_ completion: @escaping ArticleSetBlock) {
guard let account = account else {
assertionFailure("Expected feed.account, but got nil.")
2019-12-15 02:01:34 +01:00
completion(Set<Article>())
return
}
2019-12-15 02:01:34 +01:00
account.fetchArticlesAsync(.webFeed(self), completion)
}
public func fetchUnreadArticles() -> Set<Article> {
return fetchArticles().unreadArticles()
}
2019-12-15 02:01:34 +01:00
public func fetchUnreadArticlesAsync(_ completion: @escaping ArticleSetBlock) {
guard let account = account else {
assertionFailure("Expected feed.account, but got nil.")
2019-12-15 02:01:34 +01:00
completion(Set<Article>())
return
}
2019-12-15 02:01:34 +01:00
account.fetchArticlesAsync(.webFeed(self)) { completion($0.unreadArticles()) }
}
}
extension Folder: ArticleFetcher {
public func fetchArticles() throws -> Set<Article> {
2019-11-22 17:21:30 +01:00
guard let account = account else {
assertionFailure("Expected folder.account, but got nil.")
return Set<Article>()
}
return try account.fetchArticles(.folder(self, false))
}
2019-12-15 02:01:34 +01:00
public func fetchArticlesAsync(_ completion: @escaping ArticleSetBlock) {
2019-11-22 17:21:30 +01:00
guard let account = account else {
assertionFailure("Expected folder.account, but got nil.")
2019-12-15 02:01:34 +01:00
completion(Set<Article>())
2019-11-22 17:21:30 +01:00
return
}
2019-12-15 02:01:34 +01:00
account.fetchArticlesAsync(.folder(self, false), completion)
}
public func fetchUnreadArticles() throws -> Set<Article> {
guard let account = account else {
assertionFailure("Expected folder.account, but got nil.")
return Set<Article>()
}
return try account.fetchArticles(.folder(self, true))
}
2019-12-15 02:01:34 +01:00
public func fetchUnreadArticlesAsync(_ completion: @escaping ArticleSetBlock) {
guard let account = account else {
assertionFailure("Expected folder.account, but got nil.")
2019-12-15 02:01:34 +01:00
completion(Set<Article>())
return
}
2019-12-15 02:01:34 +01:00
account.fetchArticlesAsync(.folder(self, true), completion)
}
}