Auto-advancing pages in a PDF file (PDFKit)

Hi, there !

I am using a play/pause button to advance pages every x seconds. However, the following code moves to the end of the document instead of going to the next page.

I am using the methods from PDFKit to control navigation:

.goToNextPage()
.canGoToNextPage()

I am using two structs to render a View of play/pause buttons:

struct PlayButton: View {
    @Binding var isPlaying: Bool

    var body: some View {
        Button(isPlaying ? "⏹️" : "▶️") {
            isPlaying.toggle()
        }
    }
}

struct PlayerView: View {
    var pdfView: PDFView
    @State private var isPlaying: Bool
    
    init(pdfView: PDFView, isPlaying: Bool) {
        self.pdfView = pdfView
        self.isPlaying = isPlaying
    }

    var body: some View {
        VStack {
            Text("Hi There !")
                .foregroundStyle(isPlaying ? .primary : .secondary)
            PlayButton(isPlaying: $isPlaying) // Pass a binding.
        }
    }
}

and I am passing state (this is the bit I am probably doing wrong) between structs using:

@Binding
@State

Does anyone know how can I solve this problem ?

Full code:

ContentView.swift

import SwiftUI
import PDFKit

struct PlayButton: View {
    @Binding var isPlaying: Bool

    var body: some View {
        Button(isPlaying ? "⏹️" : "▶️") {
            isPlaying.toggle()
        }
    }
}

struct PlayerView: View {
    var pdfView: PDFView
    @State private var isPlaying: Bool
    
    init(pdfView: PDFView, isPlaying: Bool) {
        self.pdfView = pdfView
        self.isPlaying = isPlaying
    }

    var body: some View {
        VStack {
            Text("Play/Pause")
                .foregroundStyle(isPlaying ? .primary : .secondary)
            PlayButton(isPlaying: $isPlaying) // Pass a binding.
        }
    }
}



struct TestPDFViewNavigation: View {
    let pdfDoc: PDFDocument
    let pdfView: PDFView
    
    init(pdfDoc: PDFDocument) {
        self.pdfDoc = pdfDoc
        self.pdfView = PDFView()
        self.pdfView.document = pdfDoc
        // self.isPlaying = false
    }
    
    var body: some View {
        PDFKitView(
            pdfView: pdfView
        )
    }
    
    // PDFKitView :: PDFView -> View
    struct PDFKitView: View {
        let pdfView: PDFView
        @State var isPlaying: Bool = true
        
        init(pdfView: PDFView) {
            self.pdfView = pdfView
        }

        var body: some View {
            VStack {
                // ControlsView(pdfView: pdfView, forward: true)
                PlayerView(pdfView: pdfView, isPlaying: false)
                PDFKitRepresentedView(pdfView: pdfView, isPlaying: isPlaying)
            }
        }
    }

    // PDFKitRepresentedView :: PDFView -> UIViewRepresentable
    struct PDFKitRepresentedView: UIViewRepresentable {
        let pdfView: PDFView
        @State var isPlaying : Bool

        func makeUIView(context: Context) -> UIView {
            return {
                pdfView.autoScales = true
                pdfView.displayDirection = .horizontal
                pdfView.usePageViewController(true)
                return pdfView
            }()
        }

        func updateUIView(_ uiView: UIView, context: Context) {
            guard let pdfView = uiView as? PDFView else {
                return
            }
            DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(5*1000)) {
                while pdfView.canGoToNextPage && isPlaying {
                    pdfView.goToNextPage(self)
                    print(isPlaying)
                }
            }
        }
    }
}

let samplePDF = PDFDocument(url: Bundle.main.url(forResource: "MacBook", withExtension: "pdf")!)

struct TestPDFViewNavigation_Previews: PreviewProvider {
    static var previews: some View {
        TestPDFViewNavigation(pdfDoc: samplePDF!)
    }
}