Created
June 9, 2014 17:36
-
-
Save Nub/2d523b9ea6bc0dbfe2eb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // The following code only needs to be called once, and only requires to be called before user input is expected | |
| // It performs some very basic logic to handle search queries | |
| RACSignal* searchTextChanged = [self rac_signalForSelector:@selector(searchBar:textDidChange:) | |
| fromProtocol:@protocol(UISearchBarDelegate)]; | |
| __block NSInteger minimumSearchLength = 3; | |
| [[[[searchTextChanged | |
| map:^id(RACTuple *tuple) { | |
| // Grab searchText from method call, searchText is the second argument | |
| NSString* searchText = tuple.second; | |
| return searchText; | |
| }] doNext:^(NSString* searchText){ | |
| // Implement some view logic | |
| if (searchText.length <= minimumSearchLength && self.navController.viewControllers[0] != self.spotsDrillDown) { | |
| [self.navController setViewControllers:@[self.spotsDrillDown] animated:YES]; | |
| } | |
| }] filter:^BOOL(NSString* searchText){ | |
| // Limit searching to queries with at least a length of minimumSearchLength | |
| return searchText.length >= minimumSearchLength; | |
| }] subscribeNext:^(NSString* searchText){ | |
| // Present searchResults controller, perform search | |
| if (self.navController.viewControllers[0] != self.spotSelectViewController) { | |
| [self.navController setViewControllers:@[self.spotSelectViewController] animated:YES]; | |
| } | |
| self.spotSelectViewController.spots = [self.spotManager spotsMatchingQuery:searchText]; | |
| }]; | |
| /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |
| /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |
| /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |
| // This is the traditional alternative | |
| - (void)viewDidLoad { // or similar | |
| self.searchBar.delegate = self | |
| } | |
| #pragma mark - UISearchBarDelegate | |
| - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText { | |
| NSInteger minimumSearchLength = 3; | |
| if (searchText.length > minimumSearchLength) { | |
| self.searching = YES; | |
| self.spotSelectViewController.spots = [self.spotManager spotsMatchingQuery:searchText]; | |
| } else { | |
| self.searching = NO; | |
| [searchBar resignFirstResponder]; | |
| } | |
| } | |
| #pragma mark - Setters | |
| - (void)setSearching:(BOOL)searching { | |
| if (searching != _searching) { | |
| _searching = searching; | |
| if (_searching) { | |
| [self.navController setViewControllers:@[self.spotSelectViewController] animated:YES]; | |
| } else { | |
| [self.navController setViewControllers:@[self.spotsDrillDown] animated:YES]; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment