diff --git a/apple/OmnivoreKit/Sources/Models/DataModels/FeedItem.swift b/apple/OmnivoreKit/Sources/Models/DataModels/FeedItem.swift index fbdf25b86..ac3e82286 100644 --- a/apple/OmnivoreKit/Sources/Models/DataModels/FeedItem.swift +++ b/apple/OmnivoreKit/Sources/Models/DataModels/FeedItem.swift @@ -206,3 +206,55 @@ public extension Sequence where Element == FeedItemDep { } } } + +public extension LinkedItem { + static func lookup(byID itemID: String, inContext context: NSManagedObjectContext) -> LinkedItem? { + let fetchRequest: NSFetchRequest = LinkedItem.fetchRequest() + fetchRequest.predicate = NSPredicate( + format: "id == %@", itemID + ) + + return (try? context.fetch(fetchRequest))?.first + } + + func update( + inContext context: NSManagedObjectContext, + newReadingProgress: Double? = nil, + newAnchorIndex: Int? = nil, + newIsArchivedValue: Bool? = nil + ) { + if let newReadingProgress = newReadingProgress { + readingProgress = newReadingProgress + } + + if let newAnchorIndex = newAnchorIndex { + readingProgressAnchor = Int64(newAnchorIndex) + } + + if let newIsArchivedValue = newIsArchivedValue { + isArchived = newIsArchivedValue + } + + guard context.hasChanges else { return } + + do { + try context.save() + print("LinkedItem updated succesfully") + } catch { + context.rollback() + print("Failed to update LinkedItem: \(error.localizedDescription)") + } + } + + func remove(inContext context: NSManagedObjectContext) { + context.delete(self) + + do { + try context.save() + print("LinkedItem removed") + } catch { + context.rollback() + print("Failed to remove LinkedItem: \(error.localizedDescription)") + } + } +} diff --git a/apple/OmnivoreKit/Sources/Services/DataService/Mutations/ArchiveLink.swift b/apple/OmnivoreKit/Sources/Services/DataService/Mutations/ArchiveLink.swift index 592927982..36603ba42 100644 --- a/apple/OmnivoreKit/Sources/Services/DataService/Mutations/ArchiveLink.swift +++ b/apple/OmnivoreKit/Sources/Services/DataService/Mutations/ArchiveLink.swift @@ -44,7 +44,12 @@ public extension DataService { switch payload.data { case let .success(linkId): - // TODO: update CoreData + if let linkedItem = LinkedItem.lookup(byID: itemID, inContext: self.persistentContainer.viewContext) { + linkedItem.update( + inContext: self.persistentContainer.viewContext, + newIsArchivedValue: archived + ) + } promise(.success(linkId)) case .error: promise(.failure(.message(messageText: "Error archiving link"))) diff --git a/apple/OmnivoreKit/Sources/Services/DataService/Mutations/RemoveLink.swift b/apple/OmnivoreKit/Sources/Services/DataService/Mutations/RemoveLink.swift index 8a007edcf..1fb4fc299 100644 --- a/apple/OmnivoreKit/Sources/Services/DataService/Mutations/RemoveLink.swift +++ b/apple/OmnivoreKit/Sources/Services/DataService/Mutations/RemoveLink.swift @@ -51,7 +51,9 @@ public extension DataService { switch payload.data { case let .success(item): - // TODO: update CoreData + if let linkedItem = LinkedItem.lookup(byID: itemID, inContext: self.persistentContainer.viewContext) { + linkedItem.remove(inContext: self.persistentContainer.viewContext) + } promise(.success(item)) case .error(errorCode: _): promise(.failure(.message(messageText: "Error removing link"))) diff --git a/apple/OmnivoreKit/Sources/Services/DataService/Mutations/UpdateArticleReadingProgress.swift b/apple/OmnivoreKit/Sources/Services/DataService/Mutations/UpdateArticleReadingProgress.swift index b482c8619..d7b3d8e9f 100644 --- a/apple/OmnivoreKit/Sources/Services/DataService/Mutations/UpdateArticleReadingProgress.swift +++ b/apple/OmnivoreKit/Sources/Services/DataService/Mutations/UpdateArticleReadingProgress.swift @@ -51,7 +51,13 @@ public extension DataService { switch payload.data { case let .saved(feedItem): - self.updateManagedObject(itemID: itemID, readingProgress: readingProgress, anchorIndex: anchorIndex) + if let linkedItem = LinkedItem.lookup(byID: itemID, inContext: self.persistentContainer.viewContext) { + linkedItem.update( + inContext: self.persistentContainer.viewContext, + newReadingProgress: readingProgress, + newAnchorIndex: anchorIndex + ) + } promise(.success(feedItem)) case let .error(errorCode: errorCode): switch errorCode { @@ -70,30 +76,4 @@ 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)") - } - } }