Ver código fonte

MediaModel: Move helper methods to MediaLibraryBaseModel

Soomin Lee 5 anos atrás
pai
commit
72f130bc05

+ 28 - 0
SharedSources/MediaLibraryModel/MediaLibraryBaseModel.swift

@@ -77,3 +77,31 @@ protocol MediaCollectionModel {
     func sortModel() -> SortModel?
     func title() -> String?
 }
+
+// MARK: - Helper methods
+
+extension MLBaseModel {
+    /// Swap the given [MLType] to the cached array.
+    /// This only swaps models with the same VLCMLIdentifiers
+    /// - Parameter models: To be swapped models
+    /// - Returns: New array of `MLType` if changes have been made, else return a unchanged cached version.
+    func swapModels(with models: [MLType]) -> [MLType] {
+        var newFiles = files
+
+        // FIXME: This should be handled in a thread safe way
+        for var model in models {
+            for (currentMediaIndex, file) in files.enumerated()
+                where file.identifier() == model.identifier() {
+                    swap(&newFiles[currentMediaIndex], &model)
+                    break
+            }
+        }
+        return newFiles
+    }
+}
+
+extension VLCMLObject {
+    static func == (lhs: VLCMLObject, rhs: VLCMLObject) -> Bool {
+        return lhs.identifier() == rhs.identifier()
+    }
+}

+ 0 - 28
SharedSources/MediaLibraryModel/MediaModel.swift

@@ -33,34 +33,6 @@ 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()
-    }
-}
-
 // MARK: - ViewModel
 
 extension VLCMLMedia {

+ 1 - 1
SharedSources/MediaLibraryModel/TrackModel.swift

@@ -62,7 +62,7 @@ extension TrackModel: MediaLibraryObserver {
 
     func medialibrary(_ medialibrary: MediaLibraryService, didModifyTracks tracks: [VLCMLMedia]) {
         if !tracks.isEmpty {
-            files = swapMedias(with: tracks)
+            files = swapModels(with: tracks)
             updateView?()
         }
     }

+ 1 - 1
SharedSources/MediaLibraryModel/VideoModel.swift

@@ -62,7 +62,7 @@ extension VideoModel: MediaLibraryObserver {
 
     func medialibrary(_ medialibrary: MediaLibraryService, didModifyVideos videos: [VLCMLMedia]) {
         if !videos.isEmpty {
-            files = swapMedias(with: videos)
+            files = swapModels(with: videos)
             updateView?()
         }
     }