diff --git a/Modules/Account/Sources/Account/Account.swift b/Modules/Account/Sources/Account/Account.swift index 4b2bf43bd..36dbd2c0d 100644 --- a/Modules/Account/Sources/Account/Account.swift +++ b/Modules/Account/Sources/Account/Account.swift @@ -197,6 +197,9 @@ public final class Account: DisplayNameProvider, UnreadCountProvider, Container, private var _flattenedFeeds = Set() private var flattenedFeedsNeedUpdate = true + private var flattenedFeedsIDs: Set { + flattenedFeeds().feedIDs() + } private lazy var opmlFile = OPMLFile(filename: (dataFolder as NSString).appendingPathComponent("Subscriptions.opml"), account: self) private lazy var metadataFile = AccountMetadataFile(filename: (dataFolder as NSString).appendingPathComponent("Settings.plist"), account: self) @@ -306,7 +309,7 @@ public final class Account: DisplayNameProvider, UnreadCountProvider, Container, } DispatchQueue.main.async { - self.database.cleanupDatabaseAtStartup(subscribedToFeedIDs: self.flattenedFeeds().feedIDs()) + self.database.cleanupDatabaseAtStartup(subscribedToFeedIDs: self.flattenedFeedsIDs) self._fetchAllUnreadCounts() } @@ -745,11 +748,15 @@ public final class Account: DisplayNameProvider, UnreadCountProvider, Container, } public func fetchUnreadCountForStarredArticlesAsync() async throws -> Int? { - try await database.fetchUnreadCountForStarredArticlesAsync(feedIDs: flattenedFeeds().feedIDs()) + try await database.fetchUnreadCountForStarredArticlesAsync(feedIDs: flattenedFeedsIDs) } - + public func fetchCountForStarredArticles() throws -> Int { - try database.fetchStarredArticlesCount(feedIDs: flattenedFeeds().feedIDs()) + try database.fetchStarredArticlesCount(feedIDs: flattenedFeedsIDs) + } + + public func fetchUnreadCountForTodayAsync() async throws -> Int { + try await database.fetchUnreadCountForTodayAsync(feedIDs: flattenedFeedsIDs) } public func fetchUnreadArticleIDsAsync() async throws -> Set { @@ -1069,11 +1076,11 @@ public final class Account: DisplayNameProvider, UnreadCountProvider, Container, // MARK: - Starred Articles func _fetchStarredArticles(limit: Int? = nil) throws -> Set
{ - try database.fetchStarredArticles(feedIDs: flattenedFeeds().feedIDs(), limit: limit) + try database.fetchStarredArticles(feedIDs: flattenedFeedsIDs, limit: limit) } func _fetchStarredArticlesAsync(limit: Int? = nil) async throws -> Set
{ - try await database.fetchedStarredArticlesAsync(feedIDs: flattenedFeeds().feedIDs(), limit: limit) + try await database.fetchedStarredArticlesAsync(feedIDs: flattenedFeedsIDs, limit: limit) } // MARK: - Account Unread Articles @@ -1089,11 +1096,11 @@ public final class Account: DisplayNameProvider, UnreadCountProvider, Container, // MARK: - Today Articles func _fetchTodayArticles(limit: Int? = nil) throws -> Set
{ - try database.fetchTodayArticles(feedIDs: flattenedFeeds().feedIDs(), limit: limit) + try database.fetchTodayArticles(feedIDs: flattenedFeedsIDs, limit: limit) } func _fetchTodayArticlesAsync(limit: Int? = nil) async throws -> Set
{ - try await database.fetchTodayArticlesAsync(feedIDs: flattenedFeeds().feedIDs(), limit: limit) + try await database.fetchTodayArticlesAsync(feedIDs: flattenedFeedsIDs, limit: limit) } // MARK: - Container Articles @@ -1172,11 +1179,11 @@ public final class Account: DisplayNameProvider, UnreadCountProvider, Container, // MARK: - Search Articles func _fetchArticlesMatching(searchString: String) throws -> Set
{ - try database.fetchArticlesMatching(searchString: searchString, feedIDs: flattenedFeeds().feedIDs()) + try database.fetchArticlesMatching(searchString: searchString, feedIDs: flattenedFeedsIDs) } func _fetchArticlesMatchingAsync(searchString: String) async throws -> Set
{ - try await database.fetchArticlesMatchingAsync(searchString: searchString, feedIDs: flattenedFeeds().feedIDs()) + try await database.fetchArticlesMatchingAsync(searchString: searchString, feedIDs: flattenedFeedsIDs) } func _fetchArticlesMatchingWithArticleIDs(searchString: String, articleIDs: Set) throws -> Set
{ diff --git a/Modules/Account/Tests/AccountTests/Feedly/FeedlyTestSupport.swift b/Modules/Account/Tests/AccountTests/Feedly/FeedlyTestSupport.swift index 16ee513c7..78dd8b552 100644 --- a/Modules/Account/Tests/AccountTests/Feedly/FeedlyTestSupport.swift +++ b/Modules/Account/Tests/AccountTests/Feedly/FeedlyTestSupport.swift @@ -212,51 +212,33 @@ import SyncDatabase } } - func checkUnreadStatuses(in account: Account, againstIdsInStreamInJSONNamed name: String, subdirectory: String? = nil, testCase: XCTestCase) { + func checkUnreadStatuses(in account: Account, againstIdsInStreamInJSONNamed name: String, subdirectory: String? = nil, testCase: XCTestCase) async throws { let streamIds = testJSON(named: name, subdirectory: subdirectory) as! [String:Any] - checkUnreadStatuses(in: account, correspondToIdsInJSONPayload: streamIds, testCase: testCase) + try await checkUnreadStatuses(in: account, correspondToIdsInJSONPayload: streamIds, testCase: testCase) } - func checkUnreadStatuses(in testAccount: Account, correspondToIdsInJSONPayload streamIds: [String: Any], testCase: XCTestCase) { + func checkUnreadStatuses(in testAccount: Account, correspondToIdsInJSONPayload streamIds: [String: Any], testCase: XCTestCase) async throws { let ids = Set(streamIds["ids"] as! [String]) - let fetchIdsExpectation = testCase.expectation(description: "Fetch Article Ids") - testAccount.fetchUnreadArticleIDs { articleIdsResult in - do { - let articleIds = try articleIdsResult.get() - // Unread statuses can be paged from Feedly. - // Instead of joining test data, the best we can do is - // make sure that these ids are marked as unread (a subset of the total). - XCTAssertTrue(ids.isSubset(of: articleIds), "Some articles in `ids` are not marked as unread.") - fetchIdsExpectation.fulfill() - } catch { - XCTFail("Error unwrapping article IDs: \(error)") - } - } - testCase.wait(for: [fetchIdsExpectation], timeout: 2) + let articleIDs = try await testAccount.fetchUnreadArticleIDsAsync() + // Unread statuses can be paged from Feedly. + // Instead of joining test data, the best we can do is + // make sure that these ids are marked as unread (a subset of the total). + XCTAssertTrue(ids.isSubset(of: articleIDs), "Some articles in `ids` are not marked as unread.") } - func checkStarredStatuses(in account: Account, againstItemsInStreamInJSONNamed name: String, subdirectory: String? = nil, testCase: XCTestCase) { + func checkStarredStatuses(in account: Account, againstItemsInStreamInJSONNamed name: String, subdirectory: String? = nil, testCase: XCTestCase) async throws { let streamIds = testJSON(named: name, subdirectory: subdirectory) as! [String:Any] - checkStarredStatuses(in: account, correspondToStreamItemsIn: streamIds, testCase: testCase) + try await checkStarredStatuses(in: account, correspondToStreamItemsIn: streamIds, testCase: testCase) } - func checkStarredStatuses(in testAccount: Account, correspondToStreamItemsIn stream: [String: Any], testCase: XCTestCase) { + func checkStarredStatuses(in testAccount: Account, correspondToStreamItemsIn stream: [String: Any], testCase: XCTestCase) async throws { let items = stream["items"] as! [[String: Any]] let ids = Set(items.map { $0["id"] as! String }) - let fetchIdsExpectation = testCase.expectation(description: "Fetch Article Ids") - testAccount.fetchStarredArticleIDs { articleIdsResult in - do { - let articleIds = try articleIdsResult.get() - // Starred articles can be paged from Feedly. - // Instead of joining test data, the best we can do is - // make sure that these articles are marked as starred (a subset of the total). - XCTAssertTrue(ids.isSubset(of: articleIds), "Some articles in `ids` are not marked as starred.") - fetchIdsExpectation.fulfill() - } catch { - XCTFail("Error unwrapping article IDs: \(error)") - } - } - testCase.wait(for: [fetchIdsExpectation], timeout: 2) + let articleIDs = try await testAccount.fetchStarredArticleIDsAsync() + // Starred articles can be paged from Feedly. + // Instead of joining test data, the best we can do is + // make sure that these articles are marked as starred (a subset of the total). + XCTAssertTrue(ids.isSubset(of: articleIDs), "Some articles in `ids` are not marked as starred.") } func check(_ entries: [FeedlyEntry], correspondToStreamItemsIn stream: [String: Any]) { diff --git a/Shared/SmartFeeds/StarredFeedDelegate.swift b/Shared/SmartFeeds/StarredFeedDelegate.swift index 69effc192..49174a9ab 100644 --- a/Shared/SmartFeeds/StarredFeedDelegate.swift +++ b/Shared/SmartFeeds/StarredFeedDelegate.swift @@ -24,6 +24,6 @@ import Account return AppAssets.starredFeedImage } func fetchUnreadCount(account: Account) async throws -> Int? { - try await account.fetchUnreadCountForStarredArticles() + try await account.fetchUnreadCountForStarredArticlesAsync() } } diff --git a/Shared/SmartFeeds/TodayFeedDelegate.swift b/Shared/SmartFeeds/TodayFeedDelegate.swift index b78b352d0..fffe4d016 100644 --- a/Shared/SmartFeeds/TodayFeedDelegate.swift +++ b/Shared/SmartFeeds/TodayFeedDelegate.swift @@ -24,7 +24,7 @@ struct TodayFeedDelegate: SmartFeedDelegate { } func fetchUnreadCount(account: Account) async throws -> Int? { - try await account.fetchUnreadCountForToday() + try await account.fetchUnreadCountForTodayAsync() } } diff --git a/iOS/AppDelegate.swift b/iOS/AppDelegate.swift index 584903eb8..9fddc9e31 100644 --- a/iOS/AppDelegate.swift +++ b/iOS/AppDelegate.swift @@ -422,7 +422,7 @@ private extension AppDelegate { return } - guard let singleArticleSet = try? account.fetchArticlesSync(.articleIDs([articleID])) else { + guard let singleArticleSet = try? account.fetchArticles(.articleIDs([articleID])) else { assertionFailure("Expected article with \(articleID)") Self.logger.error("No article with articleID found \(articleID) from status notification") return diff --git a/iOS/MainFeed/MainFeedCollectionViewController.swift b/iOS/MainFeed/MainFeedCollectionViewController.swift index e2d76cf98..2ddebb20d 100644 --- a/iOS/MainFeed/MainFeedCollectionViewController.swift +++ b/iOS/MainFeed/MainFeedCollectionViewController.swift @@ -1112,7 +1112,7 @@ extension MainFeedCollectionViewController { MarkAsReadAlertController.confirm(self, coordinator: self?.coordinator, confirmTitle: title, sourceType: contentView) { [weak self] in // If you don't have this delay the screen flashes when it executes this code DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { - if let articles = try? account.fetchArticlesSync(.unread()) { + if let articles = try? account.fetchArticles(.unread()) { self?.coordinator.markAllAsRead(Array(articles)) } }