ViewerQuickLook.swift 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. import NextcloudKit
  12. struct ViewerQuickLook: UIViewControllerRepresentable {
  13. let url: URL
  14. @Binding var index: Int
  15. @Binding var isPresentedQuickLook: Bool
  16. @ObservedObject var uploadAssets: NCUploadAssets
  17. func makeUIViewController(context: Context) -> UINavigationController {
  18. let controller = QLPreviewController()
  19. controller.dataSource = context.coordinator
  20. controller.delegate = context.coordinator
  21. context.coordinator.viewController = controller
  22. controller.navigationItem.rightBarButtonItem = UIBarButtonItem(
  23. barButtonSystemItem: .done, target: context.coordinator,
  24. action: #selector(context.coordinator.dismiss)
  25. )
  26. controller.navigationItem.leftBarButtonItem = UIBarButtonItem(
  27. title: NSLocalizedString("_crop_", comment: ""), style: UIBarButtonItem.Style.plain, target: context.coordinator,
  28. action: #selector(context.coordinator.crop)
  29. )
  30. uploadAssets.startTimer(navigationItem: controller.navigationItem)
  31. DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
  32. if uploadAssets.previewStore[index].asset.type == .livePhoto {
  33. let error = NKError(errorCode: NCGlobal.shared.errorCharactersForbidden, errorDescription: "_message_disable_livephoto_")
  34. NCContentPresenter.shared.showInfo(error: error)
  35. }
  36. }
  37. let navigationController = UINavigationController(rootViewController: controller)
  38. return navigationController
  39. }
  40. func updateUIViewController(_ uiViewController: UINavigationController, context: Context) { }
  41. func makeCoordinator() -> Coordinator {
  42. return Coordinator(parent: self)
  43. }
  44. class Coordinator: NSObject, QLPreviewControllerDataSource, QLPreviewControllerDelegate, CropViewControllerDelegate {
  45. weak var viewController: QLPreviewController?
  46. let parent: ViewerQuickLook
  47. var image: UIImage?
  48. var hasChange = false
  49. init(parent: ViewerQuickLook) {
  50. self.parent = parent
  51. }
  52. @objc func dismiss() {
  53. parent.uploadAssets.stopTimer()
  54. parent.isPresentedQuickLook = false
  55. if let image = image {
  56. parent.uploadAssets.previewStore[parent.index].image = image
  57. parent.uploadAssets.previewStore[parent.index].hasChanges = hasChange
  58. }
  59. }
  60. // MARK: -
  61. func numberOfPreviewItems(in controller: QLPreviewController) -> Int {
  62. return 1
  63. }
  64. func previewController(_ controller: QLPreviewController, editingModeFor previewItem: QLPreviewItem) -> QLPreviewItemEditingMode {
  65. return .createCopy
  66. }
  67. func previewController(_ controller: QLPreviewController, didSaveEditedCopyOf previewItem: QLPreviewItem, at modifiedContentsURL: URL) {
  68. guard NCUtilityFileSystem.shared.moveFile(atPath: modifiedContentsURL.path, toPath: parent.url.path) else { return }
  69. if let image = UIImage(contentsOfFile: parent.url.path) {
  70. self.image = image
  71. self.hasChange = true
  72. }
  73. }
  74. func previewController(_ controller: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem {
  75. return parent.url as NSURL
  76. }
  77. // MARK: -
  78. func cropViewControllerDidCrop(_ cropViewController: Mantis.CropViewController, cropped: UIImage, transformation: Mantis.Transformation, cropInfo: Mantis.CropInfo) {
  79. cropViewController.dismiss(animated: true)
  80. guard let data = cropped.jpegData(compressionQuality: 1) else { return }
  81. do {
  82. try data.write(to: parent.url)
  83. self.image = cropped
  84. self.hasChange = true
  85. viewController?.reloadData()
  86. } catch { }
  87. }
  88. func cropViewControllerDidCancel(_ cropViewController: Mantis.CropViewController, original: UIImage) {
  89. cropViewController.dismiss(animated: true)
  90. }
  91. func cropViewControllerDidFailToCrop(_ cropViewController: Mantis.CropViewController, original: UIImage) {}
  92. func cropViewControllerDidBeginResize(_ cropViewController: Mantis.CropViewController) {}
  93. func cropViewControllerDidEndResize(_ cropViewController: Mantis.CropViewController, original: UIImage, cropInfo: Mantis.CropInfo) {}
  94. func cropViewControllerDidImageTransformed(_ cropViewController: Mantis.CropViewController) { }
  95. @objc func crop() {
  96. guard let image = UIImage(contentsOfFile: parent.url.path) else { return }
  97. var toolbarConfig = CropToolbarConfig()
  98. toolbarConfig.heightForVerticalOrientation = 90
  99. toolbarConfig.widthForHorizontalOrientation = 130
  100. toolbarConfig.optionButtonFontSize = 16
  101. toolbarConfig.optionButtonFontSizeForPad = 21
  102. toolbarConfig.backgroundColor = .systemGray6
  103. toolbarConfig.foregroundColor = .systemBlue
  104. var viewConfig = CropViewConfig()
  105. viewConfig.cropMaskVisualEffectType = .none
  106. viewConfig.cropBorderColor = .red
  107. var config = Mantis.Config()
  108. if let bundleIdentifier = Bundle.main.bundleIdentifier {
  109. config.localizationConfig.bundle = Bundle(identifier: bundleIdentifier)
  110. config.localizationConfig.tableName = "Localizable"
  111. }
  112. config.cropToolbarConfig = toolbarConfig
  113. config.cropViewConfig = viewConfig
  114. let cropViewController = Mantis.cropViewController(image: image, config: config)
  115. cropViewController.delegate = self
  116. cropViewController.backgroundColor = .systemBackground
  117. cropViewController.modalPresentationStyle = .fullScreen
  118. viewController?.present(cropViewController, animated: true)
  119. }
  120. }
  121. }