diff --git a/apple/OmnivoreKit/Sources/Services/DataService/Mutations/ArchiveLink.swift b/apple/OmnivoreKit/Sources/Services/DataService/Mutations/ArchiveLink.swift index 3af0f3057..592927982 100644 --- a/apple/OmnivoreKit/Sources/Services/DataService/Mutations/ArchiveLink.swift +++ b/apple/OmnivoreKit/Sources/Services/DataService/Mutations/ArchiveLink.swift @@ -44,6 +44,7 @@ public extension DataService { switch payload.data { case let .success(linkId): + // TODO: update CoreData promise(.success(linkId)) case .error: promise(.failure(.message(messageText: "Error archiving link"))) diff --git a/apple/OmnivoreKit/Sources/Services/DataService/Mutations/CreateLabelPublisher.swift b/apple/OmnivoreKit/Sources/Services/DataService/Mutations/CreateLabelPublisher.swift index cd266f4f1..62596c9f9 100644 --- a/apple/OmnivoreKit/Sources/Services/DataService/Mutations/CreateLabelPublisher.swift +++ b/apple/OmnivoreKit/Sources/Services/DataService/Mutations/CreateLabelPublisher.swift @@ -46,6 +46,7 @@ public extension DataService { switch payload.data { case let .saved(label: label): + // TODO: update CoreData promise(.success(label)) case let .error(errorCode: errorCode): promise(.failure(.message(messageText: errorCode.rawValue))) diff --git a/apple/OmnivoreKit/Sources/Services/DataService/Mutations/RemoveLabelPublisher.swift b/apple/OmnivoreKit/Sources/Services/DataService/Mutations/RemoveLabelPublisher.swift index 134954a4f..40f11e5ae 100644 --- a/apple/OmnivoreKit/Sources/Services/DataService/Mutations/RemoveLabelPublisher.swift +++ b/apple/OmnivoreKit/Sources/Services/DataService/Mutations/RemoveLabelPublisher.swift @@ -37,6 +37,7 @@ public extension DataService { switch payload.data { case .success: + // TODO: update CoreData promise(.success(true)) case .error: promise(.failure(.message(messageText: "Error removing label"))) diff --git a/apple/OmnivoreKit/Sources/Services/DataService/Mutations/RemoveLink.swift b/apple/OmnivoreKit/Sources/Services/DataService/Mutations/RemoveLink.swift index 0a89e8710..8a007edcf 100644 --- a/apple/OmnivoreKit/Sources/Services/DataService/Mutations/RemoveLink.swift +++ b/apple/OmnivoreKit/Sources/Services/DataService/Mutations/RemoveLink.swift @@ -51,6 +51,7 @@ public extension DataService { switch payload.data { case let .success(item): + // TODO: update CoreData promise(.success(item)) case .error(errorCode: _): promise(.failure(.message(messageText: "Error removing link"))) diff --git a/apple/OmnivoreKit/Sources/Services/DataService/Mutations/UpdateArticleLabelsPublisher.swift b/apple/OmnivoreKit/Sources/Services/DataService/Mutations/UpdateArticleLabelsPublisher.swift index 11d9f4098..484b11dc3 100644 --- a/apple/OmnivoreKit/Sources/Services/DataService/Mutations/UpdateArticleLabelsPublisher.swift +++ b/apple/OmnivoreKit/Sources/Services/DataService/Mutations/UpdateArticleLabelsPublisher.swift @@ -44,6 +44,7 @@ public extension DataService { switch payload.data { case let .saved(labels): + // TODO: update CoreData promise(.success(labels)) case .error: promise(.failure(.message(messageText: "failed to set labels"))) diff --git a/apple/OmnivoreKit/Sources/Services/DataService/Mutations/UpdateArticleReadingProgress.swift b/apple/OmnivoreKit/Sources/Services/DataService/Mutations/UpdateArticleReadingProgress.swift index deaa1394b..b482c8619 100644 --- a/apple/OmnivoreKit/Sources/Services/DataService/Mutations/UpdateArticleReadingProgress.swift +++ b/apple/OmnivoreKit/Sources/Services/DataService/Mutations/UpdateArticleReadingProgress.swift @@ -1,4 +1,5 @@ import Combine +import CoreData import Foundation import Models import SwiftGraphQL @@ -50,7 +51,7 @@ public extension DataService { switch payload.data { case let .saved(feedItem): - // TODO: update core data record + self.updateManagedObject(itemID: itemID, readingProgress: readingProgress, anchorIndex: anchorIndex) promise(.success(feedItem)) case let .error(errorCode: errorCode): switch errorCode { @@ -69,4 +70,30 @@ public extension DataService { .receive(on: DispatchQueue.main) .eraseToAnyPublisher() } + + private func updateManagedObject( + itemID: String, + readingProgress: Double, + anchorIndex: Int + ) { + let context = persistentContainer.viewContext + + let fetchRequest: NSFetchRequest = LinkedItem.fetchRequest() + fetchRequest.predicate = NSPredicate( + format: "id == %@", itemID + ) + + if let linkedItem = try? context.fetch(fetchRequest).first { + linkedItem.readingProgress = readingProgress + linkedItem.readingProgressAnchor = Int64(anchorIndex) + } + + do { + try context.save() + DataService.logger.debug("LinkedItem updated succesfully") + } catch { + context.rollback() + DataService.logger.debug("Failed to update LinkedItem: \(error.localizedDescription)") + } + } } diff --git a/apple/OmnivoreKit/Sources/Services/DataService/Queries/LabelsPublisher.swift b/apple/OmnivoreKit/Sources/Services/DataService/Queries/LabelsPublisher.swift index 3e01903f9..c110b92ab 100644 --- a/apple/OmnivoreKit/Sources/Services/DataService/Queries/LabelsPublisher.swift +++ b/apple/OmnivoreKit/Sources/Services/DataService/Queries/LabelsPublisher.swift @@ -33,6 +33,7 @@ public extension DataService { case let .success(payload): switch payload.data { case let .success(result: result): + // TODO: update CoreData promise(.success(result)) case .error: promise(.failure(.unknown))