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 de592b851..b23b0e8d3 100644 --- a/apple/OmnivoreKit/Sources/Models/CoreData/CoreDataModel.xcdatamodeld/CoreDataModel.xcdatamodel/contents +++ b/apple/OmnivoreKit/Sources/Models/CoreData/CoreDataModel.xcdatamodeld/CoreDataModel.xcdatamodel/contents @@ -67,7 +67,6 @@ - @@ -92,7 +91,7 @@ - + \ No newline at end of file diff --git a/apple/OmnivoreKit/Sources/Models/DataModels/FeedItem.swift b/apple/OmnivoreKit/Sources/Models/DataModels/FeedItem.swift index c84bf93ca..fbdf25b86 100644 --- a/apple/OmnivoreKit/Sources/Models/DataModels/FeedItem.swift +++ b/apple/OmnivoreKit/Sources/Models/DataModels/FeedItem.swift @@ -141,7 +141,7 @@ struct JSONArticle: Decodable { public extension FeedItemDep { func asManagedObject(inContext context: NSManagedObjectContext) -> LinkedItem { - let linkedItem = LinkedItem(context: context) + let linkedItem = LinkedItem(entity: LinkedItem.entity(), insertInto: context) linkedItem.id = id linkedItem.title = title diff --git a/apple/OmnivoreKit/Sources/Services/DataService/DataService.swift b/apple/OmnivoreKit/Sources/Services/DataService/DataService.swift index 28eb22996..01aa6bf29 100644 --- a/apple/OmnivoreKit/Sources/Services/DataService/DataService.swift +++ b/apple/OmnivoreKit/Sources/Services/DataService/DataService.swift @@ -77,20 +77,21 @@ public extension DataService { } func pageFromCache(slug: String) -> ArticleContent? { - // TODO: cerate highlightsJSON from stored highlights -// let fetchRequest: NSFetchRequest = LinkedItem.fetchRequest() -// fetchRequest.predicate = NSPredicate( -// format: "slug == %@", slug -// ) -// -// guard let linkedItem = try? persistentContainer.viewContext.fetch(fetchRequest).first else { return nil } -// -// let highlightsFetchRequest: NSFetchRequest = Highlight.fetchRequest() -// fetchRequest.predicate = NSPredicate( -// format: "linkedItemId == %@", linkedItem.id ?? "" -// ) -// -// let highlights = (try? persistentContainer.viewContext.fetch(highlightsFetchRequest)) ?? [] + let linkedItemFetchRequest: NSFetchRequest = LinkedItem.fetchRequest() + linkedItemFetchRequest.predicate = NSPredicate( + format: "slug == %@", slug + ) + + guard let linkedItem = try? persistentContainer.viewContext.fetch(linkedItemFetchRequest).first else { return nil } + + let highlightsFetchRequest: NSFetchRequest = Highlight.fetchRequest() + highlightsFetchRequest.predicate = NSPredicate( + format: "linkedItemId == %@", linkedItem.id ?? "" + ) + + guard let highlights = try? persistentContainer.viewContext.fetch(highlightsFetchRequest) else { return nil } + + let highlightsJSONString = highlights.map { InternalHighlight.make(from: $0) }.asJSONString let fetchRequest: NSFetchRequest = PersistedArticleContent.fetchRequest() fetchRequest.predicate = NSPredicate( @@ -100,7 +101,7 @@ public extension DataService { if let articleContent = (try? persistentContainer.viewContext.fetch(fetchRequest))?.first { return ArticleContent( htmlContent: articleContent.htmlContent ?? "", - highlightsJSONString: articleContent.highlightsJSONString ?? "" + highlightsJSONString: highlightsJSONString ) } else { return nil diff --git a/apple/OmnivoreKit/Sources/Services/DataService/Queries/ArticleContentQuery.swift b/apple/OmnivoreKit/Sources/Services/DataService/Queries/ArticleContentQuery.swift index 2e60be20b..22f080f51 100644 --- a/apple/OmnivoreKit/Sources/Services/DataService/Queries/ArticleContentQuery.swift +++ b/apple/OmnivoreKit/Sources/Services/DataService/Queries/ArticleContentQuery.swift @@ -49,16 +49,15 @@ public extension DataService { switch payload.data { case let .success(result: result): // store result in core data - let highlightsJSONString = result.highlights.asJSONString self?.persistArticleContent( htmlContent: result.htmlContent, slug: slug, - highlightsJSONString: highlightsJSONString + highlights: result.highlights ) promise(.success( ArticleContent( htmlContent: result.htmlContent, - highlightsJSONString: highlightsJSONString + highlightsJSONString: result.highlights.asJSONString )) ) case .error: @@ -76,23 +75,24 @@ public extension DataService { } extension DataService { - func persistArticleContent(htmlContent: String, slug: String, highlightsJSONString: String) { - let persistedArticleContent = PersistedArticleContent(context: persistentContainer.viewContext) + func persistArticleContent(htmlContent: String, slug: String, highlights: [InternalHighlight]) { + let persistedArticleContent = PersistedArticleContent( + entity: PersistedArticleContent.entity(), + insertInto: persistentContainer.viewContext + ) persistedArticleContent.htmlContent = htmlContent persistedArticleContent.slug = slug - persistedArticleContent.highlightsJSONString = highlightsJSONString - // TODO: store highlights and create json string at call time -// let fetchRequest: NSFetchRequest = LinkedItem.fetchRequest() -// fetchRequest.predicate = NSPredicate( -// format: "slug == %@", slug -// ) -// -// if let linkedItem = try? persistentContainer.viewContext.fetch(fetchRequest).first { -// _ = highlights.map { -// $0.asManagedObject(context: persistentContainer.viewContext, associatedItemID: linkedItem.id ?? "") -// } -// } + let fetchRequest: NSFetchRequest = LinkedItem.fetchRequest() + fetchRequest.predicate = NSPredicate( + format: "slug == %@", slug + ) + + if let linkedItemID = try? persistentContainer.viewContext.fetch(fetchRequest).first?.id { + _ = highlights.map { + $0.asManagedObject(context: persistentContainer.viewContext, associatedItemID: linkedItemID) + } + } do { try persistentContainer.viewContext.save() diff --git a/apple/OmnivoreKit/Sources/Services/InternalModels/InternalHighlight.swift b/apple/OmnivoreKit/Sources/Services/InternalModels/InternalHighlight.swift index 8bd23343f..ca0c25231 100644 --- a/apple/OmnivoreKit/Sources/Services/InternalModels/InternalHighlight.swift +++ b/apple/OmnivoreKit/Sources/Services/InternalModels/InternalHighlight.swift @@ -15,7 +15,7 @@ struct InternalHighlight: Encodable { let createdByMe: Bool func asManagedObject(context: NSManagedObjectContext, associatedItemID: String) -> Highlight { - let highlight = Highlight(context: context) + let highlight = Highlight(entity: Highlight.entity(), insertInto: context) highlight.linkedItemId = associatedItemID highlight.markedForDeletion = false highlight.id = id diff --git a/apple/OmnivoreKit/Sources/Services/InternalModels/InternalNewsletterEmail.swift b/apple/OmnivoreKit/Sources/Services/InternalModels/InternalNewsletterEmail.swift index cd5dd3329..605aaa6db 100644 --- a/apple/OmnivoreKit/Sources/Services/InternalModels/InternalNewsletterEmail.swift +++ b/apple/OmnivoreKit/Sources/Services/InternalModels/InternalNewsletterEmail.swift @@ -22,7 +22,7 @@ struct InternalNewsletterEmail { } func asManagedObject(inContext context: NSManagedObjectContext) -> NewsletterEmail { - let newsletterEmail = NewsletterEmail(context: context) + let newsletterEmail = NewsletterEmail(entity: NewsletterEmail.entity(), insertInto: context) newsletterEmail.emailId = emailId newsletterEmail.email = email newsletterEmail.confirmationCode = confirmationCode