Browse Source

iOS: Select all feature

Added an select all files feature in the edit mode.

Signed-off-by: Jean-Baptiste Kempf <jb@videolan.org>
Soomin Lee 8 years ago
parent
commit
6b7836097c
1 changed files with 46 additions and 0 deletions
  1. 46 0
      Sources/VLCLibraryViewController.m

+ 46 - 0
Sources/VLCLibraryViewController.m

@@ -57,6 +57,7 @@ static NSString *kUsingTableViewToShowData = @"UsingTableViewToShowData";
     VLCFolderCollectionViewFlowLayout *_folderLayout;
     VLCFolderCollectionViewFlowLayout *_folderLayout;
     LXReorderableCollectionViewFlowLayout *_reorderLayout;
     LXReorderableCollectionViewFlowLayout *_reorderLayout;
     BOOL _inFolder;
     BOOL _inFolder;
+    BOOL _isSelected;
     UILongPressGestureRecognizer *_longPressGestureRecognizer;
     UILongPressGestureRecognizer *_longPressGestureRecognizer;
     UITapGestureRecognizer *_tapTwiceGestureRecognizer;
     UITapGestureRecognizer *_tapTwiceGestureRecognizer;
 
 
@@ -64,6 +65,7 @@ static NSString *kUsingTableViewToShowData = @"UsingTableViewToShowData";
     UISearchBar *_searchBar;
     UISearchBar *_searchBar;
     UISearchDisplayController *_searchDisplayController;
     UISearchDisplayController *_searchDisplayController;
 
 
+    UIBarButtonItem *_selectAllBarButtonItem;
     UIBarButtonItem *_createFolderBarButtonItem;
     UIBarButtonItem *_createFolderBarButtonItem;
     UIBarButtonItem *_openInActivityBarButtonItem;
     UIBarButtonItem *_openInActivityBarButtonItem;
     UIBarButtonItem *_removeFromFolderBarButtonItem;
     UIBarButtonItem *_removeFromFolderBarButtonItem;
@@ -174,6 +176,12 @@ static NSString *kUsingTableViewToShowData = @"UsingTableViewToShowData";
     self.editButtonItem.title = NSLocalizedString(@"BUTTON_EDIT", nil);
     self.editButtonItem.title = NSLocalizedString(@"BUTTON_EDIT", nil);
     self.editButtonItem.tintColor = [UIColor whiteColor];
     self.editButtonItem.tintColor = [UIColor whiteColor];
 
 
+    _selectAllBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"BUTTON_ALL", nil) style:UIBarButtonItemStylePlain target:self action:@selector(handleSelection)];
+    _selectAllBarButtonItem.tintColor = [UIColor whiteColor];
+    UIFont *font = [UIFont boldSystemFontOfSize:17];
+    NSDictionary *attributes = @{NSFontAttributeName: font};
+    [_selectAllBarButtonItem setTitleTextAttributes:attributes forState:UIControlStateNormal];
+
     _emptyLibraryView.emptyLibraryLabel.text = NSLocalizedString(@"EMPTY_LIBRARY", nil);
     _emptyLibraryView.emptyLibraryLabel.text = NSLocalizedString(@"EMPTY_LIBRARY", nil);
     _emptyLibraryView.emptyLibraryLongDescriptionLabel.text = NSLocalizedString(@"EMPTY_LIBRARY_LONG", nil);
     _emptyLibraryView.emptyLibraryLongDescriptionLabel.text = NSLocalizedString(@"EMPTY_LIBRARY_LONG", nil);
     [_emptyLibraryView.emptyLibraryLongDescriptionLabel sizeToFit];
     [_emptyLibraryView.emptyLibraryLongDescriptionLabel sizeToFit];
@@ -1212,13 +1220,51 @@ static NSString *kUsingTableViewToShowData = @"UsingTableViewToShowData";
 }
 }
 
 
 #pragma mark - UI implementation
 #pragma mark - UI implementation
+- (void)handleSelection
+{
+    if (self.usingTableViewToShowData) {
+        NSInteger numberOfSections = [self.tableView numberOfSections];
+
+        for (NSInteger section = 0; section < numberOfSections; section++) {
+            NSInteger numberOfRowInSection = [self.tableView numberOfRowsInSection:section];
+
+            for (NSInteger row = 0; row < numberOfRowInSection; row++) {
+                if (!_isSelected)
+                    [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:row inSection:section] animated:NO scrollPosition:UITableViewScrollPositionNone];
+                else
+                    [self.tableView deselectRowAtIndexPath:[NSIndexPath indexPathForRow:row inSection:section] animated:NO];
+            }
+        }
+    } else {
+        NSInteger numberOfItems = [self.collectionView numberOfItemsInSection:0];
+
+        for (NSInteger item = 0; item < numberOfItems; item++) {
+            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:item inSection:0];
+
+            if (!_isSelected)
+                [self.collectionView selectItemAtIndexPath:indexPath animated:NO scrollPosition:UICollectionViewScrollPositionNone];
+            else
+                [self.collectionView deselectItemAtIndexPath:indexPath animated:NO];
+            [(VLCPlaylistCollectionViewCell *)[self.collectionView cellForItemAtIndexPath:indexPath] selectionUpdate];
+        }
+    }
+    _isSelected = !_isSelected;
+}
+
 - (void)setEditing:(BOOL)editing animated:(BOOL)animated
 - (void)setEditing:(BOOL)editing animated:(BOOL)animated
 {
 {
     [super setEditing:editing animated:animated];
     [super setEditing:editing animated:animated];
 
 
+    _isSelected = NO;
+
     UIBarButtonItem *editButton = self.editButtonItem;
     UIBarButtonItem *editButton = self.editButtonItem;
     editButton.tintColor = [UIColor whiteColor];
     editButton.tintColor = [UIColor whiteColor];
 
 
+    if (!editing && self.navigationItem.rightBarButtonItems.lastObject == _selectAllBarButtonItem)
+        [self.navigationItem setRightBarButtonItems: [self.navigationItem.rightBarButtonItems subarrayWithRange:NSMakeRange(0, self.navigationItem.rightBarButtonItems.count - 1)]];
+    else
+        [self.navigationItem setRightBarButtonItems:editing ? [self.navigationItem.rightBarButtonItems arrayByAddingObject:_selectAllBarButtonItem] : [self.navigationItem rightBarButtonItems] animated:YES];
+
     if (self.usingTableViewToShowData) {
     if (self.usingTableViewToShowData) {
         self.tableView.allowsMultipleSelectionDuringEditing = editing;
         self.tableView.allowsMultipleSelectionDuringEditing = editing;
         [self.tableView setEditing:editing animated:YES];
         [self.tableView setEditing:editing animated:YES];