Skip to content

Commit

Permalink
Bit of cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
vallezw committed Jan 8, 2025
1 parent a43ca59 commit 1fd5b9b
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 189 deletions.
8 changes: 0 additions & 8 deletions SheetAble-iPadOS.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@
4B305E7A2C833A5E00AFDCC2 /* SheetAble_iPadOSUITestsLaunchTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4B305E792C833A5E00AFDCC2 /* SheetAble_iPadOSUITestsLaunchTests.swift */; };
4B305E872C8342CB00AFDCC2 /* .gitignore in Resources */ = {isa = PBXBuildFile; fileRef = 4B305E862C8342CB00AFDCC2 /* .gitignore */; };
4B7FC72F2C887E9600B1D5E2 /* SheetView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4B7FC72E2C887E9600B1D5E2 /* SheetView.swift */; };
4B7FC7312C88850C00B1D5E2 /* DrawView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4B7FC7302C88850C00B1D5E2 /* DrawView.swift */; };
4B7FC7332C88927100B1D5E2 /* CanvasViewRepresentable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4B7FC7322C88927100B1D5E2 /* CanvasViewRepresentable.swift */; };
4B7FC73D2C91DBA300B1D5E2 /* PDFViewPage.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4B7FC73C2C91DBA300B1D5E2 /* PDFViewPage.swift */; };
4B7FC73F2C91DC9E00B1D5E2 /* PDFKitViewRepresentable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4B7FC73E2C91DC9E00B1D5E2 /* PDFKitViewRepresentable.swift */; };
4B7FC7412C91DF6300B1D5E2 /* CombinationView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4B7FC7402C91DF6300B1D5E2 /* CombinationView.swift */; };
/* End PBXBuildFile section */
Expand Down Expand Up @@ -53,9 +51,7 @@
4B305E792C833A5E00AFDCC2 /* SheetAble_iPadOSUITestsLaunchTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SheetAble_iPadOSUITestsLaunchTests.swift; sourceTree = "<group>"; };
4B305E862C8342CB00AFDCC2 /* .gitignore */ = {isa = PBXFileReference; lastKnownFileType = text; path = .gitignore; sourceTree = "<group>"; };
4B7FC72E2C887E9600B1D5E2 /* SheetView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SheetView.swift; sourceTree = "<group>"; };
4B7FC7302C88850C00B1D5E2 /* DrawView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DrawView.swift; sourceTree = "<group>"; };
4B7FC7322C88927100B1D5E2 /* CanvasViewRepresentable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CanvasViewRepresentable.swift; sourceTree = "<group>"; };
4B7FC73C2C91DBA300B1D5E2 /* PDFViewPage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PDFViewPage.swift; sourceTree = "<group>"; };
4B7FC73E2C91DC9E00B1D5E2 /* PDFKitViewRepresentable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PDFKitViewRepresentable.swift; sourceTree = "<group>"; };
4B7FC7402C91DF6300B1D5E2 /* CombinationView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CombinationView.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */
Expand Down Expand Up @@ -114,9 +110,7 @@
4B305E602C833A5E00AFDCC2 /* Assets.xcassets */,
4B305E622C833A5E00AFDCC2 /* Preview Content */,
4B7FC72E2C887E9600B1D5E2 /* SheetView.swift */,
4B7FC7302C88850C00B1D5E2 /* DrawView.swift */,
4B7FC7322C88927100B1D5E2 /* CanvasViewRepresentable.swift */,
4B7FC73C2C91DBA300B1D5E2 /* PDFViewPage.swift */,
4B7FC73E2C91DC9E00B1D5E2 /* PDFKitViewRepresentable.swift */,
4B7FC7402C91DF6300B1D5E2 /* CombinationView.swift */,
);
Expand Down Expand Up @@ -281,9 +275,7 @@
files = (
4B305E5F2C833A5C00AFDCC2 /* ContentView.swift in Sources */,
4B7FC72F2C887E9600B1D5E2 /* SheetView.swift in Sources */,
4B7FC73D2C91DBA300B1D5E2 /* PDFViewPage.swift in Sources */,
4B7FC7332C88927100B1D5E2 /* CanvasViewRepresentable.swift in Sources */,
4B7FC7312C88850C00B1D5E2 /* DrawView.swift in Sources */,
4B7FC73F2C91DC9E00B1D5E2 /* PDFKitViewRepresentable.swift in Sources */,
4B7FC7412C91DF6300B1D5E2 /* CombinationView.swift in Sources */,
4B305E5D2C833A5C00AFDCC2 /* SheetAble_iPadOSApp.swift in Sources */,
Expand Down
86 changes: 62 additions & 24 deletions SheetAble-iPadOS/CanvasViewRepresentable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,71 @@ import PencilKit
/// A SwiftUI view wrapper for PKCanvasView, which integrates PencilKit's drawing capabilities.
struct CanvasView: UIViewRepresentable {
@Binding var canvasView: PKCanvasView

func makeUIView(context: Context) -> PKCanvasView {
// Initialize the tool and tool picker
let canvas = canvasView
canvas.tool = PKInkingTool(.pen, color: .black, width: 1)
canvas.backgroundColor = .clear
canvas.isOpaque = false

let toolPicker = PKToolPicker()
toolPicker.setVisible(true, forFirstResponder: canvas)
toolPicker.addObserver(canvas)

let toolPicker: PKToolPicker // Pass the tool picker from the parent view

func makeUIView(context: Context) -> UIScrollView {
let scrollView = UIScrollView()
scrollView.delegate = context.coordinator
scrollView.minimumZoomScale = 1.0
scrollView.maximumZoomScale = 5.0

// Setup PKCanvasView
canvasView.tool = PKInkingTool(.pen, color: .black, width: 1)
canvasView.backgroundColor = .clear
canvasView.isOpaque = false
canvasView.drawingPolicy = .anyInput

// Add the canvas view to the scroll view
scrollView.addSubview(canvasView)
canvasView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
canvasView.topAnchor.constraint(equalTo: scrollView.topAnchor),
canvasView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor),
canvasView.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor),
canvasView.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor),
canvasView.widthAnchor.constraint(equalTo: scrollView.widthAnchor),
canvasView.heightAnchor.constraint(equalTo: scrollView.heightAnchor)
])

// Attach the tool picker
toolPicker.setVisible(true, forFirstResponder: canvasView)
toolPicker.addObserver(canvasView)
DispatchQueue.main.async {
canvas.becomeFirstResponder()
canvasView.becomeFirstResponder()
}

return canvas

return scrollView
}

func updateUIView(_ uiView: UIScrollView, context: Context) {
// No need to apply transforms to the canvas itself; scrollView handles zoom
}

func updateUIView(_ uiView: PKCanvasView, context: Context) {
// Ensure the tool picker remains visible
if let window = uiView.window,
let toolPicker = PKToolPicker.shared(for: window) {
toolPicker.setVisible(true, forFirstResponder: uiView)
toolPicker.addObserver(uiView)
DispatchQueue.main.async {
uiView.becomeFirstResponder()

func makeCoordinator() -> Coordinator {
Coordinator(self)
}

class Coordinator: NSObject, UIScrollViewDelegate {
var parent: CanvasView

init(_ parent: CanvasView) {
self.parent = parent
}

func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return parent.canvasView // No need for 'self' here, it's not inside a closure
}

func scrollViewDidZoom(_ scrollView: UIScrollView) {
// Ensure the tool picker remains active after zooming
if let window = parent.canvasView.window,
let toolPicker = PKToolPicker.shared(for: window) {
toolPicker.setVisible(true, forFirstResponder: parent.canvasView)

// Explicitly using 'self' here because it's inside a closure
DispatchQueue.main.async {
self.parent.canvasView.becomeFirstResponder() // 'self' is required here
}
}
}
}
Expand Down
45 changes: 10 additions & 35 deletions SheetAble-iPadOS/CombinationView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,15 @@ import SwiftUI
import PDFKit
import PencilKit

//struct CombinationView: View {
// var body: some View {
// ZStack {
// PDFViewPage()
//
// DrawView().zIndex(1)
//
// }
// }
//}
//
//#Preview {
// CombinationView()
//}

struct CombinationView: View {
@State private var pdfDocument: PDFDocument? = nil
@State private var isLoading: Bool = true
@State private var currentPage: Int = 0
@State private var canvasView: PKCanvasView = PKCanvasView()

// Manage the tool picker at this level
private let toolPicker = PKToolPicker()
@State private var zoomScale: CGFloat = 1.0 // Add zoom scale state

private let toolPicker = PKToolPicker() // Manage tool picker at this level

let pdfURL = URL(string: "https://www.sldttc.org/allpdf/21583473018.pdf")!

Expand All @@ -56,13 +42,13 @@ struct CombinationView: View {
.disabled(currentPage >= (pdfDocument.pageCount - 1))
}
.padding()

ZStack {
PDFKitView(pdfDocument: pdfDocument, currentPage: $currentPage)
CanvasView(canvasView: $canvasView)
.onAppear {
attachToolPicker()
}
// PDF View with zoomScale binding
PDFKitView(pdfDocument: pdfDocument, currentPage: $currentPage, zoomScale: $zoomScale)

// Canvas View with Scroll for Zoom and ToolPicker
CanvasView(canvasView: $canvasView, toolPicker: toolPicker)
}
}
} else {
Expand All @@ -89,8 +75,7 @@ struct CombinationView: View {
}

func updateCanvas() {
canvasView.drawing = PKDrawing()
attachToolPicker() // Reattach the tool picker each time
canvasView.drawing = PKDrawing() // Reset the canvas content for new pages
}

func downloadPDF(from url: URL) {
Expand All @@ -109,14 +94,4 @@ struct CombinationView: View {
}
task.resume()
}

// Attach tool picker to the canvas view
func attachToolPicker() {
toolPicker.setVisible(true, forFirstResponder: canvasView)
toolPicker.addObserver(canvasView)
DispatchQueue.main.async {
canvasView.becomeFirstResponder()
}
}
}

12 changes: 1 addition & 11 deletions SheetAble-iPadOS/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,9 @@ struct ContentView: View {
}.padding()}.tabItem {
Label("Received", systemImage: "tray.and.arrow.down.fill")
}

DrawView()
.tabItem {
Label("Drawing", systemImage: "square.and.pencil")
}

SheetView(sheet: sheets[0])
.tabItem {
Label("Account", systemImage: "person.crop.circle.fill")
}
PDFViewPage()
.tabItem {
Label("PDF Page", systemImage: "doc")
Label("Sheet", systemImage: "person.crop.circle.fill")
}
CombinationView()
.tabItem {
Expand Down
20 changes: 0 additions & 20 deletions SheetAble-iPadOS/DrawView.swift

This file was deleted.

6 changes: 4 additions & 2 deletions SheetAble-iPadOS/PDFKitViewRepresentable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@

import SwiftUI
import PDFKit

struct PDFKitView: UIViewRepresentable {
let pdfDocument: PDFDocument?
@Binding var currentPage: Int
@Binding var zoomScale: CGFloat // Add a zoomScale binding

func makeUIView(context: Context) -> PDFView {
let pdfView = PDFView()
Expand All @@ -29,6 +29,8 @@ struct PDFKitView: UIViewRepresentable {
uiView.go(to: page)
}
}

// Set zoom scale based on the binding
uiView.scaleFactor = zoomScale
}
}

89 changes: 0 additions & 89 deletions SheetAble-iPadOS/PDFViewPage.swift

This file was deleted.

0 comments on commit 1fd5b9b

Please sign in to comment.