123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- //
- // ViewerQuickLook.swift
- // Nextcloud
- //
- // Created by Marino Faggiana on 12/01/23.
- // Copyright © 2023 Marino Faggiana. All rights reserved.
- //
- import SwiftUI
- import QuickLook
- import Mantis
- struct ViewerQuickLook: UIViewControllerRepresentable {
- let url: URL
- //var timer: DispatchSourceTimer = DispatchSource.makeTimerSource(queue: .main)
- // @Binding var isPresentedQuickLook: Bool
- // @Binding var previewStore: PreviewStore
- func makeUIViewController(context: Context) -> UINavigationController {
- let controller = QLPreviewController()
- controller.dataSource = context.coordinator
- controller.delegate = context.coordinator
- context.coordinator.viewController = controller
- controller.navigationItem.rightBarButtonItem = UIBarButtonItem(
- barButtonSystemItem: .done, target: context.coordinator,
- action: #selector(context.coordinator.dismiss)
- )
- controller.navigationItem.leftBarButtonItem = UIBarButtonItem(
- title: NSLocalizedString("_crop_", comment: ""), style: UIBarButtonItem.Style.plain, target: context.coordinator,
- action: #selector(context.coordinator.crop)
- )
- /*
- timer.schedule(deadline: .now(), repeating: 0.3)
- timer.setEventHandler {
- let numItemsRight = controller.navigationItem.rightBarButtonItems?.count ?? 0
- if let buttonCrop = controller.navigationItem.leftBarButtonItems?.first {
- if numItemsRight > 1 && buttonCrop.isEnabled {
- buttonCrop.isEnabled = false
- if let buttonDone = controller.navigationItem.rightBarButtonItems?.last {
- buttonDone.isEnabled = false
- }
- }
- if numItemsRight == 1 && !buttonCrop.isEnabled {
- buttonCrop.isEnabled = true
- if let buttonDone = controller.navigationItem.rightBarButtonItems?.first {
- buttonDone.isEnabled = true
- }
- }
- }
- }
- timer.resume()
- */
- let navigationController = UINavigationController(rootViewController: controller)
- return navigationController
- }
- func updateUIViewController(_ uiViewController: UINavigationController, context: Context) { }
- func makeCoordinator() -> Coordinator {
- return Coordinator(parent: self)
- }
- class Coordinator: NSObject, QLPreviewControllerDataSource, QLPreviewControllerDelegate, CropViewControllerDelegate {
- weak var viewController: QLPreviewController?
- let parent: ViewerQuickLook
- var image: UIImage?
- var hasChange = false
- init(parent: ViewerQuickLook) {
- self.parent = parent
- }
- @objc func dismiss() {
- self.viewController?.dismiss(animated: true)
- /*
- parent.isPresentedQuickLook = false
- if let image = image {
- parent.previewStore.image = image
- }
- parent.previewStore.hasChanges = hasChange
- */
- }
- // MARK: -
- func numberOfPreviewItems(in controller: QLPreviewController) -> Int {
- return 1
- }
- func previewController(_ controller: QLPreviewController, editingModeFor previewItem: QLPreviewItem) -> QLPreviewItemEditingMode {
- return .createCopy
- }
- func previewController(_ controller: QLPreviewController, didSaveEditedCopyOf previewItem: QLPreviewItem, at modifiedContentsURL: URL) {
- guard NCUtilityFileSystem.shared.moveFile(atPath: modifiedContentsURL.path, toPath: parent.url.path) else { return }
- if let image = UIImage(contentsOfFile: parent.url.path) {
- self.image = image
- self.hasChange = true
- }
- }
- func previewController(_ controller: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem {
- return parent.url as NSURL
- }
- // MARK: -
- func cropViewControllerDidCrop(_ cropViewController: Mantis.CropViewController, cropped: UIImage, transformation: Mantis.Transformation, cropInfo: Mantis.CropInfo) {
- cropViewController.dismiss(animated: true)
- guard let data = cropped.jpegData(compressionQuality: 1) else { return }
- do {
- try data.write(to: parent.url)
- self.image = cropped
- self.hasChange = true
- viewController?.reloadData()
- } catch { }
- }
- func cropViewControllerDidCancel(_ cropViewController: Mantis.CropViewController, original: UIImage) {
- cropViewController.dismiss(animated: true)
- }
- func cropViewControllerDidFailToCrop(_ cropViewController: Mantis.CropViewController, original: UIImage) {}
- func cropViewControllerDidBeginResize(_ cropViewController: Mantis.CropViewController) {}
- func cropViewControllerDidEndResize(_ cropViewController: Mantis.CropViewController, original: UIImage, cropInfo: Mantis.CropInfo) {}
- func cropViewControllerDidImageTransformed(_ cropViewController: Mantis.CropViewController) { }
- @objc func crop() {
- guard let image = UIImage(contentsOfFile: parent.url.path) else { return }
- let config = Mantis.Config()
- if let bundleIdentifier = Bundle.main.bundleIdentifier {
- config.localizationConfig.bundle = Bundle(identifier: bundleIdentifier)
- config.localizationConfig.tableName = "Localizable"
- }
- let cropViewController = Mantis.cropViewController(image: image, config: config)
- cropViewController.delegate = self
- cropViewController.modalPresentationStyle = .fullScreen
- viewController?.present(cropViewController, animated: true)
- }
- }
- }
|