Browse Source

Playlist: Add reordering

This adds a longpressgesture recognizer, only for playlists we enable the ability to move cells
we added  the notification for playlists modified and propogate that to the collectionmodel
Since there seems to be a bug in Medialibrary which doesn't seem to return the correct playlist we just update all playlists
Carola Nitz 6 years ago
parent
commit
f64b2ba7e5

+ 11 - 0
SharedSources/MediaLibraryModel/CollectionModel.swift

@@ -22,6 +22,7 @@ class CollectionModel: MLBaseModel {
         self.mediaCollection = mediaCollection
         files = mediaCollection.files()
         sortModel = mediaCollection.sortModel() ?? SortModel([.default])
+        medialibrary.addObserver(self)
     }
 
     func append(_ item: VLCMLMedia) {
@@ -53,3 +54,13 @@ extension CollectionModel: EditableMLModel {
     }
 }
 
+// MARK: - MediaLibraryObserver
+extension CollectionModel: MediaLibraryObserver {
+    func medialibrary(_ medialibrary: MediaLibraryService, didModifyPlaylists playlists: [VLCMLPlaylist]) {
+        if mediaCollection is VLCMLPlaylist {
+            files = mediaCollection.files()
+            updateView?()
+        }
+    }
+}
+

+ 9 - 0
SharedSources/MediaLibraryService.swift

@@ -67,6 +67,9 @@ extension NSNotification {
                                      didAddPlaylists playlists: [VLCMLPlaylist])
 
     @objc optional func medialibrary(_ medialibrary: MediaLibraryService,
+                                     didModifyPlaylists playlists: [VLCMLPlaylist])
+
+    @objc optional func medialibrary(_ medialibrary: MediaLibraryService,
                                      didDeletePlaylistsWithIds playlistsIds: [NSNumber])
 }
 
@@ -448,6 +451,12 @@ extension MediaLibraryService {
         }
     }
 
+    func medialibrary(_ medialibrary: VLCMediaLibrary, didModifyPlaylists playlists: [VLCMLPlaylist]) {
+        for observer in observers {
+            observer.value.observer?.medialibrary?(self, didModifyPlaylists: playlists)
+        }
+    }
+
     func medialibrary(_ medialibrary: VLCMediaLibrary, didDeletePlaylistsWithIds playlistsIds: [NSNumber]) {
         for observer in observers {
             observer.value.observer?.medialibrary?(self, didDeletePlaylistsWithIds: playlistsIds)

+ 21 - 0
Sources/MediaCategories/MediaCategoryViewController.swift

@@ -29,6 +29,7 @@ class VLCMediaCategoryViewController: UICollectionViewController, UICollectionVi
 
     private var editToolbarConstraint: NSLayoutConstraint?
     private var cachedCellSize = CGSize.zero
+    private var longPressGesture: UILongPressGestureRecognizer!
 
 //    @available(iOS 11.0, *)
 //    lazy var dragAndDropManager: VLCDragAndDropManager = { () -> VLCDragAndDropManager<T> in
@@ -337,6 +338,9 @@ private extension VLCMediaCategoryViewController {
         }
         collectionView?.backgroundColor = PresentationTheme.current.colors.background
         collectionView?.alwaysBounceVertical = true
+
+        longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(self.handleLongGesture(gesture:)))
+        collectionView?.addGestureRecognizer(longPressGesture)
         if #available(iOS 11.0, *) {
             collectionView?.contentInsetAdjustmentBehavior = .always
             //            collectionView?.dragDelegate = dragAndDropManager
@@ -357,6 +361,23 @@ private extension VLCMediaCategoryViewController {
             }
         }
     }
+
+    @objc func handleLongGesture(gesture: UILongPressGestureRecognizer) {
+
+        switch gesture.state {
+        case .began:
+            guard let selectedIndexPath = collectionView.indexPathForItem(at: gesture.location(in: collectionView)) else {
+                break
+            }
+            collectionView.beginInteractiveMovementForItem(at: selectedIndexPath)
+        case .changed:
+            collectionView.updateInteractiveMovementTargetPosition(gesture.location(in: gesture.view!))
+        case .ended:
+            collectionView.endInteractiveMovement()
+        default:
+            collectionView.cancelInteractiveMovement()
+        }
+    }
 }
 
 // MARK: - Player

+ 15 - 0
Sources/VLCEditController.swift

@@ -232,6 +232,21 @@ extension VLCEditController: UICollectionViewDataSource {
             return UICollectionViewCell()
         }
     }
+
+    func collectionView(_ collectionView: UICollectionView, moveItemAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
+        guard let collectionModel = model as? CollectionModel, let playlist = collectionModel.mediaCollection as? VLCMLPlaylist else {
+            assertionFailure("can Move should've been false")
+            return
+        }
+        playlist.moveMedia(fromPosition: UInt32(sourceIndexPath.row), toDestination: UInt32(destinationIndexPath.row))
+    }
+
+    func collectionView(_ collectionView: UICollectionView, canMoveItemAt indexPath: IndexPath) -> Bool {
+        if let collectionModel = model as? CollectionModel, collectionModel.mediaCollection is VLCMLPlaylist {
+            return true
+        }
+        return false
+    }
 }
 
 // MARK: - UICollectionViewDelegate