From e700da9410947e19a17e7903551cad3d22dfe11c Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Mon, 16 Dec 2019 15:32:08 -0800 Subject: [PATCH] Mark appropriate functions as throws. --- Frameworks/Account/Account.swift | 136 ++++++++++++++++--------------- 1 file changed, 70 insertions(+), 66 deletions(-) diff --git a/Frameworks/Account/Account.swift b/Frameworks/Account/Account.swift index 14352721e..7f171b4bb 100644 --- a/Frameworks/Account/Account.swift +++ b/Frameworks/Account/Account.swift @@ -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) -> Void) { + public func fetchUnreadArticleIDs(_ completion: @escaping ArticleIDsCompletionBlock) { database.fetchUnreadArticleIDsAsync(webFeedIDs: flattenedWebFeeds().webFeedIDs(), completion: completion) } - public func fetchStarredArticleIDs(_ completion: @escaping (Set) -> Void) { + public func fetchStarredArticleIDs(_ completion: @escaping ArticleIDsCompletionBlock) { database.fetchStarredArticleIDsAsync(webFeedIDs: flattenedWebFeeds().webFeedIDs(), completion: completion) } - public func fetchArticleIDsForStatusesWithoutArticles() -> Set { - return database.fetchArticleIDsForStatusesWithoutArticles() + public func fetchArticleIDsForStatusesWithoutArticles() throws -> Set { + 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], defaultRead: Bool, completion: @escaping (() -> Void)) { - assert(Thread.isMainThread) + func update(webFeedIDsAndItems: [String: Set], 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
{ - return fetchUnreadArticles(forContainer: self) + func fetchUnreadArticles() throws -> Set
{ + return try fetchUnreadArticles(forContainer: self) } func fetchUnreadArticlesAsync(_ completion: @escaping ArticleSetBlock) { fetchUnreadArticlesAsync(forContainer: self, completion) } - func fetchTodayArticles() -> Set
{ - return database.fetchTodayArticles(flattenedWebFeeds().webFeedIDs()) + func fetchTodayArticles() throws -> Set
{ + return try database.fetchTodayArticles(flattenedWebFeeds().webFeedIDs()) } func fetchTodayArticlesAsync(_ completion: @escaping ArticleSetBlock) { database.fetchTodayArticlesAsync(flattenedWebFeeds().webFeedIDs(), completion) } - func fetchArticles(folder: Folder) -> Set
{ - return fetchArticles(forContainer: folder) + func fetchArticles(folder: Folder) throws -> Set
{ + return try fetchArticles(forContainer: folder) } func fetchArticlesAsync(folder: Folder, _ completion: @escaping ArticleSetBlock) { fetchArticlesAsync(forContainer: folder, completion) } - func fetchUnreadArticles(folder: Folder) -> Set
{ - return fetchUnreadArticles(forContainer: folder) + func fetchUnreadArticles(folder: Folder) throws -> Set
{ + return try fetchUnreadArticles(forContainer: folder) } func fetchUnreadArticlesAsync(folder: Folder, _ completion: @escaping ArticleSetBlock) { fetchUnreadArticlesAsync(forContainer: folder, completion) } - func fetchArticles(webFeed: WebFeed) -> Set
{ - let articles = database.fetchArticles(webFeed.webFeedID) + func fetchArticles(webFeed: WebFeed) throws -> Set
{ + let articles = try database.fetchArticles(webFeed.webFeedID) validateUnreadCount(webFeed, articles) return articles } @@ -975,12 +986,12 @@ private extension Account { } } - func fetchArticlesMatching(_ searchString: String) -> Set
{ - return database.fetchArticlesMatching(searchString, flattenedWebFeeds().webFeedIDs()) + func fetchArticlesMatching(_ searchString: String) throws -> Set
{ + return try database.fetchArticlesMatching(searchString, flattenedWebFeeds().webFeedIDs()) } - func fetchArticlesMatchingWithArticleIDs(_ searchString: String, _ articleIDs: Set) -> Set
{ - return database.fetchArticlesMatchingWithArticleIDs(searchString, articleIDs) + func fetchArticlesMatchingWithArticleIDs(_ searchString: String, _ articleIDs: Set) throws -> Set
{ + 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) -> Set
{ - return database.fetchArticles(articleIDs: articleIDs) + func fetchArticles(articleIDs: Set) throws -> Set
{ + return try database.fetchArticles(articleIDs: articleIDs) } func fetchArticlesAsync(articleIDs: Set, _ completion: @escaping ArticleSetBlock) { return database.fetchArticlesAsync(articleIDs: articleIDs, completion) } - func fetchUnreadArticles(webFeed: WebFeed) -> Set
{ - let articles = database.fetchUnreadArticles(Set([webFeed.webFeedID])) + func fetchUnreadArticles(webFeed: WebFeed) throws -> Set
{ + 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
{ + func fetchArticles(forContainer container: Container) throws -> Set
{ 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
{ + func fetchUnreadArticles(forContainer container: Container) throws -> Set
{ 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() } } }