ViewerQuickLook.swift 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. uploadAssets.startTimer(navigationItem: controller.navigationItem)
  30. let navigationController = UINavigationController(rootViewController: controller)
  31. return navigationController
  32. }
  33. func updateUIViewController(_ uiViewController: UINavigationController, context: Context) { }
  34. func makeCoordinator() -> Coordinator {
  35. return Coordinator(parent: self)
  36. }
  37. class Coordinator: NSObject, QLPreviewControllerDataSource, QLPreviewControllerDelegate, CropViewControllerDelegate {
  38. weak var viewController: QLPreviewController?
  39. let parent: ViewerQuickLook
  40. var image: UIImage?
  41. var hasChange = false
  42. init(parent: ViewerQuickLook) {
  43. self.parent = parent
  44. }
  45. @objc func dismiss() {
  46. parent.uploadAssets.stopTimer()
  47. parent.isPresentedQuickLook = false
  48. if let image = image {
  49. parent.uploadAssets.previewStore[parent.index].image = image
  50. parent.uploadAssets.previewStore[parent.index].hasChanges = hasChange
  51. }
  52. }
  53. // MARK: -
  54. func numberOfPreviewItems(in controller: QLPreviewController) -> Int {
  55. return 1
  56. }
  57. func previewController(_ controller: QLPreviewController, editingModeFor previewItem: QLPreviewItem) -> QLPreviewItemEditingMode {
  58. return .createCopy
  59. }
  60. func previewController(_ controller: QLPreviewController, didSaveEditedCopyOf previewItem: QLPreviewItem, at modifiedContentsURL: URL) {
  61. guard NCUtilityFileSystem.shared.moveFile(atPath: modifiedContentsURL.path, toPath: parent.url.path) else { return }
  62. if let image = UIImage(contentsOfFile: parent.url.path) {
  63. self.image = image
  64. self.hasChange = true
  65. }
  66. }
  67. func previewController(_ controller: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem {
  68. return parent.url as NSURL
  69. }
  70. // MARK: -
  71. func cropViewControllerDidCrop(_ cropViewController: Mantis.CropViewController, cropped: UIImage, transformation: Mantis.Transformation, cropInfo: Mantis.CropInfo) {
  72. cropViewController.dismiss(animated: true)
  73. guard let data = cropped.jpegData(compressionQuality: 1) else { return }
  74. do {
  75. try data.write(to: parent.url)
  76. self.image = cropped
  77. self.hasChange = true
  78. viewController?.reloadData()
  79. } catch { }
  80. }
  81. func cropViewControllerDidCancel(_ cropViewController: Mantis.CropViewController, original: UIImage) {
  82. cropViewController.dismiss(animated: true)
  83. }
  84. func cropViewControllerDidFailToCrop(_ cropViewController: Mantis.CropViewController, original: UIImage) {}
  85. func cropViewControllerDidBeginResize(_ cropViewController: Mantis.CropViewController) {}
  86. func cropViewControllerDidEndResize(_ cropViewController: Mantis.CropViewController, original: UIImage, cropInfo: Mantis.CropInfo) {}
  87. func cropViewControllerDidImageTransformed(_ cropViewController: Mantis.CropViewController) { }
  88. @objc func crop() {
  89. guard let image = UIImage(contentsOfFile: parent.url.path) else { return }
  90. let config = Mantis.Config()
  91. if let bundleIdentifier = Bundle.main.bundleIdentifier {
  92. config.localizationConfig.bundle = Bundle(identifier: bundleIdentifier)
  93. config.localizationConfig.tableName = "Localizable"
  94. }
  95. let cropViewController = Mantis.cropViewController(image: image, config: config)
  96. cropViewController.delegate = self
  97. cropViewController.modalPresentationStyle = .fullScreen
  98. viewController?.present(cropViewController, animated: true)
  99. }
  100. }
  101. }