Quellcode durchsuchen

VLCLNetworkListViewController: replace deprecated UISearchDisplayDelegate with the UISearchControllerDelegate

Pierre SAGASPE vor 7 Jahren
Ursprung
Commit
8f6050bbce

+ 2 - 1
Sources/LocalNetworkConnectivity/VLCNetworkListViewController.h

@@ -2,7 +2,7 @@
  * VLCLocalNetworkListViewController
  * VLC for iOS
  *****************************************************************************
- * Copyright (c) 2015 VideoLAN. All rights reserved.
+ * Copyright (c) 2015-2017 VideoLAN. All rights reserved.
  * $Id$
  *
  * Author: Felix Paul Kühne <fkuehne # videolan.org>
@@ -17,6 +17,7 @@ extern NSString *VLCNetworkListCellIdentifier;
 @interface VLCNetworkListViewController : UIViewController
 
 @property (nonatomic, strong) UITableView *tableView;
+@property (nonatomic, strong) UISearchController *searchController;
 
 - (IBAction)playAllAction:(id)sender;
 - (void)tableView:(UITableView *)tableView willDisplayCell:(VLCNetworkListCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath;

+ 28 - 25
Sources/LocalNetworkConnectivity/VLCNetworkListViewController.m

@@ -2,7 +2,7 @@
  * VLCLocalNetworkListViewController
  * VLC for iOS
  *****************************************************************************
- * Copyright (c) 2013-2015 VideoLAN. All rights reserved.
+ * Copyright (c) 2013-2017 VideoLAN. All rights reserved.
  * $Id$
  *
  * Authors: Felix Paul Kühne <fkuehne # videolan.org>
@@ -16,11 +16,9 @@
 
 NSString *VLCNetworkListCellIdentifier = @"VLCNetworkListCellIdentifier";
 
-@interface VLCNetworkListViewController () <UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate, UISearchDisplayDelegate>
+@interface VLCNetworkListViewController () <UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate, UISearchControllerDelegate, UISearchResultsUpdating>
 {
     NSMutableArray *_searchData;
-    UISearchBar *_searchBar;
-    UISearchDisplayController *_searchDisplayController;
     UITapGestureRecognizer *_tapTwiceGestureRecognizer;
 }
 
@@ -57,22 +55,21 @@ NSString *VLCNetworkListCellIdentifier = @"VLCNetworkListCellIdentifier";
     self.tableView.separatorColor = [UIColor VLCDarkBackgroundColor];
     self.view.backgroundColor = [UIColor VLCDarkBackgroundColor];
 
-    _searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
     UINavigationBar *navBar = self.navigationController.navigationBar;
-    _searchBar.barTintColor = navBar.barTintColor;
-    _searchBar.tintColor = navBar.tintColor;
-    _searchBar.translucent = navBar.translucent;
-    _searchBar.opaque = navBar.opaque;
-    _searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:_searchBar contentsController:self];
-    _searchDisplayController.delegate = self;
-    _searchDisplayController.searchResultsDataSource = self;
-    _searchDisplayController.searchResultsDelegate = self;
-    _searchDisplayController.searchResultsTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
-    _searchDisplayController.searchResultsTableView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
-    _searchDisplayController.searchBar.searchBarStyle = UIBarStyleBlack;
-    _searchBar.delegate = self;
-
-    self.tableView.tableHeaderView = _searchBar;
+    _searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
+    _searchController.searchResultsUpdater = self;
+    _searchController.delegate = self;
+    _searchController.dimsBackgroundDuringPresentation = NO;
+
+    _searchController.searchBar.delegate = self;
+    _searchController.searchBar.barTintColor = navBar.barTintColor;
+    _searchController.searchBar.tintColor = navBar.tintColor;
+    _searchController.searchBar.translucent = navBar.translucent;
+    _searchController.searchBar.opaque = navBar.opaque;
+    [_searchController.searchBar sizeToFit];
+    
+    _tableView.tableHeaderView = _searchController.searchBar;
+    self.definesPresentationContext = YES;
 
     self.navigationItem.rightBarButtonItems = @[[UIBarButtonItem themedRevealMenuButtonWithTarget:self andSelector:@selector(menuButtonAction:)],
                                                 [UIBarButtonItem themedPlayAllButtonWithTarget:self andSelector:@selector(playAllAction:)]];
@@ -84,7 +81,7 @@ NSString *VLCNetworkListCellIdentifier = @"VLCNetworkListCellIdentifier";
 - (void)viewWillAppear:(BOOL)animated
 {
     [super viewWillAppear:animated];
-    CGPoint contentOffset = CGPointMake(0, _searchBar.bounds.size.height);
+    CGPoint contentOffset = CGPointMake(0, _tableView.tableHeaderView.bounds.size.height);
     [self.tableView setContentOffset:contentOffset animated:NO];
 }
 
@@ -144,16 +141,22 @@ NSString *VLCNetworkListCellIdentifier = @"VLCNetworkListCellIdentifier";
     cell.contentView.backgroundColor = cell.titleLabel.backgroundColor = cell.folderTitleLabel.backgroundColor = cell.subtitleLabel.backgroundColor =  color;
 }
 
-#pragma mark - Search Display Controller Delegate
+#pragma mark - Search Controller Delegate
 
-- (void)searchDisplayController:(UISearchDisplayController *)controller didLoadSearchResultsTableView:(UITableView *)tableView
+- (void)willPresentSearchController:(UISearchController *)searchController
 {
     if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)
-        tableView.rowHeight = 80.0f;
+        _tableView.rowHeight = 80.0f;
     else
-        tableView.rowHeight = 68.0f;
+        _tableView.rowHeight = 68.0f;
 
-    tableView.backgroundColor = [UIColor blackColor];
+    _tableView.backgroundColor = [UIColor blackColor];
+}
+
+#pragma mark - Search Research Updater
+
+- (void)updateSearchResultsForSearchController:(UISearchController *)searchController
+{
 }
 
 @end

+ 15 - 10
Sources/LocalNetworkConnectivity/VLCNetworkServerBrowserViewController.m

@@ -2,7 +2,7 @@
  * VLCNetworkServerBrowserViewController.m
  * VLC for iOS
  *****************************************************************************
- * Copyright (c) 2013-2015 VideoLAN. All rights reserved.
+ * Copyright (c) 2013-2017 VideoLAN. All rights reserved.
  * $Id$
  *
  * Authors: Felix Paul Kühne <fkuehne # videolan.org>
@@ -146,7 +146,7 @@
 
 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
 {
-    if (tableView == self.searchDisplayController.searchResultsTableView)
+    if (self.searchController.isActive)
         return _searchArray.count;
 
     return self.serverBrowser.items.count;
@@ -160,7 +160,7 @@
 
 
     id<VLCNetworkServerBrowserItem> item;
-    if (tableView == self.searchDisplayController.searchResultsTableView) {
+    if (self.searchController.isActive) {
         item = _searchArray[indexPath.row];
     } else {
         item = self.serverBrowser.items[indexPath.row];
@@ -189,7 +189,7 @@
     id<VLCNetworkServerBrowserItem> item;
     NSInteger row = indexPath.row;
     BOOL singlePlayback = ![[NSUserDefaults standardUserDefaults] boolForKey:kVLCAutomaticallyPlayNextItem];
-    if (tableView == self.searchDisplayController.searchResultsTableView) {
+    if (self.searchController.isActive) {
         item = _searchArray[row];
         singlePlayback = YES;
     } else {
@@ -205,8 +205,8 @@
 - (void)triggerDownloadForCell:(VLCNetworkListCell *)cell
 {
     id<VLCNetworkServerBrowserItem> item;
-    if ([self.searchDisplayController isActive])
-        item = _searchArray[[self.searchDisplayController.searchResultsTableView indexPathForCell:cell].row];
+    if (self.searchController.isActive)
+        item = _searchArray[[self.tableView indexPathForCell:cell].row];
     else
         item = self.serverBrowser.items[[self.tableView indexPathForCell:cell].row];
 
@@ -215,14 +215,19 @@
     }
 }
 
+#pragma mark - Search Research Updater
 
-#pragma mark - search
+- (void)updateSearchResultsForSearchController:(UISearchController *)_searchController
+{
+    NSString *searchString = _searchController.searchBar.text;
+    [self searchForText:searchString];
+    [self.tableView reloadData];
+}
 
-- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
+- (void)searchForText:(NSString *)searchString
 {
     NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name contains[c] %@",searchString];
-    self.searchArray = [self.serverBrowser.items filteredArrayUsingPredicate:predicate];
-    return YES;
+    _searchArray = [self.serverBrowser.items filteredArrayUsingPredicate:predicate];
 }
 
 @end