IceCubes/IceCubesApp/App/QuickLookRepresentable.swift

73 lines
1.8 KiB
Swift
Raw Normal View History

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 {
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
}
}
}
class AppQLPreviewController: QLPreviewController {
2023-01-16 14:40:23 +01:00
private var closeButton: UIBarButtonItem {
.init(
title: NSLocalizedString("action.done", comment: ""),
style: .plain,
target: self,
action: #selector(onCloseButton)
)
2023-01-16 14:40:23 +01:00
}
2023-01-17 11:36:01 +01:00
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
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
@objc private func onCloseButton() {
dismiss(animated: true)
}
}