Convert restoreFeed to async/await.

This commit is contained in:
Brent Simmons 2024-03-27 17:18:17 -07:00
parent 934a8c89fa
commit a7ba7e3b4a
9 changed files with 100 additions and 30 deletions

View File

@ -635,11 +635,13 @@ public enum FetchType {
delegate.renameFeed(for: self, with: feed, to: name, completion: completion) delegate.renameFeed(for: self, with: feed, to: name, completion: completion)
} }
public func restoreFeed(_ feed: Feed, container: Container, completion: @escaping (Result<Void, Error>) -> Void) { public func restoreFeed(_ feed: Feed, container: Container) async throws {
delegate.restoreFeed(for: self, feed: feed, container: container, completion: completion)
try await delegate.restoreFeed(for: self, feed: feed, container: container)
} }
public func addFolder(_ name: String) async throws -> Folder { public func addFolder(_ name: String) async throws -> Folder {
try await delegate.createFolder(for: self, name: name) try await delegate.createFolder(for: self, name: name)
} }

View File

@ -42,7 +42,7 @@ import Secrets
func removeFeed(for account: Account, with feed: Feed, from container: Container, completion: @escaping (Result<Void, Error>) -> Void) func removeFeed(for account: Account, with feed: Feed, from container: Container, completion: @escaping (Result<Void, Error>) -> Void)
func moveFeed(for account: Account, with feed: Feed, from: Container, to: Container, completion: @escaping (Result<Void, Error>) -> Void) func moveFeed(for account: Account, with feed: Feed, from: Container, to: Container, completion: @escaping (Result<Void, Error>) -> Void)
func restoreFeed(for account: Account, feed: Feed, container: Container, completion: @escaping (Result<Void, Error>) -> Void) func restoreFeed(for account: Account, feed: Feed, container: Container) async throws
func restoreFolder(for account: Account, folder: Folder) async throws func restoreFolder(for account: Account, folder: Folder) async throws
func markArticles(for account: Account, articles: Set<Article>, statusKey: ArticleStatus.Key, flag: Bool) async throws func markArticles(for account: Account, articles: Set<Article>, statusKey: ArticleStatus.Key, flag: Bool) async throws

View File

@ -321,17 +321,21 @@ enum CloudKitAccountDelegateError: LocalizedError {
} }
} }
func restoreFeed(for account: Account, feed: Feed, container: Container, completion: @escaping (Result<Void, Error>) -> Void) { func restoreFeed(for account: Account, feed: Feed, container: any Container) async throws {
createFeed(for: account, url: feed.url, name: feed.editedName, container: container, validateFeed: true) { result in
switch result { try await withCheckedThrowingContinuation { continuation in
case .success:
completion(.success(())) self.createFeed(for: account, url: feed.url, name: feed.editedName, container: container, validateFeed: true) { result in
case .failure(let error): switch result {
completion(.failure(error)) case .success:
continuation.resume()
case .failure(let error):
continuation.resume(throwing: error)
}
} }
} }
} }
func createFolder(for account: Account, name: String) async throws -> Folder { func createFolder(for account: Account, name: String) async throws -> Folder {
try await withCheckedThrowingContinuation { continuation in try await withCheckedThrowingContinuation { continuation in
@ -471,17 +475,15 @@ enum CloudKitAccountDelegateError: LocalizedError {
folder.topLevelFeeds.remove(feed) folder.topLevelFeeds.remove(feed)
group.enter() group.enter()
self.restoreFeed(for: account, feed: feed, container: folder) { result in
self.refreshProgress.completeTask() Task { @MainActor in
group.leave() do {
switch result { try await self.restoreFeed(for: account, feed: feed, container: folder)
case .success: } catch {
break
case .failure(let error):
os_log(.error, log: self.log, "Restore folder feed error: %@.", error.localizedDescription) os_log(.error, log: self.log, "Restore folder feed error: %@.", error.localizedDescription)
} }
group.leave()
} }
} }
group.notify(queue: DispatchQueue.main) { group.notify(queue: DispatchQueue.main) {

View File

@ -551,7 +551,22 @@ final class FeedbinAccountDelegate: AccountDelegate {
} }
func restoreFeed(for account: Account, feed: Feed, container: Container, completion: @escaping (Result<Void, Error>) -> Void) { func restoreFeed(for account: Account, feed: Feed, container: any Container) async throws {
try await withCheckedThrowingContinuation { continuation in
self.restoreFeed(for: account, feed: feed, container: container) { result in
switch result {
case .success:
continuation.resume()
case .failure(let error):
continuation.resume(throwing: error)
}
}
}
}
private func restoreFeed(for account: Account, feed: Feed, container: Container, completion: @escaping (Result<Void, Error>) -> Void) {
if let existingFeed = account.existingFeed(withURL: feed.url) { if let existingFeed = account.existingFeed(withURL: feed.url) {
account.addFeed(existingFeed, to: container) { result in account.addFeed(existingFeed, to: container) { result in

View File

@ -533,7 +533,22 @@ final class FeedlyAccountDelegate: AccountDelegate {
to.addFeed(feed) to.addFeed(feed)
} }
@MainActor func restoreFeed(for account: Account, feed: Feed, container: Container, completion: @escaping (Result<Void, Error>) -> Void) { func restoreFeed(for account: Account, feed: Feed, container: any Container) async throws {
try await withCheckedThrowingContinuation { continuation in
self.restoreFeed(for: account, feed: feed, container: container) { result in
switch result {
case .success:
continuation.resume()
case .failure(let error):
continuation.resume(throwing: error)
}
}
}
}
private func restoreFeed(for account: Account, feed: Feed, container: Container, completion: @escaping (Result<Void, Error>) -> Void) {
if let existingFeed = account.existingFeed(withURL: feed.url) { if let existingFeed = account.existingFeed(withURL: feed.url) {
account.addFeed(existingFeed, to: container) { result in account.addFeed(existingFeed, to: container) { result in
switch result { switch result {

View File

@ -112,11 +112,11 @@ final class LocalAccountDelegate: AccountDelegate {
completion(.success(())) completion(.success(()))
} }
func restoreFeed(for account: Account, feed: Feed, container: Container, completion: @escaping (Result<Void, Error>) -> Void) { func restoreFeed(for account: Account, feed: Feed, container: any Container) async throws {
container.addFeed(feed) container.addFeed(feed)
completion(.success(()))
} }
func createFolder(for account: Account, name: String) async throws -> Folder { func createFolder(for account: Account, name: String) async throws -> Folder {
guard let folder = account.ensureFolder(with: name) else { guard let folder = account.ensureFolder(with: name) else {

View File

@ -581,7 +581,22 @@ final class NewsBlurAccountDelegate: AccountDelegate {
} }
} }
func restoreFeed(for account: Account, feed: Feed, container: Container, completion: @escaping (Result<Void, Error>) -> ()) { func restoreFeed(for account: Account, feed: Feed, container: any Container) async throws {
try await withCheckedThrowingContinuation { continuation in
self.restoreFeed(for: account, feed: feed, container: container) { result in
switch result {
case .success:
continuation.resume()
case .failure(let error):
continuation.resume(throwing: error)
}
}
}
}
private func restoreFeed(for account: Account, feed: Feed, container: Container, completion: @escaping (Result<Void, Error>) -> ()) {
if let existingFeed = account.existingFeed(withURL: feed.url) { if let existingFeed = account.existingFeed(withURL: feed.url) {
account.addFeed(existingFeed, to: container) { result in account.addFeed(existingFeed, to: container) { result in
switch result { switch result {

View File

@ -603,7 +603,22 @@ final class ReaderAPIAccountDelegate: AccountDelegate {
} }
} }
func restoreFeed(for account: Account, feed: Feed, container: Container, completion: @escaping (Result<Void, Error>) -> Void) { func restoreFeed(for account: Account, feed: Feed, container: any Container) async throws {
try await withCheckedThrowingContinuation { continuation in
self.restoreFeed(for: account, feed: feed, container: container) { result in
switch result {
case .success:
continuation.resume()
case .failure(let error):
continuation.resume(throwing: error)
}
}
}
}
private func restoreFeed(for account: Account, feed: Feed, container: Container, completion: @escaping (Result<Void, Error>) -> Void) {
if let existingFeed = account.existingFeed(withURL: feed.url) { if let existingFeed = account.existingFeed(withURL: feed.url) {
account.addFeed(existingFeed, to: container) { result in account.addFeed(existingFeed, to: container) { result in

View File

@ -187,11 +187,17 @@ import Core
} }
BatchUpdate.shared.start() BatchUpdate.shared.start()
account.restoreFeed(feed, container: container) { result in
BatchUpdate.shared.end()
self.checkResult(result)
}
Task { @MainActor in
do {
try await account.restoreFeed(feed, container: container)
BatchUpdate.shared.end()
} catch {
BatchUpdate.shared.end()
self.errorHandler(error)
}
}
} }
private func restoreFolder() { private func restoreFolder() {