remove linkedItemId from coredata highlight entity

This commit is contained in:
Satindar Dhillon 2022-04-24 21:49:02 -07:00
parent 855586c320
commit 26a52819d8
7 changed files with 16 additions and 34 deletions

View file

@ -18,14 +18,8 @@ public final class PDFViewerViewModel: ObservableObject {
self.linkedItem = linkedItem
}
public func loadHighlights(completion onComplete: @escaping ([String]) -> Void) {
let fetchRequest: NSFetchRequest<Models.Highlight> = 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) {

View file

@ -5,7 +5,6 @@
<attribute name="createdAt" optional="YES" attributeType="Date" usesScalarValueType="NO"/>
<attribute name="createdByMe" attributeType="Boolean" usesScalarValueType="YES"/>
<attribute name="id" attributeType="String"/>
<attribute name="linkedItemId" attributeType="String"/>
<attribute name="markedForDeletion" attributeType="Boolean" defaultValueString="NO" usesScalarValueType="YES"/>
<attribute name="patch" attributeType="String"/>
<attribute name="prefix" optional="YES" attributeType="String"/>
@ -80,7 +79,7 @@
</uniquenessConstraints>
</entity>
<elements>
<element name="Highlight" positionX="27" positionY="225" width="128" height="224"/>
<element name="Highlight" positionX="27" positionY="225" width="128" height="209"/>
<element name="LinkedItem" positionX="-18" positionY="63" width="128" height="314"/>
<element name="LinkedItemLabel" positionX="-36" positionY="18" width="128" height="104"/>
<element name="NewsletterEmail" positionX="0" positionY="180" width="128" height="74"/>

View file

@ -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<Models.Highlight> = 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

View file

@ -50,14 +50,7 @@ public extension DataService {
switch payload.data {
case let .saved(highlight: highlight):
self.backgroundContext.perform {
let fetchRequest: NSFetchRequest<Models.Highlight> = 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):

View file

@ -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

View file

@ -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<Models.Highlight> = 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<Models.Highlight> = Highlight.fetchRequest()

View file

@ -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 }