mirror of
https://github.com/Ranchero-Software/NetNewsWire.git
synced 2025-01-10 17:02:54 +01:00
Convert importOPML to async await.
This commit is contained in:
parent
b05aef5c51
commit
ab7c594f3e
@ -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
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user