ViewerQuickLook.swift 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. //
  2. // ViewerQuickLook.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 12/01/23.
  6. // Copyright © 2023 Marino Faggiana. All rights reserved.
  7. //
  8. import SwiftUI
  9. import QuickLook
  10. import Mantis
  11. struct ViewerQuickLook: UIViewControllerRepresentable {
  12. let url: URL
  13. //var timer: DispatchSourceTimer = DispatchSource.makeTimerSource(queue: .main)
  14. // @Binding var isPresentedQuickLook: Bool
  15. // @Binding var previewStore: PreviewStore
  16. func makeUIViewController(context: Context) -> UINavigationController {
  17. let controller = QLPreviewController()
  18. controller.dataSource = context.coordinator
  19. controller.delegate = context.coordinator
  20. context.coordinator.viewController = controller
  21. controller.navigationItem.rightBarButtonItem = UIBarButtonItem(
  22. barButtonSystemItem: .done, target: context.coordinator,
  23. action: #selector(context.coordinator.dismiss)
  24. )
  25. controller.navigationItem.leftBarButtonItem = UIBarButtonItem(
  26. title: NSLocalizedString("_crop_", comment: ""), style: UIBarButtonItem.Style.plain, target: context.coordinator,
  27. action: #selector(context.coordinator.crop)
  28. )
  29. /*
  30. timer.schedule(deadline: .now(), repeating: 0.3)
  31. timer.setEventHandler {
  32. let numItemsRight = controller.navigationItem.rightBarButtonItems?.count ?? 0
  33. if let buttonCrop = controller.navigationItem.leftBarButtonItems?.first {
  34. if numItemsRight > 1 && buttonCrop.isEnabled {
  35. buttonCrop.isEnabled = false
  36. if let buttonDone = controller.navigationItem.rightBarButtonItems?.last {
  37. buttonDone.isEnabled = false
  38. }
  39. }
  40. if numItemsRight == 1 && !buttonCrop.isEnabled {
  41. buttonCrop.isEnabled = true
  42. if let buttonDone = controller.navigationItem.rightBarButtonItems?.first {
  43. buttonDone.isEnabled = true
  44. }
  45. }
  46. }
  47. }
  48. timer.resume()
  49. */
  50. let navigationController = UINavigationController(rootViewController: controller)
  51. return navigationController
  52. }
  53. func updateUIViewController(_ uiViewController: UINavigationController, context: Context) { }
  54. func makeCoordinator() -> Coordinator {
  55. return Coordinator(parent: self)
  56. }
  57. class Coordinator: NSObject, QLPreviewControllerDataSource, QLPreviewControllerDelegate, CropViewControllerDelegate {
  58. weak var viewController: QLPreviewController?
  59. let parent: ViewerQuickLook
  60. var image: UIImage?
  61. var hasChange = false
  62. init(parent: ViewerQuickLook) {
  63. self.parent = parent
  64. }
  65. @objc func dismiss() {
  66. self.viewController?.dismiss(animated: true)
  67. /*
  68. parent.isPresentedQuickLook = false
  69. if let image = image {
  70. parent.previewStore.image = image
  71. }
  72. parent.previewStore.hasChanges = hasChange
  73. */
  74. }
  75. // MARK: -
  76. func numberOfPreviewItems(in controller: QLPreviewController) -> Int {
  77. return 1
  78. }
  79. func previewController(_ controller: QLPreviewController, editingModeFor previewItem: QLPreviewItem) -> QLPreviewItemEditingMode {
  80. return .createCopy
  81. }
  82. func previewController(_ controller: QLPreviewController, didSaveEditedCopyOf previewItem: QLPreviewItem, at modifiedContentsURL: URL) {
  83. guard NCUtilityFileSystem.shared.moveFile(atPath: modifiedContentsURL.path, toPath: parent.url.path) else { return }
  84. if let image = UIImage(contentsOfFile: parent.url.path) {
  85. self.image = image
  86. self.hasChange = true
  87. }
  88. }
  89. func previewController(_ controller: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem {
  90. return parent.url as NSURL
  91. }
  92. // MARK: -
  93. func cropViewControllerDidCrop(_ cropViewController: Mantis.CropViewController, cropped: UIImage, transformation: Mantis.Transformation, cropInfo: Mantis.CropInfo) {
  94. cropViewController.dismiss(animated: true)
  95. guard let data = cropped.jpegData(compressionQuality: 1) else { return }
  96. do {
  97. try data.write(to: parent.url)
  98. self.image = cropped
  99. self.hasChange = true
  100. viewController?.reloadData()
  101. } catch { }
  102. }
  103. func cropViewControllerDidCancel(_ cropViewController: Mantis.CropViewController, original: UIImage) {
  104. cropViewController.dismiss(animated: true)
  105. }
  106. func cropViewControllerDidFailToCrop(_ cropViewController: Mantis.CropViewController, original: UIImage) {}
  107. func cropViewControllerDidBeginResize(_ cropViewController: Mantis.CropViewController) {}
  108. func cropViewControllerDidEndResize(_ cropViewController: Mantis.CropViewController, original: UIImage, cropInfo: Mantis.CropInfo) {}
  109. func cropViewControllerDidImageTransformed(_ cropViewController: Mantis.CropViewController) { }
  110. @objc func crop() {
  111. guard let image = UIImage(contentsOfFile: parent.url.path) else { return }
  112. let config = Mantis.Config()
  113. if let bundleIdentifier = Bundle.main.bundleIdentifier {
  114. config.localizationConfig.bundle = Bundle(identifier: bundleIdentifier)
  115. config.localizationConfig.tableName = "Localizable"
  116. }
  117. let cropViewController = Mantis.cropViewController(image: image, config: config)
  118. cropViewController.delegate = self
  119. cropViewController.modalPresentationStyle = .fullScreen
  120. viewController?.present(cropViewController, animated: true)
  121. }
  122. }
  123. }