Convert to async/await.

This commit is contained in:
Brent Simmons committed 2025-11-16 20:40:00 -08:00
1 parent d7c20f5fab
commit 0205acda9f
6 files changed
+37 -48

No files matched your search

+17 -10
View File
@@ -197,6 +197,9 @@ public final class Account: DisplayNameProvider, UnreadCountProvider, Container,
private var _flattenedFeeds = Set<Feed>()
private var flattenedFeedsNeedUpdate = true
private var flattenedFeedsIDs: Set<String> {
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<String> {
@@ -1069,11 +1076,11 @@ public final class Account: DisplayNameProvider, UnreadCountProvider, Container,
// MARK: - Starred Articles
func _fetchStarredArticles(limit: Int? = nil) throws -> Set<Article> {
try database.fetchStarredArticles(feedIDs: flattenedFeeds().feedIDs(), limit: limit)
try database.fetchStarredArticles(feedIDs: flattenedFeedsIDs, limit: limit)
}
func _fetchStarredArticlesAsync(limit: Int? = nil) async throws -> Set<Article> {
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<Article> {
try database.fetchTodayArticles(feedIDs: flattenedFeeds().feedIDs(), limit: limit)
try database.fetchTodayArticles(feedIDs: flattenedFeedsIDs, limit: limit)
}
func _fetchTodayArticlesAsync(limit: Int? = nil) async throws -> Set<Article> {
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<Article> {
try database.fetchArticlesMatching(searchString: searchString, feedIDs: flattenedFeeds().feedIDs())
try database.fetchArticlesMatching(searchString: searchString, feedIDs: flattenedFeedsIDs)
}
func _fetchArticlesMatchingAsync(searchString: String) async throws -> Set<Article> {
try await database.fetchArticlesMatchingAsync(searchString: searchString, feedIDs: flattenedFeeds().feedIDs())
try await database.fetchArticlesMatchingAsync(searchString: searchString, feedIDs: flattenedFeedsIDs)
}
func _fetchArticlesMatchingWithArticleIDs(searchString: String, articleIDs: Set<String>) throws -> Set<Article> {
@@ -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]) {
+1 -1
View File
@@ -24,6 +24,6 @@ import Account
return AppAssets.starredFeedImage
}
func fetchUnreadCount(account: Account) async throws -> Int? {
try await account.fetchUnreadCountForStarredArticles()
try await account.fetchUnreadCountForStarredArticlesAsync()
}
}
+1 -1
View File
@@ -24,7 +24,7 @@ struct TodayFeedDelegate: SmartFeedDelegate {
}
func fetchUnreadCount(account: Account) async throws -> Int? {
try await account.fetchUnreadCountForToday()
try await account.fetchUnreadCountForTodayAsync()
}
}
+1 -1
View File
@@ -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
@@ -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))
}
}