fetch and store pdf data in core data

This commit is contained in:
Satindar Dhillon 2022-04-28 13:57:02 -07:00
parent ffe1eed6b6
commit 8b71188a26
3 changed files with 44 additions and 11 deletions

View file

@ -31,6 +31,7 @@
<attribute name="isArchived" attributeType="Boolean" usesScalarValueType="YES"/>
<attribute name="onDeviceImageURLString" optional="YES" attributeType="String"/>
<attribute name="pageURLString" attributeType="String"/>
<attribute name="pdfData" optional="YES" attributeType="Binary"/>
<attribute name="publishDate" optional="YES" attributeType="Date" usesScalarValueType="NO"/>
<attribute name="publisherURLString" optional="YES" attributeType="String"/>
<attribute name="readingProgress" attributeType="Double" defaultValueString="0.0" usesScalarValueType="YES"/>
@ -83,7 +84,7 @@
</entity>
<elements>
<element name="Highlight" positionX="27" positionY="225" width="128" height="224"/>
<element name="LinkedItem" positionX="-18" positionY="63" width="128" height="329"/>
<element name="LinkedItem" positionX="-18" positionY="63" width="128" height="344"/>
<element name="LinkedItemLabel" positionX="-36" positionY="18" width="128" height="119"/>
<element name="NewsletterEmail" positionX="0" positionY="180" width="128" height="74"/>
<element name="Viewer" positionX="45" positionY="234" width="128" height="89"/>

View file

@ -55,6 +55,9 @@ public extension LinkedItem {
var pdfURL: URL? {
guard isPDF else { return nil }
if let data = pdfData {
print("")
}
return URL(string: pageURLString ?? "")
}

View file

@ -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<Models.LinkedItem> = 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<Models.LinkedItem> = 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<Models.LinkedItem> = LinkedItem.fetchRequest()
linkedItemFetchRequest.predicate = NSPredicate(