From b107a86ba9c5cd05256c4fe3a4a07c6c95a639cf Mon Sep 17 00:00:00 2001 From: Jackson Harper Date: Sun, 5 Jun 2022 12:43:10 -0700 Subject: [PATCH] Handle response ids when saving items This will be used when handling pages that change their IDs when saved. --- .../DataService/Mutations/SavePDF.swift | 15 +++++++++----- .../DataService/Mutations/SaveUrl.swift | 14 +++++++++---- .../Services/DataService/OfflineSync.swift | 20 ++++++++++++------- 3 files changed, 33 insertions(+), 16 deletions(-) diff --git a/apple/OmnivoreKit/Sources/Services/DataService/Mutations/SavePDF.swift b/apple/OmnivoreKit/Sources/Services/DataService/Mutations/SavePDF.swift index 22017a74d..43cd10ef6 100644 --- a/apple/OmnivoreKit/Sources/Services/DataService/Mutations/SavePDF.swift +++ b/apple/OmnivoreKit/Sources/Services/DataService/Mutations/SavePDF.swift @@ -110,8 +110,7 @@ public extension DataService { } } - // swiftlint:disable:next line_length - func saveFilePublisher(requestId: String, uploadFileId: String, url: String) async throws { + func saveFilePublisher(requestId: String, uploadFileId: String, url: String) async throws -> String? { enum MutationResult { case saved(requestId: String, url: String) case error(errorCode: Enums.SaveErrorCode) @@ -127,7 +126,13 @@ public extension DataService { let selection = Selection { try $0.on( saveError: .init { .error(errorCode: (try? $0.errorCodes().first) ?? .unknown) }, - saveSuccess: .init { .saved(requestId: requestId, url: (try? $0.url()) ?? "") } + saveSuccess: .init { + if let requestId = try? $0.clientRequestId(), let url = try? $0.url() { + return .saved(requestId: requestId, url: url) + } else { + return .error(errorCode: .unknown) + } + } ) } @@ -148,8 +153,8 @@ public extension DataService { } switch payload.data { - case .saved: - continuation.resume() + case let .saved(requestId: requestId, url: _): + continuation.resume(returning: requestId) case let .error(errorCode: errorCode): switch errorCode { case .unauthorized: diff --git a/apple/OmnivoreKit/Sources/Services/DataService/Mutations/SaveUrl.swift b/apple/OmnivoreKit/Sources/Services/DataService/Mutations/SaveUrl.swift index 34ac0ddf3..ade5c1f5d 100644 --- a/apple/OmnivoreKit/Sources/Services/DataService/Mutations/SaveUrl.swift +++ b/apple/OmnivoreKit/Sources/Services/DataService/Mutations/SaveUrl.swift @@ -3,7 +3,7 @@ import Models import SwiftGraphQL public extension DataService { - func saveURL(id: String, url: String) async throws { + func saveURL(id: String, url: String) async throws -> String? { enum MutationResult { case saved(requestId: String, url: String) case error(errorCode: Enums.SaveErrorCode) @@ -18,7 +18,13 @@ public extension DataService { let selection = Selection { try $0.on( saveError: .init { .error(errorCode: (try? $0.errorCodes().first) ?? .unknown) }, - saveSuccess: .init { .saved(requestId: id, url: (try? $0.url()) ?? "") } + saveSuccess: .init { + if let requestId = try? $0.clientRequestId(), let url = try? $0.url() { + return .saved(requestId: requestId, url: url) + } else { + return .error(errorCode: .unknown) + } + } ) } @@ -41,8 +47,8 @@ public extension DataService { } switch payload.data { - case .saved: - continuation.resume() + case let .saved(requestId: requestId, url: _): + continuation.resume(returning: requestId) case let .error(errorCode: errorCode): switch errorCode { case .unauthorized: diff --git a/apple/OmnivoreKit/Sources/Services/DataService/OfflineSync.swift b/apple/OmnivoreKit/Sources/Services/DataService/OfflineSync.swift index d92ce1631..b3b74e7b0 100644 --- a/apple/OmnivoreKit/Sources/Services/DataService/OfflineSync.swift +++ b/apple/OmnivoreKit/Sources/Services/DataService/OfflineSync.swift @@ -35,18 +35,22 @@ public extension DataService { } } - private func updateLinkedItemStatus(id: String, status: ServerSyncStatus) async throws { + private func updateLinkedItemStatus(id: String, newId _: String?, status: ServerSyncStatus) async throws { try backgroundContext.performAndWait { let fetchRequest: NSFetchRequest = LinkedItem.fetchRequest() fetchRequest.predicate = NSPredicate(format: "id == %@", id) guard let linkedItem = (try? backgroundContext.fetch(fetchRequest))?.first else { return } + // TODO: handle item id changes + // linkedItem.id = linkedItem.serverSyncStatus = Int64(status.rawValue) } } func syncPdf(id: String, localPdfURL: URL, url: String) async throws { do { + try await updateLinkedItemStatus(id: id, newId: nil, status: .isSyncing) + let uploadRequest = try await uploadFileRequest(id: id, url: url) if let urlString = uploadRequest.urlString, let uploadUrl = URL(string: urlString) { try await uploadFile(id: id, localPdfURL: localPdfURL, url: uploadUrl) @@ -55,7 +59,7 @@ public extension DataService { throw SaveArticleError.badData } - try await updateLinkedItemStatus(id: id, status: .isNSync) + try await updateLinkedItemStatus(id: id, newId: nil, status: .isNSync) try backgroundContext.performAndWait { try backgroundContext.save() } @@ -69,14 +73,14 @@ public extension DataService { func syncPage(id: String, originalHtml: String, title: String?, url: String) async throws { do { + try await updateLinkedItemStatus(id: id, newId: nil, status: .isSyncing) + let newId = try await savePage(id: id, url: url, title: title ?? url, originalHtml: originalHtml) - print("NEW ID FOR ITEM", newId, "FROM OLD ID", id) - try await updateLinkedItemStatus(id: id, status: .isNSync) + try await updateLinkedItemStatus(id: id, newId: newId, status: .isNSync) try backgroundContext.performAndWait { try backgroundContext.save() } } catch { - print("ERROR SYNCING PAGE", error) backgroundContext.performAndWait { backgroundContext.rollback() } @@ -86,8 +90,10 @@ public extension DataService { func syncUrl(id: String, url: String) async throws { do { - try await updateLinkedItemStatus(id: id, status: .isSyncing) - try await saveURL(id: id, url: url) + try await updateLinkedItemStatus(id: id, newId: nil, status: .isSyncing) + + let newId = try await saveURL(id: id, url: url) + try await updateLinkedItemStatus(id: id, newId: newId, status: .isNSync) try backgroundContext.performAndWait { try backgroundContext.save() }