Browse Source

add ViewerQuickLook

Signed-off-by: Marino Faggiana <marino@marinofaggiana.com>
Marino Faggiana 2 years ago
parent
commit
62a9d7dc24

+ 4 - 0
Nextcloud.xcodeproj/project.pbxproj

@@ -413,6 +413,7 @@
 		F7A0D1362591FBC5008F8A13 /* String+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = F7A0D1342591FBC5008F8A13 /* String+Extension.swift */; };
 		F7A0D1362591FBC5008F8A13 /* String+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = F7A0D1342591FBC5008F8A13 /* String+Extension.swift */; };
 		F7A0D1372591FBC5008F8A13 /* String+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = F7A0D1342591FBC5008F8A13 /* String+Extension.swift */; };
 		F7A0D1372591FBC5008F8A13 /* String+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = F7A0D1342591FBC5008F8A13 /* String+Extension.swift */; };
 		F7A321AD1E9E6AD50069AD1B /* CCAdvanced.m in Sources */ = {isa = PBXBuildFile; fileRef = F7A321AC1E9E6AD50069AD1B /* CCAdvanced.m */; };
 		F7A321AD1E9E6AD50069AD1B /* CCAdvanced.m in Sources */ = {isa = PBXBuildFile; fileRef = F7A321AC1E9E6AD50069AD1B /* CCAdvanced.m */; };
+		F7A48413297022E000BD1B49 /* ViewerQuickLook.swift in Sources */ = {isa = PBXBuildFile; fileRef = F7A48412297022E000BD1B49 /* ViewerQuickLook.swift */; };
 		F7A60F86292D215000FCE1F2 /* NCTalkAccounts.swift in Sources */ = {isa = PBXBuildFile; fileRef = F7A60F84292D215000FCE1F2 /* NCTalkAccounts.swift */; };
 		F7A60F86292D215000FCE1F2 /* NCTalkAccounts.swift in Sources */ = {isa = PBXBuildFile; fileRef = F7A60F84292D215000FCE1F2 /* NCTalkAccounts.swift */; };
 		F7A60F87292D215000FCE1F2 /* NCTalkAccounts.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = F7A60F85292D215000FCE1F2 /* NCTalkAccounts.storyboard */; };
 		F7A60F87292D215000FCE1F2 /* NCTalkAccounts.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = F7A60F85292D215000FCE1F2 /* NCTalkAccounts.storyboard */; };
 		F7A76DC8256A71CD00119AB3 /* UIImage+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = F7B7504A2397D38E004E13EC /* UIImage+Extension.swift */; };
 		F7A76DC8256A71CD00119AB3 /* UIImage+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = F7B7504A2397D38E004E13EC /* UIImage+Extension.swift */; };
@@ -986,6 +987,7 @@
 		F7A0D1342591FBC5008F8A13 /* String+Extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "String+Extension.swift"; sourceTree = "<group>"; };
 		F7A0D1342591FBC5008F8A13 /* String+Extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "String+Extension.swift"; sourceTree = "<group>"; };
 		F7A321AB1E9E6AD50069AD1B /* CCAdvanced.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CCAdvanced.h; sourceTree = "<group>"; };
 		F7A321AB1E9E6AD50069AD1B /* CCAdvanced.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CCAdvanced.h; sourceTree = "<group>"; };
 		F7A321AC1E9E6AD50069AD1B /* CCAdvanced.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CCAdvanced.m; sourceTree = "<group>"; };
 		F7A321AC1E9E6AD50069AD1B /* CCAdvanced.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CCAdvanced.m; sourceTree = "<group>"; };
+		F7A48412297022E000BD1B49 /* ViewerQuickLook.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewerQuickLook.swift; sourceTree = "<group>"; };
 		F7A60F84292D215000FCE1F2 /* NCTalkAccounts.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NCTalkAccounts.swift; sourceTree = "<group>"; };
 		F7A60F84292D215000FCE1F2 /* NCTalkAccounts.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NCTalkAccounts.swift; sourceTree = "<group>"; };
 		F7A60F85292D215000FCE1F2 /* NCTalkAccounts.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; path = NCTalkAccounts.storyboard; sourceTree = "<group>"; };
 		F7A60F85292D215000FCE1F2 /* NCTalkAccounts.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; path = NCTalkAccounts.storyboard; sourceTree = "<group>"; };
 		F7A7FA6229265CF4000603EF /* NCManageE2EE.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NCManageE2EE.swift; sourceTree = "<group>"; };
 		F7A7FA6229265CF4000603EF /* NCManageE2EE.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NCManageE2EE.swift; sourceTree = "<group>"; };
@@ -1471,6 +1473,7 @@
 			isa = PBXGroup;
 			isa = PBXGroup;
 			children = (
 			children = (
 				F7C7B488245EBA4100D93E60 /* NCViewerQuickLook.swift */,
 				F7C7B488245EBA4100D93E60 /* NCViewerQuickLook.swift */,
+				F7A48412297022E000BD1B49 /* ViewerQuickLook.swift */,
 			);
 			);
 			path = NCViewerQuickLook;
 			path = NCViewerQuickLook;
 			sourceTree = "<group>";
 			sourceTree = "<group>";
@@ -3206,6 +3209,7 @@
 				F7D68FCC28CB9051009139F3 /* NCManageDatabase+DashboardWidget.swift in Sources */,
 				F7D68FCC28CB9051009139F3 /* NCManageDatabase+DashboardWidget.swift in Sources */,
 				F70CEF5623E9C7E50007035B /* UIColor+Extension.swift in Sources */,
 				F70CEF5623E9C7E50007035B /* UIColor+Extension.swift in Sources */,
 				F75CA1472962F13700B01130 /* HUDView.swift in Sources */,
 				F75CA1472962F13700B01130 /* HUDView.swift in Sources */,
+				F7A48413297022E000BD1B49 /* ViewerQuickLook.swift in Sources */,
 				F77BB748289985270090FC19 /* UITabBarController+Extension.swift in Sources */,
 				F77BB748289985270090FC19 /* UITabBarController+Extension.swift in Sources */,
 				F75AC2431F1F62450073EC19 /* NCManageAutoUploadFileName.swift in Sources */,
 				F75AC2431F1F62450073EC19 /* NCManageAutoUploadFileName.swift in Sources */,
 				F7A7FA6329265CF4000603EF /* NCManageE2EE.swift in Sources */,
 				F7A7FA6329265CF4000603EF /* NCManageE2EE.swift in Sources */,

+ 11 - 66
iOSClient/Main/Create cloud/NCUploadAssets.swift

@@ -99,6 +99,8 @@ struct UploadAssetsView: View {
     @State private var imageForCrop: UIImage = UIImage()
     @State private var imageForCrop: UIImage = UIImage()
     @State private var cropShapeType: Mantis.CropShapeType = .rect
     @State private var cropShapeType: Mantis.CropShapeType = .rect
     @State private var presetFixedRatioType: Mantis.PresetFixedRatioType = .canUseMultiplePresetFixedRatio()
     @State private var presetFixedRatioType: Mantis.PresetFixedRatioType = .canUseMultiplePresetFixedRatio()
+    @State private var fileNamePath = NSTemporaryDirectory() + "QLTmp.jpg"
+    @State private var metadata: tableMetadata?
 
 
     var gridItems: [GridItem] = [GridItem()]
     var gridItems: [GridItem] = [GridItem()]
 
 
@@ -241,6 +243,13 @@ struct UploadAssetsView: View {
         }
         }
     }
     }
 
 
+    func copyImageforQL() {
+        let image = uploadAssets.previewStore[index].originalImage
+        if let data = image.jpegData(compressionQuality: 1) {
+            try? data.write(to: URL(fileURLWithPath: fileNamePath))
+        }
+    }
+
     var body: some View {
     var body: some View {
         NavigationView {
         NavigationView {
             List {
             List {
@@ -259,8 +268,9 @@ struct UploadAssetsView: View {
                                             .onTapGesture {
                                             .onTapGesture {
                                                 self.index = index
                                                 self.index = index
                                                 isPresentedCrop = true
                                                 isPresentedCrop = true
+                                                copyImageforQL()
                                             }.fullScreenCover(isPresented: $isPresentedCrop) {
                                             }.fullScreenCover(isPresented: $isPresentedCrop) {
-                                                ImageCropper(previewStore: $uploadAssets.previewStore, index: $index, cropShapeType: $cropShapeType, presetFixedRatioType: $presetFixedRatioType)
+                                                ViewerQuickLook(url: URL(fileURLWithPath: fileNamePath), isPresented: $isPresentedCrop)
                                                     .ignoresSafeArea()
                                                     .ignoresSafeArea()
                                             }
                                             }
                                     }
                                     }
@@ -360,71 +370,6 @@ struct UploadAssetsView: View {
     }
     }
 }
 }
 
 
-// MARK: - ImageCropper
-
-struct ImageCropper: UIViewControllerRepresentable {
-
-    @Binding var previewStore: [PreviewStore]
-    @Binding var index: Int
-    @Binding var cropShapeType: Mantis.CropShapeType
-    @Binding var presetFixedRatioType: Mantis.PresetFixedRatioType
-
-    @Environment(\.presentationMode) var presentationMode
-
-    class Coordinator: CropViewControllerDelegate {
-
-        var parent: ImageCropper
-        var isModified: Bool = false
-
-        init(_ parent: ImageCropper) {
-            self.parent = parent
-        }
-
-        func cropViewControllerDidCrop(_ cropViewController: CropViewController, cropped: UIImage, transformation: Transformation, cropInfo: CropInfo) {
-            if isModified {
-                parent.previewStore[parent.index].cropImage = cropped
-            } else {
-                parent.previewStore[parent.index].cropImage = nil
-            }
-            parent.presentationMode.wrappedValue.dismiss()
-        }
-
-        func cropViewControllerDidCancel(_ cropViewController: CropViewController, original: UIImage) {
-            parent.presentationMode.wrappedValue.dismiss()
-        }
-
-        func cropViewControllerDidImageTransformed(_ cropViewController: Mantis.CropViewController) {
-            isModified = true
-        }
-
-        func cropViewControllerDidFailToCrop(_ cropViewController: CropViewController, original: UIImage) { }
-
-        func cropViewControllerDidBeginResize(_ cropViewController: CropViewController) { }
-
-        func cropViewControllerDidEndResize(_ cropViewController: CropViewController, original: UIImage, cropInfo: CropInfo) { }
-    }
-
-    func makeCoordinator() -> Coordinator {
-        Coordinator(self)
-    }
-
-    func makeUIViewController(context: Context) -> CropViewController {
-        var config = Mantis.Config()
-        if let bundleIdentifier = Bundle.main.bundleIdentifier {
-            config.localizationConfig.bundle = Bundle(identifier: bundleIdentifier)
-            config.localizationConfig.tableName = "Localizable"
-        }
-        config.cropViewConfig.cropShapeType = cropShapeType
-        config.presetFixedRatioType = presetFixedRatioType
-        let image = previewStore[index].originalImage
-        let cropViewController = Mantis.cropViewController(image: image, config: config)
-        cropViewController.delegate = context.coordinator
-        return cropViewController
-    }
-
-    func updateUIViewController(_ uiViewController: CropViewController, context: Context) { }
-}
-
 // MARK: - Preview
 // MARK: - Preview
 
 
 struct UploadAssetsView_Previews: PreviewProvider {
 struct UploadAssetsView_Previews: PreviewProvider {

+ 0 - 43
iOSClient/Viewer/NCViewerQuickLook/NCViewerQuickLook.swift

@@ -224,46 +224,3 @@ extension NCViewerQuickLook: CropViewControllerDelegate {
 class PreviewItem: NSObject, QLPreviewItem {
 class PreviewItem: NSObject, QLPreviewItem {
     var previewItemURL: URL?
     var previewItemURL: URL?
 }
 }
-
-// MARK: - UIViewControllerRepresentable
-
-struct ViewerQuickLook: UIViewControllerRepresentable {
-
-    typealias UIViewControllerType = NCViewerQuickLook
-
-    // let fileNamePath = NSTemporaryDirectory() + metadata.fileNameView
-    // CCUtility.copyFile(atPath: CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView), toPath: fileNamePath)
-
-    @Binding var fileNamePath: String
-    @Binding var metadata: tableMetadata?
-
-    @Environment(\.presentationMode) var presentationMode
-
-    class Coordinator: NCViewerQuickLookDelegate {
-
-        var parent: ViewerQuickLook
-        var isModified: Bool = false
-
-        init(_ parent: ViewerQuickLook) {
-            self.parent = parent
-        }
-
-        // DELEGATE
-
-        func dismiss(url: URL, hasChanges: Bool) {
-
-        }
-    }
-
-    func makeCoordinator() -> Coordinator {
-        Coordinator(self)
-    }
-
-    func makeUIViewController(context: Context) -> UIViewControllerType {
-        let viewerQuickLook = NCViewerQuickLook(with: URL(fileURLWithPath: fileNamePath), isEditingEnabled: true, metadata: metadata)
-        viewerQuickLook.delegateViewer = context.coordinator
-        return viewerQuickLook
-    }
-
-    func updateUIViewController(_ uiViewController: UIViewControllerType, context: Context) { }
-}

+ 113 - 0
iOSClient/Viewer/NCViewerQuickLook/ViewerQuickLook.swift

@@ -0,0 +1,113 @@
+//
+//  ViewerQuickLook.swift
+//  Nextcloud
+//
+//  Created by Marino Faggiana on 12/01/23.
+//  Copyright © 2023 Marino Faggiana. All rights reserved.
+//
+
+import SwiftUI
+import QuickLook
+import Mantis
+
+struct ViewerQuickLook: UIViewControllerRepresentable {
+
+    let url: URL
+    @Binding var isPresented: Bool
+
+    func makeUIViewController(context: Context) -> UINavigationController {
+        let controller = QLPreviewController()
+
+        controller.dataSource = context.coordinator
+        controller.delegate = context.coordinator
+        context.coordinator.viewController = controller
+
+        controller.navigationItem.leftBarButtonItem = UIBarButtonItem(
+            barButtonSystemItem: .done, target: context.coordinator,
+            action: #selector(context.coordinator.dismiss)
+        )
+
+        controller.navigationItem.rightBarButtonItem = UIBarButtonItem(
+            title: NSLocalizedString("_crop_", comment: ""), style: UIBarButtonItem.Style.plain, target: context.coordinator,
+            action: #selector(context.coordinator.crop)
+        )
+
+        let navigationController = UINavigationController(rootViewController: controller)
+        return navigationController
+    }
+
+    func updateUIViewController(_ uiViewController: UINavigationController, context: Context) { }
+
+    func makeCoordinator() -> Coordinator {
+        return Coordinator(parent: self)
+    }
+
+    class Coordinator: NSObject, QLPreviewControllerDataSource, QLPreviewControllerDelegate, CropViewControllerDelegate {
+
+        weak var viewController: QLPreviewController?
+        let parent: ViewerQuickLook
+
+        init(parent: ViewerQuickLook) {
+            self.parent = parent
+        }
+
+        @objc func dismiss() {
+            parent.isPresented = false
+        }
+
+        // MARK: -
+
+        func numberOfPreviewItems(in controller: QLPreviewController) -> Int {
+            return 1
+        }
+
+        func previewController(_ controller: QLPreviewController, editingModeFor previewItem: QLPreviewItem) -> QLPreviewItemEditingMode {
+            return .createCopy
+        }
+
+        func previewController(_ controller: QLPreviewController, didSaveEditedCopyOf previewItem: QLPreviewItem, at modifiedContentsURL: URL) {
+            guard NCUtilityFileSystem.shared.moveFile(atPath: modifiedContentsURL.path, toPath: parent.url.path) else { return }
+        }
+
+        func previewController(_ controller: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem {
+            return parent.url as NSURL
+        }
+
+        // MARK: -
+
+        func cropViewControllerDidCrop(_ cropViewController: Mantis.CropViewController, cropped: UIImage, transformation: Mantis.Transformation, cropInfo: Mantis.CropInfo) {
+            cropViewController.dismiss(animated: true)
+            guard let data = cropped.jpegData(compressionQuality: 1) else { return }
+            do {
+                try data.write(to: parent.url)
+                viewController?.reloadData()
+            } catch {  }
+        }
+        func cropViewControllerDidCancel(_ cropViewController: Mantis.CropViewController, original: UIImage) {
+            cropViewController.dismiss(animated: true)
+        }
+
+        func cropViewControllerDidFailToCrop(_ cropViewController: Mantis.CropViewController, original: UIImage) {}
+        func cropViewControllerDidBeginResize(_ cropViewController: Mantis.CropViewController) {}
+        func cropViewControllerDidEndResize(_ cropViewController: Mantis.CropViewController, original: UIImage, cropInfo: Mantis.CropInfo) {}
+        func cropViewControllerDidImageTransformed(_ cropViewController: Mantis.CropViewController) { }
+
+        @objc func crop() {
+
+            guard let image = UIImage(contentsOfFile: parent.url.path) else { return }
+            let 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)
+
+            cropViewController.delegate = self
+            cropViewController.modalPresentationStyle = .fullScreen
+
+            viewController?.present(cropViewController, animated: true)
+        }
+
+    }
+}