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 d85af33e8..8b34e1252 100644
--- a/apple/OmnivoreKit/Sources/Models/CoreData/CoreDataModel.xcdatamodeld/CoreDataModel.xcdatamodel/contents
+++ b/apple/OmnivoreKit/Sources/Models/CoreData/CoreDataModel.xcdatamodeld/CoreDataModel.xcdatamodel/contents
@@ -31,6 +31,7 @@
+
@@ -83,7 +84,7 @@
-
+
diff --git a/apple/OmnivoreKit/Sources/Models/DataModels/FeedItem.swift b/apple/OmnivoreKit/Sources/Models/DataModels/FeedItem.swift
index dcce41ed7..2d8c3761d 100644
--- a/apple/OmnivoreKit/Sources/Models/DataModels/FeedItem.swift
+++ b/apple/OmnivoreKit/Sources/Models/DataModels/FeedItem.swift
@@ -55,6 +55,9 @@ public extension LinkedItem {
var pdfURL: URL? {
guard isPDF else { return nil }
+ if let data = pdfData {
+ print("")
+ }
return URL(string: pageURLString ?? "")
}
diff --git a/apple/OmnivoreKit/Sources/Services/DataService/Queries/ArticleContentQuery.swift b/apple/OmnivoreKit/Sources/Services/DataService/Queries/ArticleContentQuery.swift
index f9b3b8f46..79d47eeeb 100644
--- a/apple/OmnivoreKit/Sources/Services/DataService/Queries/ArticleContentQuery.swift
+++ b/apple/OmnivoreKit/Sources/Services/DataService/Queries/ArticleContentQuery.swift
@@ -82,20 +82,23 @@ extension DataService {
}
func persistArticleContent(htmlContent: String, slug: String, highlights: [InternalHighlight]) {
- backgroundContext.perform {
+ backgroundContext.perform { [weak self] in
+ guard let self = self else { return }
let fetchRequest: NSFetchRequest = LinkedItem.fetchRequest()
- fetchRequest.predicate = NSPredicate(
- format: "slug == %@", slug
- )
+ fetchRequest.predicate = NSPredicate(format: "%K == %@", #keyPath(LinkedItem.slug), slug)
let linkedItem = try? self.backgroundContext.fetch(fetchRequest).first
- if let linkedItem = linkedItem {
- let highlightObjects = highlights.map {
- $0.asManagedObject(context: self.backgroundContext)
- }
- linkedItem.addToHighlights(NSSet(array: highlightObjects))
- linkedItem.htmlContent = htmlContent
+ guard let linkedItem = linkedItem else { return }
+
+ let highlightObjects = highlights.map {
+ $0.asManagedObject(context: self.backgroundContext)
+ }
+ linkedItem.addToHighlights(NSSet(array: highlightObjects))
+ linkedItem.htmlContent = htmlContent
+
+ if linkedItem.isPDF {
+ self.fetchPDFData(slug: slug, pageURLString: linkedItem.unwrappedPageURLString)
}
do {
@@ -108,6 +111,32 @@ extension DataService {
}
}
+ func fetchPDFData(slug: String, pageURLString: String) {
+ Task {
+ guard let url = URL(string: pageURLString) else { return }
+ let result: (Data, URLResponse)? = try? await URLSession.shared.data(from: url)
+ guard let httpResponse = result?.1 as? HTTPURLResponse, 200 ..< 300 ~= httpResponse.statusCode else { return }
+ guard let data = result?.0 else { return }
+
+ await backgroundContext.perform { [weak self] in
+ let fetchRequest: NSFetchRequest = LinkedItem.fetchRequest()
+ fetchRequest.predicate = NSPredicate(format: "%K == %@", #keyPath(LinkedItem.slug), slug)
+
+ let linkedItem = try? self?.backgroundContext.fetch(fetchRequest).first
+ guard let linkedItem = linkedItem else { return }
+ linkedItem.pdfData = data
+
+ do {
+ try self?.backgroundContext.save()
+ print("PDF data saved succesfully")
+ } catch {
+ self?.backgroundContext.rollback()
+ print("Failed to save PDF data: \(error)")
+ }
+ }
+ }
+ }
+
func cachedArticleContent(slug: String) -> ArticleContent? {
let linkedItemFetchRequest: NSFetchRequest = LinkedItem.fetchRequest()
linkedItemFetchRequest.predicate = NSPredicate(