|
16 | 16 | #import "RDVTabBarController.h" |
17 | 17 | #import "RDVTabBarItem.h" |
18 | 18 | #import "NProjectViewController.h" |
| 19 | +#import "ProjectListCell.h" |
19 | 20 |
|
20 | | -@interface Project_RootViewController () |
| 21 | + |
| 22 | +@interface Project_RootViewController ()<UISearchBarDelegate, UITableViewDataSource, UITableViewDelegate> |
21 | 23 | @property (strong, nonatomic) XTSegmentControl *mySegmentControl; |
22 | 24 | @property (strong, nonatomic) iCarousel *myCarousel; |
23 | 25 | @property (strong, nonatomic) NSMutableDictionary *myProjectsDict; |
24 | 26 | @property (assign, nonatomic) NSInteger oldSelectedIndex; |
| 27 | + |
| 28 | +@property (strong, nonatomic) UISearchBar *mySearchBar; |
| 29 | +@property (strong, nonatomic) UISearchDisplayController *mySearchDisplayController; |
| 30 | + |
| 31 | +@property (strong, nonatomic) NSMutableArray *searchResults; |
| 32 | +@property (strong, nonatomic) NSString *searchString; |
| 33 | + |
25 | 34 | @end |
26 | 35 |
|
27 | 36 | @implementation Project_RootViewController |
@@ -85,17 +94,11 @@ - (void)viewDidLoad |
85 | 94 | } |
86 | 95 |
|
87 | 96 | - (void)setupNavBtn{ |
88 | | -// self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(searchItemClicked:)]; |
| 97 | + self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(searchItemClicked:)]; |
89 | 98 | self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(gotoNewProject)]; |
90 | 99 |
|
91 | 100 | } |
92 | 101 |
|
93 | | --(void)gotoNewProject{ |
94 | | - UIStoryboard *newProjectStoryboard = [UIStoryboard storyboardWithName:@"NewProject" bundle:nil]; |
95 | | - UIViewController *newProjectVC = [newProjectStoryboard instantiateViewControllerWithIdentifier:@"NewProjectVC"]; |
96 | | - [self.navigationController pushViewController:newProjectVC animated:YES]; |
97 | | -} |
98 | | - |
99 | 102 | - (void)configSegmentItems{ |
100 | 103 | _segmentItems = @[@"全部项目", @"我参与的", @"我创建的"]; |
101 | 104 | } |
@@ -137,19 +140,7 @@ - (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index |
137 | 140 | }else{ |
138 | 141 | __weak Project_RootViewController *weakSelf = self; |
139 | 142 | listView = [[ProjectListView alloc] initWithFrame:carousel.bounds projects:curPros block:^(Project *project) { |
140 | | - if (curPros.type < ProjectsTypeTaProject) { |
141 | | - [[Coding_NetAPIManager sharedManager] request_Project_UpdateVisit_WithObj:project andBlock:^(id data, NSError *error) { |
142 | | - if (data) { |
143 | | - project.un_read_activities_count = [NSNumber numberWithInteger:0]; |
144 | | - [listView refreshUI]; |
145 | | - } |
146 | | - }]; |
147 | | - } |
148 | | - |
149 | | - NProjectViewController *vc = [[NProjectViewController alloc] init]; |
150 | | - vc.myProject = project; |
151 | | - [weakSelf.navigationController pushViewController:vc animated:YES]; |
152 | | - |
| 143 | + [weakSelf goToProject:project]; |
153 | 144 | NSLog(@"\n=====%@", project.name); |
154 | 145 | } tabBarHeight:CGRectGetHeight(self.rdv_tabBarController.tabBar.frame)]; |
155 | 146 | } |
@@ -186,9 +177,170 @@ - (void)carouselCurrentItemIndexDidChange:(iCarousel *)carousel |
186 | 177 | }]; |
187 | 178 | } |
188 | 179 |
|
| 180 | +#pragma mark VC |
| 181 | +-(void)gotoNewProject{ |
| 182 | + UIStoryboard *newProjectStoryboard = [UIStoryboard storyboardWithName:@"NewProject" bundle:nil]; |
| 183 | + UIViewController *newProjectVC = [newProjectStoryboard instantiateViewControllerWithIdentifier:@"NewProjectVC"]; |
| 184 | + [self.navigationController pushViewController:newProjectVC animated:YES]; |
| 185 | +} |
| 186 | + |
| 187 | +- (void)goToProject:(Project *)project{ |
| 188 | + Projects *curPros = [_myProjectsDict objectForKey:[NSNumber numberWithUnsignedInteger:_myCarousel.currentItemIndex]]; |
| 189 | + ProjectListView *listView = (ProjectListView *)self.myCarousel.currentItemView; |
| 190 | + if (curPros.type < ProjectsTypeTaProject) { |
| 191 | + [[Coding_NetAPIManager sharedManager] request_Project_UpdateVisit_WithObj:project andBlock:^(id data, NSError *error) { |
| 192 | + if (data) { |
| 193 | + project.un_read_activities_count = [NSNumber numberWithInteger:0]; |
| 194 | + [listView refreshUI]; |
| 195 | + } |
| 196 | + }]; |
| 197 | + } |
| 198 | + NProjectViewController *vc = [[NProjectViewController alloc] init]; |
| 199 | + vc.myProject = project; |
| 200 | + [self.navigationController pushViewController:vc animated:YES]; |
| 201 | +} |
| 202 | + |
189 | 203 | #pragma mark Search |
190 | 204 | - (void)searchItemClicked:(id)sender{ |
191 | 205 | NSLog(@"%@", sender); |
| 206 | + if (!_mySearchBar) { |
| 207 | + _mySearchBar = ({ |
| 208 | + UISearchBar *searchBar = [[UISearchBar alloc] init]; |
| 209 | + searchBar.delegate = self; |
| 210 | + [searchBar sizeToFit]; |
| 211 | + [searchBar setPlaceholder:@"项目名称/创建人"]; |
| 212 | + [searchBar setTintColor:[UIColor whiteColor]]; |
| 213 | + [searchBar insertBGColor:[UIColor colorWithHexString:@"0x28303b"]]; |
| 214 | + searchBar; |
| 215 | + }); |
| 216 | + [self.navigationController.view addSubview:_mySearchBar]; |
| 217 | + [_mySearchBar setY:20]; |
| 218 | + } |
| 219 | + if (!_mySearchDisplayController) { |
| 220 | + _mySearchDisplayController = ({ |
| 221 | + UISearchDisplayController *searchVC = [[UISearchDisplayController alloc] initWithSearchBar:_mySearchBar contentsController:self]; |
| 222 | + searchVC.searchResultsTableView.contentInset = UIEdgeInsetsMake(CGRectGetHeight(self.mySearchBar.frame), 0, CGRectGetHeight(self.rdv_tabBarController.tabBar.frame), 0); |
| 223 | + searchVC.searchResultsTableView.tableFooterView = [[UIView alloc] init]; |
| 224 | + [searchVC.searchResultsTableView registerClass:[ProjectListCell class] forCellReuseIdentifier:kCellIdentifier_ProjectList]; |
| 225 | + searchVC.searchResultsDataSource = self; |
| 226 | + searchVC.searchResultsDelegate = self; |
| 227 | + if (kHigher_iOS_6_1) { |
| 228 | + searchVC.displaysSearchBarInNavigationBar = NO; |
| 229 | + } |
| 230 | + searchVC; |
| 231 | + }); |
| 232 | + } |
| 233 | + |
| 234 | + [_mySearchBar becomeFirstResponder]; |
| 235 | +} |
| 236 | + |
| 237 | +#pragma mark Table |
| 238 | +- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ |
| 239 | + return 1; |
| 240 | +} |
| 241 | + |
| 242 | +- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ |
| 243 | + if (self.searchResults) { |
| 244 | + return [self.searchResults count]; |
| 245 | + }else{ |
| 246 | + return 0; |
| 247 | + } |
| 248 | +} |
| 249 | + |
| 250 | +- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ |
| 251 | + ProjectListCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier_ProjectList forIndexPath:indexPath]; |
| 252 | + cell.project = [self.searchResults objectAtIndex:indexPath.row]; |
| 253 | + [tableView addLineforPlainCell:cell forRowAtIndexPath:indexPath withLeftSpace:kPaddingLeftWidth]; |
| 254 | + return cell; |
| 255 | + |
| 256 | +} |
| 257 | + |
| 258 | +- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ |
| 259 | + return [ProjectListCell cellHeight]; |
| 260 | +} |
| 261 | + |
| 262 | +- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ |
| 263 | + [tableView deselectRowAtIndexPath:indexPath animated:YES]; |
| 264 | + [self.mySearchBar resignFirstResponder]; |
| 265 | + [self goToProject:[self.searchResults objectAtIndex:indexPath.row]]; |
| 266 | +} |
| 267 | + |
| 268 | +#pragma mark UISearchBarDelegate |
| 269 | + |
| 270 | +- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{ |
| 271 | + NSLog(@"textDidChange: %@", searchText); |
| 272 | + [self searchProjectWithStr:searchText]; |
| 273 | +} |
| 274 | + |
| 275 | + |
| 276 | +- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{ |
| 277 | + NSLog(@"searchBarSearchButtonClicked: %@", searchBar.text); |
| 278 | + [self searchProjectWithStr:searchBar.text]; |
| 279 | +} |
| 280 | + |
| 281 | +- (void)searchProjectWithStr:(NSString *)string{ |
| 282 | + self.searchString = string; |
| 283 | + [self updateFilteredContentForSearchString:string]; |
| 284 | + [self.mySearchDisplayController.searchResultsTableView reloadData]; |
| 285 | +} |
| 286 | + |
| 287 | +- (void)updateFilteredContentForSearchString:(NSString *)searchString{ |
| 288 | + // start out with the entire list |
| 289 | + Projects *curPros = [_myProjectsDict objectForKey:@0]; |
| 290 | + if (curPros) { |
| 291 | + self.searchResults = [curPros.list mutableCopy]; |
| 292 | + }else{ |
| 293 | + self.searchResults = nil; |
| 294 | + } |
| 295 | + |
| 296 | + // strip out all the leading and trailing spaces |
| 297 | + NSString *strippedStr = [searchString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; |
| 298 | + |
| 299 | + // break up the search terms (separated by spaces) |
| 300 | + NSArray *searchItems = nil; |
| 301 | + if (strippedStr.length > 0) |
| 302 | + { |
| 303 | + searchItems = [strippedStr componentsSeparatedByString:@" "]; |
| 304 | + } |
| 305 | + |
| 306 | + // build all the "AND" expressions for each value in the searchString |
| 307 | + NSMutableArray *andMatchPredicates = [NSMutableArray array]; |
| 308 | + |
| 309 | + for (NSString *searchString in searchItems) |
| 310 | + { |
| 311 | + // each searchString creates an OR predicate for: name, global_key |
| 312 | + NSMutableArray *searchItemsPredicate = [NSMutableArray array]; |
| 313 | + |
| 314 | + // name field matching |
| 315 | + NSExpression *lhs = [NSExpression expressionForKeyPath:@"name"]; |
| 316 | + NSExpression *rhs = [NSExpression expressionForConstantValue:searchString]; |
| 317 | + NSPredicate *finalPredicate = [NSComparisonPredicate |
| 318 | + predicateWithLeftExpression:lhs |
| 319 | + rightExpression:rhs |
| 320 | + modifier:NSDirectPredicateModifier |
| 321 | + type:NSContainsPredicateOperatorType |
| 322 | + options:NSCaseInsensitivePredicateOption]; |
| 323 | + [searchItemsPredicate addObject:finalPredicate]; |
| 324 | + |
| 325 | + // owner_user_name field matching |
| 326 | + lhs = [NSExpression expressionForKeyPath:@"owner_user_name"]; |
| 327 | + rhs = [NSExpression expressionForConstantValue:searchString]; |
| 328 | + finalPredicate = [NSComparisonPredicate |
| 329 | + predicateWithLeftExpression:lhs |
| 330 | + rightExpression:rhs |
| 331 | + modifier:NSDirectPredicateModifier |
| 332 | + type:NSContainsPredicateOperatorType |
| 333 | + options:NSCaseInsensitivePredicateOption]; |
| 334 | + [searchItemsPredicate addObject:finalPredicate]; |
| 335 | + |
| 336 | + // at this OR predicate to ourr master AND predicate |
| 337 | + NSCompoundPredicate *orMatchPredicates = (NSCompoundPredicate *)[NSCompoundPredicate orPredicateWithSubpredicates:searchItemsPredicate]; |
| 338 | + [andMatchPredicates addObject:orMatchPredicates]; |
| 339 | + } |
| 340 | + |
| 341 | + NSCompoundPredicate *finalCompoundPredicate = (NSCompoundPredicate *)[NSCompoundPredicate andPredicateWithSubpredicates:andMatchPredicates]; |
| 342 | + |
| 343 | + self.searchResults = [[self.searchResults filteredArrayUsingPredicate:finalCompoundPredicate] mutableCopy]; |
192 | 344 | } |
193 | 345 |
|
194 | 346 | @end |
0 commit comments