Browse Source

Implement files lock / unlock

- allow files lock for single & multiple files (select)
- - If mixed locked files are selected prefer unlocking files & ignore already unlocked files
- after a lock has been applied to a file, reload metadata to catch new lock times / owner, etc. (= 2 requests for a lock operation per file)
- use single function (& single action) with parameter instead of 2 functions for lock / unlock, compare to Favourite / Offline

- fix bug not recognising tap on moreLock button

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

+ 1 - 1
iOSClient/Main/Collection Common/NCCollectionViewCommon.swift

@@ -791,7 +791,7 @@ class NCCollectionViewCommon: UIViewController, UIGestureRecognizerDelegate, UIS
 
 
         guard let metadata = NCManageDatabase.shared.getMetadataFromOcId(objectId) else { return }
         guard let metadata = NCManageDatabase.shared.getMetadataFromOcId(objectId) else { return }
 
 
-        if namedButtonMore == NCGlobal.shared.buttonMoreMore {
+        if namedButtonMore == NCGlobal.shared.buttonMoreMore || namedButtonMore == NCGlobal.shared.buttonMoreLock {
             toggleMenu(metadata: metadata, imageIcon: image)
             toggleMenu(metadata: metadata, imageIcon: image)
         } else if namedButtonMore == NCGlobal.shared.buttonMoreStop {
         } else if namedButtonMore == NCGlobal.shared.buttonMoreStop {
             NCNetworking.shared.cancelTransferMetadata(metadata) { }
             NCNetworking.shared.cancelTransferMetadata(metadata) { }

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

@@ -95,6 +95,8 @@ extension NCSelectableNavigationView where Self: UIViewController {
         var selectedMetadatas: [tableMetadata] = []
         var selectedMetadatas: [tableMetadata] = []
         var selectedMediaMetadatas: [tableMetadata] = []
         var selectedMediaMetadatas: [tableMetadata] = []
         var isAnyOffline = false
         var isAnyOffline = false
+        var isAnyFolder = false
+        var isAnyLocked = false
 
 
         for ocId in selectOcId {
         for ocId in selectOcId {
             guard let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId) else { continue }
             guard let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId) else { continue }
@@ -102,7 +104,8 @@ extension NCSelectableNavigationView where Self: UIViewController {
             if [NCCommunicationCommon.typeClassFile.image.rawValue, NCCommunicationCommon.typeClassFile.video.rawValue].contains(metadata.classFile) {
             if [NCCommunicationCommon.typeClassFile.image.rawValue, NCCommunicationCommon.typeClassFile.video.rawValue].contains(metadata.classFile) {
                 selectedMediaMetadatas.append(metadata)
                 selectedMediaMetadatas.append(metadata)
             }
             }
-
+            if metadata.directory { isAnyFolder = true }
+            if metadata.lock { isAnyLocked = true }
             guard !isAnyOffline else { continue }
             guard !isAnyOffline else { continue }
             if metadata.directory,
             if metadata.directory,
                let directory = NCManageDatabase.shared.getTableDirectory(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", appDelegate.account, metadata.serverUrl + "/" + metadata.fileName)) {
                let directory = NCManageDatabase.shared.getTableDirectory(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", appDelegate.account, metadata.serverUrl + "/" + metadata.fileName)) {
@@ -114,6 +117,13 @@ 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 {
+            actions.append(.lockUnlockFiles(shouldLock: !isAnyLocked, metadatas: selectedMetadatas, completion: {
+                self.reloadDataSource()
+                self.tapSelect()
+            }))
+        }
+
         if !selectedMediaMetadatas.isEmpty {
         if !selectedMediaMetadatas.isEmpty {
             actions.append(.saveMediaAction(selectedMediaMetadatas: selectedMediaMetadatas, completion: tapSelect))
             actions.append(.saveMediaAction(selectedMediaMetadatas: selectedMediaMetadatas, completion: tapSelect))
         }
         }

+ 4 - 0
iOSClient/Menu/NCCollectionViewCommon+Menu.swift

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

+ 20 - 0
iOSClient/Menu/NCMenuAction.swift

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

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

@@ -27,7 +27,7 @@ import NCCommunication
 import UIKit
 import UIKit
 
 
 extension UIViewController {
 extension UIViewController {
-    fileprivate func handleProfileAction(_ action: NCCHovercard.Action, for userId: String) {
+    fileprivate func handleProfileAction(_ action: NCHovercard.Action, for userId: String) {
         switch action.appId {
         switch action.appId {
         case "email":
         case "email":
             guard
             guard

+ 17 - 0
iOSClient/Networking/NCNetworking.swift

@@ -1169,6 +1169,23 @@ import Queuer
         }
         }
     }
     }
 
 
+    // MARK: - Lock Files
+
+    @objc func lockUnlockFile(_ metadata: tableMetadata, shoulLock: Bool, completion: @escaping (_ errorCode: Int, _ errorDescription: String) -> Void) {
+        NCCommunication.shared.lockUnlockFile(shouldLock: shoulLock, fileId: metadata.fileId) { errorCode, errorDescription in
+            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)
+                return
+            }
+            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 }
+                NCManageDatabase.shared.addMetadata(metadata)
+            }
+        }
+    }
+
     // MARK: - WebDav Rename
     // MARK: - WebDav Rename
 
 
     @objc func renameMetadata(_ metadata: tableMetadata, fileNameNew: String, viewController: UIViewController?, completion: @escaping (_ errorCode: Int, _ errorDescription: String?) -> Void) {
     @objc func renameMetadata(_ metadata: tableMetadata, fileNameNew: String, viewController: UIViewController?, completion: @escaping (_ errorCode: Int, _ errorDescription: String?) -> Void) {