ViewerQuickLook.swift 5.7 KB

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