ViewerQuickLook.swift 5.8 KB

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