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-30 07:27:06 +01:00
|
|
|
func makeUIViewController(context _: Context) -> UIViewController {
|
2023-09-16 14:15:03 +02:00
|
|
|
AppQLPreviewController(selectedURL: selectedURL, urls: urls)
|
2023-01-16 14:40:23 +01:00
|
|
|
}
|
2023-01-17 11:36:01 +01:00
|
|
|
|
2023-01-16 14:40:23 +01:00
|
|
|
func updateUIViewController(
|
2023-01-28 06:45:15 +01:00
|
|
|
_: UIViewController, context _: Context
|
2023-01-17 11:36:01 +01:00
|
|
|
) {}
|
2023-01-28 06:45:15 +01:00
|
|
|
}
|
2023-01-17 11:36:01 +01:00
|
|
|
|
2023-09-14 11:04:14 +02:00
|
|
|
@MainActor
|
2023-01-28 06:45:15 +01:00
|
|
|
class AppQLPreviewController: UIViewController {
|
|
|
|
let selectedURL: URL
|
|
|
|
let urls: [URL]
|
2023-01-30 07:27:06 +01:00
|
|
|
|
|
|
|
var qlController: QLPreviewController?
|
|
|
|
|
2023-01-28 06:45:15 +01:00
|
|
|
init(selectedURL: URL, urls: [URL]) {
|
|
|
|
self.selectedURL = selectedURL
|
|
|
|
self.urls = urls
|
|
|
|
super.init(nibName: nil, bundle: nil)
|
2023-01-16 14:40:23 +01:00
|
|
|
}
|
2023-01-30 07:27:06 +01:00
|
|
|
|
|
|
|
@available(*, unavailable)
|
|
|
|
required init?(coder _: NSCoder) {
|
2023-01-28 06:45:15 +01:00
|
|
|
fatalError("init(coder:) has not been implemented")
|
|
|
|
}
|
2023-01-30 07:27:06 +01:00
|
|
|
|
2023-01-28 06:45:15 +01:00
|
|
|
override func viewWillAppear(_ animated: Bool) {
|
|
|
|
super.viewWillAppear(animated)
|
2023-01-30 07:27:06 +01:00
|
|
|
if qlController == nil {
|
|
|
|
qlController = QLPreviewController()
|
|
|
|
qlController?.dataSource = self
|
|
|
|
qlController?.delegate = self
|
|
|
|
qlController?.currentPreviewItemIndex = urls.firstIndex(of: selectedURL) ?? 0
|
|
|
|
present(qlController!, animated: true)
|
2023-01-16 14:40:23 +01:00
|
|
|
}
|
2023-01-28 06:45:15 +01:00
|
|
|
}
|
|
|
|
}
|
2023-01-17 11:36:01 +01:00
|
|
|
|
2023-01-30 07:27:06 +01:00
|
|
|
extension AppQLPreviewController: QLPreviewControllerDataSource {
|
2023-09-14 11:04:14 +02:00
|
|
|
nonisolated func numberOfPreviewItems(in _: QLPreviewController) -> Int {
|
2023-09-16 14:15:03 +02:00
|
|
|
urls.count
|
2023-01-28 06:45:15 +01:00
|
|
|
}
|
2023-01-17 11:36:01 +01:00
|
|
|
|
2023-09-14 11:04:14 +02:00
|
|
|
nonisolated func previewController(_: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem {
|
2023-09-16 14:15:03 +02:00
|
|
|
urls[index] as QLPreviewItem
|
2023-01-16 14:40:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-30 07:27:06 +01:00
|
|
|
extension AppQLPreviewController: QLPreviewControllerDelegate {
|
2023-09-14 11:04:14 +02:00
|
|
|
nonisolated func previewController(_: QLPreviewController, editingModeFor _: QLPreviewItem) -> QLPreviewItemEditingMode {
|
2023-01-28 06:45:15 +01:00
|
|
|
.createCopy
|
2023-01-16 14:40:23 +01:00
|
|
|
}
|
2023-01-17 11:36:01 +01:00
|
|
|
|
2023-09-14 11:04:14 +02:00
|
|
|
nonisolated func previewControllerWillDismiss(_: QLPreviewController) {
|
|
|
|
Task { @MainActor in
|
|
|
|
dismiss(animated: true)
|
|
|
|
}
|
2023-01-16 14:40:23 +01:00
|
|
|
}
|
2023-02-22 19:09:39 +01:00
|
|
|
|
2023-09-14 11:04:14 +02:00
|
|
|
nonisolated func previewControllerDidDismiss(_: QLPreviewController) {
|
|
|
|
Task { @MainActor in
|
|
|
|
dismiss(animated: true)
|
|
|
|
}
|
2023-02-22 07:04:02 +01:00
|
|
|
}
|
2023-01-28 06:45:15 +01:00
|
|
|
}
|
2023-01-17 11:36:01 +01:00
|
|
|
|
2023-01-28 06:45:15 +01:00
|
|
|
struct TransparentBackground: UIViewControllerRepresentable {
|
2023-01-30 07:27:06 +01:00
|
|
|
public func makeUIViewController(context _: Context) -> UIViewController {
|
2023-09-16 14:15:03 +02:00
|
|
|
TransparentController()
|
2023-01-28 06:45:15 +01:00
|
|
|
}
|
2023-01-30 07:27:06 +01:00
|
|
|
|
|
|
|
public func updateUIViewController(_: UIViewController, context _: Context) {}
|
|
|
|
|
2023-01-28 06:45:15 +01:00
|
|
|
class TransparentController: UIViewController {
|
|
|
|
override func viewDidLoad() {
|
|
|
|
super.viewDidLoad()
|
|
|
|
view.backgroundColor = .clear
|
|
|
|
}
|
2023-01-30 07:27:06 +01:00
|
|
|
|
2023-01-28 06:45:15 +01:00
|
|
|
override func willMove(toParent parent: UIViewController?) {
|
|
|
|
super.willMove(toParent: parent)
|
|
|
|
parent?.view?.backgroundColor = .clear
|
|
|
|
parent?.modalPresentationStyle = .overCurrentContext
|
|
|
|
}
|
2023-01-16 14:40:23 +01:00
|
|
|
}
|
|
|
|
}
|