Browse Source

VLCEditController: Move add to playlist logic into models

Soomin Lee 6 years ago
parent
commit
3da64e92bd

+ 20 - 0
SharedSources/MediaLibraryModel/AlbumModel.swift

@@ -37,6 +37,26 @@ class AlbumModel: MLBaseModel {
     func delete(_ items: [VLCMLObject]) {
     func delete(_ items: [VLCMLObject]) {
         preconditionFailure("AlbumModel: Cannot delete album")
         preconditionFailure("AlbumModel: Cannot delete album")
     }
     }
+
+    func createPlaylist(_ name: String, _ fileIndexes: Set<IndexPath>? = nil) {
+        let playlist = medialibrary.createPlaylist(with: name)
+
+        guard let fileIndexes = fileIndexes else {
+            return
+        }
+
+        for index in fileIndexes  where index.row < files.count {
+            // Get all tracks from a VLCMLAlbum
+            guard let tracks = files[index.row].tracks(with: .default, desc: false) else {
+                assertionFailure("AlbumModel: createPlaylist: Fail to retreive tracks.")
+                return
+            }
+
+            tracks.forEach() {
+                playlist.appendMedia(withIdentifier: $0.identifier())
+            }
+        }
+    }
 }
 }
 
 
 // MARK: - Sort
 // MARK: - Sort

+ 21 - 0
SharedSources/MediaLibraryModel/ArtistModel.swift

@@ -37,6 +37,27 @@ class ArtistModel: MLBaseModel {
     func delete(_ items: [VLCMLObject]) {
     func delete(_ items: [VLCMLObject]) {
         preconditionFailure("ArtistModel: Cannot delete artist")
         preconditionFailure("ArtistModel: Cannot delete artist")
     }
     }
+
+    func createPlaylist(_ name: String, _ fileIndexes: Set<IndexPath>? = nil) {
+        let playlist = medialibrary.createPlaylist(with: name)
+
+        guard let fileIndexes = fileIndexes else {
+            return
+        }
+
+        for index in fileIndexes  where index.row < files.count {
+            // Get all tracks from a VLCMLArtist
+            guard let tracks = files[index.row].tracks(with: .default, desc: false) else {
+                assertionFailure("ArtistModel: createPlaylist: Fail to retreive tracks.")
+                return
+            }
+
+            tracks.forEach() {
+                playlist.appendMedia(withIdentifier: $0.identifier())
+            }
+        }
+    }
+
 }
 }
 // MARK: - Edit
 // MARK: - Edit
 extension ArtistModel: EditableMLModel {
 extension ArtistModel: EditableMLModel {

+ 4 - 0
SharedSources/MediaLibraryModel/CollectionModel.swift

@@ -38,6 +38,10 @@ class CollectionModel: MLBaseModel {
     func delete(_ items: [VLCMLObject]) {
     func delete(_ items: [VLCMLObject]) {
        assertionFailure("still needs implementation")
        assertionFailure("still needs implementation")
     }
     }
+
+    func createPlaylist(_ name: String, _ fileIndexes: Set<IndexPath>?) {
+        assertionFailure("still needs implementation")
+    }
 }
 }
 
 
 // MARK: - Edit
 // MARK: - Edit

+ 20 - 0
SharedSources/MediaLibraryModel/GenreModel.swift

@@ -37,6 +37,26 @@ class GenreModel: MLBaseModel {
     func delete(_ items: [VLCMLObject]) {
     func delete(_ items: [VLCMLObject]) {
         preconditionFailure("GenreModel: Cannot delete genre")
         preconditionFailure("GenreModel: Cannot delete genre")
     }
     }
+
+    func createPlaylist(_ name: String, _ fileIndexes: Set<IndexPath>? = nil) {
+        let playlist = medialibrary.createPlaylist(with: name)
+
+        guard let fileIndexes = fileIndexes else {
+            return
+        }
+
+        for index in fileIndexes  where index.row < files.count {
+            // Get all tracks from a VLCMLGenre
+            guard let tracks = files[index.row].tracks(with: .default, desc: false) else {
+                assertionFailure("GenreModel: createPlaylist: Fail to retreive tracks.")
+                return
+            }
+
+            tracks.forEach() {
+                playlist.appendMedia(withIdentifier: $0.identifier())
+            }
+        }
+    }
 }
 }
 
 
 // MARK: - Sort
 // MARK: - Sort

+ 2 - 0
SharedSources/MediaLibraryModel/MediaLibraryBaseModel.swift

@@ -25,6 +25,7 @@ protocol MediaLibraryBaseModel {
     func append(_ item: VLCMLObject)
     func append(_ item: VLCMLObject)
     func delete(_ items: [VLCMLObject])
     func delete(_ items: [VLCMLObject])
     func sort(by criteria: VLCMLSortingCriteria)
     func sort(by criteria: VLCMLSortingCriteria)
+    func createPlaylist(_ name: String, _ fileIndexes: Set<IndexPath>?)
 }
 }
 
 
 protocol MLBaseModel: AnyObject, MediaLibraryBaseModel {
 protocol MLBaseModel: AnyObject, MediaLibraryBaseModel {
@@ -44,6 +45,7 @@ protocol MLBaseModel: AnyObject, MediaLibraryBaseModel {
     // FIXME: Ideally items should be MLType but Swift isn't happy so it will always fail
     // FIXME: Ideally items should be MLType but Swift isn't happy so it will always fail
     func delete(_ items: [VLCMLObject])
     func delete(_ items: [VLCMLObject])
     func sort(by criteria: VLCMLSortingCriteria)
     func sort(by criteria: VLCMLSortingCriteria)
+    func createPlaylist(_ name: String, _ fileIndexes: Set<IndexPath>?)
 }
 }
 
 
 extension MLBaseModel {
 extension MLBaseModel {

+ 12 - 0
SharedSources/MediaLibraryModel/MediaModel.swift

@@ -29,6 +29,18 @@ extension MediaModel {
             assertionFailure("MediaModel: Delete failed: \(error.localizedDescription)")
             assertionFailure("MediaModel: Delete failed: \(error.localizedDescription)")
         }
         }
     }
     }
+
+    func createPlaylist(_ name: String, _ fileIndexes: Set<IndexPath>? = nil) {
+        let playlist = medialibrary.createPlaylist(with: name)
+
+        guard let fileIndexes = fileIndexes else {
+            return
+        }
+
+        for index in fileIndexes  where index.row < files.count {
+            playlist.appendMedia(withIdentifier: files[index.row].identifier())
+        }
+    }
 }
 }
 
 
 // MARK: - VLCMLMedia
 // MARK: - VLCMLMedia

+ 20 - 0
SharedSources/MediaLibraryModel/PlaylistModel.swift

@@ -52,6 +52,26 @@ class PlaylistModel: MLBaseModel {
         append(medialibrary.createPlaylist(with: name))
         append(medialibrary.createPlaylist(with: name))
         updateView?()
         updateView?()
     }
     }
+
+    func createPlaylist(_ name: String, _ fileIndexes: Set<IndexPath>? = nil) {
+        let playlist = medialibrary.createPlaylist(with: name)
+
+        guard let fileIndexes = fileIndexes else {
+            return
+        }
+
+        for index in fileIndexes  where index.row < files.count {
+            // Get all tracks from a VLCMLPlaylist
+            guard let media = files[index.row].media else {
+                assertionFailure("PlaylistModel: createPlaylist: Failed to retreive media.")
+                return
+            }
+
+            media.forEach() {
+                playlist.appendMedia(withIdentifier: $0.identifier())
+            }
+        }
+    }
 }
 }
 
 
 // MARK: - Sort
 // MARK: - Sort

+ 4 - 0
SharedSources/MediaLibraryModel/ShowEpisodeModel.swift

@@ -36,6 +36,10 @@ class ShowEpisodeModel: MLBaseModel {
     func delete(_ items: [VLCMLObject]) {
     func delete(_ items: [VLCMLObject]) {
         preconditionFailure("ShowEpisodeModel: Cannot delete showEpisode")
         preconditionFailure("ShowEpisodeModel: Cannot delete showEpisode")
     }
     }
+
+    func createPlaylist(_ name: String, _ fileIndexes: Set<IndexPath>? = nil) {
+        assertionFailure("ShowEpisodeModel: createPlaylist: Not integrated.")
+    }
 }
 }
 
 
 // MARK: - Sort
 // MARK: - Sort

+ 10 - 16
Sources/VLCEditController.swift

@@ -93,27 +93,21 @@ private extension VLCEditController {
 // MARK: - VLCEditToolbarDelegate
 // MARK: - VLCEditToolbarDelegate
 
 
 extension VLCEditController: VLCEditToolbarDelegate {
 extension VLCEditController: VLCEditToolbarDelegate {
-
     func editToolbarDidAddToPlaylist(_ editToolbar: VLCEditToolbar) {
     func editToolbarDidAddToPlaylist(_ editToolbar: VLCEditToolbar) {
-        //Todo: replace with Viewcontroller that shows existing Playlists
         let alertInfo = TextFieldAlertInfo(alertTitle: NSLocalizedString("PLAYLISTS", comment: ""),
         let alertInfo = TextFieldAlertInfo(alertTitle: NSLocalizedString("PLAYLISTS", comment: ""),
-                                           alertDescription: NSLocalizedString("PLAYLIST_DESCRIPTION", comment: ""),
-                                           placeHolder: NSLocalizedString("PLAYLIST_PLACEHOLDER", comment:""))
-
+                                           placeHolder: "NEW_PLAYLIST")
         presentTextFieldAlert(with: alertInfo, completionHandler: {
         presentTextFieldAlert(with: alertInfo, completionHandler: {
-            [weak self] text -> Void in
-            guard let strongSelf = self else {
-                return
-            }
-            let playlist = strongSelf.mediaLibraryService.createPlaylist(with: text)
-
-            for indexPath in strongSelf.selectedCellIndexPaths {
-                guard let media = strongSelf.model.anyfiles[indexPath.row] as? VLCMLMedia else {
-                    assertionFailure("we're not handling collections yet")
-                    return
+            [unowned self] text -> Void in
+            guard text != "" else {
+                DispatchQueue.main.async {
+                    VLCAlertViewController.alertViewManager(title: NSLocalizedString("ERROR_EMPTY_NAME",
+                                                                                     comment: ""),
+                                                            viewController: self)
                 }
                 }
-                playlist.appendMedia(withIdentifier: media.identifier())
+                return
             }
             }
+            self.model.createPlaylist(text, self.selectedCellIndexPaths)
+            self.selectedCellIndexPaths.removeAll()
         })
         })
     }
     }