Browse Source

MediaLibraryModel: Filter cache directly after a user initiated delete

This will allow the UI to be more reactive due to the fact that it
doesn't have to wait for delegate callbacks to update.

Additionally, this could lead to out of bounds crashes due to timining
issues with the callbacks.
Soomin Lee 5 years ago
parent
commit
0e91947783

+ 1 - 0
SharedSources/MediaLibraryModel/CollectionModel.swift

@@ -62,6 +62,7 @@ class CollectionModel: MLBaseModel {
             catch let error as NSError {
                 assertionFailure("CollectionModel: Delete failed: \(error.localizedDescription)")
             }
+            filterFilesFromDeletion(of: items)
         }
     }
 

+ 9 - 0
SharedSources/MediaLibraryModel/MediaLibraryBaseModel.swift

@@ -95,6 +95,15 @@ extension MLBaseModel {
         }
         return newFiles
     }
+
+    func filterFilesFromDeletion(of items: [VLCMLObject]) {
+        files = files.filter() {
+            for item in items where $0.identifier() == item.identifier() {
+                return false
+            }
+            return true
+        }
+    }
 }
 
 extension VLCMLObject {

+ 2 - 0
SharedSources/MediaLibraryModel/MediaModel.swift

@@ -30,6 +30,8 @@ extension MediaModel {
         catch let error as NSError {
             assertionFailure("MediaModel: Delete failed: \(error.localizedDescription)")
         }
+
+        filterFilesFromDeletion(of: items)
     }
 }
 

+ 4 - 0
SharedSources/MediaLibraryModel/PlaylistModel.swift

@@ -45,6 +45,10 @@ class PlaylistModel: MLBaseModel {
                 assertionFailure("PlaylistModel: Failed to delete playlist: \(playlist.identifier())")
             }
         }
+
+        // Update directly the UI without waiting the delegate to avoid showing 'ghost' items
+        filterFilesFromDeletion(of: items)
+        updateView?()
     }
 
     // Creates a VLCMLPlaylist appending it and updates linked view