diff --git a/apple/OmnivoreKit/Sources/App/Views/WebReader/WebReaderViewModel.swift b/apple/OmnivoreKit/Sources/App/Views/WebReader/WebReaderViewModel.swift index 8795a0177..92a0b249a 100644 --- a/apple/OmnivoreKit/Sources/App/Views/WebReader/WebReaderViewModel.swift +++ b/apple/OmnivoreKit/Sources/App/Views/WebReader/WebReaderViewModel.swift @@ -24,19 +24,18 @@ final class WebReaderViewModel: ObservableObject { if let content = dataService.pageFromCache(slug: slug) { articleContent = content - // continue to load from the web if possible + } else { + dataService.articleContentPublisher(username: username, slug: slug).sink( + receiveCompletion: { [weak self] completion in + guard case .failure = completion else { return } + self?.isLoading = false + }, + receiveValue: { [weak self] articleContent in + self?.articleContent = articleContent + } + ) + .store(in: &subscriptions) } - - dataService.articleContentPublisher(username: username, slug: slug).sink( - receiveCompletion: { [weak self] completion in - guard case .failure = completion else { return } - self?.isLoading = false - }, - receiveValue: { [weak self] articleContent in - self?.articleContent = articleContent - } - ) - .store(in: &subscriptions) } func createHighlight( 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 5da70984b..35c15e85a 100644 --- a/apple/OmnivoreKit/Sources/Models/CoreData/CoreDataModel.xcdatamodeld/CoreDataModel.xcdatamodel/contents +++ b/apple/OmnivoreKit/Sources/Models/CoreData/CoreDataModel.xcdatamodeld/CoreDataModel.xcdatamodel/contents @@ -25,6 +25,7 @@ + @@ -67,15 +68,6 @@ - - - - - - - - - @@ -89,10 +81,9 @@ - + - \ No newline at end of file diff --git a/apple/OmnivoreKit/Sources/Services/DataService/DataService.swift b/apple/OmnivoreKit/Sources/Services/DataService/DataService.swift index 25391e216..68776f3fd 100644 --- a/apple/OmnivoreKit/Sources/Services/DataService/DataService.swift +++ b/apple/OmnivoreKit/Sources/Services/DataService/DataService.swift @@ -83,6 +83,7 @@ public extension DataService { ) guard let linkedItem = try? persistentContainer.viewContext.fetch(linkedItemFetchRequest).first else { return nil } + guard let htmlContent = linkedItem.htmlContent else { return nil } let highlightsFetchRequest: NSFetchRequest = Highlight.fetchRequest() highlightsFetchRequest.predicate = NSPredicate( @@ -91,21 +92,10 @@ public extension DataService { guard let highlights = try? persistentContainer.viewContext.fetch(highlightsFetchRequest) else { return nil } - let highlightsJSONString = highlights.map { InternalHighlight.make(from: $0) }.asJSONString - - let fetchRequest: NSFetchRequest = PersistedArticleContent.fetchRequest() - fetchRequest.predicate = NSPredicate( - format: "slug = %@", slug + return ArticleContent( + htmlContent: htmlContent, + highlightsJSONString: highlights.map { InternalHighlight.make(from: $0) }.asJSONString ) - - if let articleContent = (try? persistentContainer.viewContext.fetch(fetchRequest))?.first { - return ArticleContent( - htmlContent: articleContent.htmlContent ?? "", - highlightsJSONString: highlightsJSONString - ) - } else { - return nil - } } func invalidateCachedPage(slug _: String?) {} diff --git a/apple/OmnivoreKit/Sources/Services/DataService/Queries/ArticleContentQuery.swift b/apple/OmnivoreKit/Sources/Services/DataService/Queries/ArticleContentQuery.swift index dea30e725..686f4c0b6 100644 --- a/apple/OmnivoreKit/Sources/Services/DataService/Queries/ArticleContentQuery.swift +++ b/apple/OmnivoreKit/Sources/Services/DataService/Queries/ArticleContentQuery.swift @@ -76,31 +76,18 @@ public extension DataService { extension DataService { func persistArticleContent(htmlContent: String, slug: String, highlights: [InternalHighlight]) { - let fetchContentRequest: NSFetchRequest = PersistedArticleContent.fetchRequest() - fetchContentRequest.predicate = NSPredicate( - format: "slug == %@", slug - ) - - if let content = (try? persistentContainer.viewContext.fetch(fetchContentRequest))?.first { - content.htmlContent = htmlContent - } else { - let persistedArticleContent = PersistedArticleContent( - entity: PersistedArticleContent.entity(), - insertInto: persistentContainer.viewContext - ) - persistedArticleContent.htmlContent = htmlContent - persistedArticleContent.slug = slug - } - let fetchRequest: NSFetchRequest = LinkedItem.fetchRequest() fetchRequest.predicate = NSPredicate( format: "slug == %@", slug ) - if let linkedItemID = try? persistentContainer.viewContext.fetch(fetchRequest).first?.id { + let linkedItem = try? persistentContainer.viewContext.fetch(fetchRequest).first + + if let linkedItem = linkedItem, let linkedItemID = linkedItem.id { _ = highlights.map { $0.asManagedObject(context: persistentContainer.viewContext, associatedItemID: linkedItemID) } + linkedItem.htmlContent = htmlContent } do {