Browse Source

New share extension

marinofaggiana 4 years ago
parent
commit
80c106e991

+ 6 - 0
Nextcloud.xcodeproj/project.pbxproj

@@ -254,6 +254,9 @@
 		F79918A221997FA300C2E308 /* UICKeyChainStore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = F79918A021997F9000C2E308 /* UICKeyChainStore.framework */; };
 		F79A65C32191D90F00FF6DCC /* NCSelect.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = F79A65C22191D90F00FF6DCC /* NCSelect.storyboard */; };
 		F79A65C62191D95E00FF6DCC /* NCSelect.swift in Sources */ = {isa = PBXBuildFile; fileRef = F79A65C52191D95E00FF6DCC /* NCSelect.swift */; };
+		F79EC77F26316193004E59D6 /* NCRenameFile.swift in Sources */ = {isa = PBXBuildFile; fileRef = F70D87CE25EE6E58008CBBBD /* NCRenameFile.swift */; };
+		F79EC784263161BA004E59D6 /* NCRenameFile.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = F70D87CD25EE6E58008CBBBD /* NCRenameFile.storyboard */; };
+		F79EC78926316AC4004E59D6 /* NCPopupViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = F702F30725EE5D47008F8E80 /* NCPopupViewController.swift */; };
 		F7A0D1352591FBC5008F8A13 /* String+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = F7A0D1342591FBC5008F8A13 /* String+Extensions.swift */; };
 		F7A0D1362591FBC5008F8A13 /* String+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = F7A0D1342591FBC5008F8A13 /* String+Extensions.swift */; };
 		F7A0D1372591FBC5008F8A13 /* String+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = F7A0D1342591FBC5008F8A13 /* String+Extensions.swift */; };
@@ -1811,6 +1814,7 @@
 				F7EDE51C262DD0C400414FE6 /* NCSelectCommandViewCopyMove.xib in Resources */,
 				F7148063262ED66200693E51 /* NCEmptyView.xib in Resources */,
 				F7145A231D12E3B700CAFEEC /* Localizable.strings in Resources */,
+				F79EC784263161BA004E59D6 /* NCRenameFile.storyboard in Resources */,
 				F714804F262ED4F900693E51 /* NCGridCell.xib in Resources */,
 				F714805E262ED52900693E51 /* NCSectionFooter.xib in Resources */,
 				F700222D1EC479840080073F /* Custom.xcassets in Resources */,
@@ -1961,6 +1965,7 @@
 			files = (
 				F73D5E48246DE09200DF6467 /* NCElementsJSON.swift in Sources */,
 				F7EDE4E5262D7BBE00414FE6 /* NCSectionHeaderFooter.swift in Sources */,
+				F79EC78926316AC4004E59D6 /* NCPopupViewController.swift in Sources */,
 				F7A0D1362591FBC5008F8A13 /* String+Extensions.swift in Sources */,
 				F7EDE4D6262D7B9600414FE6 /* NCListCell.swift in Sources */,
 				F70460532499095400BB98A7 /* NotificationCenter+MainThread.swift in Sources */,
@@ -1983,6 +1988,7 @@
 				F7BAADC91ED5A87C00B7EAD4 /* NCDatabase.swift in Sources */,
 				F7EDE4F4262D7C9A00414FE6 /* NCOperationQueue.swift in Sources */,
 				F780710A1EDAB65800EAFFF6 /* NSNotificationCenter+MainThread.m in Sources */,
+				F79EC77F26316193004E59D6 /* NCRenameFile.swift in Sources */,
 				F7EDE4EA262D7C6C00414FE6 /* NCSortMenu.swift in Sources */,
 				F7148041262EBE4000693E51 /* NCShareExtension.swift in Sources */,
 				F76B3CCF1EAE01BD00921AC9 /* NCBrand.swift in Sources */,

+ 27 - 1
Share/NCShareExtension.swift

@@ -24,7 +24,7 @@
 import Foundation
 import NCCommunication
 
-class NCShareExtension: UIViewController, NCListCellDelegate, NCEmptyDataSetDelegate {
+class NCShareExtension: UIViewController, NCListCellDelegate, NCEmptyDataSetDelegate, NCRenameFileDelegate {
     
     @IBOutlet weak var collectionView: UICollectionView!
     @IBOutlet weak var tableView: UITableView!
@@ -323,8 +323,28 @@ class NCShareExtension: UIViewController, NCListCellDelegate, NCEmptyDataSetDele
         setNavigationBar()
     }
     
+    func rename(fileName: String, fileNameNew: String) {
+        
+        if let row = self.filesName.firstIndex(where: {$0 == fileName}) {
+            filesName[row] = fileNameNew
+            tableView.reloadData()
+        }
+    }
+    
     @objc func renameButtonPressed(sender: NCShareExtensionButtonWithIndexPath) {
         
+        if let fileName = sender.fileName {
+            if let vcRename = UIStoryboard(name: "NCRenameFile", bundle: nil).instantiateInitialViewController() as? NCRenameFile {
+            
+                vcRename.delegate = self
+                vcRename.fileName = fileName
+                vcRename.imagePreview = sender.image
+
+                let popup = NCPopupViewController(contentController: vcRename, popupWidth: 300, popupHeight: 360)
+                                        
+                self.present(popup, animated: true)
+            }
+        }
     }
     
     @objc func deleteButtonPressed(sender: NCShareExtensionButtonWithIndexPath) {
@@ -566,10 +586,14 @@ extension NCShareExtension: UITableViewDataSource {
         
         renameButton?.setImage(NCUtility.shared.loadImage(named: "pencil").image(color: labelColor, size: 15), for: .normal)
         renameButton?.indexPath = indexPath
+        renameButton?.fileName = fileName
+        renameButton?.image = imageCell?.image
         renameButton?.addTarget(self, action:#selector(renameButtonPressed(sender:)), for: .touchUpInside)
 
         deleteButton?.setImage(NCUtility.shared.loadImage(named: "trash").image(color: .red, size: 15), for: .normal)
         deleteButton?.indexPath = indexPath
+        deleteButton?.fileName = fileName
+        deleteButton?.image = imageCell?.image
         deleteButton?.addTarget(self, action:#selector(deleteButtonPressed(sender:)), for: .touchUpInside)
         
         return cell
@@ -794,4 +818,6 @@ extension NCShareExtension {
 
 class NCShareExtensionButtonWithIndexPath: UIButton {
     var indexPath:IndexPath?
+    var fileName: String?
+    var image: UIImage?
 }

+ 82 - 35
iOSClient/Rename file/NCRenameFile.swift

@@ -24,6 +24,15 @@
 import Foundation
 import NCCommunication
 
+public protocol NCRenameFileDelegate {
+    func rename(fileName: String, fileNameNew: String)
+}
+
+// optional func
+public extension NCRenameFileDelegate {
+    func rename(fileName: String, fileNameNew: String) {}
+}
+
 class NCRenameFile: UIViewController, UITextFieldDelegate {
 
     @IBOutlet weak var titleLabel: UILabel!
@@ -37,8 +46,10 @@ class NCRenameFile: UIViewController, UITextFieldDelegate {
     @IBOutlet weak var renameButton: UIButton!
 
     var metadata: tableMetadata?
+    var fileName: String?
     var imagePreview: UIImage?
     var disableChangeExt: Bool = false
+    var delegate: NCRenameFileDelegate?
     
     // MARK: - Life Cycle
     
@@ -87,6 +98,26 @@ class NCRenameFile: UIViewController, UITextFieldDelegate {
                 
                 fileNameWithoutExtTrailingContraint.constant = 90
             }
+            
+        } else if let fileName = self.fileName {
+        
+            titleLabel.text = NSLocalizedString("_rename_file_", comment: "")
+
+            fileNameWithoutExt.text = (fileName as NSString).deletingPathExtension
+            fileNameWithoutExt.delegate = self
+            fileNameWithoutExt.becomeFirstResponder()
+            fileNameWithoutExtTrailingContraint.constant = 90
+
+            ext.text = (fileName as NSString).pathExtension
+            ext.delegate = self
+            
+            if imagePreview == nil {
+                previewFile.image = NCBrandColor.cacheImages.file
+            } else {
+                previewFile.image = imagePreview
+            }
+            previewFile.layer.cornerRadius = 10
+            previewFile.layer.masksToBounds = true
         }
         
         cancelButton.setTitle(NSLocalizedString("_cancel_", comment: ""), for: .normal)
@@ -116,7 +147,7 @@ class NCRenameFile: UIViewController, UITextFieldDelegate {
     override func viewDidAppear(_ animated: Bool) {
         super.viewDidAppear(animated)
         
-        if metadata == nil {
+        if metadata == nil && fileName == nil {
             dismiss(animated: true)
         }
         
@@ -145,56 +176,72 @@ class NCRenameFile: UIViewController, UITextFieldDelegate {
     
     @IBAction func rename(_ sender: Any) {
 
-        guard let metadata = metadata else { return }
         var fileNameWithoutExtNew = ""
         var extNew = ""
         var fileNameNew = ""
         
-        if fileNameWithoutExt.text == nil || fileNameWithoutExt.text?.count == 0 {
-            self.fileNameWithoutExt.text = metadata.fileNameWithoutExt
-            return
-        } else {
-            fileNameWithoutExtNew = fileNameWithoutExt.text!
-        }
+        if let metadata = self.metadata {
         
-        if metadata.directory {
-            
-            fileNameNew = fileNameWithoutExtNew
-            renameMetadata(metadata, fileNameNew: fileNameNew)
-            
-        } else {
-            
-            if ext.text == nil || ext.text?.count == 0 {
-                self.ext.text = metadata.ext
+            if fileNameWithoutExt.text == nil || fileNameWithoutExt.text?.count == 0 {
+                self.fileNameWithoutExt.text = metadata.fileNameWithoutExt
                 return
             } else {
-                extNew = ext.text!
+                fileNameWithoutExtNew = fileNameWithoutExt.text!
             }
             
-            if extNew != metadata.ext {
+            if metadata.directory {
                 
-                let message = String(format: NSLocalizedString("_rename_ext_message_", comment: ""), extNew, metadata.ext)
-                let alertController = UIAlertController(title: NSLocalizedString("_rename_ext_title_", comment: ""), message: message, preferredStyle: .alert)
-                            
-                var title = NSLocalizedString("_use_", comment: "") + " ." + extNew
-                alertController.addAction(UIAlertAction(title: title, style: .default, handler: { action in
-                    
-                    fileNameNew = fileNameWithoutExtNew + "." + extNew
-                    self.renameMetadata(metadata, fileNameNew: fileNameNew)
-                }))
+                fileNameNew = fileNameWithoutExtNew
+                renameMetadata(metadata, fileNameNew: fileNameNew)
+                
+            } else {
                 
-                title = NSLocalizedString("_keep_", comment: "") + " ." + metadata.ext
-                alertController.addAction(UIAlertAction(title: title, style: .default, handler: { action in
+                if ext.text == nil || ext.text?.count == 0 {
                     self.ext.text = metadata.ext
-                }))
+                    return
+                } else {
+                    extNew = ext.text!
+                }
                 
-                self.present(alertController, animated: true)
+                if extNew != metadata.ext {
+                    
+                    let message = String(format: NSLocalizedString("_rename_ext_message_", comment: ""), extNew, metadata.ext)
+                    let alertController = UIAlertController(title: NSLocalizedString("_rename_ext_title_", comment: ""), message: message, preferredStyle: .alert)
+                                
+                    var title = NSLocalizedString("_use_", comment: "") + " ." + extNew
+                    alertController.addAction(UIAlertAction(title: title, style: .default, handler: { action in
+                        
+                        fileNameNew = fileNameWithoutExtNew + "." + extNew
+                        self.renameMetadata(metadata, fileNameNew: fileNameNew)
+                    }))
+                    
+                    title = NSLocalizedString("_keep_", comment: "") + " ." + metadata.ext
+                    alertController.addAction(UIAlertAction(title: title, style: .default, handler: { action in
+                        self.ext.text = metadata.ext
+                    }))
+                    
+                    self.present(alertController, animated: true)
+                    
+                } else {
                 
-            } else {
+                    fileNameNew = fileNameWithoutExtNew + "." + extNew
+                    renameMetadata(metadata, fileNameNew: fileNameNew)
+                }
+            }
             
-                fileNameNew = fileNameWithoutExtNew + "." + extNew
-                renameMetadata(metadata, fileNameNew: fileNameNew)
+        } else if let fileName = self.fileName {
+            
+            if fileNameWithoutExt.text == nil || fileNameWithoutExt.text?.count == 0 {
+                fileNameWithoutExt.text = (fileName as NSString).deletingPathExtension
+                return
+            } else if ext.text == nil || ext.text?.count == 0 {
+                ext.text = (fileName as NSString).pathExtension
+                return
             }
+            
+            fileNameNew = (fileNameWithoutExt.text ?? "") + "." + (ext.text ?? "")
+            self.delegate?.rename(fileName: fileName, fileNameNew: fileNameNew)
+            self.dismiss(animated: true)
         }
     }