Mark appropriate functions as throws.
This commit is contained in:
parent
f2052f110f
commit
e700da9410
@ -14,6 +14,7 @@ import Foundation
|
||||
import RSCore
|
||||
import Articles
|
||||
import RSParser
|
||||
import RSDatabase
|
||||
import ArticlesDatabase
|
||||
import RSWeb
|
||||
import os.log
|
||||
@ -670,24 +671,24 @@ public final class Account: DisplayNameProvider, UnreadCountProvider, Container,
|
||||
}
|
||||
}
|
||||
|
||||
public func fetchUnreadCountForToday(_ completion: @escaping (Int) -> Void) {
|
||||
public func fetchUnreadCountForToday(_ completion: @escaping SingleUnreadCountCompletionBlock) {
|
||||
database.fetchUnreadCountForToday(for: flattenedWebFeeds().webFeedIDs(), completion: completion)
|
||||
}
|
||||
|
||||
public func fetchUnreadCountForStarredArticles(_ completion: @escaping (Int) -> Void) {
|
||||
public func fetchUnreadCountForStarredArticles(_ completion: @escaping SingleUnreadCountCompletionBlock) {
|
||||
database.fetchStarredAndUnreadCount(for: flattenedWebFeeds().webFeedIDs(), completion: completion)
|
||||
}
|
||||
|
||||
public func fetchUnreadArticleIDs(_ completion: @escaping (Set<String>) -> Void) {
|
||||
public func fetchUnreadArticleIDs(_ completion: @escaping ArticleIDsCompletionBlock) {
|
||||
database.fetchUnreadArticleIDsAsync(webFeedIDs: flattenedWebFeeds().webFeedIDs(), completion: completion)
|
||||
}
|
||||
|
||||
public func fetchStarredArticleIDs(_ completion: @escaping (Set<String>) -> Void) {
|
||||
public func fetchStarredArticleIDs(_ completion: @escaping ArticleIDsCompletionBlock) {
|
||||
database.fetchStarredArticleIDsAsync(webFeedIDs: flattenedWebFeeds().webFeedIDs(), completion: completion)
|
||||
}
|
||||
|
||||
public func fetchArticleIDsForStatusesWithoutArticles() -> Set<String> {
|
||||
return database.fetchArticleIDsForStatusesWithoutArticles()
|
||||
public func fetchArticleIDsForStatusesWithoutArticles() throws -> Set<String> {
|
||||
return try database.fetchArticleIDsForStatusesWithoutArticles()
|
||||
}
|
||||
|
||||
public func unreadCount(for webFeed: WebFeed) -> Int {
|
||||
@ -708,38 +709,48 @@ public final class Account: DisplayNameProvider, UnreadCountProvider, Container,
|
||||
webFeedDictionaryNeedsUpdate = true
|
||||
}
|
||||
|
||||
func update(_ webFeed: WebFeed, with parsedFeed: ParsedFeed, _ completion: @escaping (() -> Void)) {
|
||||
func update(_ webFeed: WebFeed, with parsedFeed: ParsedFeed, _ completion: @escaping DatabaseCompletionBlock) {
|
||||
// Used only by an On My Mac account.
|
||||
webFeed.takeSettings(from: parsedFeed)
|
||||
let webFeedIDsAndItems = [webFeed.webFeedID: parsedFeed.items]
|
||||
update(webFeedIDsAndItems: webFeedIDsAndItems, defaultRead: false, completion: completion)
|
||||
}
|
||||
|
||||
func update(webFeedIDsAndItems: [String: Set<ParsedItem>], defaultRead: Bool, completion: @escaping (() -> Void)) {
|
||||
assert(Thread.isMainThread)
|
||||
func update(webFeedIDsAndItems: [String: Set<ParsedItem>], defaultRead: Bool, completion: @escaping DatabaseCompletionBlock) {
|
||||
precondition(Thread.isMainThread)
|
||||
guard !webFeedIDsAndItems.isEmpty else {
|
||||
completion()
|
||||
return
|
||||
}
|
||||
database.update(webFeedIDsAndItems: webFeedIDsAndItems, defaultRead: defaultRead) { (newArticles, updatedArticles) in
|
||||
var userInfo = [String: Any]()
|
||||
let webFeeds = Set(webFeedIDsAndItems.compactMap { (key, _) -> WebFeed? in
|
||||
self.existingWebFeed(withWebFeedID: key)
|
||||
})
|
||||
if let newArticles = newArticles, !newArticles.isEmpty {
|
||||
self.updateUnreadCounts(for: webFeeds) {
|
||||
NotificationCenter.default.post(name: .DownloadArticlesDidUpdateUnreadCounts, object: self, userInfo: nil)
|
||||
database.update(webFeedIDsAndItems: webFeedIDsAndItems, defaultRead: defaultRead) { updateArticlesResult in
|
||||
|
||||
func process(_ newAndUpdatedArticles: NewAndUpdatedArticles) {
|
||||
var userInfo = [String: Any]()
|
||||
let webFeeds = Set(webFeedIDsAndItems.compactMap { (key, _) -> WebFeed? in
|
||||
self.existingWebFeed(withWebFeedID: key)
|
||||
})
|
||||
if let newArticles = newAndUpdatedArticles.newArticles, !newArticles.isEmpty {
|
||||
self.updateUnreadCounts(for: webFeeds) {
|
||||
NotificationCenter.default.post(name: .DownloadArticlesDidUpdateUnreadCounts, object: self, userInfo: nil)
|
||||
}
|
||||
userInfo[UserInfoKey.newArticles] = newArticles
|
||||
}
|
||||
userInfo[UserInfoKey.newArticles] = newArticles
|
||||
}
|
||||
if let updatedArticles = updatedArticles, !updatedArticles.isEmpty {
|
||||
userInfo[UserInfoKey.updatedArticles] = updatedArticles
|
||||
}
|
||||
userInfo[UserInfoKey.webFeeds] = webFeeds
|
||||
if let updatedArticles = newAndUpdatedArticles.updatedArticles, !updatedArticles.isEmpty {
|
||||
userInfo[UserInfoKey.updatedArticles] = updatedArticles
|
||||
}
|
||||
userInfo[UserInfoKey.webFeeds] = webFeeds
|
||||
|
||||
completion()
|
||||
completion(.success)
|
||||
|
||||
NotificationCenter.default.post(name: .AccountDidDownloadArticles, object: self, userInfo: userInfo)
|
||||
NotificationCenter.default.post(name: .AccountDidDownloadArticles, object: self, userInfo: userInfo)
|
||||
}
|
||||
|
||||
switch updateArticlesResult {
|
||||
case .success(let newAndUpdatedArticles):
|
||||
process(newAndUpdatedArticles)
|
||||
case .failure(let databaseError):
|
||||
completion(.failure(databaseError))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -930,40 +941,40 @@ private extension Account {
|
||||
database.fetchedStarredArticlesAsync(flattenedWebFeeds().webFeedIDs(), completion)
|
||||
}
|
||||
|
||||
func fetchUnreadArticles() -> Set<Article> {
|
||||
return fetchUnreadArticles(forContainer: self)
|
||||
func fetchUnreadArticles() throws -> Set<Article> {
|
||||
return try fetchUnreadArticles(forContainer: self)
|
||||
}
|
||||
|
||||
func fetchUnreadArticlesAsync(_ completion: @escaping ArticleSetBlock) {
|
||||
fetchUnreadArticlesAsync(forContainer: self, completion)
|
||||
}
|
||||
|
||||
func fetchTodayArticles() -> Set<Article> {
|
||||
return database.fetchTodayArticles(flattenedWebFeeds().webFeedIDs())
|
||||
func fetchTodayArticles() throws -> Set<Article> {
|
||||
return try database.fetchTodayArticles(flattenedWebFeeds().webFeedIDs())
|
||||
}
|
||||
|
||||
func fetchTodayArticlesAsync(_ completion: @escaping ArticleSetBlock) {
|
||||
database.fetchTodayArticlesAsync(flattenedWebFeeds().webFeedIDs(), completion)
|
||||
}
|
||||
|
||||
func fetchArticles(folder: Folder) -> Set<Article> {
|
||||
return fetchArticles(forContainer: folder)
|
||||
func fetchArticles(folder: Folder) throws -> Set<Article> {
|
||||
return try fetchArticles(forContainer: folder)
|
||||
}
|
||||
|
||||
func fetchArticlesAsync(folder: Folder, _ completion: @escaping ArticleSetBlock) {
|
||||
fetchArticlesAsync(forContainer: folder, completion)
|
||||
}
|
||||
|
||||
func fetchUnreadArticles(folder: Folder) -> Set<Article> {
|
||||
return fetchUnreadArticles(forContainer: folder)
|
||||
func fetchUnreadArticles(folder: Folder) throws -> Set<Article> {
|
||||
return try fetchUnreadArticles(forContainer: folder)
|
||||
}
|
||||
|
||||
func fetchUnreadArticlesAsync(folder: Folder, _ completion: @escaping ArticleSetBlock) {
|
||||
fetchUnreadArticlesAsync(forContainer: folder, completion)
|
||||
}
|
||||
|
||||
func fetchArticles(webFeed: WebFeed) -> Set<Article> {
|
||||
let articles = database.fetchArticles(webFeed.webFeedID)
|
||||
func fetchArticles(webFeed: WebFeed) throws -> Set<Article> {
|
||||
let articles = try database.fetchArticles(webFeed.webFeedID)
|
||||
validateUnreadCount(webFeed, articles)
|
||||
return articles
|
||||
}
|
||||
@ -975,12 +986,12 @@ private extension Account {
|
||||
}
|
||||
}
|
||||
|
||||
func fetchArticlesMatching(_ searchString: String) -> Set<Article> {
|
||||
return database.fetchArticlesMatching(searchString, flattenedWebFeeds().webFeedIDs())
|
||||
func fetchArticlesMatching(_ searchString: String) throws -> Set<Article> {
|
||||
return try database.fetchArticlesMatching(searchString, flattenedWebFeeds().webFeedIDs())
|
||||
}
|
||||
|
||||
func fetchArticlesMatchingWithArticleIDs(_ searchString: String, _ articleIDs: Set<String>) -> Set<Article> {
|
||||
return database.fetchArticlesMatchingWithArticleIDs(searchString, articleIDs)
|
||||
func fetchArticlesMatchingWithArticleIDs(_ searchString: String, _ articleIDs: Set<String>) throws -> Set<Article> {
|
||||
return try database.fetchArticlesMatchingWithArticleIDs(searchString, articleIDs)
|
||||
}
|
||||
|
||||
func fetchArticlesMatchingAsync(_ searchString: String, _ completion: @escaping ArticleSetBlock) {
|
||||
@ -991,16 +1002,16 @@ private extension Account {
|
||||
database.fetchArticlesMatchingWithArticleIDsAsync(searchString, articleIDs, completion)
|
||||
}
|
||||
|
||||
func fetchArticles(articleIDs: Set<String>) -> Set<Article> {
|
||||
return database.fetchArticles(articleIDs: articleIDs)
|
||||
func fetchArticles(articleIDs: Set<String>) throws -> Set<Article> {
|
||||
return try database.fetchArticles(articleIDs: articleIDs)
|
||||
}
|
||||
|
||||
func fetchArticlesAsync(articleIDs: Set<String>, _ completion: @escaping ArticleSetBlock) {
|
||||
return database.fetchArticlesAsync(articleIDs: articleIDs, completion)
|
||||
}
|
||||
|
||||
func fetchUnreadArticles(webFeed: WebFeed) -> Set<Article> {
|
||||
let articles = database.fetchUnreadArticles(Set([webFeed.webFeedID]))
|
||||
func fetchUnreadArticles(webFeed: WebFeed) throws -> Set<Article> {
|
||||
let articles = try database.fetchUnreadArticles(Set([webFeed.webFeedID]))
|
||||
validateUnreadCount(webFeed, articles)
|
||||
return articles
|
||||
}
|
||||
@ -1013,9 +1024,9 @@ private extension Account {
|
||||
}
|
||||
|
||||
|
||||
func fetchArticles(forContainer container: Container) -> Set<Article> {
|
||||
func fetchArticles(forContainer container: Container) throws -> Set<Article> {
|
||||
let feeds = container.flattenedWebFeeds()
|
||||
let articles = database.fetchArticles(feeds.webFeedIDs())
|
||||
let articles = try database.fetchArticles(feeds.webFeedIDs())
|
||||
validateUnreadCountsAfterFetchingUnreadArticles(feeds, articles)
|
||||
return articles
|
||||
}
|
||||
@ -1028,9 +1039,9 @@ private extension Account {
|
||||
}
|
||||
}
|
||||
|
||||
func fetchUnreadArticles(forContainer container: Container) -> Set<Article> {
|
||||
func fetchUnreadArticles(forContainer container: Container) throws -> Set<Article> {
|
||||
let feeds = container.flattenedWebFeeds()
|
||||
let articles = database.fetchUnreadArticles(feeds.webFeedIDs())
|
||||
let articles = try database.fetchUnreadArticles(feeds.webFeedIDs())
|
||||
validateUnreadCountsAfterFetchingUnreadArticles(feeds, articles)
|
||||
return articles
|
||||
}
|
||||
@ -1136,30 +1147,23 @@ private extension Account {
|
||||
func fetchAllUnreadCounts() {
|
||||
fetchingAllUnreadCounts = true
|
||||
|
||||
database.fetchAllNonZeroUnreadCounts { (unreadCountDictionary) in
|
||||
if unreadCountDictionary.isEmpty {
|
||||
database.fetchAllNonZeroUnreadCounts { (unreadCountDictionaryResult) in
|
||||
if let unreadCountDictionary = try? unreadCountDictionaryResult.get() {
|
||||
self.flattenedWebFeeds().forEach{ (feed) in
|
||||
// When the unread count is zero, it won’t appear in unreadCountDictionary.
|
||||
if let unreadCount = unreadCountDictionary[feed.webFeedID] {
|
||||
feed.unreadCount = unreadCount
|
||||
}
|
||||
else {
|
||||
feed.unreadCount = 0
|
||||
}
|
||||
}
|
||||
|
||||
self.fetchingAllUnreadCounts = false
|
||||
self.updateUnreadCount()
|
||||
self.isUnreadCountsInitialized = true
|
||||
self.postUnreadCountDidInitializeNotification()
|
||||
return
|
||||
}
|
||||
|
||||
self.flattenedWebFeeds().forEach{ (feed) in
|
||||
// When the unread count is zero, it won’t appear in unreadCountDictionary.
|
||||
|
||||
if let unreadCount = unreadCountDictionary[feed.webFeedID] {
|
||||
feed.unreadCount = unreadCount
|
||||
}
|
||||
else {
|
||||
feed.unreadCount = 0
|
||||
}
|
||||
}
|
||||
|
||||
self.fetchingAllUnreadCounts = false
|
||||
self.updateUnreadCount()
|
||||
self.isUnreadCountsInitialized = true
|
||||
self.postUnreadCountDidInitializeNotification()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user