save highlightsJSON on persisted article content
This commit is contained in:
@ -68,6 +68,7 @@
|
||||
</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 +93,7 @@
|
||||
<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"/>
|
||||
<element name="PersistedArticleContent" positionX="9" positionY="108" width="128" height="59"/>
|
||||
<element name="PersistedArticleContent" positionX="9" positionY="108" width="128" height="74"/>
|
||||
<element name="Viewer" positionX="45" positionY="234" width="128" height="89"/>
|
||||
</elements>
|
||||
</model>
|
||||
@ -3,16 +3,23 @@ import Foundation
|
||||
public struct ArticleContentDep {
|
||||
public let htmlContent: String
|
||||
public let highlights: [HighlightDep]
|
||||
public let storedHighlightsJSONString: String?
|
||||
|
||||
public init(
|
||||
htmlContent: String,
|
||||
highlights: [HighlightDep]
|
||||
highlights: [HighlightDep],
|
||||
storedHighlightsJSONString: String?
|
||||
) {
|
||||
self.htmlContent = htmlContent
|
||||
self.highlights = highlights
|
||||
self.storedHighlightsJSONString = storedHighlightsJSONString
|
||||
}
|
||||
|
||||
public var highlightsJSONString: String {
|
||||
if let storedHighlightsJSONString = storedHighlightsJSONString {
|
||||
return storedHighlightsJSONString
|
||||
}
|
||||
|
||||
let jsonData = try? JSONEncoder().encode(highlights)
|
||||
guard let jsonData = jsonData else { return "[]" }
|
||||
return String(data: jsonData, encoding: .utf8) ?? "[]"
|
||||
|
||||
@ -81,8 +81,12 @@ public extension DataService {
|
||||
fetchRequest.predicate = NSPredicate(
|
||||
format: "slug = %@", slug
|
||||
)
|
||||
if let htmlContent = try? persistentContainer.viewContext.fetch(fetchRequest).first?.htmlContent {
|
||||
return ArticleContentDep(htmlContent: htmlContent, highlights: [])
|
||||
if let articleContent = try? persistentContainer.viewContext.fetch(fetchRequest).first {
|
||||
return ArticleContentDep(
|
||||
htmlContent: articleContent.htmlContent ?? "",
|
||||
highlights: [],
|
||||
storedHighlightsJSONString: articleContent.highlightsJSONString
|
||||
)
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -13,7 +13,8 @@ public extension DataService {
|
||||
let articleSelection = Selection.Article {
|
||||
ArticleContentDep(
|
||||
htmlContent: try $0.content(),
|
||||
highlights: try $0.highlights(selection: highlightSelection.list)
|
||||
highlights: try $0.highlights(selection: highlightSelection.list),
|
||||
storedHighlightsJSONString: nil
|
||||
)
|
||||
}
|
||||
|
||||
@ -43,7 +44,7 @@ public extension DataService {
|
||||
switch payload.data {
|
||||
case let .success(result: result):
|
||||
// store result in core data
|
||||
self?.persistArticleContent(htmlContent: result.htmlContent, slug: slug)
|
||||
self?.persistArticleContent(htmlContent: result.htmlContent, slug: slug, highlights: result.highlights)
|
||||
promise(.success(result))
|
||||
case .error:
|
||||
promise(.failure(.unknown))
|
||||
@ -60,11 +61,15 @@ public extension DataService {
|
||||
}
|
||||
|
||||
extension DataService {
|
||||
func persistArticleContent(htmlContent: String, slug: String) {
|
||||
func persistArticleContent(htmlContent: String, slug: String, highlights: [HighlightDep]) {
|
||||
let persistedArticleContent = PersistedArticleContent(context: persistentContainer.viewContext)
|
||||
persistedArticleContent.htmlContent = htmlContent
|
||||
persistedArticleContent.slug = slug
|
||||
|
||||
if let jsonData = try? JSONEncoder().encode(highlights) {
|
||||
persistedArticleContent.highlightsJSONString = String(data: jsonData, encoding: .utf8)
|
||||
}
|
||||
|
||||
do {
|
||||
try persistentContainer.viewContext.save()
|
||||
print("PersistedArticleContent saved succesfully")
|
||||
|
||||
Reference in New Issue
Block a user