diff --git a/apple/OmnivoreKit/Sources/App/PDFSupport/PDFViewerViewModel.swift b/apple/OmnivoreKit/Sources/App/PDFSupport/PDFViewerViewModel.swift index bf2404ebc..094ec4e4d 100644 --- a/apple/OmnivoreKit/Sources/App/PDFSupport/PDFViewerViewModel.swift +++ b/apple/OmnivoreKit/Sources/App/PDFSupport/PDFViewerViewModel.swift @@ -9,6 +9,7 @@ public final class PDFViewerViewModel: ObservableObject { @Published public var readerView: Bool = false public var linkedItem: LinkedItem + private var storedURL: URL? var subscriptions = Set() let services: Services @@ -18,6 +19,28 @@ public final class PDFViewerViewModel: ObservableObject { self.linkedItem = linkedItem } + public func dataURL(remoteURL: URL) -> URL { + if let storedURL = storedURL { + return storedURL + } + + guard let data = linkedItem.pdfData else { return remoteURL } + + let subPath = linkedItem.unwrappedTitle.isEmpty ? UUID().uuidString : linkedItem.unwrappedTitle + + let path = FileManager.default + .urls(for: .cachesDirectory, in: .userDomainMask)[0] + .appendingPathComponent(subPath) + + do { + try data.write(to: path) + storedURL = path + return path + } catch { + return remoteURL + } + } + public func loadHighlightPatches(completion onComplete: @escaping ([String]) -> Void) { onComplete(linkedItem.highlights.asArray(of: Highlight.self).map { $0.patch ?? "" }) } diff --git a/apple/OmnivoreKit/Sources/Models/DataModels/FeedItem.swift b/apple/OmnivoreKit/Sources/Models/DataModels/FeedItem.swift index 2d8c3761d..dcce41ed7 100644 --- a/apple/OmnivoreKit/Sources/Models/DataModels/FeedItem.swift +++ b/apple/OmnivoreKit/Sources/Models/DataModels/FeedItem.swift @@ -55,9 +55,6 @@ public extension LinkedItem { var pdfURL: URL? { guard isPDF else { return nil } - if let data = pdfData { - print("") - } return URL(string: pageURLString ?? "") } diff --git a/apple/Sources/MainApp.swift b/apple/Sources/MainApp.swift index 9e2520831..712c76265 100644 --- a/apple/Sources/MainApp.swift +++ b/apple/Sources/MainApp.swift @@ -46,6 +46,6 @@ struct MainApp: App { } private func pdfViewerProvider(url: URL, viewModel: PDFViewerViewModel) -> AnyView { - AnyView(PDFViewer(pdfURL: url, viewModel: viewModel)) + AnyView(PDFViewer(remoteURL: url, viewModel: viewModel)) } } diff --git a/apple/Sources/PDFViewer.swift b/apple/Sources/PDFViewer.swift index a35656c8b..611b19c12 100644 --- a/apple/Sources/PDFViewer.swift +++ b/apple/Sources/PDFViewer.swift @@ -21,8 +21,8 @@ import Utils @State var readerView: Bool = false @State private var shareLink: ShareLink? - init(pdfURL: URL, viewModel: PDFViewerViewModel) { - self.pdfURL = pdfURL + init(remoteURL: URL, viewModel: PDFViewerViewModel) { + self.pdfURL = viewModel.dataURL(remoteURL: remoteURL) self.viewModel = viewModel self.document = HighlightedDocument(url: pdfURL, viewModel: viewModel) self.coordinator = PDFViewCoordinator(document: document, viewModel: viewModel)