Convert importOPML to async await.

This commit is contained in:
Brent Simmons 2024-04-14 21:36:50 -07:00
parent b05aef5c51
commit ab7c594f3e
2 changed files with 24 additions and 49 deletions

View File

@ -179,65 +179,25 @@ enum CloudKitAccountDelegateError: LocalizedError {
func importOPML(for account: Account, opmlFile: URL) async throws {
try await withCheckedThrowingContinuation { continuation in
self.importOPML(for: account, opmlFile: opmlFile) { result in
switch result {
case .success:
continuation.resume()
case .failure(let error):
continuation.resume(throwing: error)
}
}
}
}
private func importOPML(for account:Account, opmlFile: URL, completion: @escaping (Result<Void, Error>) -> Void) {
guard refreshProgress.isComplete else {
completion(.success(()))
return
}
var fileData: Data?
do {
fileData = try Data(contentsOf: opmlFile)
} catch {
completion(.failure(error))
return
}
guard let opmlData = fileData else {
completion(.success(()))
return
}
let opmlData = try Data(contentsOf: opmlFile)
let parserData = ParserData(url: opmlFile.absoluteString, data: opmlData)
var opmlDocument: RSOPMLDocument?
do {
opmlDocument = try RSOPMLParser.parseOPML(with: parserData)
} catch {
completion(.failure(error))
return
}
guard let loadDocument = opmlDocument else {
completion(.success(()))
return
}
let opmlDocument = try RSOPMLParser.parseOPML(with: parserData)
guard let opmlItems = loadDocument.children, let rootExternalID = account.externalID else {
guard let opmlItems = opmlDocument.children, let rootExternalID = account.externalID else {
return
}
let normalizedItems = OPMLNormalizer.normalize(opmlItems)
refreshProgress.addToNumberOfTasksAndRemaining(1)
self.accountZone.importOPML(rootExternalID: rootExternalID, items: normalizedItems) { _ in
self.refreshProgress.completeTask()
self.standardRefreshAll(for: account, completion: completion)
}
defer { refreshProgress.completeTask() }
try await accountZone.importOPML(rootExternalID: rootExternalID, items: normalizedItems)
try await standardRefreshAll(for: account)
}
func createFeed(for account: Account, url: String, name: String?, container: Container, validateFeed: Bool) async throws -> Feed {
@ -742,6 +702,20 @@ private extension CloudKitAccountDelegate {
}
func standardRefreshAll(for account: Account) async throws {
try await withCheckedThrowingContinuation { continuation in
self.standardRefreshAll(for: account) { result in
switch result {
case .success:
continuation.resume()
case .failure(let error):
continuation.resume(throwing: error)
}
}
}
}
func standardRefreshAll(for account: Account, completion: @escaping (Result<Void, Error>) -> Void) {
let intialFeedsCount = account.flattenedFeeds().count

View File

@ -58,7 +58,8 @@ enum CloudKitAccountZoneError: LocalizedError {
migrateChangeToken()
}
func importOPML(rootExternalID: String, items: [RSOPMLItem], completion: @escaping (Result<Void, Error>) -> Void) {
func importOPML(rootExternalID: String, items: [RSOPMLItem]) async throws {
var records = [CKRecord]()
var feedRecords = [String: CKRecord]()
@ -89,7 +90,7 @@ enum CloudKitAccountZoneError: LocalizedError {
}
}
save(records, completion: completion)
try await save(records)
}
/// Persist a web feed record to iCloud and return the external key