Ver código fonte

EditToolbar: Refactor button into more generic EditButtons

Edgar Fouillet 5 anos atrás
pai
commit
ffdbde1492

+ 95 - 0
Sources/EditButtons.swift

@@ -0,0 +1,95 @@
+/*****************************************************************************
+* EditButtons.swift
+*
+* Copyright © 2019 VLC authors and VideoLAN
+*
+* Authors: Edgar Fouillet <vlc # edgar.fouillet.eu>
+*
+* Refer to the COPYING file of the official project for license.
+*****************************************************************************/
+
+enum EditButtonType {
+    case addToPlaylist
+    case rename
+    case delete
+    case share
+}
+
+class EditButton {
+    var identifier: EditButtonType
+    var title: String
+    var image: String
+    var accessibilityLabel: String
+    var accessibilityHint: String
+
+    init(identifier: EditButtonType, title: String, image: String, accessibilityLabel: String, accessibilityHint: String) {
+        self.identifier = identifier
+        self.title = title
+        self.image = image
+        self.accessibilityLabel = accessibilityLabel
+        self.accessibilityHint = accessibilityHint
+    }
+
+    func button(_ selector: Selector) -> UIButton {
+        let generatedButton = UIButton(type: .system)
+        generatedButton.setImage(UIImage(named: image), for: .normal)
+        generatedButton.contentHorizontalAlignment = .left
+        generatedButton.addTarget(self, action: selector, for: .touchUpInside)
+        generatedButton.widthAnchor.constraint(equalToConstant: 44).isActive = true
+        generatedButton.tintColor = .orange
+        generatedButton.accessibilityLabel = accessibilityLabel
+        generatedButton.accessibilityHint = accessibilityHint
+        return generatedButton
+    }
+}
+
+class EditButtonsFactory {
+    static func buttonList(for file: VLCMLObject?) -> [EditButtonType] {
+        var actionList = [EditButtonType]()
+
+        if let file = file {
+            actionList.append(.addToPlaylist)
+            if !(file is VLCMLArtist) && !(file is VLCMLGenre) && !(file is VLCMLAlbum) && !(file is VLCMLVideoGroup) {
+                actionList.append(.rename)
+            }
+            if !(file is VLCMLVideoGroup) {
+                actionList.append(.delete)
+            }
+            actionList.append(.share)
+        }
+        return actionList
+    }
+
+    static func generate(buttons: [EditButtonType]) -> [EditButton] {
+        var editButtons = [EditButton]()
+        for button in buttons {
+            switch button {
+                case .addToPlaylist:
+                    editButtons.append(EditButton(identifier: button,
+                                                  title: NSLocalizedString("ADD_TO_PLAYLIST", comment: ""),
+                                                  image: "addToPlaylist",
+                                                  accessibilityLabel: NSLocalizedString("ADD_TO_PLAYLIST", comment: ""),
+                                                  accessibilityHint: NSLocalizedString("ADD_TO_PLAYLIST_HINT", comment: "")))
+                case .rename:
+                    editButtons.append(EditButton(identifier: button,
+                                                  title: NSLocalizedString("BUTTON_RENAME", comment: ""),
+                                                  image: "rename",
+                                                  accessibilityLabel: NSLocalizedString("BUTTON_RENAME", comment: ""),
+                                                  accessibilityHint: NSLocalizedString("RENAME_HINT", comment: "")))
+                case .delete:
+                    editButtons.append(EditButton(identifier: button,
+                                                  title: NSLocalizedString("BUTTON_DELETE", comment: ""),
+                                                  image: "delete",
+                                                  accessibilityLabel: NSLocalizedString("BUTTON_DELETE", comment: ""),
+                                                  accessibilityHint: NSLocalizedString("DELETE_HINT", comment: "")))
+                case .share:
+                    editButtons.append(EditButton(identifier: button,
+                                                  title: NSLocalizedString("SHARE_LABEL", comment: ""),
+                                                  image: "share",
+                                                  accessibilityLabel: NSLocalizedString("SHARE_LABEL", comment: ""),
+                                                  accessibilityHint: NSLocalizedString("SHARE_HINT", comment: "")))
+            }
+        }
+        return editButtons
+    }
+}

+ 1 - 1
Sources/EditController.swift

@@ -169,7 +169,7 @@ extension EditController: EditToolbarDelegate {
         })
     }
 
-    func editToolbarDidShare(_ editToolbar: EditToolbar, presentFrom button: UIButton) {
+    func editToolbarDidShare(_ editToolbar: EditToolbar) {
         guard !selectedCellIndexPaths.isEmpty else {
             assertionFailure("EditController: Share called without selection")
             return

+ 20 - 45
Sources/EditToolbar.swift

@@ -13,7 +13,7 @@ protocol EditToolbarDelegate: class {
     func editToolbarDidDelete(_ editToolbar: EditToolbar)
     func editToolbarDidAddToPlaylist(_ editToolbar: EditToolbar)
     func editToolbarDidRename(_ editToolbar: EditToolbar)
-    func editToolbarDidShare(_ editToolbar: EditToolbar, presentFrom button: UIButton)
+    func editToolbarDidShare(_ editToolbar: EditToolbar)
 }
 
 class EditToolbar: UIView {
@@ -38,39 +38,6 @@ class EditToolbar: UIView {
         return rightStackView
     }()
 
-    private lazy var shareButton: UIButton = {
-        let shareButton = UIButton(type: .system)
-        shareButton.addTarget(self, action: #selector(share), for: .touchUpInside)
-        shareButton.setImage(UIImage(named: "share"), for: .normal)
-        shareButton.tintColor = .orange
-        shareButton.widthAnchor.constraint(equalToConstant: 44).isActive = true
-        shareButton.accessibilityLabel = NSLocalizedString("SHARE_LABEL", comment: "")
-        shareButton.accessibilityHint = NSLocalizedString("SHARE_HINT", comment: "")
-        return shareButton
-    }()
-
-    private lazy var renameButton: UIButton = {
-        let renameButton = UIButton(type: .system)
-        renameButton.addTarget(self, action: #selector(rename), for: .touchUpInside)
-        renameButton.setImage(UIImage(named: "rename"), for: .normal)
-        renameButton.tintColor = .orange
-        renameButton.widthAnchor.constraint(equalToConstant: 44).isActive = true
-        renameButton.accessibilityLabel = NSLocalizedString("BUTTON_RENAME", comment: "")
-        renameButton.accessibilityHint = NSLocalizedString("RENAME_HINT", comment: "")
-        return renameButton
-    }()
-
-    private lazy var deleteButton: UIButton = {
-        let deleteButton = UIButton(type: .system)
-        deleteButton.addTarget(self, action: #selector(deleteSelection), for: .touchUpInside)
-        deleteButton.setImage(UIImage(named: "delete"), for: .normal)
-        deleteButton.tintColor = .orange
-        deleteButton.widthAnchor.constraint(equalToConstant: 44).isActive = true
-        deleteButton.accessibilityLabel = NSLocalizedString("BUTTON_DELETE", comment: "")
-        deleteButton.accessibilityHint = NSLocalizedString("DELETE_HINT", comment: "")
-        return deleteButton
-    }()
-
     private var addToPlaylistButton: UIButton = {
         let addToPlaylistButton = UIButton(type: .system)
         addToPlaylistButton.setTitle(NSLocalizedString("ADD_TO_PLAYLIST", comment: ""), for: .normal)
@@ -96,22 +63,30 @@ class EditToolbar: UIView {
     }
 
     @objc func share() {
-        delegate?.editToolbarDidShare(self, presentFrom: shareButton)
+        delegate?.editToolbarDidShare(self)
     }
 
     private func setupRightStackView() {
-        let file = model.anyfiles.first
-
-        if !(file is VLCMLArtist) && !(file is VLCMLGenre) && !(file is VLCMLAlbum)
-            && !(file is VLCMLVideoGroup) {
-            rightStackView.addArrangedSubview(renameButton)
+        var buttonList = EditButtonsFactory.buttonList(for: model.anyfiles.first)
+        // For now we remove the first button which is Add to playlist since it is not in the same group
+        if buttonList.contains(.addToPlaylist) {
+            if let index = buttonList.firstIndex(of: .addToPlaylist) {
+                buttonList.remove(at: index)
+            }
         }
-
-        if  !(file is VLCMLVideoGroup) {
-            rightStackView.addArrangedSubview(deleteButton)
+        let buttons = EditButtonsFactory.generate(buttons: buttonList)
+        for button in buttons {
+            switch button.identifier {
+                case .addToPlaylist:
+                    rightStackView.addArrangedSubview(button.button(#selector(addToPlaylist)))
+                case .rename:
+                    rightStackView.addArrangedSubview(button.button(#selector(rename)))
+                case .delete:
+                    rightStackView.addArrangedSubview(button.button(#selector(deleteSelection)))
+                case .share:
+                    rightStackView.addArrangedSubview(button.button(#selector(share)))
+            }
         }
-
-        rightStackView.addArrangedSubview(shareButton)
     }
 
     private func setupStackView() {

+ 4 - 0
VLC.xcodeproj/project.pbxproj

@@ -88,6 +88,7 @@
 		6D220D0D234B8E3700BD694F /* FileManager+DeleteMediaFolder.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6D220D0C234B8E3700BD694F /* FileManager+DeleteMediaFolder.swift */; };
 		6D4756B123607D4A005F670E /* EditActions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6D4756B023607D49005F670E /* EditActions.swift */; };
 		6D8E642A234CBF2200EBC8FC /* AudioCollectionModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6D8E6429234CBF2200EBC8FC /* AudioCollectionModel.swift */; };
+		6DBCE3CD2368811000BCA7D5 /* EditButtons.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6DBCE3CC2368811000BCA7D5 /* EditButtons.swift */; };
 		7AC8629D1765DC560011611A /* style.css in Resources */ = {isa = PBXBuildFile; fileRef = 7AC8629B1765DC560011611A /* style.css */; };
 		7AC862A61765E9510011611A /* jquery-1.10.1.min.js in Resources */ = {isa = PBXBuildFile; fileRef = 7AC8629E1765E90C0011611A /* jquery-1.10.1.min.js */; };
 		7AC862A71765E9510011611A /* jquery.fileupload.js in Resources */ = {isa = PBXBuildFile; fileRef = 7AC8629F1765E90C0011611A /* jquery.fileupload.js */; };
@@ -569,6 +570,7 @@
 		6D220D0C234B8E3700BD694F /* FileManager+DeleteMediaFolder.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = "FileManager+DeleteMediaFolder.swift"; path = "Sources/FileManager+DeleteMediaFolder.swift"; sourceTree = "<group>"; };
 		6D4756B023607D49005F670E /* EditActions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = EditActions.swift; path = Sources/EditActions.swift; sourceTree = "<group>"; };
 		6D8E6429234CBF2200EBC8FC /* AudioCollectionModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AudioCollectionModel.swift; sourceTree = "<group>"; };
+		6DBCE3CC2368811000BCA7D5 /* EditButtons.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = EditButtons.swift; path = Sources/EditButtons.swift; sourceTree = "<group>"; };
 		703A80CCC005093CCDFBECBF /* Pods-VLC-iOS-Screenshots.distribution.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-VLC-iOS-Screenshots.distribution.xcconfig"; path = "Pods/Target Support Files/Pods-VLC-iOS-Screenshots/Pods-VLC-iOS-Screenshots.distribution.xcconfig"; sourceTree = "<group>"; };
 		7AC8629B1765DC560011611A /* style.css */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.css; path = style.css; sourceTree = "<group>"; };
 		7AC8629E1765E90C0011611A /* jquery-1.10.1.min.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = "jquery-1.10.1.min.js"; sourceTree = "<group>"; };
@@ -1406,6 +1408,7 @@
 				41D7DD2620C3060300AD94F6 /* PagerStripViewController.swift */,
 				4170AF9622848DCB008881BA /* LibrarySearchDataSource.swift */,
 				6D4756B023607D49005F670E /* EditActions.swift */,
+				6DBCE3CC2368811000BCA7D5 /* EditButtons.swift */,
 			);
 			name = "Everything Playlist";
 			sourceTree = "<group>";
@@ -3075,6 +3078,7 @@
 				E08AD1712304DDE800E023B9 /* MediaPlayerActionSheet.swift in Sources */,
 				DD3EFF2D1BDEBCE500B68579 /* VLCLocalNetworkServiceBrowserFTP.m in Sources */,
 				E021B63622FB80360041CB7A /* MediaMoreOptionsActionSheet.swift in Sources */,
+				6DBCE3CD2368811000BCA7D5 /* EditButtons.swift in Sources */,
 				7D30F3C3183AB24C00FFC021 /* VLCHTTPFileDownloader.m in Sources */,
 				41273A3C1A955C4100A2EF77 /* VLCMigrationViewController.m in Sources */,
 				4144156A20ECD2620078EC37 /* VLCSectionTableHeaderView.swift in Sources */,