diff --git a/apple/OmnivoreKit/Sources/Services/DataService/Mutations/SavePDF.swift b/apple/OmnivoreKit/Sources/Services/DataService/Mutations/SavePDF.swift index 86717ff88..437e23a3b 100644 --- a/apple/OmnivoreKit/Sources/Services/DataService/Mutations/SavePDF.swift +++ b/apple/OmnivoreKit/Sources/Services/DataService/Mutations/SavePDF.swift @@ -10,17 +10,17 @@ private struct UploadFileRequestPayload { } private extension DataService { - public func uploadFileRequest(item: LinkedItem) async throws -> URL { + public func uploadFileRequest(id: String, url: String) async throws -> URL { enum MutationResult { case success(payload: UploadFileRequestPayload) case error(errorCode: Enums.UploadFileRequestErrorCode?) } let input = InputObjects.UploadFileRequestInput( - url: item.unwrappedPageURLString, + url: url, contentType: "application/pdf", createPageEntry: OptionalArgument(true), - clientRequestId: OptionalArgument(item.unwrappedID) + clientRequestId: OptionalArgument(id) ) let selection = Selection { @@ -78,8 +78,8 @@ private extension DataService { } } - public func uploadFile(item: LinkedItem, url: URL) -> URLSessionTask? { - if let localPdfURL = item.localPdfURL, let localUrl = URL(string: localPdfURL) { + public func uploadFile(localPdfURL: String?, url: URL) -> URLSessionTask? { + if let localPdfURL = localPdfURL, let localUrl = URL(string: localPdfURL) { var request = URLRequest(url: url) request.httpMethod = "PUT" request.setValue("application/pdf", forHTTPHeaderField: "content-type") diff --git a/apple/OmnivoreKit/Sources/Services/DataService/Mutations/SavePage.swift b/apple/OmnivoreKit/Sources/Services/DataService/Mutations/SavePage.swift index 7229db5c3..8108ae05c 100644 --- a/apple/OmnivoreKit/Sources/Services/DataService/Mutations/SavePage.swift +++ b/apple/OmnivoreKit/Sources/Services/DataService/Mutations/SavePage.swift @@ -3,23 +3,23 @@ import Models import SwiftGraphQL public extension DataService { - func savePage(item: LinkedItem) async throws { + func savePage(id: String, url: String, title: String, originalHtml: String) async throws { enum MutationResult { case saved(requestId: String, url: String) case error(errorCode: Enums.SaveErrorCode) } let input = InputObjects.SavePageInput( - url: item.unwrappedPageURLString, + url: url, source: "ios-page", - clientRequestId: item.unwrappedID, - title: OptionalArgument(item.title), - originalContent: item.originalHtml! + clientRequestId: id, + title: OptionalArgument(title), + originalContent: originalHtml ) let selection = Selection { try $0.on( - saveSuccess: .init { .saved(requestId: item.unwrappedID, url: (try? $0.url()) ?? "") }, + saveSuccess: .init { .saved(requestId: id, url: (try? $0.url()) ?? "") }, saveError: .init { .error(errorCode: (try? $0.errorCodes().first) ?? .unknown) } diff --git a/apple/OmnivoreKit/Sources/Services/DataService/Mutations/SaveUrl.swift b/apple/OmnivoreKit/Sources/Services/DataService/Mutations/SaveUrl.swift index 2cacf3d5c..887e412c5 100644 --- a/apple/OmnivoreKit/Sources/Services/DataService/Mutations/SaveUrl.swift +++ b/apple/OmnivoreKit/Sources/Services/DataService/Mutations/SaveUrl.swift @@ -3,23 +3,21 @@ import Models import SwiftGraphQL public extension DataService { - func saveURL(item: LinkedItem) async throws { + func saveURL(id: String, url: String) async throws { enum MutationResult { case saved(requestId: String, url: String) case error(errorCode: Enums.SaveErrorCode) } let input = InputObjects.SaveUrlInput( - url: item.unwrappedPageURLString, + url: url, source: "ios-url", - clientRequestId: item.unwrappedID + clientRequestId: id ) - print("UPLOADING ITEM:", item.unwrappedID, item) - let requestId = item.unwrappedID let selection = Selection { try $0.on( - saveSuccess: .init { .saved(requestId: requestId, url: (try? $0.url()) ?? "") }, + saveSuccess: .init { .saved(requestId: id, url: (try? $0.url()) ?? "") }, saveError: .init { .error(errorCode: (try? $0.errorCodes().first) ?? .unknown) } ) } diff --git a/apple/OmnivoreKit/Sources/Services/DataService/OfflineSync.swift b/apple/OmnivoreKit/Sources/Services/DataService/OfflineSync.swift index 225a4e4a5..115a84bc4 100644 --- a/apple/OmnivoreKit/Sources/Services/DataService/OfflineSync.swift +++ b/apple/OmnivoreKit/Sources/Services/DataService/OfflineSync.swift @@ -35,38 +35,40 @@ extension DataService { } } - public func syncLocalCreatedLinkedItem(item: LinkedItem) async -> Bool { + public func syncLocalCreatedLinkedItem(item: LinkedItem) { switch item.contentReader { case "PDF": - // SaveFile - do { - let uploadRequest = try await uploadFileRequest(item: item) - uploadFile(item: item, url: uploadRequest) - } catch { - logger.debug("Failed to upload PDF LinkedItem: \(error.localizedDescription)") - return false + let id = item.unwrappedID + let localPdfURL = item.localPdfURL + let url = item.unwrappedPageURLString + Task { + let uploadRequestUrl = try await uploadFileRequest(id: id, url: url) + await uploadFile(localPdfURL: localPdfURL, url: uploadRequestUrl) + try await backgroundContext.perform { + item.serverSyncStatus = Int64(ServerSyncStatus.isNSync.rawValue) + try self.backgroundContext.save() + } } case "WEB": - do { - if item.originalHtml != nil { - try await savePage(item: item) + let id = item.unwrappedID + let url = item.unwrappedPageURLString + let title = item.unwrappedTitle + let originalHtml = item.originalHtml + + Task { + if let originalHtml = originalHtml { + try await savePage(id: id, url: url, title: title, originalHtml: originalHtml) } else { - try await saveURL(item: item) + try await saveURL(id: id, url: url) + } + try await backgroundContext.perform { + item.serverSyncStatus = Int64(ServerSyncStatus.isNSync.rawValue) + try self.backgroundContext.save() } - item.serverSyncStatus = Int64(ServerSyncStatus.isNSync.rawValue) - print("ITEMS SYNCED") - try backgroundContext.save() - return true - } catch { - print("Error saving", error) - backgroundContext.rollback() - logger.debug("Failed to sync LinkedItem: \(error.localizedDescription)") - return false } default: - return false + break } - return false } private func syncLinkedItems(unsyncedLinkedItems: [LinkedItem]) { @@ -78,8 +80,7 @@ extension DataService { // TODO: We will want to sync items that need creation in the background // these items are forced to sync when saved, but should be re-tried in // the background. - // syncLocalCreatedLinkedItem(item: item) - break + syncLocalCreatedLinkedItem(item: item) case .isNSync, .isSyncing: break case .needsDeletion: