ViewerQuickLook.swift 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. @Binding var isPresentedQuickLook: Bool
  14. @Binding var previewStore: PreviewStore
  15. @Binding var timer: DispatchSourceTimer
  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.3)
  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. parent.timer.suspend()
  65. parent.isPresentedQuickLook = false
  66. if let image = image {
  67. parent.previewStore.image = image
  68. }
  69. parent.previewStore.hasChanges = hasChange
  70. }
  71. // MARK: -
  72. func numberOfPreviewItems(in controller: QLPreviewController) -> Int {
  73. return 1
  74. }
  75. func previewController(_ controller: QLPreviewController, editingModeFor previewItem: QLPreviewItem) -> QLPreviewItemEditingMode {
  76. return .createCopy
  77. }
  78. func previewController(_ controller: QLPreviewController, didSaveEditedCopyOf previewItem: QLPreviewItem, at modifiedContentsURL: URL) {
  79. guard NCUtilityFileSystem.shared.moveFile(atPath: modifiedContentsURL.path, toPath: parent.url.path) else { return }
  80. if let image = UIImage(contentsOfFile: parent.url.path) {
  81. self.image = image
  82. self.hasChange = true
  83. }
  84. }
  85. func previewController(_ controller: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem {
  86. return parent.url as NSURL
  87. }
  88. // MARK: -
  89. func cropViewControllerDidCrop(_ cropViewController: Mantis.CropViewController, cropped: UIImage, transformation: Mantis.Transformation, cropInfo: Mantis.CropInfo) {
  90. cropViewController.dismiss(animated: true)
  91. guard let data = cropped.jpegData(compressionQuality: 1) else { return }
  92. do {
  93. try data.write(to: parent.url)
  94. self.image = cropped
  95. self.hasChange = true
  96. viewController?.reloadData()
  97. } catch { }
  98. }
  99. func cropViewControllerDidCancel(_ cropViewController: Mantis.CropViewController, original: UIImage) {
  100. cropViewController.dismiss(animated: true)
  101. }
  102. func cropViewControllerDidFailToCrop(_ cropViewController: Mantis.CropViewController, original: UIImage) {}
  103. func cropViewControllerDidBeginResize(_ cropViewController: Mantis.CropViewController) {}
  104. func cropViewControllerDidEndResize(_ cropViewController: Mantis.CropViewController, original: UIImage, cropInfo: Mantis.CropInfo) {}
  105. func cropViewControllerDidImageTransformed(_ cropViewController: Mantis.CropViewController) { }
  106. @objc func crop() {
  107. guard let image = UIImage(contentsOfFile: parent.url.path) else { return }
  108. let config = Mantis.Config()
  109. if let bundleIdentifier = Bundle.main.bundleIdentifier {
  110. config.localizationConfig.bundle = Bundle(identifier: bundleIdentifier)
  111. config.localizationConfig.tableName = "Localizable"
  112. }
  113. let cropViewController = Mantis.cropViewController(image: image, config: config)
  114. cropViewController.delegate = self
  115. cropViewController.modalPresentationStyle = .fullScreen
  116. viewController?.present(cropViewController, animated: true)
  117. }
  118. }
  119. }