Browse Source

Improove files lock

- use NotificationCenter to reload data after lock/unlock instead of completion handler
- - keep using completion handler in action to dismiss selection
- adjust title for single / multiple files

Signed-off-by: Henrik Storch <henrik.storch@nextcloud.com>
Henrik Storch 3 years ago
parent
commit
acc2221bf2

+ 1 - 4
iOSClient/Main/Collection Common/NCSelectableNavigationView.swift

@@ -118,10 +118,7 @@ extension NCSelectableNavigationView where Self: UIViewController {
         actions.append(.openInAction(selectedMetadatas: selectedMetadatas, viewController: self, completion: tapSelect))
         actions.append(.openInAction(selectedMetadatas: selectedMetadatas, viewController: self, completion: tapSelect))
 
 
         if !isAnyFolder {
         if !isAnyFolder {
-            actions.append(.lockUnlockFiles(shouldLock: !isAnyLocked, metadatas: selectedMetadatas, completion: {
-                self.reloadDataSource()
-                self.tapSelect()
-            }))
+            actions.append(.lockUnlockFiles(shouldLock: !isAnyLocked, metadatas: selectedMetadatas, completion: tapSelect))
         }
         }
 
 
         if !selectedMediaMetadatas.isEmpty {
         if !selectedMediaMetadatas.isEmpty {

+ 10 - 6
iOSClient/Main/NCFunctionCenter.swift

@@ -616,7 +616,8 @@ import SVGKit
             }
             }
         }
         }
         let titleOffline = isOffline ? NSLocalizedString("_remove_available_offline_", comment: "") :  NSLocalizedString("_set_available_offline_", comment: "")
         let titleOffline = isOffline ? NSLocalizedString("_remove_available_offline_", comment: "") :  NSLocalizedString("_set_available_offline_", comment: "")
-
+        let titleLock = metadata.lock ? NSLocalizedString("_unlock_file_", comment: "") :  NSLocalizedString("_lock_file_", comment: "")
+        let iconLock = metadata.lock ? "lock.open" : "lock"
         let copy = UIAction(title: NSLocalizedString("_copy_file_", comment: ""), image: UIImage(systemName: "doc.on.doc")) { _ in
         let copy = UIAction(title: NSLocalizedString("_copy_file_", comment: ""), image: UIImage(systemName: "doc.on.doc")) { _ in
             self.copyPasteboard(pasteboardOcIds: [metadata.ocId], hudView: viewController.view)
             self.copyPasteboard(pasteboardOcIds: [metadata.ocId], hudView: viewController.view)
         }
         }
@@ -637,7 +638,10 @@ import SVGKit
                 viewController.reloadDataSource()
                 viewController.reloadDataSource()
             }
             }
         }
         }
-
+        
+        let lockUnlock = UIAction(title: titleLock, image: NCUtility.shared.loadImage(named: iconLock)) { _ in
+            NCNetworking.shared.lockUnlockFile(metadata, shoulLock: !metadata.lock)
+        }
         let save = UIAction(title: titleSave, image: UIImage(systemName: "square.and.arrow.down")) { _ in
         let save = UIAction(title: titleSave, image: UIImage(systemName: "square.and.arrow.down")) { _ in
             if metadataMOV != nil {
             if metadataMOV != nil {
                 self.saveLivePhoto(metadata: metadata, metadataMOV: metadataMOV!)
                 self.saveLivePhoto(metadata: metadata, metadataMOV: metadataMOV!)
@@ -741,7 +745,7 @@ import SVGKit
 
 
         // FILE
         // FILE
 
 
-        var children: [UIMenuElement] = [favorite, offline, openIn, rename, moveCopy, copy, copyPath, delete]
+        var children: [UIMenuElement] = [favorite, lockUnlock, offline, openIn, rename, moveCopy, copy, copyPath, delete]
 
 
         if (metadata.contentType != "image/svg+xml") && (metadata.classFile == NCCommunicationCommon.typeClassFile.image.rawValue || metadata.classFile == NCCommunicationCommon.typeClassFile.video.rawValue) {
         if (metadata.contentType != "image/svg+xml") && (metadata.classFile == NCCommunicationCommon.typeClassFile.image.rawValue || metadata.classFile == NCCommunicationCommon.typeClassFile.video.rawValue) {
             children.insert(save, at: 2)
             children.insert(save, at: 2)
@@ -756,18 +760,18 @@ import SVGKit
         }
         }
 
 
         if enableViewInFolder {
         if enableViewInFolder {
-            children.insert(viewInFolder, at: children.count-1)
+            children.insert(viewInFolder, at: children.count - 1)
         }
         }
 
 
         if (!isFolderEncrypted && metadata.contentType != "image/gif" && metadata.contentType != "image/svg+xml") && (metadata.contentType == "com.adobe.pdf" || metadata.contentType == "application/pdf" || metadata.classFile == NCCommunicationCommon.typeClassFile.image.rawValue) {
         if (!isFolderEncrypted && metadata.contentType != "image/gif" && metadata.contentType != "image/svg+xml") && (metadata.contentType == "com.adobe.pdf" || metadata.contentType == "application/pdf" || metadata.classFile == NCCommunicationCommon.typeClassFile.image.rawValue) {
-            children.insert(modify, at: children.count-1)
+            children.insert(modify, at: children.count - 1)
         }
         }
 
 
         if metadata.classFile == NCCommunicationCommon.typeClassFile.image.rawValue && viewController is NCCollectionViewCommon && !NCBrandOptions.shared.disable_background_image {
         if metadata.classFile == NCCommunicationCommon.typeClassFile.image.rawValue && viewController is NCCollectionViewCommon && !NCBrandOptions.shared.disable_background_image {
             let viewController: NCCollectionViewCommon = viewController as! NCCollectionViewCommon
             let viewController: NCCollectionViewCommon = viewController as! NCCollectionViewCommon
             let layoutKey = viewController.layoutKey
             let layoutKey = viewController.layoutKey
             if layoutKey == NCGlobal.shared.layoutViewFiles {
             if layoutKey == NCGlobal.shared.layoutViewFiles {
-                children.insert(saveBackground, at: children.count-1)
+                children.insert(saveBackground, at: children.count - 1)
             }
             }
         }
         }
 
 

+ 1 - 1
iOSClient/Menu/NCCollectionViewCommon+Menu.swift

@@ -89,7 +89,7 @@ extension NCCollectionViewCommon {
         )
         )
 
 
         if !metadata.directory {
         if !metadata.directory {
-            actions.append(.lockUnlockFiles(shouldLock: !metadata.lock, metadatas: [metadata], completion: self.reloadDataSource))
+            actions.append(.lockUnlockFiles(shouldLock: !metadata.lock, metadatas: [metadata]))
         }
         }
 
 
         //
         //

+ 8 - 7
iOSClient/Menu/NCMenuAction.swift

@@ -215,20 +215,21 @@ extension NCMenuAction {
 
 
     /// Lock or unlock a file using files_lock
     /// Lock or unlock a file using files_lock
     static func lockUnlockFiles(shouldLock: Bool, metadatas: [tableMetadata], completion: (() -> Void)? = nil) -> NCMenuAction {
     static func lockUnlockFiles(shouldLock: Bool, metadatas: [tableMetadata], completion: (() -> Void)? = nil) -> NCMenuAction {
-        let titleKey = !shouldLock ? "_unlock_file_" : "_lock_file_"
+        let titleKey: String
+        if metadatas.count == 1 {
+            titleKey = shouldLock ? "_lock_file_" : "_unlock_file_"
+        } else {
+            titleKey = shouldLock ? "_lock_selected_files_" : "_unlock_selected_files_"
+        }
         let imageName = !shouldLock ? "lock.open" : "lock"
         let imageName = !shouldLock ? "lock.open" : "lock"
         return NCMenuAction(
         return NCMenuAction(
             title: NSLocalizedString(titleKey, comment: ""),
             title: NSLocalizedString(titleKey, comment: ""),
             icon: NCUtility.shared.loadImage(named: imageName),
             icon: NCUtility.shared.loadImage(named: imageName),
             action: { _ in
             action: { _ in
-                let dispatchGroup = DispatchGroup()
                 for metadata in metadatas where metadata.lock != shouldLock {
                 for metadata in metadatas where metadata.lock != shouldLock {
-                    dispatchGroup.enter()
-                    NCNetworking.shared.lockUnlockFile(metadata, shoulLock: shouldLock) { _, _ in
-                        dispatchGroup.leave()
-                    }
+                    NCNetworking.shared.lockUnlockFile(metadata, shoulLock: shouldLock)
                 }
                 }
-                dispatchGroup.notify(queue: .main, execute: completion ?? { })
+                completion?()
             }
             }
         )
         )
     }
     }

+ 2 - 3
iOSClient/Networking/NCNetworking.swift

@@ -1171,17 +1171,16 @@ import Queuer
 
 
     // MARK: - Lock Files
     // MARK: - Lock Files
 
 
-    @objc func lockUnlockFile(_ metadata: tableMetadata, shoulLock: Bool, completion: @escaping (_ errorCode: Int, _ errorDescription: String) -> Void) {
+    @objc func lockUnlockFile(_ metadata: tableMetadata, shoulLock: Bool) {
         NCCommunication.shared.lockUnlockFile(shouldLock: shoulLock, fileId: metadata.fileId) { errorCode, errorDescription in
         NCCommunication.shared.lockUnlockFile(shouldLock: shoulLock, fileId: metadata.fileId) { errorCode, errorDescription in
             guard errorCode == 0 else {
             guard errorCode == 0 else {
-                completion(errorCode, errorDescription)
                 NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode, priority: .max)
                 NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode, priority: .max)
                 return
                 return
             }
             }
             NCNetworking.shared.readFile(serverUrlFileName: metadata.serverUrl + "/" + metadata.fileName, account: metadata.account) { account, metadata, errorCode, errorDescription in
             NCNetworking.shared.readFile(serverUrlFileName: metadata.serverUrl + "/" + metadata.fileName, account: metadata.account) { account, metadata, errorCode, errorDescription in
-                completion(errorCode, errorDescription)
                 guard errorCode == 0, let metadata = metadata else { return }
                 guard errorCode == 0, let metadata = metadata else { return }
                 NCManageDatabase.shared.addMetadata(metadata)
                 NCManageDatabase.shared.addMetadata(metadata)
+                NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterReloadDataSource)
             }
             }
         }
         }
     }
     }

+ 4 - 2
iOSClient/Supporting Files/en.lproj/Localizable.strings

@@ -148,8 +148,10 @@
 "_leave_share_"             = "Leave this share";
 "_leave_share_"             = "Leave this share";
 
 
 /* Files lock */
 /* Files lock */
-"_lock_"                    = "Lock";
-"_unlock_"                  = "Unock";
+"_lock_file_"                       = "Lock file";
+"_unlock_file_"                     = "Unock file";
+"_lock_selected_files_"             = "Lock files";
+"_unlock_selected_files_"           = "Unock files";
 
 
 /* Remove a file from a list, don't delete it entirely */
 /* Remove a file from a list, don't delete it entirely */
 "_remove_file_"             = "Remove file";
 "_remove_file_"             = "Remove file";