Browse Source

MediaModels: Handle MediaModified

Soomin Lee 6 years ago
parent
commit
a88a93986a

+ 20 - 0
SharedSources/MediaLibraryModel/MediaModel.swift

@@ -35,6 +35,26 @@ extension MediaModel {
 
 // MARK: - Helpers
 
+extension MediaModel {
+    /// Swap the given [VLCMLMedia] to the cached array.
+    /// This only swaps media with the same VLCMLIdentifiers
+    /// - Parameter medias: To be swapped medias
+    /// - Returns: New array of `VLCMLMedia` if changes have been made, else return a unchanged cached version.
+    func swapMedias(with medias: [VLCMLMedia]) -> [VLCMLMedia] {
+        var newFiles = files
+
+        // FIXME: This should be handled in a thread safe way
+        for var media in medias {
+            for (currentMediaIndex, file) in files.enumerated()
+                where file.identifier() == media.identifier() {
+                    swap(&newFiles[currentMediaIndex], &media)
+                    break
+            }
+        }
+        return newFiles
+    }
+}
+
 extension VLCMLMedia {
     static func == (lhs: VLCMLMedia, rhs: VLCMLMedia) -> Bool {
         return lhs.identifier() == rhs.identifier()

+ 7 - 0
SharedSources/MediaLibraryModel/TrackModel.swift

@@ -60,6 +60,13 @@ extension TrackModel: MediaLibraryObserver {
         updateView?()
     }
 
+    func medialibrary(_ medialibrary: MediaLibraryService, didModifyTracks tracks: [VLCMLMedia]) {
+        if !tracks.isEmpty {
+            files = swapMedias(with: tracks)
+            updateView?()
+        }
+    }
+
     func medialibrary(_ medialibrary: MediaLibraryService, didDeleteMediaWithIds ids: [NSNumber]) {
         files = files.filter() {
             for id in ids where $0.identifier() == id.int64Value {

+ 7 - 0
SharedSources/MediaLibraryModel/VideoModel.swift

@@ -61,6 +61,13 @@ extension VideoModel: MediaLibraryObserver {
         updateView?()
     }
 
+    func medialibrary(_ medialibrary: MediaLibraryService, didModifyVideos videos: [VLCMLMedia]) {
+        if !videos.isEmpty {
+            files = swapMedias(with: videos)
+            updateView?()
+        }
+    }
+
     func medialibrary(_ medialibrary: MediaLibraryService, didDeleteMediaWithIds ids: [NSNumber]) {
         files = files.filter() {
             for id in ids where $0.identifier() == id.int64Value {