'Is there a way of detecting how much more a PDF has to load using PDFKit and SwiftUI?

I have a SwiftUI app where I'm loading a pdf from an online URL. While it loads, I'm showing a standard ProgressView with a spinning icon, then showing the pdf. However, I've seen that you can also show the amount loaded in a ProgressView. Is there any way to tell how much of the PDF has loaded? My code is below.

import PDFKit
import SwiftUI
import Combine

class DataLoader : ObservableObject {
    @Published var data : Data?
    var cancellable : AnyCancellable?
    
    func loadUrl(url: URL) {
        cancellable = URLSession.shared.dataTaskPublisher(for: url)
            .map { $0.data }
            .receive(on: RunLoop.main)
            .sink(receiveCompletion: { (completion) in
                switch completion {
                case .failure(let failureType):
                    print(failureType)
                    //handle potential errors here
                case .finished:
                    break
                }
        }, receiveValue: { (data) in
            self.data = data
        })
    }
}

struct PDFSwiftUIView : View {
    @StateObject private var dataLoader = DataLoader()
    var StringToBeLoaded: String
    
    var body: some View {
        VStack {
            if let data = dataLoader.data {
                PDFRepresentedView(data: data)
            } else {
                ProgressView("PDF Loading - Please Wait").progressViewStyle(.circular)
            }
        }.onAppear {
            dataLoader.loadUrl(url: URL(string: StringToBeLoaded)!)
        }
    }
}

struct PDFRepresentedView: UIViewRepresentable {
    typealias UIViewType = PDFView
    
    let data: Data
    let singlePage: Bool = false
    
    func makeUIView(context _: UIViewRepresentableContext<PDFRepresentedView>) -> UIViewType {
        let pdfView = PDFView()
        
        pdfView.document = PDFDocument(data: data)
        pdfView.autoScales = true
        if singlePage {
            pdfView.displayMode = .singlePage
        }
        return pdfView
    }
    
    func updateUIView(_ pdfView: UIViewType, context: UIViewRepresentableContext<PDFRepresentedView>) {
        pdfView.document = PDFDocument(data: data)
    }
}


struct ContentV_Previews: PreviewProvider {
    static var previews: some View {
        PDFSwiftUIView(StringToBeLoaded: "https://www.campwinadu.com/wp-content/uploads/2022/03/Winadu-Monthly-Bugle-March-2022-1.pdf")
    }
}


and the SwiftUI View that displays that:

import SwiftUI
import PDFKit

struct ContentView: View {
    
    
    var body: some View {
      PDFSwiftUIView(StringToBeLoaded: "https://www.campwinadu.com/wp-content/uploads/2022/03/Winadu-Monthly-Bugle-March-2022-1.pdf")
    }
   
}

Once again, to restate, I can load the PDF and detect when it is loaded, but I want to know if there is any way to detect how much has been loaded, ideally to be able to show a progress bar. Thank you.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source