Sfoglia il codice sorgente

EditController: Rework edit selection logic

This reworks the selection logic by using as much as possible the
UICollectionView API for it.

We do keep a cached version of the selected indexPaths in order to have
persistence. For example:  between view changes.
Soomin Lee 5 anni fa
parent
commit
e1d1cb85f4

+ 33 - 23
Sources/EditController.swift

@@ -15,9 +15,12 @@ protocol EditControllerDelegate: class {
 }
 
 class EditController: UIViewController {
+    // Cache selectedIndexPath separately to indexPathsForSelectedItems in order to have persistance
     private var selectedCellIndexPaths = Set<IndexPath>()
     private let model: MediaLibraryBaseModel
     private let mediaLibraryService: MediaLibraryService
+    private let presentingView: UICollectionView
+
     private lazy var addToPlaylistViewController: AddToPlaylistViewController = {
         var addToPlaylistViewController = AddToPlaylistViewController(playlists: mediaLibraryService.playlists())
         addToPlaylistViewController.delegate = self
@@ -32,9 +35,12 @@ class EditController: UIViewController {
         self.view = editToolbar
     }
 
-    init(mediaLibraryService: MediaLibraryService, model: MediaLibraryBaseModel) {
+    init(mediaLibraryService: MediaLibraryService,
+         model: MediaLibraryBaseModel,
+         presentingView: UICollectionView) {
         self.mediaLibraryService = mediaLibraryService
         self.model = model
+        self.presentingView = presentingView
         super.init(nibName: nil, bundle: nil)
     }
 
@@ -42,7 +48,13 @@ class EditController: UIViewController {
         fatalError("init(coder:) has not been implemented")
     }
 
-    func resetSelections() {
+    func resetSelections(resetUI: Bool) {
+        for indexPath in selectedCellIndexPaths {
+            presentingView.deselectItem(at: indexPath, animated: true)
+            if resetUI {
+                collectionView(presentingView, didDeselectItemAt: indexPath)
+            }
+        }
         selectedCellIndexPaths.removeAll()
     }
 }
@@ -131,7 +143,7 @@ private extension EditController {
                 }
             }
         }
-        selectedCellIndexPaths.removeAll()
+        resetSelections(resetUI: true)
     }
 }
 
@@ -194,6 +206,7 @@ extension EditController: EditToolbarDelegate {
                                           action: {
                                             [weak self] action in
                                             self?.model.delete(objectsToDelete)
+                                            // Update directly the cached indexes since cells will be destroyed
                                             self?.selectedCellIndexPaths.removeAll()
         })
 
@@ -280,8 +293,9 @@ extension EditController: EditToolbarDelegate {
             guard let strongself = self else {
                 return
             }
-            strongself.delegate?.editController(editController: strongself, cellforItemAt: indexPath)?.isChecked = false
-            strongself.selectedCellIndexPaths.remove(indexPath)
+            strongself.presentingView.deselectItem(at: indexPath, animated: true)
+            strongself.collectionView(strongself.presentingView, didDeselectItemAt: indexPath)
+
             //call until all items are renamed
             if !strongself.selectedCellIndexPaths.isEmpty {
                 strongself.editToolbarDidRename(editToolbar)
@@ -290,6 +304,18 @@ extension EditController: EditToolbarDelegate {
     }
 }
 
+// MARK: - UICollectionViewDelegate
+
+extension EditController: UICollectionViewDelegate {
+    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
+        selectedCellIndexPaths.insert(indexPath)
+    }
+
+    func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
+        selectedCellIndexPaths.remove(indexPath)
+    }
+}
+
 // MARK: - UICollectionViewDataSource
 
 extension EditController: UICollectionViewDataSource {
@@ -305,7 +331,7 @@ extension EditController: UICollectionViewDataSource {
         if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: editCell.defaultReuseIdentifier,
                                                          for: indexPath) as? MediaEditCell {
             cell.media = model.anyfiles[indexPath.row]
-            cell.isChecked = selectedCellIndexPaths.contains(indexPath)
+            cell.isSelected = selectedCellIndexPaths.contains(indexPath)
             cell.isAccessibilityElement = true
             if let collectionModel = model as? CollectionModel, collectionModel.mediaCollection is VLCMLPlaylist {
                 cell.dragImage.isHidden = false
@@ -333,22 +359,6 @@ extension EditController: UICollectionViewDataSource {
     }
 }
 
-// MARK: - UICollectionViewDelegate
-
-extension EditController: UICollectionViewDelegate {
-    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
-        if let cell = collectionView.cellForItem(at: indexPath) as? MediaEditCell {
-            cell.isChecked = !cell.isChecked
-            if cell.isChecked {
-                // cell selected, saving indexPath
-                selectedCellIndexPaths.insert(indexPath)
-            } else {
-                selectedCellIndexPaths.remove(indexPath)
-            }
-        }
-    }
-}
-
 // MARK: - UICollectionViewDelegateFlowLayout
 
 extension EditController: UICollectionViewDelegateFlowLayout {
@@ -385,7 +395,7 @@ extension EditController: AddToPlaylistViewControllerDelegate {
                 assertionFailure("EditController: AddToPlaylistViewControllerDelegate: Failed to add item.")
             }
         }
-        resetSelections()
+        resetSelections(resetUI: false)
         addToPlaylistViewController.dismiss(animated: true, completion: nil)
     }
 

+ 5 - 2
Sources/MediaCategories/MediaCategoryViewController.swift

@@ -31,7 +31,9 @@ class MediaCategoryViewController: UICollectionViewController, UISearchBarDelega
     private let searchBarSize: CGFloat = 50.0
     private var rendererButton: UIButton
     private lazy var editController: EditController = {
-        let editController = EditController(mediaLibraryService:services.medialibraryService, model: model)
+        let editController = EditController(mediaLibraryService:services.medialibraryService,
+                                            model: model,
+                                            presentingView: collectionView)
         editController.delegate = self
         return editController
     }()
@@ -294,7 +296,7 @@ class MediaCategoryViewController: UICollectionViewController, UISearchBarDelega
         collectionView?.dataSource = editing ? editController : self
         collectionView?.delegate = editing ? editController : self
 
-        editController.resetSelections()
+        editController.resetSelections(resetUI: true)
         displayEditToolbar()
 
         PlaybackService.sharedInstance().setPlayerHidden(editing)
@@ -600,6 +602,7 @@ private extension MediaCategoryViewController {
             let editCellNib = UINib(nibName: editCell.nibName, bundle: nil)
             collectionView?.register(editCellNib, forCellWithReuseIdentifier: editCell.defaultReuseIdentifier)
         }
+        collectionView.allowsMultipleSelection = true
         collectionView?.backgroundColor = PresentationTheme.current.colors.background
         collectionView?.alwaysBounceVertical = true
         longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(self.handleLongGesture(gesture:)))

+ 5 - 4
Sources/MediaEditCell.swift

@@ -132,9 +132,10 @@ class MediaEditCell: BaseCollectionViewCell {
         thumbnailImageView.image = videoGroup.thumbnail()
     }
 
-    var isChecked: Bool = false {
-         didSet {
-            checkboxImageView.image = isChecked ? UIImage(named: "checkboxSelected") : UIImage(named: "checkboxEmpty")
+    override var isSelected: Bool {
+        didSet {
+            checkboxImageView.image = isSelected ? UIImage(named: "checkboxSelected")
+                : UIImage(named: "checkboxEmpty")
         }
     }
 
@@ -169,7 +170,7 @@ class MediaEditCell: BaseCollectionViewCell {
         sizeLabel.text = ""
         accessibilityLabel = ""
         thumbnailImageView.image = nil
-        isChecked = false
+        isSelected = false
         thumbnailImageView.layer.cornerRadius = 0
         AudioAspectRatio.isActive = true
         VideoAspectRatio.isActive = false