From dd55cd86ae5206ba05eced80d582ab5292ff07f4 Mon Sep 17 00:00:00 2001 From: Satindar Dhillon Date: Tue, 19 Apr 2022 14:55:17 -0700 Subject: [PATCH] update highlight publishers to make coredata changes --- .../App/PDFSupport/PDFViewerViewModel.swift | 25 +++---------------- .../Mutations/DeleteHighlight.swift | 19 ++++++++++++++ .../Mutations/MergeHighlight.swift | 3 ++- .../Mutations/UpdateHighlightAttributes.swift | 8 +++++- .../InternalModels/InternalHighlight.swift | 14 ++++++++++- .../CachedPDFHighlights.swift | 23 +---------------- 6 files changed, 45 insertions(+), 47 deletions(-) diff --git a/apple/OmnivoreKit/Sources/App/PDFSupport/PDFViewerViewModel.swift b/apple/OmnivoreKit/Sources/App/PDFSupport/PDFViewerViewModel.swift index 712d19be3..745e78528 100644 --- a/apple/OmnivoreKit/Sources/App/PDFSupport/PDFViewerViewModel.swift +++ b/apple/OmnivoreKit/Sources/App/PDFSupport/PDFViewerViewModel.swift @@ -32,6 +32,7 @@ public final class PDFViewerViewModel: ObservableObject { .store(in: &subscriptions) } + // TODO: use core data instead private func allHighlights(fetchedHighlights: [HighlightDep]) -> [HighlightDep] { var resultSet = [String: HighlightDep]() @@ -79,6 +80,7 @@ public final class PDFViewerViewModel: ObservableObject { .store(in: &subscriptions) } + // TODO: able to delete this now? public func mergeHighlight( shortId: String, highlightID: String, @@ -86,22 +88,6 @@ public final class PDFViewerViewModel: ObservableObject { patch: String, overlapHighlightIdList: [String] ) { - services.dataService.persistHighlight( - pdfID: feedItem.id, - highlight: HighlightDep( - id: highlightID, - shortId: shortId, - quote: quote, - prefix: nil, - suffix: nil, - patch: patch, - annotation: nil, - createdByMe: true - ) - ) - - removeLocalHighlights(highlightIds: overlapHighlightIdList) - services.dataService .mergeHighlightPublisher( shortId: shortId, @@ -121,8 +107,7 @@ public final class PDFViewerViewModel: ObservableObject { } public func removeHighlights(highlightIds: [String]) { - removeLocalHighlights(highlightIds: highlightIds) - + // TODO: update function to take an array? highlightIds.forEach { highlightId in services.dataService.deleteHighlightPublisher(highlightId: highlightId) .sink { [weak self] completion in @@ -162,8 +147,4 @@ public final class PDFViewerViewModel: ObservableObject { return components?.url } - - private func removeLocalHighlights(highlightIds: [String]) { - services.dataService.removeHighlights(highlightIds: highlightIds) - } } diff --git a/apple/OmnivoreKit/Sources/Services/DataService/Mutations/DeleteHighlight.swift b/apple/OmnivoreKit/Sources/Services/DataService/Mutations/DeleteHighlight.swift index 60d021949..65f8ce947 100644 --- a/apple/OmnivoreKit/Sources/Services/DataService/Mutations/DeleteHighlight.swift +++ b/apple/OmnivoreKit/Sources/Services/DataService/Mutations/DeleteHighlight.swift @@ -1,4 +1,5 @@ import Combine +import CoreData import Foundation import Models import SwiftGraphQL @@ -42,6 +43,7 @@ public extension DataService { switch payload.data { case let .saved(id: id): + self.deletePersistedHighlight(objectID: id) promise(.success(id)) case let .error(errorCode: errorCode): promise(.failure(.message(messageText: errorCode.rawValue))) @@ -55,4 +57,21 @@ public extension DataService { .receive(on: DispatchQueue.main) .eraseToAnyPublisher() } + + func deletePersistedHighlight(objectID: String) { + let context = persistentContainer.viewContext + let fetchRequest: NSFetchRequest = Highlight.fetchRequest() + fetchRequest.predicate = NSPredicate(format: "id == %@", objectID) + for highlight in (try? context.fetch(fetchRequest)) ?? [] { + context.delete(highlight) + } + + do { + try context.save() + print("Highlight deleted succesfully") + } catch { + context.rollback() + print("Failed to delete Highlight: \(error.localizedDescription)") + } + } } diff --git a/apple/OmnivoreKit/Sources/Services/DataService/Mutations/MergeHighlight.swift b/apple/OmnivoreKit/Sources/Services/DataService/Mutations/MergeHighlight.swift index 272046f6c..4f8b8d4bf 100644 --- a/apple/OmnivoreKit/Sources/Services/DataService/Mutations/MergeHighlight.swift +++ b/apple/OmnivoreKit/Sources/Services/DataService/Mutations/MergeHighlight.swift @@ -60,7 +60,8 @@ public extension DataService { case let .saved(highlight: highlight): _ = highlight.persist( context: self.persistentContainer.viewContext, - associatedItemID: articleId + associatedItemID: articleId, + oldHighlightsIds: overlapHighlightIdList ) promise(.success(highlight.encoded())) case let .error(errorCode: errorCode): diff --git a/apple/OmnivoreKit/Sources/Services/DataService/Mutations/UpdateHighlightAttributes.swift b/apple/OmnivoreKit/Sources/Services/DataService/Mutations/UpdateHighlightAttributes.swift index b47768af2..27754240f 100644 --- a/apple/OmnivoreKit/Sources/Services/DataService/Mutations/UpdateHighlightAttributes.swift +++ b/apple/OmnivoreKit/Sources/Services/DataService/Mutations/UpdateHighlightAttributes.swift @@ -1,4 +1,5 @@ import Combine +import CoreData import Foundation import Models import SwiftGraphQL @@ -48,9 +49,14 @@ public extension DataService { switch payload.data { case let .saved(highlight: highlight): + let context = self.persistentContainer.viewContext + let fetchRequest: NSFetchRequest = Highlight.fetchRequest() + fetchRequest.predicate = NSPredicate(format: "id == %@", highlight.id) + let itemID = (try? context.fetch(fetchRequest))?.first?.linkedItemId ?? "" + _ = highlight.persist( context: self.persistentContainer.viewContext, - associatedItemID: "" // TODO: pass in articleID or just use update core data func + associatedItemID: itemID ) promise(.success(highlight.id)) case let .error(errorCode: errorCode): diff --git a/apple/OmnivoreKit/Sources/Services/InternalModels/InternalHighlight.swift b/apple/OmnivoreKit/Sources/Services/InternalModels/InternalHighlight.swift index dd70d59d3..c856705b3 100644 --- a/apple/OmnivoreKit/Sources/Services/InternalModels/InternalHighlight.swift +++ b/apple/OmnivoreKit/Sources/Services/InternalModels/InternalHighlight.swift @@ -31,9 +31,21 @@ struct InternalHighlight: Encodable { return highlight } - func persist(context: NSManagedObjectContext, associatedItemID: String) -> Highlight? { + func persist( + context: NSManagedObjectContext, + associatedItemID: String, + oldHighlightsIds: [String] = [] + ) -> Highlight? { let highlight = asManagedObject(context: context, associatedItemID: associatedItemID) + if !oldHighlightsIds.isEmpty { + let fetchRequest: NSFetchRequest = Highlight.fetchRequest() + fetchRequest.predicate = NSPredicate(format: "id IN %@", oldHighlightsIds) + for highlight in (try? context.fetch(fetchRequest)) ?? [] { + context.delete(highlight) + } + } + do { try context.save() print("Highlight saved succesfully") diff --git a/apple/OmnivoreKit/Sources/Services/Persistence/PersistableModels/CachedPDFHighlights.swift b/apple/OmnivoreKit/Sources/Services/Persistence/PersistableModels/CachedPDFHighlights.swift index b036a6b8c..c5f7fc784 100644 --- a/apple/OmnivoreKit/Sources/Services/Persistence/PersistableModels/CachedPDFHighlights.swift +++ b/apple/OmnivoreKit/Sources/Services/Persistence/PersistableModels/CachedPDFHighlights.swift @@ -3,6 +3,7 @@ import CoreData import Foundation import Models +// TODO: possibly remove this file? public extension DataService { func cachedHighlights(pdfID: String) -> [HighlightDep] { let fetchRequest: NSFetchRequest = Highlight.fetchRequest() @@ -28,26 +29,4 @@ public extension DataService { print("Failed to save Highlight: \(error)") } } - - func removeHighlights(highlightIds: [String]) { - for highlightID in highlightIds { - deletedHighlightsIDs.insert(highlightID) - } - - let fetchRequest: NSFetchRequest = Highlight.fetchRequest() - fetchRequest.predicate = NSPredicate(format: "id IN %@", highlightIds) - guard let highlights = try? persistentContainer.viewContext.fetch(fetchRequest) else { return } - - for highlight in highlights { - highlight.markedForDeletion = true - } - - do { - try persistentContainer.viewContext.save() - print("Highlight(s) updated succesfully") - } catch { - persistentContainer.viewContext.rollback() - print("Failed to update Highlight(s): \(error)") - } - } }