ViewerQuickLook.swift 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 isPresented: 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. timer.schedule(deadline: .now(), repeating: 0.1)
  30. timer.setEventHandler {
  31. let numItemsRight = controller.navigationItem.rightBarButtonItems?.count ?? 0
  32. if let buttonCrop = controller.navigationItem.leftBarButtonItems?.first {
  33. if numItemsRight > 1 && buttonCrop.isEnabled {
  34. buttonCrop.isEnabled = false
  35. if let buttonDone = controller.navigationItem.rightBarButtonItems?.last {
  36. buttonDone.isEnabled = false
  37. }
  38. }
  39. if numItemsRight == 1 && !buttonCrop.isEnabled {
  40. buttonCrop.isEnabled = true
  41. if let buttonDone = controller.navigationItem.rightBarButtonItems?.first {
  42. buttonDone.isEnabled = true
  43. }
  44. }
  45. }
  46. }
  47. let navigationController = UINavigationController(rootViewController: controller)
  48. return navigationController
  49. }
  50. func updateUIViewController(_ uiViewController: UINavigationController, context: Context) { }
  51. func makeCoordinator() -> Coordinator {
  52. return Coordinator(parent: self)
  53. }
  54. class Coordinator: NSObject, QLPreviewControllerDataSource, QLPreviewControllerDelegate, CropViewControllerDelegate {
  55. weak var viewController: QLPreviewController?
  56. let parent: ViewerQuickLook
  57. init(parent: ViewerQuickLook) {
  58. self.parent = parent
  59. }
  60. @objc func dismiss() {
  61. parent.isPresented = false
  62. }
  63. // MARK: -
  64. func numberOfPreviewItems(in controller: QLPreviewController) -> Int {
  65. return 1
  66. }
  67. func previewController(_ controller: QLPreviewController, editingModeFor previewItem: QLPreviewItem) -> QLPreviewItemEditingMode {
  68. return .createCopy
  69. }
  70. func previewController(_ controller: QLPreviewController, didSaveEditedCopyOf previewItem: QLPreviewItem, at modifiedContentsURL: URL) {
  71. guard NCUtilityFileSystem.shared.moveFile(atPath: modifiedContentsURL.path, toPath: parent.url.path) else { return }
  72. parent.previewStore.hasChanges = true
  73. if let image = UIImage(contentsOfFile: parent.url.path) {
  74. parent.previewStore.image = image
  75. }
  76. }
  77. func previewController(_ controller: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem {
  78. return parent.url as NSURL
  79. }
  80. // MARK: -
  81. func cropViewControllerDidCrop(_ cropViewController: Mantis.CropViewController, cropped: UIImage, transformation: Mantis.Transformation, cropInfo: Mantis.CropInfo) {
  82. cropViewController.dismiss(animated: true)
  83. guard let data = cropped.jpegData(compressionQuality: 1) else { return }
  84. do {
  85. try data.write(to: parent.url)
  86. parent.previewStore.hasChanges = true
  87. parent.previewStore.image = cropped
  88. viewController?.reloadData()
  89. } catch { }
  90. }
  91. func cropViewControllerDidCancel(_ cropViewController: Mantis.CropViewController, original: UIImage) {
  92. cropViewController.dismiss(animated: true)
  93. // Resume timer verify navigationItem
  94. DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
  95. self.parent.timer.resume()
  96. }
  97. }
  98. func cropViewControllerDidFailToCrop(_ cropViewController: Mantis.CropViewController, original: UIImage) {}
  99. func cropViewControllerDidBeginResize(_ cropViewController: Mantis.CropViewController) {}
  100. func cropViewControllerDidEndResize(_ cropViewController: Mantis.CropViewController, original: UIImage, cropInfo: Mantis.CropInfo) {}
  101. func cropViewControllerDidImageTransformed(_ cropViewController: Mantis.CropViewController) { }
  102. @objc func crop() {
  103. guard let image = UIImage(contentsOfFile: parent.url.path) else { return }
  104. let config = Mantis.Config()
  105. if let bundleIdentifier = Bundle.main.bundleIdentifier {
  106. config.localizationConfig.bundle = Bundle(identifier: bundleIdentifier)
  107. config.localizationConfig.tableName = "Localizable"
  108. }
  109. let cropViewController = Mantis.cropViewController(image: image, config: config)
  110. cropViewController.delegate = self
  111. cropViewController.modalPresentationStyle = .fullScreen
  112. // Suspend timer verify navigationItem
  113. parent.timer.suspend()
  114. viewController?.present(cropViewController, animated: true)
  115. }
  116. }
  117. }