[vlc-commits] [Git][videolan/vlc][master] macosx: Unify looping over mediaItems

Rémi Denis-Courmont (@Courmisch) gitlab at videolan.org
Thu Jul 7 20:14:30 UTC 2022



Rémi Denis-Courmont pushed to branch master at VideoLAN / VLC


Commits:
4d9fdbbf by Claudio Cambra at 2022-07-07T20:14:19+00:00
macosx: Unify looping over mediaItems

- - - - -


5 changed files:

- modules/gui/macosx/library/VLCLibraryCollectionViewAlbumItem.m
- modules/gui/macosx/library/VLCLibraryCollectionViewArtistItem.m
- modules/gui/macosx/library/VLCLibraryDataTypes.h
- modules/gui/macosx/library/VLCLibraryDataTypes.m
- modules/gui/macosx/library/VLCLibraryMenuController.m


Changes:

=====================================
modules/gui/macosx/library/VLCLibraryCollectionViewAlbumItem.m
=====================================
@@ -198,14 +198,14 @@ NSString *VLCLibraryAlbumCellIdentifier = @"VLCLibraryAlbumCellIdentifier";
 
     // We want to add all the tracks to the playlist but only play the first one immediately,
     // otherwise we will skip straight to the last track of the album
-    BOOL playImmediately = YES;
-    for(VLCMediaLibraryMediaItem* mediaItem in _representedAlbum.tracksAsMediaItems) {
+    __block BOOL playImmediately = YES;
+    [_representedAlbum iterateMediaItemsWithBlock:^(VLCMediaLibraryMediaItem* mediaItem) {
         [_libraryController appendItemToPlaylist:mediaItem playImmediately:playImmediately];
 
         if(playImmediately) {
             playImmediately = NO;
         }
-    }
+    }];
 }
 
 - (IBAction)addToPlaylist:(id)sender
@@ -214,9 +214,9 @@ NSString *VLCLibraryAlbumCellIdentifier = @"VLCLibraryAlbumCellIdentifier";
         _libraryController = [[VLCMain sharedInstance] libraryController];
     }
 
-    for(VLCMediaLibraryMediaItem* mediaItem in _representedAlbum.tracksAsMediaItems) {
+    [_representedAlbum iterateMediaItemsWithBlock:^(VLCMediaLibraryMediaItem* mediaItem) {
         [_libraryController appendItemToPlaylist:mediaItem playImmediately:NO];
-    }
+    }];
 }
 
 -(void)mouseDown:(NSEvent *)theEvent


=====================================
modules/gui/macosx/library/VLCLibraryCollectionViewArtistItem.m
=====================================
@@ -211,16 +211,14 @@ NSString *VLCLibraryArtistCellIdentifier = @"VLCLibraryArtistCellIdentifier";
 
     // We want to add all the tracks to the playlist but only play the first one immediately,
     // otherwise we will skip straight to the last track of the last album from the artist
-    BOOL playImmediately = YES;
-    for(VLCMediaLibraryAlbum* album in _representedArtist.albums) {
-        for(VLCMediaLibraryMediaItem* mediaItem in album.tracksAsMediaItems) {
-            [_libraryController appendItemToPlaylist:mediaItem playImmediately:playImmediately];
-
-            if(playImmediately) {
-                playImmediately = NO;
-            }
+    __block BOOL playImmediately = YES;
+    [_representedArtist iterateMediaItemsWithBlock:^(VLCMediaLibraryMediaItem* mediaItem) {
+        [_libraryController appendItemToPlaylist:mediaItem playImmediately:playImmediately];
+
+        if(playImmediately) {
+            playImmediately = NO;
         }
-    }
+    }];
 }
 
 - (IBAction)addToPlaylist:(id)sender
@@ -229,11 +227,9 @@ NSString *VLCLibraryArtistCellIdentifier = @"VLCLibraryArtistCellIdentifier";
         _libraryController = [[VLCMain sharedInstance] libraryController];
     }
 
-    for(VLCMediaLibraryAlbum* album in _representedArtist.albums) {
-        for(VLCMediaLibraryMediaItem* mediaItem in album.tracksAsMediaItems) {
-            [_libraryController appendItemToPlaylist:mediaItem playImmediately:NO];
-        }
-    }
+    [_representedArtist iterateMediaItemsWithBlock:^(VLCMediaLibraryMediaItem* mediaItem) {
+        [_libraryController appendItemToPlaylist:mediaItem playImmediately:NO];
+    }];
 }
 
 -(void)mouseDown:(NSEvent *)theEvent


=====================================
modules/gui/macosx/library/VLCLibraryDataTypes.h
=====================================
@@ -99,6 +99,7 @@ extern const long long int VLCMediaLibraryMediaItemDurationDenominator;
 
 + (nullable instancetype)artistWithID:(int64_t)artistID;
 - (instancetype)initWithArtist:(struct vlc_ml_artist_t *)p_artist;
+- (void)iterateMediaItemsWithBlock:(void (^)(VLCMediaLibraryMediaItem*))mediaItemBlock;
 
 @property (readonly) int64_t artistID;
 @property (readonly) NSString *name;
@@ -114,6 +115,7 @@ extern const long long int VLCMediaLibraryMediaItemDurationDenominator;
 @interface VLCMediaLibraryAlbum : NSObject
 
 - (instancetype)initWithAlbum:(struct vlc_ml_album_t *)p_album;
+- (void)iterateMediaItemsWithBlock:(void (^)(VLCMediaLibraryMediaItem*))mediaItemBlock;
 
 @property (readonly) int64_t albumID;
 @property (readonly) NSString *title;


=====================================
modules/gui/macosx/library/VLCLibraryDataTypes.m
=====================================
@@ -233,6 +233,14 @@ NSString *VLCMediaLibraryMediaItemLibraryID = @"VLCMediaLibraryMediaItemLibraryI
     return [mutableArray copy];
 }
 
+- (void)iterateMediaItemsWithBlock:(void (^)(VLCMediaLibraryMediaItem*))mediaItemBlock;
+{
+    for(VLCMediaLibraryAlbum* album in self.albums) {
+        [album iterateMediaItemsWithBlock:mediaItemBlock];
+    }
+}
+
+
 @end
 
 @implementation VLCMediaLibraryAlbum
@@ -274,6 +282,13 @@ NSString *VLCMediaLibraryMediaItemLibraryID = @"VLCMediaLibraryMediaItemLibraryI
     return [mutableArray copy];
 }
 
+- (void)iterateMediaItemsWithBlock:(void (^)(VLCMediaLibraryMediaItem*))mediaItemBlock
+{
+    for(VLCMediaLibraryMediaItem* mediaItem in self.tracksAsMediaItems) {
+        mediaItemBlock(mediaItem);
+    }
+}
+
 @end
 
 @implementation VLCMediaLibraryGenre


=====================================
modules/gui/macosx/library/VLCLibraryMenuController.m
=====================================
@@ -85,29 +85,26 @@
 }
 
 #pragma mark - actions
-
 - (void)addToPlaylist:(BOOL)playImmediately
 {
+    void (^mediaItemPlaylistHandler)(VLCMediaLibraryMediaItem*) = ^(VLCMediaLibraryMediaItem* mediaItem) {
+        [[[VLCMain sharedInstance] libraryController] appendItemToPlaylist:mediaItem playImmediately:playImmediately];
+    };
+
     switch(_currentRepresentedType) {
         case VLC_ML_PARENT_ARTIST:
         {
-            for(VLCMediaLibraryAlbum* album in _representedArtist.albums) {
-                for(VLCMediaLibraryMediaItem* mediaItem in album.tracksAsMediaItems) {
-                    [[[VLCMain sharedInstance] libraryController] appendItemToPlaylist:mediaItem playImmediately:playImmediately];
-                }
-            }
+            [_representedArtist iterateMediaItemsWithBlock:mediaItemPlaylistHandler];
             break;
         }
         case VLC_ML_PARENT_ALBUM:
         {
-            for(VLCMediaLibraryMediaItem* mediaItem in _representedAlbum.tracksAsMediaItems) {
-                [[[VLCMain sharedInstance] libraryController] appendItemToPlaylist:mediaItem playImmediately:playImmediately];
-            }
+            [_representedAlbum iterateMediaItemsWithBlock:mediaItemPlaylistHandler];
             break;
         }
         case VLC_ML_PARENT_UNKNOWN:
         {
-            [[[VLCMain sharedInstance] libraryController] appendItemToPlaylist:_representedMediaItem playImmediately:playImmediately];
+            mediaItemPlaylistHandler(_representedMediaItem);
             break;
         }
         default:
@@ -167,46 +164,34 @@
 
 - (void)moveToTrash:(id)sender
 {
-    NSArray *filesToTrash;
+    NSFileManager *fileManager = [NSFileManager defaultManager];
+
+    void (^mediaItemTrashHandler)(VLCMediaLibraryMediaItem*) = ^(VLCMediaLibraryMediaItem* mediaItem) {
+        for (VLCMediaLibraryFile *fileToTrash in mediaItem.files) {
+            [fileManager trashItemAtURL:fileToTrash.fileURL resultingItemURL:nil error:nil];
+        }
+    };
 
     switch(_currentRepresentedType) {
         case VLC_ML_PARENT_ARTIST:
         {
-            NSMutableArray *allMediaItemFiles = [[NSMutableArray alloc] init];
-            for(VLCMediaLibraryAlbum* album in _representedArtist.albums) {
-                for(VLCMediaLibraryMediaItem* mediaItem in album.tracksAsMediaItems) {
-                    [allMediaItemFiles addObjectsFromArray: mediaItem.files];
-                }
-            }
-
-            filesToTrash = [allMediaItemFiles copy];
+            [_representedArtist iterateMediaItemsWithBlock:mediaItemTrashHandler];
             break;
         }
         case VLC_ML_PARENT_ALBUM:
         {
-            NSMutableArray *allMediaItemFiles = [[NSMutableArray alloc] init];
-            for(VLCMediaLibraryMediaItem* mediaItem in _representedAlbum.tracksAsMediaItems) {
-                [allMediaItemFiles addObjectsFromArray: mediaItem.files];
-            }
-
-            filesToTrash = [allMediaItemFiles copy];
+            [_representedAlbum iterateMediaItemsWithBlock:mediaItemTrashHandler];
             break;
         }
         case VLC_ML_PARENT_UNKNOWN:
         {
-            filesToTrash = _representedMediaItem.files;
+            mediaItemTrashHandler(_representedMediaItem);
             break;
         }
         default:
             NSLog(@"No represented media type, not moving anything to trash.");
             return;
     }
-
-    NSFileManager *fileManager = [NSFileManager defaultManager];
-
-    for (VLCMediaLibraryFile *fileToTrash in filesToTrash) {
-        [fileManager trashItemAtURL:fileToTrash.fileURL resultingItemURL:nil error:nil];
-    }
 }
 
 - (void)showInformation:(id)sender



View it on GitLab: https://code.videolan.org/videolan/vlc/-/commit/4d9fdbbfe88b23ae2ede9ee5b9f9286d65c787fd

-- 
View it on GitLab: https://code.videolan.org/videolan/vlc/-/commit/4d9fdbbfe88b23ae2ede9ee5b9f9286d65c787fd
You're receiving this email because of your account on code.videolan.org.


VideoLAN code repository instance


More information about the vlc-commits mailing list