2023-01-16 14:40:23 +01:00
|
|
|
import QuickLook
|
2023-01-17 11:36:01 +01:00
|
|
|
import SwiftUI
|
|
|
|
import UIKit
|
2023-01-16 14:40:23 +01:00
|
|
|
|
|
|
|
extension URL: Identifiable {
|
|
|
|
public var id: String {
|
|
|
|
absoluteString
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct QuickLookPreview: UIViewControllerRepresentable {
|
|
|
|
let selectedURL: URL
|
|
|
|
let urls: [URL]
|
2023-01-17 11:36:01 +01:00
|
|
|
|
2023-01-16 14:40:23 +01:00
|
|
|
func makeUIViewController(context: Context) -> UINavigationController {
|
2023-01-17 15:14:50 +01:00
|
|
|
let controller = AppQLPreviewController()
|
2023-01-16 14:40:23 +01:00
|
|
|
controller.dataSource = context.coordinator
|
|
|
|
controller.delegate = context.coordinator
|
|
|
|
let nav = UINavigationController(rootViewController: controller)
|
|
|
|
return nav
|
|
|
|
}
|
2023-01-17 11:36:01 +01:00
|
|
|
|
2023-01-16 14:40:23 +01:00
|
|
|
func updateUIViewController(
|
2023-01-17 11:36:01 +01:00
|
|
|
_: UINavigationController, context _: Context
|
|
|
|
) {}
|
|
|
|
|
2023-01-16 14:40:23 +01:00
|
|
|
func makeCoordinator() -> Coordinator {
|
|
|
|
return Coordinator(parent: self)
|
|
|
|
}
|
2023-01-17 11:36:01 +01:00
|
|
|
|
2023-01-16 14:40:23 +01:00
|
|
|
class Coordinator: NSObject, QLPreviewControllerDataSource, QLPreviewControllerDelegate {
|
|
|
|
let parent: QuickLookPreview
|
2023-01-17 11:36:01 +01:00
|
|
|
|
2023-01-16 14:40:23 +01:00
|
|
|
init(parent: QuickLookPreview) {
|
|
|
|
self.parent = parent
|
|
|
|
}
|
2023-01-17 11:36:01 +01:00
|
|
|
|
|
|
|
func numberOfPreviewItems(in _: QLPreviewController) -> Int {
|
2023-01-16 14:40:23 +01:00
|
|
|
return parent.urls.count
|
|
|
|
}
|
2023-01-17 11:36:01 +01:00
|
|
|
|
|
|
|
func previewController(_: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem {
|
2023-01-16 14:40:23 +01:00
|
|
|
return parent.urls[index] as QLPreviewItem
|
|
|
|
}
|
2023-01-17 11:36:01 +01:00
|
|
|
|
|
|
|
func previewController(_: QLPreviewController, editingModeFor _: QLPreviewItem) -> QLPreviewItemEditingMode {
|
2023-01-16 14:40:23 +01:00
|
|
|
.createCopy
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-17 15:14:50 +01:00
|
|
|
class AppQLPreviewController: QLPreviewController {
|
2023-01-16 14:40:23 +01:00
|
|
|
private var closeButton: UIBarButtonItem {
|
|
|
|
.init(title: "Done", style: .plain, target: self, action: #selector(onCloseButton))
|
|
|
|
}
|
2023-01-17 11:36:01 +01:00
|
|
|
|
2023-01-16 14:40:23 +01:00
|
|
|
override func viewDidLoad() {
|
|
|
|
super.viewDidLoad()
|
2023-01-17 13:02:05 +01:00
|
|
|
if UIDevice.current.userInterfaceIdiom != .pad {
|
|
|
|
navigationItem.rightBarButtonItem = closeButton
|
|
|
|
}
|
2023-01-16 14:40:23 +01:00
|
|
|
}
|
2023-01-17 11:36:01 +01:00
|
|
|
|
2023-01-16 14:40:23 +01:00
|
|
|
override func viewDidLayoutSubviews() {
|
|
|
|
super.viewDidLayoutSubviews()
|
|
|
|
navigationItem.rightBarButtonItem = closeButton
|
|
|
|
}
|
2023-01-17 11:36:01 +01:00
|
|
|
|
2023-01-16 14:40:23 +01:00
|
|
|
@objc private func onCloseButton() {
|
|
|
|
dismiss(animated: true)
|
|
|
|
}
|
|
|
|
}
|