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 b3cb44951..6cd4746fd 100644 --- a/apple/OmnivoreKit/Sources/Models/CoreData/CoreDataModel.xcdatamodeld/CoreDataModel.xcdatamodel/contents +++ b/apple/OmnivoreKit/Sources/Models/CoreData/CoreDataModel.xcdatamodeld/CoreDataModel.xcdatamodel/contents @@ -68,6 +68,7 @@ + @@ -92,7 +93,7 @@ - + \ No newline at end of file diff --git a/apple/OmnivoreKit/Sources/Models/DataModels/ArticleContentDep.swift b/apple/OmnivoreKit/Sources/Models/DataModels/ArticleContentDep.swift index 101900231..f33fec12f 100644 --- a/apple/OmnivoreKit/Sources/Models/DataModels/ArticleContentDep.swift +++ b/apple/OmnivoreKit/Sources/Models/DataModels/ArticleContentDep.swift @@ -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) ?? "[]" diff --git a/apple/OmnivoreKit/Sources/Services/DataService/DataService.swift b/apple/OmnivoreKit/Sources/Services/DataService/DataService.swift index e88b97434..5ff65440e 100644 --- a/apple/OmnivoreKit/Sources/Services/DataService/DataService.swift +++ b/apple/OmnivoreKit/Sources/Services/DataService/DataService.swift @@ -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 } diff --git a/apple/OmnivoreKit/Sources/Services/DataService/Queries/ArticleContentQuery.swift b/apple/OmnivoreKit/Sources/Services/DataService/Queries/ArticleContentQuery.swift index f63a51c00..703f8683b 100644 --- a/apple/OmnivoreKit/Sources/Services/DataService/Queries/ArticleContentQuery.swift +++ b/apple/OmnivoreKit/Sources/Services/DataService/Queries/ArticleContentQuery.swift @@ -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")