Commit 72f130bc authored by Soomin Lee's avatar Soomin Lee

MediaModel: Move helper methods to MediaLibraryBaseModel

parent 68ad2a70
......@@ -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()
}
}
......@@ -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 {
......
......@@ -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?()
}
}
......
......@@ -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?()
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment