set pdf data in local document and pass that url to pspdfkit

This commit is contained in:
Satindar Dhillon 2022-04-28 14:58:46 -07:00
parent 8b71188a26
commit 29c6193bda
4 changed files with 26 additions and 6 deletions

View file

@ -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<AnyCancellable>()
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 ?? "" })
}

View file

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

View file

@ -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))
}
}

View file

@ -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)