소스 검색

EditController: Refactor Add to playlist

Soomin Lee 6 년 전
부모
커밋
6f11135f5f

+ 2 - 0
Resources/en.lproj/Localizable.strings

@@ -323,6 +323,8 @@
 
 "PLAYLIST_PLACEHOLDER" = "Playlist title";
 "PLAYLIST_DESCRIPTION" = "Choose a title for your new playlist";
+"ERROR_PLAYLIST_CREATION" = "Failed to create a playlist";
+"ERROR_PLAYLIST_TRACKS" = "Failed to retreive tracks";
 
 "RENAME_PLACEHOLDER" = "New title";
 "ERROR_RENAME_FAILED" = "Renaming failed";

+ 0 - 23
SharedSources/MediaLibraryModel/AlbumModel.swift

@@ -37,29 +37,6 @@ class AlbumModel: MLBaseModel {
     func delete(_ items: [VLCMLObject]) {
         preconditionFailure("AlbumModel: Albums can not be deleted, they disappear when their last title got deleted")
     }
-
-    func createPlaylist(_ name: String, _ fileIndexes: Set<IndexPath>? = nil) {
-        guard let playlist = medialibrary.createPlaylist(with: name) else {
-            assertionFailure("AlbumModel: createPlaylist: Failed to create a playlist.")
-            return
-        }
-
-        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

+ 0 - 24
SharedSources/MediaLibraryModel/ArtistModel.swift

@@ -37,30 +37,6 @@ class ArtistModel: MLBaseModel {
     func delete(_ items: [VLCMLObject]) {
         preconditionFailure("ArtistModel: Artists can not be deleted, they disappear when their last title got deleted")
     }
-
-    func createPlaylist(_ name: String, _ fileIndexes: Set<IndexPath>? = nil) {
-        guard let playlist = medialibrary.createPlaylist(with: name) else {
-            assertionFailure("ArtistModel: createPlaylist: Failed to create a playlist.")
-            return
-        }
-
-        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
 extension ArtistModel: EditableMLModel {

+ 0 - 4
SharedSources/MediaLibraryModel/CollectionModel.swift

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

+ 0 - 23
SharedSources/MediaLibraryModel/GenreModel.swift

@@ -37,29 +37,6 @@ class GenreModel: MLBaseModel {
     func delete(_ items: [VLCMLObject]) {
         preconditionFailure("GenreModel: Genres can not be deleted, they disappear when their last title got deleted")
     }
-
-    func createPlaylist(_ name: String, _ fileIndexes: Set<IndexPath>? = nil) {
-        guard let playlist = medialibrary.createPlaylist(with: name) else {
-            assertionFailure("GenreModel: createPlaylist: Failed to create a playlist.")
-            return
-        }
-
-        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

+ 0 - 2
SharedSources/MediaLibraryModel/MediaLibraryBaseModel.swift

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

+ 0 - 15
SharedSources/MediaLibraryModel/MediaModel.swift

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

+ 0 - 23
SharedSources/MediaLibraryModel/PlaylistModel.swift

@@ -56,29 +56,6 @@ class PlaylistModel: MLBaseModel {
         append(playlist)
         updateView?()
     }
-
-    func createPlaylist(_ name: String, _ fileIndexes: Set<IndexPath>? = nil) {
-        guard let playlist = medialibrary.createPlaylist(with: name) else {
-            assertionFailure("PlaylistModel: createPlaylist: Failed to create a playlist.")
-            return
-        }
-
-        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

+ 0 - 4
SharedSources/MediaLibraryModel/ShowEpisodeModel.swift

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

+ 39 - 7
Sources/VLCEditController.swift

@@ -95,6 +95,41 @@ private extension VLCEditController {
 
         present(alertController, animated: true, completion: nil)
     }
+
+    private func createPlaylist(_ name: String) {
+        guard let playlist = mediaLibraryService.createPlaylist(with: name) else {
+            assertionFailure("MediaModel: createPlaylist: Failed to create a playlist.")
+            DispatchQueue.main.async {
+                VLCAlertViewController.alertViewManager(title: NSLocalizedString("ERROR_PLAYLIST_CREATION",
+                                                                                 comment: ""),
+                                                        viewController: self)
+            }
+            return
+        }
+
+        // In the case of Video, Tracks
+        if let files = model.anyfiles as? [VLCMLMedia] {
+            for index in selectedCellIndexPaths where index.row < files.count {
+                playlist.appendMedia(withIdentifier: files[index.row].identifier())
+            }
+        } else if let mediaCollectionArray = model.anyfiles as? [MediaCollectionModel] {
+            for index in selectedCellIndexPaths where index.row < mediaCollectionArray.count {
+                guard let tracks = mediaCollectionArray[index.row].files() else {
+                    assertionFailure("EditController: Fail to retreive tracks.")
+                    DispatchQueue.main.async {
+                        VLCAlertViewController.alertViewManager(title: NSLocalizedString("ERROR_PLAYLIST_TRACKS",
+                                                                                         comment: ""),
+                                                                viewController: self)
+                    }
+                    return
+                }
+                tracks.forEach() {
+                    playlist.appendMedia(withIdentifier: $0.identifier())
+                }
+            }
+        }
+        selectedCellIndexPaths.removeAll()
+    }
 }
 
 // MARK: - VLCEditToolbarDelegate
@@ -104,8 +139,7 @@ extension VLCEditController: VLCEditToolbarDelegate {
         let alertInfo = TextFieldAlertInfo(alertTitle: NSLocalizedString("PLAYLISTS", comment: ""),
                                            placeHolder: NSLocalizedString("PLAYLIST_PLACEHOLDER",
                                                                           comment: ""))
-        presentTextFieldAlert(with: alertInfo,
-                              completionHandler: {
+        presentTextFieldAlert(with: alertInfo) {
             [unowned self] text -> Void in
             guard text != "" else {
                 DispatchQueue.main.async {
@@ -115,9 +149,8 @@ extension VLCEditController: VLCEditToolbarDelegate {
                 }
                 return
             }
-            self.model.createPlaylist(text, self.selectedCellIndexPaths)
-            self.selectedCellIndexPaths.removeAll()
-        })
+            self.createPlaylist(text)
+        }
     }
 
     func editToolbarDidAddToPlaylist(_ editToolbar: VLCEditToolbar) {
@@ -293,8 +326,7 @@ extension VLCEditController: AddToPlaylistViewControllerDelegate {
 
     func addToPlaylistViewController(_ addToPlaylistViewController: AddToPlaylistViewController,
                                      newPlaylistWithName name: String) {
-        model.createPlaylist(name, self.selectedCellIndexPaths)
-        selectedCellIndexPaths.removeAll()
+        createPlaylist(name)
         addToPlaylistViewController.dismiss(animated: true, completion: nil)
     }
 }