[vlc-commits] [Git][videolan/vlc][master] macosx: Cache video media lists and update according to model notifications

Jean-Baptiste Kempf (@jbk) gitlab at videolan.org
Thu Oct 27 10:07:48 UTC 2022



Jean-Baptiste Kempf pushed to branch master at VideoLAN / VLC


Commits:
c2b2948d by Claudio Cambra at 2022-10-27T09:53:03+00:00
macosx: Cache video media lists and update according to model notifications

Signed-off-by: Claudio Cambra <developer at claudiocambra.com>

- - - - -


3 changed files:

- modules/gui/macosx/library/VLCLibraryVideoDataSource.h
- modules/gui/macosx/library/VLCLibraryVideoDataSource.m
- modules/gui/macosx/library/VLCLibraryWindow.m


Changes:

=====================================
modules/gui/macosx/library/VLCLibraryVideoDataSource.h
=====================================
@@ -36,6 +36,8 @@ NS_ASSUME_NONNULL_BEGIN
 @property (readwrite, assign) VLCLibraryModel *libraryModel;
 @property (readwrite, assign) NSCollectionView *libraryMediaCollectionView;
 
+- (void)reloadData;
+
 @end
 
 NS_ASSUME_NONNULL_END


=====================================
modules/gui/macosx/library/VLCLibraryVideoDataSource.m
=====================================
@@ -30,8 +30,51 @@
 #import "main/CompatibilityFixes.h"
 #import "extensions/NSString+Helpers.h"
 
+ at interface VLCLibraryVideoDataSource () <NSCollectionViewDelegate, NSCollectionViewDataSource>
+{
+    NSArray *_recentsArray;
+    NSArray *_libraryArray;
+}
+
+ at end
+
 @implementation VLCLibraryVideoDataSource
 
+- (instancetype)init
+{
+    self = [super init];
+    if(self) {
+        NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
+        [notificationCenter addObserver:self
+                               selector:@selector(libraryModelUpdated:)
+                                   name:VLCLibraryModelVideoMediaListUpdated
+                                 object:nil];
+        [notificationCenter addObserver:self
+                               selector:@selector(libraryModelUpdated:)
+                                   name:VLCLibraryModelRecentMediaListUpdated
+                                 object:nil];
+    }
+    return self;
+}
+
+- (void)libraryModelUpdated:(NSNotification *)aNotification
+{
+    [self reloadData];
+}
+
+- (void)reloadData
+{
+    if(!_libraryModel) {
+        return;
+    }
+    
+    dispatch_async(dispatch_get_main_queue(), ^{
+        _recentsArray = [_libraryModel listOfRecentMedia];
+        _libraryArray = [_libraryModel listOfVideoMedia];
+        [_libraryMediaCollectionView reloadData];
+    });
+}
+
 - (NSInteger)collectionView:(NSCollectionView *)collectionView
      numberOfItemsInSection:(NSInteger)section
 {
@@ -58,19 +101,16 @@
 {
     VLCLibraryCollectionViewItem *viewItem = [collectionView makeItemWithIdentifier:VLCLibraryCellIdentifier forIndexPath:indexPath];
 
-    NSArray *mediaArray;
     switch(indexPath.section) {
         case VLCVideoLibraryRecentsSection:
-            mediaArray = [_libraryModel listOfRecentMedia];
+            viewItem.representedItem = _recentsArray[indexPath.item];
             break;
         case VLCVideoLibraryLibrarySection:
         default:
-            mediaArray = [_libraryModel listOfVideoMedia];
+            viewItem.representedItem = _libraryArray[indexPath.item];
             break;
     }
 
-    viewItem.representedItem = mediaArray[indexPath.item];
-
     return viewItem;
 }
 
@@ -108,13 +148,11 @@ canDragItemsAtIndexPaths:(NSSet<NSIndexPath *> *)indexPaths
 writeItemsAtIndexPaths:(NSSet<NSIndexPath *> *)indexPaths
           toPasteboard:(NSPasteboard *)pasteboard
 {
-    NSArray *mediaArray = [_libraryModel listOfVideoMedia];
-
     NSUInteger numberOfIndexPaths = indexPaths.count;
     NSMutableArray *encodedLibraryItemsArray = [NSMutableArray arrayWithCapacity:numberOfIndexPaths];
     NSMutableArray *filePathsArray = [NSMutableArray arrayWithCapacity:numberOfIndexPaths];
     for (NSIndexPath *indexPath in indexPaths) {
-        VLCMediaLibraryMediaItem *mediaItem = mediaArray[indexPath.item];
+        VLCMediaLibraryMediaItem *mediaItem = _libraryArray[indexPath.item];
         [encodedLibraryItemsArray addObject:mediaItem];
 
         VLCMediaLibraryFile *file = mediaItem.files.firstObject;


=====================================
modules/gui/macosx/library/VLCLibraryWindow.m
=====================================
@@ -299,6 +299,7 @@ static void addShadow(NSImageView *__unsafe_unretained imageView)
                forSupplementaryViewOfKind:NSCollectionElementKindSectionHeader
                            withIdentifier:VLCLibrarySupplementaryElementViewIdentifier];
     [(NSCollectionViewFlowLayout *)_videoLibraryCollectionView.collectionViewLayout setHeaderReferenceSize:[VLCLibraryCollectionViewSupplementaryElementView defaultHeaderSize]];
+    [_libraryVideoDataSource reloadData];
 
     _libraryAudioDataSource = [[VLCLibraryAudioDataSource alloc] init];
     _libraryAudioDataSource.libraryModel = mainInstance.libraryController.libraryModel;
@@ -605,7 +606,7 @@ static void addShadow(NSImageView *__unsafe_unretained imageView)
         [_libraryTargetView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_videoLibraryView(>=572.)]|" options:0 metrics:0 views:dict]];
         [_libraryTargetView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_videoLibraryView(>=444.)]|" options:0 metrics:0 views:dict]];
         
-        [_videoLibraryCollectionView reloadData];
+        [_libraryVideoDataSource reloadData];
     }
     
     _librarySortButton.hidden = NO;
@@ -965,7 +966,7 @@ static void addShadow(NSImageView *__unsafe_unretained imageView)
 - (void)updateLibraryRepresentation:(NSNotification *)aNotification
 {
     if (_videoLibraryView.superview != nil) {
-        [_videoLibraryCollectionView reloadData];
+        [_libraryVideoDataSource reloadData];
     } else if (_audioLibraryView.superview != nil) {
         [_libraryAudioDataSource reloadAppearance];
     }



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

-- 
View it on GitLab: https://code.videolan.org/videolan/vlc/-/commit/c2b2948ddb731985ac1ef5e09bb5b10ab0ab92b7
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