marinofaggiana 4 years ago
parent
commit
7672d936d5

+ 14 - 50
iOSClient/Main/CCMain.m

@@ -136,11 +136,9 @@
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(triggerProgressTask:) name:k_notificationCenter_progressTask object:nil];
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deleteFile:) name:k_notificationCenter_deleteFile object:nil];
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(favoriteFile:) name:k_notificationCenter_favoriteFile object:nil];
-    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moveFile:) name:k_notificationCenter_moveFile object:nil];
-    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(copyFile:) name:k_notificationCenter_copyFile object:nil];
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeTheming) name:k_notificationCenter_changeTheming object:nil];
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(createFolder:) name:k_notificationCenter_createFolder object:nil];
-    
+
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadDatasourceTransfer:) name:k_notificationCenter_downloadStartFile object:nil];
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadDatasourceTransfer:) name:k_notificationCenter_downloadedFile object:nil];
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadDatasourceTransfer:) name:k_notificationCenter_downloadCancelFile object:nil];
@@ -148,6 +146,12 @@
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadDatasourceTransfer:) name:k_notificationCenter_uploadStartFile object:nil];
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadDatasourceTransfer:) name:k_notificationCenter_uploadedFile object:nil];
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadDatasourceTransfer:) name:k_notificationCenter_uploadCancelFile object:nil];
+    
+    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadDatasourceTransfer:) name:k_notificationCenter_renameFile object:nil];
+    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadDatasourceTransfer:) name:k_notificationCenter_moveFile object:nil];
+    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadDatasourceTransfer:) name:k_notificationCenter_copyFile object:nil];
+
+
 
     // Search
     self.definesPresentationContext = YES;
@@ -489,52 +493,6 @@
     [self reloadDatasource:self.serverUrl ocId:nil];
 }
 
-- (void)moveFile:(NSNotification *)notification
-{
-    NSDictionary *userInfo = notification.userInfo;
-    tableMetadata *metadata = userInfo[@"metadata"];
-//    tableMetadata *metadataNew = userInfo[@"metadataNew"];
-    NSInteger errorCode = [userInfo[@"errorCode"] integerValue];
-    NSString *errorDescription = userInfo[@"errorDescription"];
-    
-    if (![metadata.serverUrl isEqualToString:self.serverUrl]) { return; }
-    
-    if (arrayMoveMetadata.count > 0) {
-        tableMetadata *metadata = arrayMoveMetadata.firstObject;
-        NSString *serverUrlTo = arrayMoveServerUrlTo.firstObject;
-        [arrayMoveMetadata removeObjectAtIndex:0];
-        [arrayMoveServerUrlTo removeObjectAtIndex:0];
-        [[NCNetworking shared] moveMetadata:metadata serverUrlTo:serverUrlTo overwrite:true completion:^(NSInteger errorCode, NSString *errorDescription) { }];
-    }
-    
-    if (errorCode != 0 && self.view.window != nil) {
-        [[NCContentPresenter shared] messageNotification:@"_error_" description:errorDescription delay:k_dismissAfterSecond type:messageTypeError errorCode:errorCode forced:false];
-    }
-}
-
-- (void)copyFile:(NSNotification *)notification
-{
-    NSDictionary *userInfo = notification.userInfo;
-    tableMetadata *metadata = userInfo[@"metadata"];
-//    NSString *serverUrlTo = userInfo[@"serverUrlTo"];
-    NSInteger errorCode = [userInfo[@"errorCode"] integerValue];
-    NSString *errorDescription = userInfo[@"errorDescription"];
-    
-    if (![metadata.serverUrl isEqualToString:self.serverUrl]) { return; }
-    
-    if (arrayCopyMetadata.count > 0) {
-        tableMetadata *metadata = arrayCopyMetadata.firstObject;
-        NSString *serverUrlTo = arrayCopyServerUrlTo.firstObject;
-        [arrayCopyMetadata removeObjectAtIndex:0];
-        [arrayCopyServerUrlTo removeObjectAtIndex:0];
-        [[NCNetworking shared] copyMetadata:metadata serverUrlTo:serverUrlTo overwrite:true completion:^(NSInteger errorCode, NSString *errorDescription) { }];
-    }
-    
-    if (errorCode != 0 && self.view.window != nil) {
-        [[NCContentPresenter shared] messageNotification:@"_error_" description:errorDescription delay:k_dismissAfterSecond type:messageTypeError errorCode:errorCode forced:false];
-    }
-}
-
 - (void)favoriteFile:(NSNotification *)notification
 {
     if (self.view.window == nil) { return; }
@@ -1975,7 +1933,13 @@
 {
     NSDictionary *userInfo = notification.userInfo;
     tableMetadata *metadata = userInfo[@"metadata"];
-
+    NSInteger errorCode = [userInfo[@"errorCode"] integerValue];
+    NSString *errorDescription = userInfo[@"errorDescription"];
+    
+    if (errorCode != 0 && self.view.window != nil) {
+        [[NCContentPresenter shared] messageNotification:@"_error_" description:errorDescription delay:k_dismissAfterSecond type:messageTypeError errorCode:errorCode forced:false];
+    }
+    
     [self reloadDatasource:metadata.serverUrl ocId:metadata.ocId];
 }
 

+ 29 - 0
iOSClient/Main/Menu/NCCollectionViewCommon+Menu.swift

@@ -121,6 +121,35 @@ extension NCCollectionViewCommon {
                 )
             )
         }
+        
+        actions.append(
+            NCMenuAction(
+                title: NSLocalizedString("_rename_", comment: ""),
+                icon: CCGraphics.changeThemingColorImage(UIImage(named: "rename"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
+                action: { menuAction in
+                    let alertController = UIAlertController(title: NSLocalizedString("_rename_", comment: ""), message: nil, preferredStyle: .alert)
+                    
+                    alertController.addTextField { (textField) in
+                        textField.text = metadata.fileNameView
+                        textField.delegate = self as? UITextFieldDelegate
+                        textField.addTarget(self, action: #selector(self.minCharTextFieldDidChange(sender:)), for: UIControl.Event.editingChanged)
+                    }
+
+                    let cancelAction = UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel, handler: nil)
+
+                    let okAction = UIAlertAction(title: NSLocalizedString("_ok_", comment: ""), style: .default, handler: { action in
+                        let fileNameNew = alertController.textFields![0].text
+                        NCNetworking.shared.renameMetadata(metadata, fileNameNew: fileNameNew!, urlBase: appDelegate.urlBase, viewController: self) { (errorCode, errorDescription) in }
+                    })
+                    okAction.isEnabled = false
+                    alertController.addAction(cancelAction)
+                    alertController.addAction(okAction)
+
+                    self.present(alertController, animated: true, completion: nil)
+                }
+            )
+        )
+        
 
         if !isFolderEncrypted && serverUrl != "" {
             actions.append(

+ 31 - 0
iOSClient/Main/NCCollectionCommon.swift

@@ -388,6 +388,17 @@ class NCCollectionCommon: NSObject {
         }
     }
     
+    func notificationRenameFile(collectionView: UICollectionView?, dataSource: NCDataSource?, metadata: tableMetadata, errorCode: Int, errorDescription: String) {
+        if let row = dataSource?.reloadMetadata(ocId: metadata.ocId) {
+            let indexPath = IndexPath(row: row, section: 0)
+            collectionView?.performBatchUpdates({
+                collectionView?.reloadItems(at: [indexPath])
+            }, completion: { (_) in
+                collectionView?.reloadData()
+            })
+        }
+    }
+    
     func notificationDownloadStartFile(collectionView: UICollectionView?, dataSource: NCDataSource?, metadata: tableMetadata) {
         if let row = dataSource?.reloadMetadata(ocId: metadata.ocId) {
             let indexPath = IndexPath(row: row, section: 0)
@@ -668,6 +679,7 @@ class NCCollectionViewCommon: UIViewController, UIGestureRecognizerDelegate, NCL
         NotificationCenter.default.addObserver(self, selector: #selector(deleteFile(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_deleteFile), object: nil)
         NotificationCenter.default.addObserver(self, selector: #selector(moveFile(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_moveFile), object: nil)
         NotificationCenter.default.addObserver(self, selector: #selector(copyFile(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_copyFile), object: nil)
+        NotificationCenter.default.addObserver(self, selector: #selector(renameFile(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_renameFile), object: nil)
 
         NotificationCenter.default.addObserver(self, selector: #selector(downloadStartFile(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_downloadStartFile), object: nil)
         NotificationCenter.default.addObserver(self, selector: #selector(downloadedFile(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_downloadedFile), object: nil)
@@ -717,6 +729,15 @@ class NCCollectionViewCommon: UIViewController, UIGestureRecognizerDelegate, NCL
         }
     }
     
+    // MARK: - Utility
+    
+    @objc func minCharTextFieldDidChange(sender: UITextField) {
+        guard let alertController = self.presentedViewController as? UIAlertController else { return }
+        guard let password = alertController.textFields?.first else { return }
+        guard let ok = alertController.actions.last else { return }
+        ok.isEnabled =  password.text?.count ?? 0 >= 8
+    }
+    
     // MARK: - NotificationCenter
 
     @objc func changeTheming() {
@@ -745,6 +766,16 @@ class NCCollectionViewCommon: UIViewController, UIGestureRecognizerDelegate, NCL
     
     @objc func copyFile(_ notification: NSNotification) { }
     
+    @objc func renameFile(_ notification: NSNotification) {
+        if self.view?.window == nil { return }
+        
+        if let userInfo = notification.userInfo as NSDictionary? {
+            if let metadata = userInfo["metadata"] as? tableMetadata, let errorCode = userInfo["errorCode"] as? Int, let errorDescription = userInfo["errorDescription"] as? String {
+                NCCollectionCommon.shared.notificationRenameFile(collectionView: collectionView, dataSource: dataSource, metadata: metadata, errorCode: errorCode, errorDescription: errorDescription)
+            }
+        }
+    }
+    
     @objc func downloadStartFile(_ notification: NSNotification) {
         if self.view?.window == nil { return }
         

+ 3 - 13
iOSClient/Networking/NCNetworking.swift

@@ -990,8 +990,6 @@ import Queuer
                         try FileManager.default.moveItem(atPath: atPath, toPath: toPath)
                     } catch { }
                 }
-                
-                NotificationCenter.default.postOnMainThread(name: k_notificationCenter_reloadDataSource, userInfo: ["ocId":metadata.ocId, "serverUrl":metadata.serverUrl])
             }
                     
             self.NotificationPost(name: k_notificationCenter_renameFile, userInfo: ["metadata": metadata, "errorCode": errorCode], errorDescription: errorDescription, completion: completion)
@@ -1035,16 +1033,10 @@ import Queuer
                 }
                 
                 NCManageDatabase.sharedInstance.moveMetadata(ocId: metadata.ocId, serverUrlTo: serverUrlTo)
-                guard let metadataNew = NCManageDatabase.sharedInstance.getMetadataFromOcId(metadata.ocId) else { return }
-                                
-                NotificationCenter.default.postOnMainThread(name: k_notificationCenter_reloadDataSource, userInfo: ["serverUrl":metadata.serverUrl])
-                NotificationCenter.default.postOnMainThread(name: k_notificationCenter_reloadDataSource, userInfo: ["serverUrl":serverUrlTo])
-                
-                self.NotificationPost(name: k_notificationCenter_moveFile, userInfo: ["metadata": metadata, "metadataNew": metadataNew, "errorCode": errorCode], errorDescription: errorDescription, completion: completion)
-
-            } else {
-                self.NotificationPost(name: k_notificationCenter_moveFile, userInfo: ["metadata": metadata, "errorCode": errorCode], errorDescription: errorDescription, completion: completion)
             }
+            
+            guard let metadataNew = NCManageDatabase.sharedInstance.getMetadataFromOcId(metadata.ocId) else { return }
+            self.NotificationPost(name: k_notificationCenter_moveFile, userInfo: ["metadata": metadata, "metadataNew": metadataNew, "errorCode": errorCode], errorDescription: errorDescription, completion: completion)
         }
     }
     
@@ -1078,8 +1070,6 @@ import Queuer
         
         NCCommunication.shared.copyFileOrFolder(serverUrlFileNameSource: serverUrlFileNameSource, serverUrlFileNameDestination: serverUrlFileNameDestination, overwrite: overwrite) { (account, errorCode, errorDescription) in
                     
-            NotificationCenter.default.postOnMainThread(name: k_notificationCenter_reloadDataSource, userInfo: ["serverUrl":serverUrlTo])
-
             self.NotificationPost(name: k_notificationCenter_copyFile, userInfo: ["metadata": metadata, "errorCode": errorCode], errorDescription: errorDescription, completion: completion)
         }
     }

+ 2 - 1
iOSClient/Select/NCSelect.swift

@@ -377,7 +377,8 @@ extension NCSelect: UICollectionViewDelegate {
             visualController.isOverwriteHide = isOverwriteHide
             visualController.overwrite = overwrite
             visualController.heightToolBarTop = heightToolBarTop
-                
+            visualController.array = array
+
             visualController.titleCurrentFolder = metadataPush!.fileNameView
             visualController.serverUrl = serverUrlPush