diff --git a/modules/gui/macosx/library/VLCLibraryDataSource.m b/modules/gui/macosx/library/VLCLibraryDataSource.m index 0902e6866c495b75f71dd6874be003709e9e77c5..037afb83cded972e0dfa1a9301bd5f33066b3d4d 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 63c1321ab945c4254bbbb95d19995f172df5daa6..4dd09c8603ab25aab931883d115cfe81867dd5f2 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; +@property (readwrite) VLCLibraryMode libraryMode; + +@property (readonly) size_t numberOfAudioMedia; +@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 8c7fa8a416630b49eb7e21db442c29f63941118e..e6a8639f47d54128520f93e40d24dcb107cf34da 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 9da9eb7c06235d0f0d8b218b72848ca1218a719a..394d29b88268b21ae305e3f2dbd28f5b7b74bda7 100644 --- a/modules/gui/macosx/library/VLCLibraryWindow.m +++ b/modules/gui/macosx/library/VLCLibraryWindow.m @@ -62,6 +62,10 @@ static const float f_playlist_row_height = 72.; selector:@selector(shouldShowFullscreenController:) name:VLCVideoWindowShouldShowFullscreenController object:nil]; + [notificationCenter addObserver:self + selector:@selector(updateLibraryRepresentation:) + name:VLCLibraryModelAudioMediaListUpdated + object:nil]; [notificationCenter addObserver:self selector:@selector(updateLibraryRepresentation:) name:VLCLibraryModelVideoMediaListUpdated @@ -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