[vlc-commits] macosx/library: introduce modes and a very basic audio media representation

Felix Paul Kühne git at videolan.org
Mon Apr 29 19:26:46 CEST 2019


vlc | branch: master | Felix Paul Kühne <felix at feepk.net> | Sun Apr 21 19:20:43 2019 +0200| [72d032eb5a6894b435b30d09b5b575eac8439a50] | committer: Felix Paul Kühne

macosx/library: introduce modes and a very basic audio media representation

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=72d032eb5a6894b435b30d09b5b575eac8439a50
---

 modules/gui/macosx/library/VLCLibraryDataSource.m | 40 ++++++++++++++++---
 modules/gui/macosx/library/VLCLibraryModel.h      | 12 ++++++
 modules/gui/macosx/library/VLCLibraryModel.m      | 48 ++++++++++++++++++++++-
 modules/gui/macosx/library/VLCLibraryWindow.m     | 25 ++++++++++--
 4 files changed, 115 insertions(+), 10 deletions(-)

diff --git a/modules/gui/macosx/library/VLCLibraryDataSource.m b/modules/gui/macosx/library/VLCLibraryDataSource.m
index 0902e6866c..037afb83cd 100644
--- a/modules/gui/macosx/library/VLCLibraryDataSource.m
+++ b/modules/gui/macosx/library/VLCLibraryDataSource.m
@@ -30,20 +30,48 @@
 
 @implementation VLCLibraryDataSource
 
-- (NSInteger)collectionView:(NSCollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
+- (NSInteger)collectionView:(NSCollectionView *)collectionView
+     numberOfItemsInSection:(NSInteger)section
 {
-    return [_libraryModel numberOfVideoMedia];
+    switch (_libraryModel.libraryMode) {
+        case VLCLibraryModeAudio:
+            return [_libraryModel numberOfAudioMedia];
+            break;
+
+        case VLCLibraryModeVideo:
+            return [_libraryModel numberOfVideoMedia];
+
+        default:
+            return 0;
+            break;
+    }
 }
 
-- (NSCollectionViewItem *)collectionView:(NSCollectionView *)collectionView itemForRepresentedObjectAtIndexPath:(NSIndexPath *)indexPath
+- (NSCollectionViewItem *)collectionView:(NSCollectionView *)collectionView
+     itemForRepresentedObjectAtIndexPath:(NSIndexPath *)indexPath
 {
     VLCLibraryCollectionViewItem *viewItem = [collectionView makeItemWithIdentifier:VLCLibraryCellIdentifier forIndexPath:indexPath];
 
-    NSArray *videoMedia = [_libraryModel listOfVideoMedia];
-    VLCMediaLibraryMediaItem *mediaItem = videoMedia[indexPath.item];
+    NSArray *mediaArray;
+    switch (_libraryModel.libraryMode) {
+        case VLCLibraryModeAudio:
+            mediaArray = [_libraryModel listOfAudioMedia];
+            break;
+
+        case VLCLibraryModeVideo:
+            mediaArray = [_libraryModel listOfVideoMedia];
+            break;
+
+        default:
+            NSAssert(1, @"no representation for selected library mode");
+            mediaArray = @[];
+            break;
+    }
+
+    VLCMediaLibraryMediaItem *mediaItem = mediaArray[indexPath.item];
 
     viewItem.mediaTitleTextField.stringValue = mediaItem.title;
-    viewItem.durationTextField.stringValue = [NSString stringWithTime:mediaItem.duration];
+    viewItem.durationTextField.stringValue = [NSString stringWithTime:mediaItem.duration / 1000];
 
     NSImage *image;
     if (mediaItem.artworkGenerated) {
diff --git a/modules/gui/macosx/library/VLCLibraryModel.h b/modules/gui/macosx/library/VLCLibraryModel.h
index 63c1321ab9..4dd09c8603 100644
--- a/modules/gui/macosx/library/VLCLibraryModel.h
+++ b/modules/gui/macosx/library/VLCLibraryModel.h
@@ -26,14 +26,26 @@
 
 NS_ASSUME_NONNULL_BEGIN
 
+typedef NS_ENUM(NSInteger, VLCLibraryMode) {
+    VLCLibraryModeAudio,
+    VLCLibraryModeVideo,
+    VLCLibraryModeNetwork,
+};
+
 @class VLCMediaLibraryMediaItem;
 
+extern NSString *VLCLibraryModelAudioMediaListUpdated;
 extern NSString *VLCLibraryModelVideoMediaListUpdated;
 
 @interface VLCLibraryModel : NSObject
 
 - (instancetype)initWithLibrary:(vlc_medialibrary_t *)library;
 
+ at property (readwrite) VLCLibraryMode libraryMode;
+
+ at property (readonly) size_t numberOfAudioMedia;
+ at property (readonly) NSArray <VLCMediaLibraryMediaItem *> *listOfAudioMedia;
+
 @property (readonly) size_t numberOfVideoMedia;
 @property (readonly) NSArray <VLCMediaLibraryMediaItem *> *listOfVideoMedia;
 
diff --git a/modules/gui/macosx/library/VLCLibraryModel.m b/modules/gui/macosx/library/VLCLibraryModel.m
index 8c7fa8a416..e6a8639f47 100644
--- a/modules/gui/macosx/library/VLCLibraryModel.m
+++ b/modules/gui/macosx/library/VLCLibraryModel.m
@@ -24,6 +24,7 @@
 
 #import "library/VLCLibraryDataTypes.h"
 
+NSString *VLCLibraryModelAudioMediaListUpdated = @"VLCLibraryModelAudioMediaListUpdated";
 NSString *VLCLibraryModelVideoMediaListUpdated = @"VLCLibraryModelVideoMediaListUpdated";
 
 @interface VLCLibraryModel ()
@@ -31,10 +32,12 @@ NSString *VLCLibraryModelVideoMediaListUpdated = @"VLCLibraryModelVideoMediaList
     vlc_medialibrary_t *_p_mediaLibrary;
     vlc_ml_event_callback_t *_p_eventCallback;
 
+    NSArray *_cachedAudioMedia;
     NSArray *_cachedVideoMedia;
     NSNotificationCenter *_defaultNotificationCenter;
 }
 
+- (void)updateCachedListOfAudioMedia;
 - (void)updateCachedListOfVideoMedia;
 
 @end
@@ -48,7 +51,19 @@ static void libraryCallback(void *p_data, const vlc_ml_event_t *p_event)
         case VLC_ML_EVENT_MEDIA_DELETED:
             dispatch_async(dispatch_get_main_queue(), ^{
                 VLCLibraryModel *libraryModel = (__bridge VLCLibraryModel *)p_data;
-                [libraryModel updateCachedListOfVideoMedia];
+                switch (libraryModel.libraryMode) {
+                    case VLCLibraryModeAudio:
+                        [libraryModel updateCachedListOfAudioMedia];
+                        break;
+
+                    case VLCLibraryModeVideo:
+                        [libraryModel updateCachedListOfVideoMedia];
+                        break;
+
+                    default:
+                        break;
+                }
+
             });
             break;
         default:
@@ -76,6 +91,37 @@ static void libraryCallback(void *p_data, const vlc_ml_event_t *p_event)
     }
 }
 
+- (size_t)numberOfAudioMedia
+{
+    if (_cachedAudioMedia) {
+        [self updateCachedListOfAudioMedia];
+    }
+
+    return _cachedAudioMedia.count;
+}
+
+- (void)updateCachedListOfAudioMedia
+{
+    vlc_ml_media_list_t *p_media_list = vlc_ml_list_audio_media(_p_mediaLibrary, NULL);
+    NSMutableArray *mutableArray = [[NSMutableArray alloc] initWithCapacity:p_media_list->i_nb_items];
+    for (size_t x = 0; x < p_media_list->i_nb_items; x++) {
+        VLCMediaLibraryMediaItem *mediaItem = [[VLCMediaLibraryMediaItem alloc] initWithMediaItem:&p_media_list->p_items[x]];
+        [mutableArray addObject:mediaItem];
+    }
+    _cachedAudioMedia = [mutableArray copy];
+    vlc_ml_media_list_release(p_media_list);
+    [_defaultNotificationCenter postNotificationName:VLCLibraryModelAudioMediaListUpdated object:self];
+}
+
+- (NSArray<VLCMediaLibraryMediaItem *> *)listOfAudioMedia
+{
+    if (!_cachedAudioMedia) {
+        [self updateCachedListOfAudioMedia];
+    }
+
+    return _cachedAudioMedia;
+}
+
 - (size_t)numberOfVideoMedia
 {
     if (!_cachedVideoMedia) {
diff --git a/modules/gui/macosx/library/VLCLibraryWindow.m b/modules/gui/macosx/library/VLCLibraryWindow.m
index 9da9eb7c06..394d29b882 100644
--- a/modules/gui/macosx/library/VLCLibraryWindow.m
+++ b/modules/gui/macosx/library/VLCLibraryWindow.m
@@ -64,6 +64,10 @@ static const float f_playlist_row_height = 72.;
                              object:nil];
     [notificationCenter addObserver:self
                            selector:@selector(updateLibraryRepresentation:)
+                               name:VLCLibraryModelAudioMediaListUpdated
+                             object:nil];
+    [notificationCenter addObserver:self
+                           selector:@selector(updateLibraryRepresentation:)
                                name:VLCLibraryModelVideoMediaListUpdated
                              object:nil];
 
@@ -72,7 +76,7 @@ static const float f_playlist_row_height = 72.;
 
     _segmentedTitleControl.segmentCount = 3;
     [_segmentedTitleControl setTarget:self];
-    [_segmentedTitleControl setAction:@selector(segmentedControlAction)];
+    [_segmentedTitleControl setAction:@selector(segmentedControlAction:)];
     [_segmentedTitleControl setLabel:_NS("Music") forSegment:0];
     [_segmentedTitleControl setLabel:_NS("Video") forSegment:1];
     [_segmentedTitleControl setLabel:_NS("Network") forSegment:2];
@@ -96,7 +100,8 @@ static const float f_playlist_row_height = 72.;
     _libraryCollectionView.dataSource = _libraryDataSource;
     _libraryCollectionView.delegate = _libraryDataSource;
     [_libraryCollectionView registerClass:[VLCLibraryCollectionViewItem class] forItemWithIdentifier:VLCLibraryCellIdentifier];
-    [_libraryCollectionView reloadData];
+
+    [self segmentedControlAction:nil];
 }
 
 - (void)dealloc
@@ -104,8 +109,22 @@ static const float f_playlist_row_height = 72.;
     [[NSNotificationCenter defaultCenter] removeObserver:self];
 }
 
-- (void)segmentedControlAction
+- (void)segmentedControlAction:(id)sender
 {
+    switch (_segmentedTitleControl.selectedSegment) {
+        case 0:
+            _libraryDataSource.libraryModel.libraryMode = VLCLibraryModeAudio;
+            break;
+
+        case 1:
+            _libraryDataSource.libraryModel.libraryMode = VLCLibraryModeVideo;
+            break;
+
+        default:
+            _libraryDataSource.libraryModel.libraryMode = VLCLibraryModeNetwork;
+            break;
+    }
+    [_libraryCollectionView reloadData];
 }
 
 - (void)playlistDoubleClickAction:(id)sender



More information about the vlc-commits mailing list