Переглянути джерело

VLCLibraryViewController: replace the deprecated DisplayViewController with the UISearchViewController

Carola Nitz 7 роки тому
батько
коміт
53a5c40003

+ 0 - 1
Sources/VLCAppDelegate.m

@@ -415,7 +415,6 @@ didFailToContinueUserActivityWithType:(NSString *)userActivityType
 - (void)applicationWillResignActive:(UIApplication *)application
 {
     _passcodeValidated = NO;
-    [self.libraryViewController setEditing:NO animated:NO];
     [self validatePasscode];
     [[MLMediaLibrary sharedMediaLibrary] applicationWillExit];
 }

+ 0 - 2
Sources/VLCLibrarySearchDisplayDataSource.h

@@ -12,8 +12,6 @@
 
 @interface VLCLibrarySearchDisplayDataSource : NSObject<UITableViewDataSource>
 
-- (NSString *)cellIdentifier;
-- (UINib *)nibclass;
 - (NSManagedObject *)objectAtIndex:(NSUInteger)index;
 - (void)shouldReloadTableForSearchString:(NSString *)searchString searchableFiles:(NSArray *)files;
 

+ 12 - 40
Sources/VLCLibrarySearchDisplayDataSource.m

@@ -13,8 +13,6 @@
 #import "VLCLibrarySearchDisplayDataSource.h"
 #import "VLCPlaylistTableViewCell.h"
 
-static NSString *PlaylistCellIdentifier = @"PlaylistCell";
-
 @interface VLCLibrarySearchDisplayDataSource() <UITableViewDataSource>
 {
     NSMutableArray *_searchData;
@@ -32,63 +30,36 @@ static NSString *PlaylistCellIdentifier = @"PlaylistCell";
     return self;
 }
 
-- (NSString *)cellIdentifier
-{
-    return PlaylistCellIdentifier;
-}
-
-- (UINib *)nibclass
-{
-    return [UINib nibWithNibName:@"VLCPlaylistTableViewCell" bundle:nil];
-}
-
 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
 {
-    @synchronized (_searchData) {
-        return _searchData.count;
-    }
+    return _searchData.count;
 }
 
 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 {
-    VLCPlaylistTableViewCell *cell = (VLCPlaylistTableViewCell *)[tableView dequeueReusableCellWithIdentifier:PlaylistCellIdentifier forIndexPath:indexPath];
-
-    [cell collapsWithAnimation:NO];
+    VLCPlaylistTableViewCell *cell = (VLCPlaylistTableViewCell *)[tableView dequeueReusableCellWithIdentifier:kPlaylistCellIdentifier forIndexPath:indexPath];
 
     NSInteger row = indexPath.row;
 
-    @synchronized (_searchData) {
-        if (row < _searchData.count)
-            cell.mediaObject = _searchData[row];
-    }
+    if (row < _searchData.count)
+        cell.mediaObject = _searchData[row];
 
     return cell;
 }
 
 - (NSManagedObject *)objectAtIndex:(NSUInteger)index
 {
-    @synchronized (_searchData) {
-        return index < _searchData.count ? _searchData[index] : nil;
-    }
-}
-
-- (void)removeAllObjects
-{
-    @synchronized (_searchData) {
-        [_searchData removeAllObjects];
-    }
+    return index < _searchData.count ? _searchData[index] : nil;
 }
 
-- (void)addObject:(NSManagedObject *)object
+- (void)shouldReloadTableForSearchString:(NSString *)searchString searchableFiles:(NSArray *)files
 {
-    @synchronized (_searchData) {
-        [_searchData addObject:object];
+    if (!searchString || [searchString isEqualToString:@""]) {
+        _searchData = [files mutableCopy];
+        return;
     }
-}
 
-- (void)shouldReloadTableForSearchString:(NSString *)searchString searchableFiles:(NSArray *)files
-{
-    [self removeAllObjects];
+    [_searchData removeAllObjects];
     NSRange nameRange;
 
     for (NSManagedObject *item in files) {
@@ -107,7 +78,7 @@ static NSString *PlaylistCellIdentifier = @"PlaylistCell";
             nameRange = [self _searchFile:(MLFile*)item forString:searchString];
 
         if (nameRange.location != NSNotFound)
-            [self addObject:item];
+            [_searchData addObject:item];
     }
 }
 
@@ -282,4 +253,5 @@ static NSString *PlaylistCellIdentifier = @"PlaylistCell";
 
     return nameRange;
 }
+
 @end

+ 37 - 67
Sources/VLCLibraryViewController.m

@@ -49,7 +49,7 @@ static NSString *kUsingTableViewToShowData = @"UsingTableViewToShowData";
 
 @end
 
-@interface VLCLibraryViewController () <VLCFolderCollectionViewDelegateFlowLayout, LXReorderableCollectionViewDataSource, LXReorderableCollectionViewDelegateFlowLayout, UITableViewDataSource, UITableViewDelegate, MLMediaLibrary, VLCMediaListDelegate, UISearchBarDelegate, UISearchDisplayDelegate> {
+@interface VLCLibraryViewController () <VLCFolderCollectionViewDelegateFlowLayout, LXReorderableCollectionViewDataSource, LXReorderableCollectionViewDelegateFlowLayout, UITableViewDataSource, UITableViewDelegate, MLMediaLibrary, VLCMediaListDelegate, UISearchResultsUpdating, UISearchControllerDelegate> {
     VLCLibraryMode _libraryMode;
     VLCLibraryMode _previousLibraryMode;
     UIBarButtonItem *_menuButton;
@@ -65,8 +65,7 @@ static NSString *kUsingTableViewToShowData = @"UsingTableViewToShowData";
     VLCLibrarySearchDisplayDataSource *_searchDataSource;
     VLCMediaDataSource *_mediaDataSource;
 
-    UISearchBar *_searchBar;
-    UISearchDisplayController *_searchDisplayController;
+    UISearchController *_searchController;
 
     UIBarButtonItem *_selectAllBarButtonItem;
     UIBarButtonItem *_createFolderBarButtonItem;
@@ -98,7 +97,7 @@ static NSString *kUsingTableViewToShowData = @"UsingTableViewToShowData";
 
 - (void)loadView
 {
-    [self setupContentViewWithContentInset:NO];
+    [self setupContentView];
     [self setViewFromDeviceOrientation];
     [self updateViewsForCurrentDisplayMode];
     _libraryMode = VLCLibraryModeAllFiles;
@@ -110,7 +109,7 @@ static NSString *kUsingTableViewToShowData = @"UsingTableViewToShowData";
     _emptyLibraryView.emptyLibraryLongDescriptionLabel.numberOfLines = 0;
 }
 
-- (void)setupContentViewWithContentInset:(BOOL)setInset
+- (void)setupContentView
 {
     CGRect viewDimensions = [UIApplication sharedApplication].keyWindow.bounds;
     UIView *contentView = [[UIView alloc] initWithFrame:viewDimensions];
@@ -118,7 +117,7 @@ static NSString *kUsingTableViewToShowData = @"UsingTableViewToShowData";
     contentView.backgroundColor = [UIColor VLCDarkBackgroundColor];
 
     if (self.usingTableViewToShowData) {
-        if(!_tableView) {
+        if (!_tableView) {
             _tableView = [[UITableView alloc] initWithFrame:viewDimensions style:UITableViewStylePlain];
             _tableView.backgroundColor = [UIColor VLCDarkBackgroundColor];
             CGRect frame = _tableView.bounds;
@@ -130,11 +129,11 @@ static NSString *kUsingTableViewToShowData = @"UsingTableViewToShowData";
             _tableView.separatorColor = [UIColor VLCDarkBackgroundColor];
             _tableView.delegate = self;
             _tableView.dataSource = self;
-            _tableView.opaque = YES;
             _tableView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
             _tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
-            _tableView.tableHeaderView = _searchBar;
-            _tableView.tableFooterView = [UIView new];
+            _tableView.tableHeaderView = _searchController.searchBar;
+            UINib *nib = [UINib nibWithNibName:@"VLCPlaylistTableViewCell" bundle:nil];
+            [_tableView registerNib:nib forCellReuseIdentifier:kPlaylistCellIdentifier];
         }
         _tableView.frame = contentView.bounds;
         [contentView addSubview:_tableView];
@@ -147,16 +146,17 @@ static NSString *kUsingTableViewToShowData = @"UsingTableViewToShowData";
             _collectionView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
             _collectionView.delegate = self;
             _collectionView.dataSource = self;
-            _collectionView.opaque = YES;
             _collectionView.backgroundColor = [UIColor VLCDarkBackgroundColor];
             _collectionView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
             _longPressGestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(_collectionViewHandleLongPressGesture:)];
             [_collectionView addGestureRecognizer:_longPressGestureRecognizer];
             [_collectionView registerNib:[UINib nibWithNibName:@"VLCPlaylistCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"PlaylistCell"];
+
         }
         _collectionView.frame = contentView.bounds;
         [contentView addSubview:_collectionView];
         [_collectionView reloadData];
+        [_searchController setActive:NO];
     }
     self.view = contentView;
 }
@@ -227,26 +227,18 @@ static NSString *kUsingTableViewToShowData = @"UsingTableViewToShowData";
     self.navigationController.toolbar.tintColor = [UIColor whiteColor];
     [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
 
-    _searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
-    UINavigationBar *navBar = self.navigationController.navigationBar;
-    _searchBar.barTintColor = navBar.barTintColor;
-    // cancel button tint color of UISearchBar with white color
-    [[UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], NSForegroundColorAttributeName, nil] forState:UIControlStateNormal];
-    _searchBar.tintColor = navBar.tintColor;
-    _searchBar.translucent = navBar.translucent;
-    _searchBar.opaque = navBar.opaque;
-    _searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:_searchBar contentsController:self];
-    _searchDisplayController.delegate = self;
-    _searchDisplayController.searchResultsDataSource = _searchDataSource;
-    _searchDisplayController.searchResultsDelegate = self;
-    _searchDisplayController.searchResultsTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
-    _searchDisplayController.searchResultsTableView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
-    [_searchDisplayController.searchResultsTableView registerNib:[_searchDataSource nibclass] forCellReuseIdentifier:[_searchDataSource cellIdentifier]];
-    _searchBar.delegate = self;
-
-    [self setSearchBar:YES resetContent:YES];
     self.edgesForExtendedLayout = UIRectEdgeNone;
 
+    [self setupSearchController];
+}
+
+- (void)setupSearchController
+{
+    _searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
+    _searchController.searchResultsUpdater = self;
+    _searchController.dimsBackgroundDuringPresentation = NO;
+    _searchController.delegate = self;
+    [self setSearchBar:YES resetContent:YES];
 }
 
 - (void)viewWillAppear:(BOOL)animated
@@ -421,14 +413,11 @@ static NSString *kUsingTableViewToShowData = @"UsingTableViewToShowData";
 
 - (void)setSearchBar:(BOOL)enable resetContent:(BOOL)resetContent
 {
-    if (enable)
-        self.tableView.tableHeaderView = _searchBar;
-    else
-        self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.tableView.bounds.size.width, CGFLOAT_MIN)];
+    _tableView.tableHeaderView = enable ? _searchController.searchBar : nil;
     if (resetContent) {
-        CGPoint contentOffset = self.tableView.contentOffset;
-        contentOffset.y += CGRectGetHeight(self.tableView.tableHeaderView.frame);
-        self.tableView.contentOffset = contentOffset;
+        CGPoint contentOffset = _tableView.contentOffset;
+        contentOffset.y += CGRectGetHeight(_tableView.tableHeaderView.frame);
+        _tableView.contentOffset = contentOffset;
     }
 }
 
@@ -523,10 +512,6 @@ static NSString *kUsingTableViewToShowData = @"UsingTableViewToShowData";
     static NSString *CellIdentifier = @"PlaylistCell";
 
     VLCPlaylistTableViewCell *cell = (VLCPlaylistTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
-    if (cell == nil)
-        cell = [VLCPlaylistTableViewCell cellWithReuseIdentifier:CellIdentifier];
-    else
-        [cell collapsWithAnimation:NO];
 
     UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRightOnTableViewCellGestureAction:)];
     [swipeRight setDirection:(UISwipeGestureRecognizerDirectionRight)];
@@ -597,16 +582,6 @@ static NSString *kUsingTableViewToShowData = @"UsingTableViewToShowData";
     [tableView deselectRowAtIndexPath:indexPath animated:YES];
     NSManagedObject *selectedObject;
 
-    NSUInteger row = indexPath.row;
-    if (tableView == self.searchDisplayController.searchResultsTableView) {
-        selectedObject = [_searchDataSource objectAtIndex:row];
-    } else {
-        selectedObject = [_mediaDataSource objectAtIndex:row];
-    }
-
-    if (_searchDisplayController.active)
-        [_searchDisplayController setActive:NO animated:NO];
-
     if (selectedObject != nil)
         [self openMediaObject:selectedObject];
 }
@@ -1336,7 +1311,7 @@ static NSString *kUsingTableViewToShowData = @"UsingTableViewToShowData";
 {
     [[NSUserDefaults standardUserDefaults] setBool:usingTableViewToShowData forKey:kUsingTableViewToShowData];
     [self updateViewsForCurrentDisplayMode];
-    [self setupContentViewWithContentInset:usingTableViewToShowData];
+    [self setupContentView];
 }
 
 #pragma mark - autorotation
@@ -1388,23 +1363,24 @@ static NSString *kUsingTableViewToShowData = @"UsingTableViewToShowData";
     } completion:nil];
 }
 
-#pragma mark - UISearchBar Delegate
+#pragma mark - SearchController Delegate
 
-- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
+- (void)didPresentSearchController:(UISearchController *)searchController
 {
-    if (!self.usingTableViewToShowData) {
-        [self.collectionView.collectionViewLayout invalidateLayout];
-    }
-    [self.searchDisplayController setActive:NO];
-    [self.navigationController setNavigationBarHidden:NO animated:NO];
+    _tableView.dataSource = _searchDataSource;
 }
 
-#pragma mark - Search Display Controller Delegate
+- (void)didDismissSearchController:(UISearchController *)searchController
+{
+    _tableView.dataSource = self;
+}
 
-- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
+#pragma mark - Search Research Updater
+
+- (void)updateSearchResultsForSearchController:(UISearchController *)searchController
 {
-    [_searchDataSource shouldReloadTableForSearchString:searchString searchableFiles:[_mediaDataSource allObjects]];
-    return YES;
+    [_searchDataSource shouldReloadTableForSearchString:searchController.searchBar.text searchableFiles:[_mediaDataSource allObjects]];
+    [_tableView reloadData];
 }
 
 #pragma mark - handoff
@@ -1431,10 +1407,4 @@ static NSString *kUsingTableViewToShowData = @"UsingTableViewToShowData";
     }
 }
 
-- (void)searchDisplayController:(UISearchDisplayController *)controller didLoadSearchResultsTableView:(UITableView *)tableView
-{
-    tableView.rowHeight = 90.;
-    tableView.backgroundColor = [UIColor blackColor];
-}
-
 @end

+ 1 - 1
Sources/VLCPlaylistTableViewCell.h

@@ -11,7 +11,7 @@
  * Refer to the COPYING file of the official project for license.
  *****************************************************************************/
 
-#import <Foundation/Foundation.h>
+static NSString *kPlaylistCellIdentifier = @"PlaylistCell";
 
 @class VLCLinearProgressIndicator;
 @interface VLCPlaylistTableViewCell : UITableViewCell

+ 6 - 0
Sources/VLCPlaylistTableViewCell.m

@@ -44,6 +44,12 @@
     return cell;
 }
 
+- (void)prepareForReuse
+{
+    [super prepareForReuse];
+    [self collapsWithAnimation:NO];
+}
+
 - (void)setNeedsDisplay
 {
     [self collapsWithAnimation:NO];