Browse Source

added order in MenuAction

Signed-off-by: Marino Faggiana <marino@marinofaggiana.com>
Marino Faggiana 2 năm trước cách đây
mục cha
commit
10e582c6bb

+ 2 - 2
iOSClient/Menu/AppDelegate+Menu.swift

@@ -115,7 +115,7 @@ extension AppDelegate {
         )
 
         if CCUtility.isEnd(toEndEnabled: appDelegate.account) && appDelegate.activeServerUrl == serverUrlHome {
-            actions.append(.seperator)
+            actions.append(.seperator(order: 0))
         }
 
         let titleCreateFolder = isEncrypted ? NSLocalizedString("_create_folder_e2ee_", comment: "") : NSLocalizedString("_create_folder_", comment: "")
@@ -144,7 +144,7 @@ extension AppDelegate {
         }
 
         if CCUtility.isEnd(toEndEnabled: appDelegate.account) && appDelegate.activeServerUrl == serverUrlHome {
-            actions.append(.seperator)
+            actions.append(.seperator(order: 0))
         }
 
         if serverVersionMajor >= NCGlobal.shared.nextcloudVersion18 && directory?.richWorkspace == nil && !isEncrypted && NextcloudKit.shared.isNetworkReachable() {

+ 21 - 11
iOSClient/Menu/NCCollectionViewCommon+Menu.swift

@@ -68,6 +68,7 @@ extension NCCollectionViewCommon {
             NCMenuAction(
                 title: metadata.fileNameView,
                 icon: iconHeader,
+                order: 0,
                 action: nil
             )
         )
@@ -100,11 +101,12 @@ extension NCCollectionViewCommon {
                     title: String(format: NSLocalizedString("_locked_by_", comment: ""), lockOwnerName),
                     details: lockTimeString,
                     icon: lockIcon,
+                    order: 10,
                     action: nil)
             )
         }
 
-        actions.append(.seperator)
+        actions.append(.seperator(order: 20))
 
         //
         // FAVORITE
@@ -115,6 +117,7 @@ extension NCCollectionViewCommon {
                 NCMenuAction(
                     title: metadata.favorite ? NSLocalizedString("_remove_favorites_", comment: "") : NSLocalizedString("_add_favorites_", comment: ""),
                     icon: NCUtility.shared.loadImage(named: "star.fill", color: NCBrandColor.shared.yellowFavorite),
+                    order: 20,
                     action: { _ in
                         NCNetworking.shared.favoriteMetadata(metadata) { error in
                             if error != .success {
@@ -134,6 +137,7 @@ extension NCCollectionViewCommon {
                 NCMenuAction(
                     title: NSLocalizedString("_details_", comment: ""),
                     icon: NCUtility.shared.loadImage(named: "info"),
+                    order: 30,
                     action: { _ in
                         NCFunctionCenter.shared.openShare(viewController: self, metadata: metadata, indexPage: .activity)
                     }
@@ -148,10 +152,9 @@ extension NCCollectionViewCommon {
         if !metadata.directory, metadata.canUnlock(as: appDelegate.userId), hasLockCapability {
             let lockAction = NCMenuAction.lockUnlockFiles(shouldLock: !metadata.lock, metadatas: [metadata])
             if metadata.lock {
-                // make unlock first action, after info rows & seperator
-                actions.insert(lockAction, at: 3)
+                actions.append(NCMenuAction.lockUnlockFiles(shouldLock: !metadata.lock, metadatas: [metadata], order: 15))
             } else {
-                actions.append(lockAction)
+                actions.append(NCMenuAction.lockUnlockFiles(shouldLock: !metadata.lock, metadatas: [metadata], order: 40))
             }
         }
 
@@ -159,7 +162,7 @@ extension NCCollectionViewCommon {
         // OFFLINE
         //
         if !isFolderEncrypted {
-            actions.append(.setAvailableOfflineAction(selectedMetadatas: [metadata], isAnyOffline: isOffline, viewController: self, completion: {
+            actions.append(.setAvailableOfflineAction(selectedMetadatas: [metadata], isAnyOffline: isOffline, viewController: self, order: 50, completion: {
                 self.reloadDataSource()
             }))
         }
@@ -188,6 +191,7 @@ extension NCCollectionViewCommon {
                     NCMenuAction(
                         title: title,
                         icon: icon!,
+                        order: 60,
                         action: { _ in
                             NCViewer.shared.view(viewController: self, metadata: metadata, metadatas: [metadata], imageIcon: imageIcon, editor: editor, isRichDocument: isRichDocument)
                         }
@@ -200,21 +204,21 @@ extension NCCollectionViewCommon {
         // OPEN IN
         //
         if !metadata.directory && !NCBrandOptions.shared.disable_openin_file {
-            actions.append(.openInAction(selectedMetadatas: [metadata], viewController: self))
+            actions.append(.openInAction(selectedMetadatas: [metadata], viewController: self, order: 70))
         }
 
         //
         // PRINT
         //
         if metadata.isPrintable {
-            actions.append(.printAction(metadata: metadata))
+            actions.append(.printAction(metadata: metadata, order: 80))
         }
 
         //
         // SAVE
         //
         if (metadata.classFile == NKCommon.typeClassFile.image.rawValue && metadata.contentType != "image/svg+xml") || metadata.classFile == NKCommon.typeClassFile.video.rawValue {
-            actions.append(.saveMediaAction(selectedMediaMetadatas: [metadata]))
+            actions.append(.saveMediaAction(selectedMediaMetadatas: [metadata], order: 90))
         }
 
         //
@@ -225,6 +229,7 @@ extension NCCollectionViewCommon {
                 NCMenuAction(
                     title: NSLocalizedString("_save_as_scan_", comment: ""),
                     icon: NCUtility.shared.loadImage(named: "viewfinder.circle"),
+                    order: 100,
                     action: { _ in
                         NCFunctionCenter.shared.openDownload(metadata: metadata, selector: NCGlobal.shared.selectorSaveAsScan)
                     }
@@ -240,6 +245,7 @@ extension NCCollectionViewCommon {
                 NCMenuAction(
                     title: NSLocalizedString("_rename_", comment: ""),
                     icon: NCUtility.shared.loadImage(named: "pencil"),
+                    order: 110,
                     action: { _ in
 
                         if let vcRename = UIStoryboard(name: "NCRenameFile", bundle: nil).instantiateInitialViewController() as? NCRenameFile {
@@ -260,14 +266,14 @@ extension NCCollectionViewCommon {
         // COPY - MOVE
         //
         if !isFolderEncrypted && serverUrl != "" {
-            actions.append(.moveOrCopyAction(selectedMetadatas: [metadata]))
+            actions.append(.moveOrCopyAction(selectedMetadatas: [metadata], order: 120))
         }
 
         //
         // COPY
         //
         if !metadata.directory {
-            actions.append(.copyAction(selectOcId: [metadata.ocId], hudView: self.view))
+            actions.append(.copyAction(selectOcId: [metadata.ocId], hudView: self.view, order: 130))
         }
         
         //
@@ -278,6 +284,7 @@ extension NCCollectionViewCommon {
                 NCMenuAction(
                     title: NSLocalizedString("_modify_", comment: ""),
                     icon: NCUtility.shared.loadImage(named: "pencil.tip.crop.circle"),
+                    order: 140,
                     action: { menuAction in
                         NCFunctionCenter.shared.openDownload(metadata: metadata, selector: NCGlobal.shared.selectorLoadFileQuickLook)
                     }
@@ -293,6 +300,7 @@ extension NCCollectionViewCommon {
                 NCMenuAction(
                     title: NSLocalizedString("_change_color_", comment: ""),
                     icon: NCUtility.shared.loadImage(named: "palette"),
+                    order: 150,
                     action: { _ in
                         if let picker = UIStoryboard(name: "NCColorPicker", bundle: nil).instantiateInitialViewController() as? NCColorPicker {
                             picker.metadata = metadata
@@ -308,7 +316,7 @@ extension NCCollectionViewCommon {
         //
         // DELETE
         //
-        actions.append(.deleteAction(selectedMetadatas: [metadata], metadataFolder: metadataFolder, viewController: self))
+        actions.append(.deleteAction(selectedMetadatas: [metadata], metadataFolder: metadataFolder, viewController: self, order: 160))
 
         //
         // SET FOLDER E2EE
@@ -318,6 +326,7 @@ extension NCCollectionViewCommon {
                 NCMenuAction(
                     title: NSLocalizedString("_e2e_set_folder_encrypted_", comment: ""),
                     icon: NCUtility.shared.loadImage(named: "lock"),
+                    order: 170,
                     action: { _ in
                         NextcloudKit.shared.markE2EEFolder(fileId: metadata.fileId, delete: false) { account, error in
                             if error == .success {
@@ -343,6 +352,7 @@ extension NCCollectionViewCommon {
                 NCMenuAction(
                     title: NSLocalizedString("_e2e_remove_folder_encrypted_", comment: ""),
                     icon: NCUtility.shared.loadImage(named: "lock"),
+                    order: 180,
                     action: { _ in
                         NextcloudKit.shared.markE2EEFolder(fileId: metadata.fileId, delete: true) { account, error in
                             if error == .success {

+ 23 - 12
iOSClient/Menu/NCMenuAction.swift

@@ -36,16 +36,18 @@ class NCMenuAction {
     var isOn: Bool = false
     var action: ((_ menuAction: NCMenuAction) -> Void)?
     var rowHeight: CGFloat { self.title == NCMenuAction.seperatorIdentifier ? NCMenuAction.seperatorHeight : self.details != nil ? 80 : 60 }
+    var order: Int = 0
 
-    init(title: String, details: String? = nil, icon: UIImage, action: ((_ menuAction: NCMenuAction) -> Void)?) {
+    init(title: String, details: String? = nil, icon: UIImage, order: Int = 0, action: ((_ menuAction: NCMenuAction) -> Void)?) {
         self.title = title
         self.details = details
         self.icon = icon
         self.action = action
         self.selectable = false
+        self.order = order
     }
 
-    init(title: String, details: String? = nil, icon: UIImage, onTitle: String? = nil, onIcon: UIImage? = nil, selected: Bool, on: Bool, action: ((_ menuAction: NCMenuAction) -> Void)?) {
+    init(title: String, details: String? = nil, icon: UIImage, onTitle: String? = nil, onIcon: UIImage? = nil, selected: Bool, on: Bool, order: Int = 0, action: ((_ menuAction: NCMenuAction) -> Void)?) {
         self.title = title
         self.details = details
         self.icon = icon
@@ -55,6 +57,7 @@ class NCMenuAction {
         self.selected = selected
         self.isOn = on
         self.selectable = true
+        self.order = order
     }
 }
 
@@ -65,8 +68,8 @@ extension NCMenuAction {
     static let seperatorHeight: CGFloat = 0.5
 
     /// A static seperator, with no actions, text, or icons
-    static var seperator: NCMenuAction {
-        return NCMenuAction(title: seperatorIdentifier, icon: UIImage(), action: nil)
+    static func seperator(order: Int = 0) -> NCMenuAction {
+        return NCMenuAction(title: seperatorIdentifier, icon: UIImage(), order: order, action: nil)
     }
 
     /// Select all items
@@ -79,10 +82,11 @@ extension NCMenuAction {
     }
 
     /// Copy files to pasteboard
-    static func copyAction(selectOcId: [String], hudView: UIView, completion: (() -> Void)? = nil) -> NCMenuAction {
+    static func copyAction(selectOcId: [String], hudView: UIView, order: Int = 0, completion: (() -> Void)? = nil) -> NCMenuAction {
         NCMenuAction(
             title: NSLocalizedString("_copy_file_", comment: ""),
             icon: NCUtility.shared.loadImage(named: "doc.on.doc"),
+            order: order,
             action: { _ in
                 NCFunctionCenter.shared.copyPasteboard(pasteboardOcIds: selectOcId, hudView: hudView)
                 completion?()
@@ -91,7 +95,7 @@ extension NCMenuAction {
     }
 
     /// Delete files either from cache or from Nextcloud
-    static func deleteAction(selectedMetadatas: [tableMetadata], metadataFolder: tableMetadata? = nil, viewController: UIViewController, completion: (() -> Void)? = nil) -> NCMenuAction {
+    static func deleteAction(selectedMetadatas: [tableMetadata], metadataFolder: tableMetadata? = nil, viewController: UIViewController, order: Int = 0, completion: (() -> Void)? = nil) -> NCMenuAction {
         var titleDelete = NSLocalizedString("_delete_", comment: "")
         if selectedMetadatas.count > 1 {
             titleDelete = NSLocalizedString("_delete_selected_files_", comment: "")
@@ -123,6 +127,7 @@ extension NCMenuAction {
         return NCMenuAction(
             title: titleDelete,
             icon: NCUtility.shared.loadImage(named: "trash"),
+            order: order,
             action: { _ in
                 let alertController = UIAlertController(
                     title: titleDelete,
@@ -149,10 +154,11 @@ extension NCMenuAction {
     }
 
     /// Open "share view" (activity VC) to open files in another app
-    static func openInAction(selectedMetadatas: [tableMetadata], viewController: UIViewController, completion: (() -> Void)? = nil) -> NCMenuAction {
+    static func openInAction(selectedMetadatas: [tableMetadata], viewController: UIViewController, order: Int = 0, completion: (() -> Void)? = nil) -> NCMenuAction {
         NCMenuAction(
             title: NSLocalizedString("_open_in_", comment: ""),
             icon: NCUtility.shared.loadImage(named: "square.and.arrow.up"),
+            order: order,
             action: { _ in
                 NCFunctionCenter.shared.openActivityViewController(selectedMetadata: selectedMetadatas)
                 completion?()
@@ -161,7 +167,7 @@ extension NCMenuAction {
     }
 
     /// Save selected files to user's photo library
-    static func saveMediaAction(selectedMediaMetadatas: [tableMetadata], completion: (() -> Void)? = nil) -> NCMenuAction {
+    static func saveMediaAction(selectedMediaMetadatas: [tableMetadata], order: Int = 0, completion: (() -> Void)? = nil) -> NCMenuAction {
         var title: String = NSLocalizedString("_save_selected_files_", comment: "")
         var icon = NCUtility.shared.loadImage(named: "square.and.arrow.down")
         if selectedMediaMetadatas.allSatisfy({ NCManageDatabase.shared.getMetadataLivePhoto(metadata: $0) != nil }) {
@@ -172,6 +178,7 @@ extension NCMenuAction {
         return NCMenuAction(
             title: title,
             icon: icon,
+            order: order,
             action: { _ in
                 for metadata in selectedMediaMetadatas {
                     if let metadataMOV = NCManageDatabase.shared.getMetadataLivePhoto(metadata: metadata) {
@@ -190,10 +197,11 @@ extension NCMenuAction {
     }
 
     /// Set (or remove) a file as *available offline*. Downloads the file if not downloaded already
-    static func setAvailableOfflineAction(selectedMetadatas: [tableMetadata], isAnyOffline: Bool, viewController: UIViewController, completion: (() -> Void)? = nil) -> NCMenuAction {
+    static func setAvailableOfflineAction(selectedMetadatas: [tableMetadata], isAnyOffline: Bool, viewController: UIViewController, order: Int = 0, completion: (() -> Void)? = nil) -> NCMenuAction {
         NCMenuAction(
             title: isAnyOffline ? NSLocalizedString("_remove_available_offline_", comment: "") : NSLocalizedString("_set_available_offline_", comment: ""),
             icon: NCUtility.shared.loadImage(named: "tray.and.arrow.down"),
+            order: order,
             action: { _ in
                 if !isAnyOffline, selectedMetadatas.count > 3 {
                     let alert = UIAlertController(
@@ -215,10 +223,11 @@ extension NCMenuAction {
     }
 
     /// Open view that lets the user move or copy the files within Nextcloud
-    static func moveOrCopyAction(selectedMetadatas: [tableMetadata], completion: (() -> Void)? = nil) -> NCMenuAction {
+    static func moveOrCopyAction(selectedMetadatas: [tableMetadata], order: Int = 0, completion: (() -> Void)? = nil) -> NCMenuAction {
         NCMenuAction(
             title: NSLocalizedString("_move_or_copy_selected_files_", comment: ""),
             icon: NCUtility.shared.loadImage(named: "arrow.up.right.square"),
+            order: order,
             action: { _ in
                 NCFunctionCenter.shared.openSelectView(items: selectedMetadatas)
                 completion?()
@@ -227,10 +236,11 @@ extension NCMenuAction {
     }
 
     /// Open AirPrint view to print a single file
-    static func printAction(metadata: tableMetadata) -> NCMenuAction {
+    static func printAction(metadata: tableMetadata, order: Int = 0) -> NCMenuAction {
         NCMenuAction(
             title: NSLocalizedString("_print_", comment: ""),
             icon: NCUtility.shared.loadImage(named: "printer"),
+            order: order,
             action: { _ in
                 NCFunctionCenter.shared.openDownload(metadata: metadata, selector: NCGlobal.shared.selectorPrint)
             }
@@ -238,7 +248,7 @@ extension NCMenuAction {
     }
 
     /// 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], order: Int = 0, completion: (() -> Void)? = nil) -> NCMenuAction {
         let titleKey: String
         if metadatas.count == 1 {
             titleKey = shouldLock ? "_lock_file_" : "_unlock_file_"
@@ -249,6 +259,7 @@ extension NCMenuAction {
         return NCMenuAction(
             title: NSLocalizedString(titleKey, comment: ""),
             icon: NCUtility.shared.loadImage(named: imageName),
+            order: order,
             action: { _ in
                 for metadata in metadatas where metadata.lock != shouldLock {
                     NCNetworking.shared.lockUnlockFile(metadata, shoulLock: shouldLock)

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

@@ -110,6 +110,7 @@ extension UIViewController {
 
     func presentMenu(with actions: [NCMenuAction]) {
         guard !actions.isEmpty else { return }
+        let actions = actions.sorted(by: { $0.order < $1.order })
         guard let menuViewController = NCMenu.makeNCMenu(with: actions) else {
             let error = NKError(errorCode: NCGlobal.shared.errorInternalError, errorDescription: "_internal_generic_error_")
             NCContentPresenter.shared.showError(error: error)