From 26a52819d8aeddc064febc5e72127cca7488f21f Mon Sep 17 00:00:00 2001 From: Satindar Dhillon Date: Sun, 24 Apr 2022 21:49:02 -0700 Subject: [PATCH] remove linkedItemId from coredata highlight entity --- .../App/PDFSupport/PDFViewerViewModel.swift | 10 ++-------- .../CoreDataModel.xcdatamodel/contents | 3 +-- .../Sources/Services/DataService/DataService.swift | 8 +------- .../Mutations/UpdateHighlightAttributes.swift | 9 +-------- .../DataService/Queries/ArticleContentQuery.swift | 4 ++-- .../InternalModels/InternalHighlight.swift | 14 ++++++++------ apple/Sources/PDFViewer.swift | 2 +- 7 files changed, 16 insertions(+), 34 deletions(-) diff --git a/apple/OmnivoreKit/Sources/App/PDFSupport/PDFViewerViewModel.swift b/apple/OmnivoreKit/Sources/App/PDFSupport/PDFViewerViewModel.swift index 550bc550e..f2cf00f13 100644 --- a/apple/OmnivoreKit/Sources/App/PDFSupport/PDFViewerViewModel.swift +++ b/apple/OmnivoreKit/Sources/App/PDFSupport/PDFViewerViewModel.swift @@ -18,14 +18,8 @@ public final class PDFViewerViewModel: ObservableObject { self.linkedItem = linkedItem } - public func loadHighlights(completion onComplete: @escaping ([String]) -> Void) { - let fetchRequest: NSFetchRequest = Highlight.fetchRequest() - fetchRequest.predicate = NSPredicate( - format: "linkedItemId == %@", linkedItem.unwrappedID - ) - - let highlights = (try? services.dataService.viewContext.fetch(fetchRequest)) ?? [] - onComplete(highlights.map { $0.patch ?? "" }) + public func loadHighlightPatches(completion onComplete: @escaping ([String]) -> Void) { + onComplete(linkedItem.highlights.asArray(of: Highlight.self).map { $0.patch ?? "" }) } public func createHighlight(shortId: String, highlightID: String, quote: String, patch: String) { diff --git a/apple/OmnivoreKit/Sources/Models/CoreData/CoreDataModel.xcdatamodeld/CoreDataModel.xcdatamodel/contents b/apple/OmnivoreKit/Sources/Models/CoreData/CoreDataModel.xcdatamodeld/CoreDataModel.xcdatamodel/contents index 35c15e85a..68676626e 100644 --- a/apple/OmnivoreKit/Sources/Models/CoreData/CoreDataModel.xcdatamodeld/CoreDataModel.xcdatamodel/contents +++ b/apple/OmnivoreKit/Sources/Models/CoreData/CoreDataModel.xcdatamodeld/CoreDataModel.xcdatamodel/contents @@ -5,7 +5,6 @@ - @@ -80,7 +79,7 @@ - + diff --git a/apple/OmnivoreKit/Sources/Services/DataService/DataService.swift b/apple/OmnivoreKit/Sources/Services/DataService/DataService.swift index 24a4b700f..7930373fe 100644 --- a/apple/OmnivoreKit/Sources/Services/DataService/DataService.swift +++ b/apple/OmnivoreKit/Sources/Services/DataService/DataService.swift @@ -72,14 +72,8 @@ public extension DataService { guard let linkedItem = try? persistentContainer.viewContext.fetch(linkedItemFetchRequest).first else { return nil } guard let htmlContent = linkedItem.htmlContent else { return nil } - - let highlightsFetchRequest: NSFetchRequest = Highlight.fetchRequest() - highlightsFetchRequest.predicate = NSPredicate( - format: "linkedItemId == %@", linkedItem.id ?? "" + let highlights = linkedItem.highlights.asArray(of: Highlight.self ) - - guard let highlights = try? persistentContainer.viewContext.fetch(highlightsFetchRequest) else { return nil } - return ArticleContent( htmlContent: htmlContent, highlightsJSONString: highlights.map { InternalHighlight.make(from: $0) }.asJSONString diff --git a/apple/OmnivoreKit/Sources/Services/DataService/Mutations/UpdateHighlightAttributes.swift b/apple/OmnivoreKit/Sources/Services/DataService/Mutations/UpdateHighlightAttributes.swift index ef16ad02a..f0550bf1a 100644 --- a/apple/OmnivoreKit/Sources/Services/DataService/Mutations/UpdateHighlightAttributes.swift +++ b/apple/OmnivoreKit/Sources/Services/DataService/Mutations/UpdateHighlightAttributes.swift @@ -50,14 +50,7 @@ public extension DataService { switch payload.data { case let .saved(highlight: highlight): self.backgroundContext.perform { - let fetchRequest: NSFetchRequest = Highlight.fetchRequest() - fetchRequest.predicate = NSPredicate(format: "id == %@", highlight.id) - let itemID = (try? self.backgroundContext.fetch(fetchRequest))?.first?.linkedItemId ?? "" - - highlight.persist( - context: self.backgroundContext, - associatedItemID: itemID - ) + highlight.persist(context: self.backgroundContext, associatedItemID: nil) } promise(.success(highlight.id)) case let .error(errorCode: errorCode): diff --git a/apple/OmnivoreKit/Sources/Services/DataService/Queries/ArticleContentQuery.swift b/apple/OmnivoreKit/Sources/Services/DataService/Queries/ArticleContentQuery.swift index 69ecf3686..aa166d058 100644 --- a/apple/OmnivoreKit/Sources/Services/DataService/Queries/ArticleContentQuery.swift +++ b/apple/OmnivoreKit/Sources/Services/DataService/Queries/ArticleContentQuery.swift @@ -84,9 +84,9 @@ extension DataService { let linkedItem = try? self.backgroundContext.fetch(fetchRequest).first - if let linkedItem = linkedItem, let linkedItemID = linkedItem.id { + if let linkedItem = linkedItem { let highlightObjects = highlights.map { - $0.asManagedObject(context: self.backgroundContext, associatedItemID: linkedItemID) + $0.asManagedObject(context: self.backgroundContext) } linkedItem.addToHighlights(NSSet(array: highlightObjects)) linkedItem.htmlContent = htmlContent diff --git a/apple/OmnivoreKit/Sources/Services/InternalModels/InternalHighlight.swift b/apple/OmnivoreKit/Sources/Services/InternalModels/InternalHighlight.swift index 26e93b9f0..450d5e759 100644 --- a/apple/OmnivoreKit/Sources/Services/InternalModels/InternalHighlight.swift +++ b/apple/OmnivoreKit/Sources/Services/InternalModels/InternalHighlight.swift @@ -14,7 +14,7 @@ struct InternalHighlight: Encodable { let updatedAt: Date? let createdByMe: Bool - func asManagedObject(context: NSManagedObjectContext, associatedItemID: String) -> Highlight { + func asManagedObject(context: NSManagedObjectContext) -> Highlight { let fetchRequest: NSFetchRequest = Highlight.fetchRequest() fetchRequest.predicate = NSPredicate( format: "id == %@", id @@ -22,7 +22,6 @@ struct InternalHighlight: Encodable { let existingHighlight = (try? context.fetch(fetchRequest))?.first let highlight = existingHighlight ?? Highlight(entity: Highlight.entity(), insertInto: context) - highlight.linkedItemId = associatedItemID highlight.markedForDeletion = false highlight.id = id highlight.shortId = shortId @@ -54,13 +53,16 @@ struct InternalHighlight: Encodable { func persist( context: NSManagedObjectContext, - associatedItemID: String, + associatedItemID: String?, oldHighlightsIds: [String] = [] ) { context.perform { - let highlight = asManagedObject(context: context, associatedItemID: associatedItemID) - let linkedItem = LinkedItem.lookup(byID: associatedItemID, inContext: context) - linkedItem?.addToHighlights(highlight) + let highlight = asManagedObject(context: context) + + if let associatedItemID = associatedItemID { + let linkedItem = LinkedItem.lookup(byID: associatedItemID, inContext: context) + linkedItem?.addToHighlights(highlight) + } if !oldHighlightsIds.isEmpty { let fetchRequest: NSFetchRequest = Highlight.fetchRequest() diff --git a/apple/Sources/PDFViewer.swift b/apple/Sources/PDFViewer.swift index 1ed68de0d..a35656c8b 100644 --- a/apple/Sources/PDFViewer.swift +++ b/apple/Sources/PDFViewer.swift @@ -301,7 +301,7 @@ import Utils } private func applyHighlights(documentProvider: PDFDocumentProvider) { - viewModel.loadHighlights { [weak self] highlightPatches in + viewModel.loadHighlightPatches { [weak self] highlightPatches in var annnotations: [Annotation] = [] for patch in highlightPatches { guard let data = patch.data(using: String.Encoding.utf8) else { continue }