create highlight json string at article opening time so it's always up to date

This commit is contained in:
Satindar Dhillon
2022-04-20 16:07:57 -07:00
parent 6658d1d69b
commit 155c47b0d1
6 changed files with 37 additions and 37 deletions

View File

@ -67,7 +67,6 @@
</uniquenessConstraints>
</entity>
<entity name="PersistedArticleContent" representedClassName="PersistedArticleContent" syncable="YES" codeGenerationType="class">
<attribute name="highlightsJSONString" optional="YES" attributeType="String"/>
<attribute name="htmlContent" attributeType="String"/>
<attribute name="slug" attributeType="String"/>
<uniquenessConstraints>
@ -92,7 +91,7 @@
<element name="LinkedItem" positionX="-18" positionY="63" width="128" height="299"/>
<element name="LinkedItemLabel" positionX="-36" positionY="18" width="128" height="104"/>
<element name="NewsletterEmail" positionX="0" positionY="180" width="128" height="74"/>
<element name="PersistedArticleContent" positionX="9" positionY="108" width="128" height="74"/>
<element name="PersistedArticleContent" positionX="9" positionY="108" width="128" height="59"/>
<element name="Viewer" positionX="45" positionY="234" width="128" height="89"/>
</elements>
</model>

View File

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

View File

@ -77,20 +77,21 @@ public extension DataService {
}
func pageFromCache(slug: String) -> ArticleContent? {
// TODO: cerate highlightsJSON from stored highlights
// let fetchRequest: NSFetchRequest<Models.LinkedItem> = LinkedItem.fetchRequest()
// fetchRequest.predicate = NSPredicate(
// format: "slug == %@", slug
// )
//
// guard let linkedItem = try? persistentContainer.viewContext.fetch(fetchRequest).first else { return nil }
//
// let highlightsFetchRequest: NSFetchRequest<Models.Highlight> = Highlight.fetchRequest()
// fetchRequest.predicate = NSPredicate(
// format: "linkedItemId == %@", linkedItem.id ?? ""
// )
//
// let highlights = (try? persistentContainer.viewContext.fetch(highlightsFetchRequest)) ?? []
let linkedItemFetchRequest: NSFetchRequest<Models.LinkedItem> = LinkedItem.fetchRequest()
linkedItemFetchRequest.predicate = NSPredicate(
format: "slug == %@", slug
)
guard let linkedItem = try? persistentContainer.viewContext.fetch(linkedItemFetchRequest).first else { return nil }
let highlightsFetchRequest: NSFetchRequest<Models.Highlight> = 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<Models.PersistedArticleContent> = 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

View File

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

View File

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

View File

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