Forráskód Böngészése

Adapt to nullability changes from VLCMediaLibraryKit 0.2.0

Soomin Lee 6 éve
szülő
commit
af105b460a

+ 5 - 2
SharedSources/MediaLibraryModel/AlbumModel.swift

@@ -39,7 +39,10 @@ class AlbumModel: MLBaseModel {
     }
 
     func createPlaylist(_ name: String, _ fileIndexes: Set<IndexPath>? = nil) {
-        let playlist = medialibrary.createPlaylist(with: name)
+        guard let playlist = medialibrary.createPlaylist(with: name) else {
+            assertionFailure("AlbumModel: createPlaylist: Failed to create a playlist.")
+            return
+        }
 
         guard let fileIndexes = fileIndexes else {
             return
@@ -91,7 +94,7 @@ extension VLCMLAlbum: MediaCollectionModel {
         return nil
     }
 
-    func files() -> [VLCMLMedia] {
+    func files() -> [VLCMLMedia]? {
         return tracks
     }
 }

+ 5 - 2
SharedSources/MediaLibraryModel/ArtistModel.swift

@@ -39,7 +39,10 @@ class ArtistModel: MLBaseModel {
     }
 
     func createPlaylist(_ name: String, _ fileIndexes: Set<IndexPath>? = nil) {
-        let playlist = medialibrary.createPlaylist(with: name)
+        guard let playlist = medialibrary.createPlaylist(with: name) else {
+            assertionFailure("ArtistModel: createPlaylist: Failed to create a playlist.")
+            return
+        }
 
         guard let fileIndexes = fileIndexes else {
             return
@@ -91,7 +94,7 @@ extension VLCMLArtist: MediaCollectionModel {
         return SortModel([.alpha])
     }
 
-    func files() -> [VLCMLMedia] {
+    func files() -> [VLCMLMedia]? {
         return tracks()
     }
 }

+ 2 - 2
SharedSources/MediaLibraryModel/CollectionModel.swift

@@ -20,7 +20,7 @@ class CollectionModel: MLBaseModel {
     required init(mediaService: MediaLibraryService, mediaCollection: MediaCollectionModel) {
         self.medialibrary = mediaService
         self.mediaCollection = mediaCollection
-        files = mediaCollection.files()
+        files = mediaCollection.files() ?? []
         sortModel = mediaCollection.sortModel() ?? SortModel([.default])
         medialibrary.addObserver(self)
     }
@@ -58,7 +58,7 @@ extension CollectionModel: EditableMLModel {
 extension CollectionModel: MediaLibraryObserver {
     func medialibrary(_ medialibrary: MediaLibraryService, didModifyPlaylists playlists: [VLCMLPlaylist]) {
         if mediaCollection is VLCMLPlaylist {
-            files = mediaCollection.files()
+            files = mediaCollection.files() ?? []
             updateView?()
         }
     }

+ 5 - 2
SharedSources/MediaLibraryModel/GenreModel.swift

@@ -39,7 +39,10 @@ class GenreModel: MLBaseModel {
     }
 
     func createPlaylist(_ name: String, _ fileIndexes: Set<IndexPath>? = nil) {
-        let playlist = medialibrary.createPlaylist(with: name)
+        guard let playlist = medialibrary.createPlaylist(with: name) else {
+            assertionFailure("GenreModel: createPlaylist: Failed to create a playlist.")
+            return
+        }
 
         guard let fileIndexes = fileIndexes else {
             return
@@ -103,7 +106,7 @@ extension VLCMLGenre: MediaCollectionModel {
         return SortModel([.alpha])
     }
 
-    func files() -> [VLCMLMedia] {
+    func files() -> [VLCMLMedia]? {
         return tracks()
     }
 }

+ 1 - 1
SharedSources/MediaLibraryModel/MediaLibraryBaseModel.swift

@@ -73,6 +73,6 @@ protocol EditableMLModel {
 }
 
 protocol MediaCollectionModel {
-    func files() -> [VLCMLMedia]
+    func files() -> [VLCMLMedia]?
     func sortModel() -> SortModel?
 }

+ 8 - 3
SharedSources/MediaLibraryModel/MediaModel.swift

@@ -21,7 +21,9 @@ extension MediaModel {
     func delete(_ items: [VLCMLObject]) {
         do {
             for case let media as VLCMLMedia in items {
-                try FileManager.default.removeItem(atPath: media.mainFile().mrl.path)
+                if let mainFile = media.mainFile() {
+                    try FileManager.default.removeItem(atPath: mainFile.mrl.path)
+                }
             }
             medialibrary.reload()
         }
@@ -31,7 +33,10 @@ extension MediaModel {
     }
 
     func createPlaylist(_ name: String, _ fileIndexes: Set<IndexPath>? = nil) {
-        let playlist = medialibrary.createPlaylist(with: name)
+        guard let playlist = medialibrary.createPlaylist(with: name) else {
+            assertionFailure("MediaModel: createPlaylist: Failed to create a playlist.")
+            return
+        }
 
         guard let fileIndexes = fileIndexes else {
             return
@@ -57,7 +62,7 @@ extension VLCMLMedia {
     }
 
     @objc func formatSize() -> String {
-        return ByteCountFormatter.string(fromByteCount: Int64(mainFile().size()),
+        return ByteCountFormatter.string(fromByteCount: Int64(mainFile()?.size() ?? 0),
                                          countStyle: .file)
     }
 

+ 13 - 5
SharedSources/MediaLibraryModel/PlaylistModel.swift

@@ -49,12 +49,19 @@ class PlaylistModel: MLBaseModel {
 
     // Creates a VLCMLPlaylist appending it and updates linked view
     func create(name: String) {
-        append(medialibrary.createPlaylist(with: name))
+        guard let playlist = medialibrary.createPlaylist(with: name) else {
+            assertionFailure("PlaylistModel: create: Failed to create a playlist.")
+            return
+        }
+        append(playlist)
         updateView?()
     }
 
     func createPlaylist(_ name: String, _ fileIndexes: Set<IndexPath>? = nil) {
-        let playlist = medialibrary.createPlaylist(with: name)
+        guard let playlist = medialibrary.createPlaylist(with: name) else {
+            assertionFailure("PlaylistModel: createPlaylist: Failed to create a playlist.")
+            return
+        }
 
         guard let fileIndexes = fileIndexes else {
             return
@@ -113,8 +120,9 @@ extension PlaylistModel: MediaLibraryObserver {
 
 extension VLCMLPlaylist {
     func numberOfTracksString() -> String {
-        let tracksString = media.count == 1 ? NSLocalizedString("TRACK", comment: "") : NSLocalizedString("TRACKS", comment: "")
-        return String(format: tracksString, media.count)
+        let mediaCount = media?.count ?? 0
+        let tracksString = mediaCount > 1 ? NSLocalizedString("TRACKS", comment: "") : NSLocalizedString("TRACK", comment: "")
+        return String(format: tracksString, mediaCount)
     }
 }
 
@@ -123,7 +131,7 @@ extension VLCMLPlaylist: MediaCollectionModel {
         return nil
     }
 
-    func files() -> [VLCMLMedia] {
+    func files() -> [VLCMLMedia]? {
         return media
     }
 }

+ 15 - 12
SharedSources/MediaLibraryService.swift

@@ -207,7 +207,10 @@ private extension MediaLibraryService {
         }
 
         for label in allLabels {
-            let newPlaylist = createPlaylist(with: label.name)
+            guard let newPlaylist = createPlaylist(with: label.name) else {
+                assertionFailure("MediaLibraryService: Migration: Unable to create playlist.")
+                continue
+            }
 
             guard let files = label.files as? Set<MLFile> else {
                 assertionFailure("MediaLibraryService: Migration: Unable to retreive files from label")
@@ -272,18 +275,18 @@ extension MediaLibraryService {
 
     /// Returns number of *ALL* files(audio and video) present in the medialibrary database
     func numberOfFiles() -> Int {
-        var media = medialib.audioFiles(with: .filename, desc: false)
-
-        media += medialib.videoFiles(with: .filename, desc: false)
-        return media.count
+        return (medialib.audioFiles()?.count ?? 0) + (medialib.videoFiles()?.count ?? 0)
     }
 
     /// Returns *ALL* file found for a specified VLCMLMediaType
     ///
     /// - Parameter type: Type of the media
     /// - Returns: Array of VLCMLMedia
-    func media(ofType type: VLCMLMediaType, sortingCriteria sort: VLCMLSortingCriteria = .filename, desc: Bool = false) -> [VLCMLMedia] {
-        return type == .video ? medialib.videoFiles(with: sort, desc: desc) : medialib.audioFiles(with: sort, desc: desc)
+    func media(ofType type: VLCMLMediaType,
+               sortingCriteria sort: VLCMLSortingCriteria = .filename,
+               desc: Bool = false) -> [VLCMLMedia] {
+        return type == .video ? medialib.videoFiles(with: sort, desc: desc) ?? []
+                              : medialib.audioFiles(with: sort, desc: desc) ?? []
     }
 
     func fetchMedia(with mrl: URL?) -> VLCMLMedia? {
@@ -298,11 +301,11 @@ extension MediaLibraryService {
 
 @objc extension MediaLibraryService {
     func artists(sortingCriteria sort: VLCMLSortingCriteria = .artist, desc: Bool = false) -> [VLCMLArtist] {
-        return medialib.artists(with: sort, desc: desc, all: true)
+        return medialib.artists(with: sort, desc: desc, all: true) ?? []
     }
 
     func albums(sortingCriteria sort: VLCMLSortingCriteria = .album, desc: Bool = false) -> [VLCMLAlbum] {
-        return medialib.albums(with: sort, desc: desc)
+        return medialib.albums(with: sort, desc: desc) ?? []
     }
 }
 
@@ -323,7 +326,7 @@ extension MediaLibraryService {
 // MARK: - Playlist methods
 
 @objc extension MediaLibraryService {
-    func createPlaylist(with name: String) -> VLCMLPlaylist {
+    func createPlaylist(with name: String) -> VLCMLPlaylist? {
         return medialib.createPlaylist(withName: name)
     }
 
@@ -332,7 +335,7 @@ extension MediaLibraryService {
     }
 
     func playlists(sortingCriteria sort: VLCMLSortingCriteria = .default, desc: Bool = false) -> [VLCMLPlaylist] {
-        return medialib.playlists(with: sort, desc: desc)
+        return medialib.playlists(with: sort, desc: desc) ?? []
     }
 }
 
@@ -340,7 +343,7 @@ extension MediaLibraryService {
 
 extension MediaLibraryService {
     func genres(sortingCriteria sort: VLCMLSortingCriteria = .default, desc: Bool = false) -> [VLCMLGenre] {
-        return medialib.genres(with: sort, desc: desc)
+        return medialib.genres(with: sort, desc: desc) ?? []
     }
 }
 

+ 2 - 2
Sources/MediaCategoryCells/MediaCollectionViewCell.swift

@@ -55,7 +55,7 @@ class MediaCollectionViewCell: BaseCollectionViewCell {
         thumbnailView.layer.masksToBounds = true
         thumbnailView.layer.cornerRadius = thumbnailView.frame.size.width / 2.0
         titleLabel.text = audiotrack.title
-        descriptionLabel.text = audiotrack.albumTrack.artist.name
+        descriptionLabel.text = audiotrack.albumTrack?.artist?.name ?? ""
         if audiotrack.isThumbnailGenerated() {
             thumbnailView.image = UIImage(contentsOfFile: audiotrack.thumbnail.path)
         }
@@ -64,7 +64,7 @@ class MediaCollectionViewCell: BaseCollectionViewCell {
 
     func update(album: VLCMLAlbum) {
         titleLabel.text = album.title
-        descriptionLabel.text = album.albumArtist != nil ? album.albumArtist.name : ""
+        descriptionLabel.text = album.albumArtist?.name ?? ""
     }
 
     func update(artist: VLCMLArtist) {

+ 1 - 1
Sources/MediaCategoryCells/MovieCollectionViewCell.swift

@@ -68,7 +68,7 @@ class MovieCollectionViewCell: BaseCollectionViewCell {
 
     func update(playlist: VLCMLPlaylist) {
         collectionOverlay.isHidden = false
-        numberOfTracks.text = String(playlist.media.count)
+        numberOfTracks.text = String(playlist.media?.count ?? 0)
         titleLabel.text = playlist.name
         descriptionLabel.text = playlist.numberOfTracksString()
     }

+ 1 - 1
Sources/MediaEditCell.swift

@@ -88,7 +88,7 @@ class MediaEditCell: BaseCollectionViewCell {
 
     func updateForAlbum(album: VLCMLAlbum) {
         titleLabel.text = album.title
-        timeLabel.text = album.albumArtist != nil ? album.albumArtist.name : ""
+        timeLabel.text = album.albumArtist?.name ?? ""
         //TODO: add size, number of tracks, thumbnail
     }
 

+ 8 - 5
Sources/VLCEditController.swift

@@ -170,12 +170,15 @@ extension VLCEditController: VLCEditToolbarDelegate {
         var fileURLS = [URL]()
         for indexPath in selectedCellIndexPaths {
             let file = model.anyfiles[indexPath.row]
-            if let collection = file as? MediaCollectionModel {
-                collection.files().forEach {
-                    fileURLS.append($0.mainFile().mrl)
+            if let collection = file as? MediaCollectionModel,
+                let files = collection.files() {
+                files.forEach {
+                    if let mainFile = $0.mainFile() {
+                        fileURLS.append(mainFile.mrl)
+                    }
                 }
-            } else if let media = file as? VLCMLMedia {
-                fileURLS.append(media.mainFile().mrl)
+            } else if let media = file as? VLCMLMedia, let mainFile = media.mainFile() {
+                fileURLS.append(mainFile.mrl)
             } else {
                 assertionFailure("we're trying to share something that doesn't have an mrl")
                 return fileURLS