ViewerQuickLook.swift 6.5 KB

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