فهرست منبع

normalized ViewerQuickLook

Signed-off-by: Marino Faggiana <marino@marinofaggiana.com>
Marino Faggiana 2 سال پیش
والد
کامیت
ffed4f9e88
2فایلهای تغییر یافته به همراه76 افزوده شده و 39 حذف شده
  1. 3 1
      iOSClient/Main/NCFunctionCenter.swift
  2. 73 38
      iOSClient/Viewer/NCViewerQuickLook/NCViewerQuickLook.swift

+ 3 - 1
iOSClient/Main/NCFunctionCenter.swift

@@ -73,7 +73,9 @@ import Photos
             let fileNamePath = NSTemporaryDirectory() + metadata.fileNameView
             CCUtility.copyFile(atPath: CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView), toPath: fileNamePath)
             let viewerQuickLook = NCViewerQuickLook(with: URL(fileURLWithPath: fileNamePath), isEditingEnabled: true, metadata: metadata)
-            self.appDelegate.window?.rootViewController?.present(viewerQuickLook, animated: true)
+            let navigationController = UINavigationController(rootViewController: viewerQuickLook)
+            navigationController.modalPresentationStyle = .fullScreen
+            self.appDelegate.window?.rootViewController?.present(navigationController, animated: true)
 
         case NCGlobal.shared.selectorLoadFileView:
             guard UIApplication.shared.applicationState == .active else { break }

+ 73 - 38
iOSClient/Viewer/NCViewerQuickLook/NCViewerQuickLook.swift

@@ -30,9 +30,8 @@ import NextcloudKit
 import Mantis
 import SwiftUI
 
-protocol NCViewerQuickLookDelegate: AnyObject {
-    func dismiss(url: URL, hasChanges: Bool)
-}
+// if the document has any changes
+private var hasChangesQuickLook: Bool = false
 
 @objc class NCViewerQuickLook: QLPreviewController {
 
@@ -40,16 +39,7 @@ protocol NCViewerQuickLookDelegate: AnyObject {
     var previewItems: [PreviewItem] = []
     var isEditingEnabled: Bool
     var metadata: tableMetadata?
-    var delegateViewer: NCViewerQuickLookDelegate?
-
-    // if the document has any changes (annotations)
-    var hasChanges = false
-
-    // used to display the save alert
-    var parentVC: UIViewController?
-
-    // if the Crop is presented
-    var isPresentCrop = false
+    var timer: Timer?
 
     required init?(coder: NSCoder) {
         fatalError("init(coder:) has not been implemented")
@@ -71,32 +61,53 @@ protocol NCViewerQuickLookDelegate: AnyObject {
         self.dataSource = self
         self.delegate = self
         self.currentPreviewItemIndex = 0
+        hasChangesQuickLook = false
     }
 
     override func viewDidLoad() {
         super.viewDidLoad()
         guard isEditingEnabled else { return }
 
+        let buttonDone = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(dismission))
+        let buttonCrop = UIBarButtonItem(image: UIImage(systemName: "crop"), style: .plain, target: self, action: #selector(crop))
+        navigationItem.leftBarButtonItems = [buttonDone, buttonCrop]
+
         if metadata?.livePhoto == true {
             let error = NKError(errorCode: NCGlobal.shared.errorCharactersForbidden, errorDescription: "_message_disable_overwrite_livephoto_")
             NCContentPresenter.shared.showInfo(error: error)
         }
+        
+        startTimer(navigationItem: navigationItem)
+    }
 
-        navigationItem.leftBarButtonItem = UIBarButtonItem(title: NSLocalizedString("_crop_", comment: ""), style: UIBarButtonItem.Style.plain, target: self, action: #selector(crop))
+    func startTimer(navigationItem: UINavigationItem) {
+        self.timer = Timer.scheduledTimer(withTimeInterval: 0.5, repeats: true, block: { _ in
+            guard let buttonDone = navigationItem.leftBarButtonItems?.first, let buttonCrop = navigationItem.leftBarButtonItems?.last else { return }
+            buttonCrop.isEnabled = true
+            buttonDone.isEnabled = true
+            if let markup = navigationItem.rightBarButtonItems?.first(where: { $0.accessibilityIdentifier == "QLOverlayMarkupButtonAccessibilityIdentifier" }) {
+                if let originalButton = markup.value(forKey: "originalButton") as AnyObject? {
+                    if let symbolImageName = originalButton.value(forKey: "symbolImageName") as? String {
+                        if symbolImageName == "pencil.tip.crop.circle.on" {
+                            buttonCrop.isEnabled = false
+                            buttonDone.isEnabled = false
+                        }
+                    }
+                }
+            }
+        })
     }
 
-    override func viewDidAppear(_ animated: Bool) {
-        super.viewDidAppear(animated)
-        // needs to be saved bc in didDisappear presentingVC is already nil
-        self.parentVC = presentingViewController
+    func stopTimer() {
+        self.timer?.invalidate()
     }
 
-    override func viewDidDisappear(_ animated: Bool) {
-        super.viewDidDisappear(animated)
+    @objc func dismission() {
 
-        guard !isPresentCrop else { return }
-        delegateViewer?.dismiss(url: url, hasChanges: hasChanges)
-        guard isEditingEnabled, hasChanges, let metadata = metadata else { return }
+        guard isEditingEnabled, hasChangesQuickLook, let metadata = metadata else {
+            dismiss(animated: true)
+            return
+        }
 
         let alertController = UIAlertController(title: NSLocalizedString("_save_", comment: ""), message: nil, preferredStyle: .alert)
         var message: String?
@@ -115,24 +126,42 @@ protocol NCViewerQuickLookDelegate: AnyObject {
             self.saveModifiedFile(override: false)
         })
         alertController.addAction(UIAlertAction(title: NSLocalizedString("_discard_changes_", comment: ""), style: .destructive) { _ in })
-        parentVC?.present(alertController, animated: true)
+        present(alertController, animated: true)
     }
 
     @objc func crop() {
 
         guard let image = UIImage(contentsOfFile: url.path) else { return }
-        let config = Mantis.Config()
 
+        var toolbarConfig = CropToolbarConfig()
+        toolbarConfig.heightForVerticalOrientation = 80
+        toolbarConfig.widthForHorizontalOrientation = 100
+        toolbarConfig.optionButtonFontSize = 16
+        toolbarConfig.optionButtonFontSizeForPad = 21
+        toolbarConfig.backgroundColor = .systemGray6
+        toolbarConfig.foregroundColor = .systemBlue
+
+        var viewConfig = CropViewConfig()
+        viewConfig.cropMaskVisualEffectType = .none
+        viewConfig.cropBorderColor = .red
+
+        var config = Mantis.Config()
         if let bundleIdentifier = Bundle.main.bundleIdentifier {
             config.localizationConfig.bundle = Bundle(identifier: bundleIdentifier)
             config.localizationConfig.tableName = "Localizable"
         }
-        let cropViewController = Mantis.cropViewController(image: image, config: config)
+        config.cropToolbarConfig = toolbarConfig
+        config.cropViewConfig = viewConfig
+
+        let toolbar = CropToolbar()
+        toolbar.iconProvider = CropToolbarIcon()
+
+        let cropViewController = Mantis.cropViewController(image: image, config: config, cropToolbar: toolbar)
 
         cropViewController.delegate = self
+        cropViewController.backgroundColor = .systemBackground
         cropViewController.modalPresentationStyle = .fullScreen
 
-        self.isPresentCrop = true
         self.present(cropViewController, animated: true)
     }
 }
@@ -187,40 +216,46 @@ extension NCViewerQuickLook: QLPreviewControllerDataSource, QLPreviewControllerD
         metadataForUpload.size = size
         metadataForUpload.status = NCGlobal.shared.metadataStatusWaitUpload
 
-        NCNetworkingProcessUpload.shared.createProcessUploads(metadatas: [metadataForUpload]) { _ in }
+        NCNetworkingProcessUpload.shared.createProcessUploads(metadatas: [metadataForUpload]) { _ in
+            self.dismiss(animated: true)
+        }
     }
 
     func previewController(_ controller: QLPreviewController, didSaveEditedCopyOf previewItem: QLPreviewItem, at modifiedContentsURL: URL) {
         // easier to handle that way than to use `.updateContents`
         // needs to be moved otherwise it will only be called once!
         guard NCUtilityFileSystem.shared.moveFile(atPath: modifiedContentsURL.path, toPath: url.path) else { return }
-        hasChanges = true
+        hasChangesQuickLook = true
     }
 }
 
 extension NCViewerQuickLook: CropViewControllerDelegate {
 
     func cropViewControllerDidCrop(_ cropViewController: Mantis.CropViewController, cropped: UIImage, transformation: Mantis.Transformation, cropInfo: Mantis.CropInfo) {
-        cropViewController.dismiss(animated: true) {
-            self.isPresentCrop = false
-        }
-
+        cropViewController.dismiss(animated: true)
         guard let data = cropped.jpegData(compressionQuality: 1) else { return }
-
         do {
             try data.write(to: self.url)
+            hasChangesQuickLook = true
             reloadData()
         } catch {  }
     }
 
     func cropViewControllerDidCancel(_ cropViewController: Mantis.CropViewController, original: UIImage) {
-
-        cropViewController.dismiss(animated: true) {
-            self.isPresentCrop = false
-        }
+        cropViewController.dismiss(animated: true)
     }
 }
 
 class PreviewItem: NSObject, QLPreviewItem {
     var previewItemURL: URL?
 }
+
+class CropToolbarIcon: CropToolbarIconProvider {
+    func getCropIcon() -> UIImage? {
+       return UIImage(systemName: "checkmark.circle")
+    }
+
+    func getCancelIcon() -> UIImage? {
+        return UIImage(systemName: "xmark.circle")
+    }
+}