Quellcode durchsuchen

Move to UISearchController in FLEXFileBrowserTableViewController

Ryan Olson vor 10 Jahren
Ursprung
Commit
b70a1a2f48

+ 1 - 1
Classes/GlobalStateExplorers/FLEXFileBrowserTableViewController.h

@@ -10,7 +10,7 @@
 
 #import "FLEXFileBrowserSearchOperation.h"
 
-@interface FLEXFileBrowserTableViewController : UITableViewController <UISearchDisplayDelegate, FLEXFileBrowserSearchOperationDelegate>
+@interface FLEXFileBrowserTableViewController : UITableViewController
 
 - (id)initWithPath:(NSString *)path;
 

+ 35 - 80
Classes/GlobalStateExplorers/FLEXFileBrowserTableViewController.m

@@ -16,18 +16,14 @@
 @interface FLEXFileBrowserTableViewCell : UITableViewCell
 @end
 
-@interface FLEXFileBrowserTableViewController () <FLEXFileBrowserFileOperationControllerDelegate>
+@interface FLEXFileBrowserTableViewController () <FLEXFileBrowserFileOperationControllerDelegate, FLEXFileBrowserSearchOperationDelegate, UISearchResultsUpdating, UISearchControllerDelegate>
 
 @property (nonatomic, copy) NSString *path;
 @property (nonatomic, copy) NSArray *childPaths;
-@property (nonatomic, copy) NSString *searchString;
 @property (nonatomic, strong) NSArray *searchPaths;
 @property (nonatomic, strong) NSNumber *recursiveSize;
 @property (nonatomic, strong) NSNumber *searchPathsSize;
-#pragma clang diagnostic push
-#pragma clang diagnostic ignored "-Wdeprecated-declarations"
-@property (nonatomic, strong) UISearchDisplayController *searchController;
-#pragma clang diagnostic pop
+@property (nonatomic, strong) UISearchController *searchController;
 @property (nonatomic) NSOperationQueue *operationQueue;
 @property (nonatomic, strong) UIDocumentInteractionController *documentController;
 @property (nonatomic, strong) id<FLEXFileBrowserFileOperationController> fileOperationController;
@@ -49,16 +45,10 @@
         self.title = [path lastPathComponent];
         self.operationQueue = [NSOperationQueue new];
         
-        //add search controller
-        UISearchBar *searchBar = [UISearchBar new];
-        [searchBar sizeToFit];
-#pragma clang diagnostic push
-#pragma clang diagnostic ignored "-Wdeprecated-declarations"
-        self.searchController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
-#pragma clang diagnostic pop
+        self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
+        self.searchController.searchResultsUpdater = self;
         self.searchController.delegate = self;
-        self.searchController.searchResultsDataSource = self;
-        self.searchController.searchResultsDelegate = self;
+        self.searchController.dimsBackgroundDuringPresentation = NO;
         self.tableView.tableHeaderView = self.searchController.searchBar;
         
         //computing path size
@@ -107,22 +97,20 @@
 {
     self.searchPaths = searchResult;
     self.searchPathsSize = @(size);
-    [self.searchController.searchResultsTableView reloadData];
+    [self.tableView reloadData];
 }
 
-#pragma mark - UISearchDisplayDelegate
+#pragma mark - UISearchResultsUpdating
 
-- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
+- (void)updateSearchResultsForSearchController:(UISearchController *)searchController
 {
-    self.searchString = searchString;
-    [self reloadSearchPaths];
-
-    return YES;
+    [self reloadDisplayedPaths];
 }
 
-- (void)searchDisplayController:(UISearchDisplayController *)controller willHideSearchResultsTableView:(UITableView *)tableView
+#pragma mark - UISearchControllerDelegate
+
+- (void)willDismissSearchController:(UISearchController *)searchController
 {
-    //confirm to clear all operations
     [self.operationQueue cancelAllOperations];
     [self reloadChildPaths];
     [self.tableView reloadData];
@@ -138,25 +126,14 @@
 
 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
 {
-    if (tableView == self.tableView) {
-        return [self.childPaths count];
-    } else {
-        return [self.searchPaths count];
-    }
+    return self.searchController.isActive ? [self.searchPaths count] : [self.childPaths count];
 }
 
 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
 {
-    NSNumber *currentSize = nil;
-    NSArray *currentPaths = nil;
-    
-    if (tableView == self.tableView) {
-        currentSize = self.recursiveSize;
-        currentPaths = self.childPaths;
-    } else {
-        currentSize = self.searchPathsSize;
-        currentPaths = self.searchPaths;
-    }
+    BOOL isSearchActive = self.searchController.isActive;
+    NSNumber *currentSize = isSearchActive ? self.searchPathsSize : self.recursiveSize;
+    NSArray *currentPaths = isSearchActive ? self.searchPaths : self.childPaths;
     
     NSString *sizeString = nil;
     if (!currentSize) {
@@ -170,14 +147,7 @@
 
 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 {
-    NSString *fullPath = nil;
-    if (tableView == self.tableView) {
-        NSString *subpath = self.childPaths[indexPath.row];
-        fullPath = [self.path stringByAppendingPathComponent:subpath];
-    } else {
-        fullPath = self.searchPaths[indexPath.row];
-    }
-    
+    NSString *fullPath = [self filePathAtIndexPath:indexPath];
     NSDictionary *attributes = [[NSFileManager defaultManager] attributesOfItemAtPath:fullPath error:NULL];
     BOOL isDirectory = [[attributes fileType] isEqual:NSFileTypeDirectory];
     NSString *subtitle = nil;
@@ -218,16 +188,8 @@
 
 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
 {
-    NSString *subpath = nil;
-    NSString *fullPath = nil;
-    
-    if (tableView == self.tableView) {
-        subpath = self.childPaths[indexPath.row];
-        fullPath = [self.path stringByAppendingPathComponent:subpath];
-    } else {
-        fullPath = self.searchPaths[indexPath.row];
-        subpath = [fullPath lastPathComponent];
-    }
+    NSString *fullPath = [self filePathAtIndexPath:indexPath];
+    NSString *subpath = [fullPath lastPathComponent];
     
     BOOL isDirectory = NO;
     BOOL stillExists = [[NSFileManager defaultManager] fileExistsAtPath:fullPath isDirectory:&isDirectory];
@@ -313,16 +275,8 @@
 
 - (void)fileBrowserRename:(UITableViewCell *)sender
 {
-    NSString *fullPath = nil;
-
     NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
-    if (indexPath) {
-        NSString *subpath = self.childPaths[indexPath.row];
-        fullPath = [self.path stringByAppendingPathComponent:subpath];
-    } else {
-        indexPath = [self.searchController.searchResultsTableView indexPathForCell:sender];
-        fullPath = self.searchPaths[indexPath.row];
-    }
+    NSString *fullPath = [self filePathAtIndexPath:indexPath];
 
     self.fileOperationController = [[FLEXFileBrowserFileRenameOperationController alloc] initWithPath:fullPath];
     self.fileOperationController.delegate = self;
@@ -331,17 +285,9 @@
 
 - (void)fileBrowserDelete:(UITableViewCell *)sender
 {
-    NSString *fullPath = nil;
-
     NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
-    if (indexPath) {
-        NSString *subpath = self.childPaths[indexPath.row];
-        fullPath = [self.path stringByAppendingPathComponent:subpath];
-    } else {
-        indexPath = [self.searchController.searchResultsTableView indexPathForCell:sender];
-        fullPath = self.searchPaths[indexPath.row];
-    }
-
+    NSString *fullPath = [self filePathAtIndexPath:indexPath];
+    
     self.fileOperationController = [[FLEXFileBrowserFileDeleteOperationController alloc] initWithPath:fullPath];
     self.fileOperationController.delegate = self;
     [self.fileOperationController show];
@@ -351,16 +297,20 @@
 {
     if (self.searchController.isActive) {
         [self reloadSearchPaths];
-        [self.searchController.searchResultsTableView reloadData];
     } else {
         [self reloadChildPaths];
-        [self.tableView reloadData];
     }
+    [self.tableView reloadData];
 }
 
 - (void)reloadChildPaths
 {
-    self.childPaths = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:self.path error:NULL];
+    NSMutableArray *childPaths = [NSMutableArray array];
+    NSArray *subpaths = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:self.path error:NULL];
+    for (NSString *subpath in subpaths) {
+        [childPaths addObject:[self.path stringByAppendingPathComponent:subpath]];
+    }
+    self.childPaths = childPaths;
 }
 
 - (void)reloadSearchPaths
@@ -370,11 +320,16 @@
 
     //clear pre search request and start a new one
     [self.operationQueue cancelAllOperations];
-    FLEXFileBrowserSearchOperation *newOperation = [[FLEXFileBrowserSearchOperation alloc] initWithPath:self.path searchString:self.searchString];
+    FLEXFileBrowserSearchOperation *newOperation = [[FLEXFileBrowserSearchOperation alloc] initWithPath:self.path searchString:self.searchController.searchBar.text];
     newOperation.delegate = self;
     [self.operationQueue addOperation:newOperation];
 }
 
+- (NSString *)filePathAtIndexPath:(NSIndexPath *)indexPath
+{
+    return self.searchController.isActive ? self.searchPaths[indexPath.row] : self.childPaths[indexPath.row];
+}
+
 @end