2012-01-05 00:44:15 +00:00
|
|
|
//
|
|
|
|
// OPSearchDelegate.m
|
|
|
|
// OnePassword
|
|
|
|
//
|
|
|
|
// Created by Maarten Billemont on 04/01/12.
|
|
|
|
// Copyright (c) 2012 Lyndir. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import "OPSearchDelegate.h"
|
|
|
|
#import "OPAppDelegate.h"
|
2012-01-19 16:40:39 +00:00
|
|
|
#import "OPElementGeneratedEntity.h"
|
2012-01-05 00:44:15 +00:00
|
|
|
|
|
|
|
@interface OPSearchDelegate (Private)
|
|
|
|
|
2012-01-24 23:30:43 +00:00
|
|
|
- (void)configureCell:(UITableViewCell *)cell inTableView:(UITableView *)tableView atIndexPath:(NSIndexPath *)indexPath;
|
2012-01-05 00:44:15 +00:00
|
|
|
- (void)update;
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation OPSearchDelegate
|
|
|
|
@synthesize fetchedResultsController;
|
|
|
|
@synthesize delegate;
|
|
|
|
@synthesize searchDisplayController;
|
2012-01-24 23:30:43 +00:00
|
|
|
@synthesize searchTipContainer;
|
2012-01-05 00:44:15 +00:00
|
|
|
|
2012-01-19 16:40:39 +00:00
|
|
|
- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller {
|
|
|
|
|
|
|
|
self.searchDisplayController.searchBar.text = @"";
|
|
|
|
self.searchDisplayController.searchBar.prompt = @"Enter the site's domain name (eg. apple.com):";
|
2012-01-24 23:30:43 +00:00
|
|
|
|
|
|
|
[UIView animateWithDuration:0.2f animations:^{
|
|
|
|
self.searchTipContainer.alpha = 0;
|
|
|
|
}];
|
|
|
|
|
2012-01-19 16:40:39 +00:00
|
|
|
[self update];
|
2012-01-05 00:44:15 +00:00
|
|
|
}
|
|
|
|
|
2012-01-19 16:40:39 +00:00
|
|
|
- (void)searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller {
|
|
|
|
|
|
|
|
self.searchDisplayController.searchBar.prompt = nil;
|
|
|
|
}
|
2012-01-05 00:44:15 +00:00
|
|
|
|
2012-01-24 23:30:43 +00:00
|
|
|
- (void)searchDisplayController:(UISearchDisplayController *)controller didLoadSearchResultsTableView:(UITableView *)tableView {
|
|
|
|
|
|
|
|
tableView.backgroundColor = [UIColor blackColor];
|
|
|
|
tableView.rowHeight = 34.0f;
|
|
|
|
tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
|
|
|
|
}
|
|
|
|
|
2012-01-19 16:40:39 +00:00
|
|
|
- (void)searchDisplayController:(UISearchDisplayController *)controller willShowSearchResultsTableView:(UITableView *)tableView {
|
|
|
|
|
|
|
|
[tableView setEditing:self.searchDisplayController.searchContentsController.editing animated:NO];
|
2012-01-05 00:44:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
|
2012-01-19 16:40:39 +00:00
|
|
|
|
2012-01-05 00:44:15 +00:00
|
|
|
[self update];
|
2012-01-19 16:40:39 +00:00
|
|
|
|
2012-01-05 00:44:15 +00:00
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)update {
|
2012-01-19 16:40:39 +00:00
|
|
|
|
2012-01-29 11:41:48 +00:00
|
|
|
NSString *query = self.searchDisplayController.searchBar.text;
|
|
|
|
if (!query)
|
|
|
|
query = @"";
|
|
|
|
|
|
|
|
NSFetchRequest *fetchRequest = [[OPAppDelegate get].managedObjectModel
|
|
|
|
fetchRequestFromTemplateWithName:@"OPSearchElement"
|
|
|
|
substitutionVariables:[NSDictionary dictionaryWithObjectsAndKeys:
|
|
|
|
query, @"query",
|
|
|
|
[OPAppDelegate get].keyPhraseHashHex, @"mpHashHex",
|
|
|
|
nil]];
|
2012-01-05 00:44:15 +00:00
|
|
|
[fetchRequest setSortDescriptors:
|
2012-01-19 16:40:39 +00:00
|
|
|
[NSArray arrayWithObject:[[NSSortDescriptor alloc] initWithKey:@"uses" ascending:NO]]];
|
|
|
|
|
2012-01-05 00:44:15 +00:00
|
|
|
self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
|
2012-01-19 16:40:39 +00:00
|
|
|
managedObjectContext:[OPAppDelegate managedObjectContext]
|
2012-01-05 00:44:15 +00:00
|
|
|
sectionNameKeyPath:nil cacheName:nil];
|
|
|
|
self.fetchedResultsController.delegate = self;
|
2012-01-19 16:40:39 +00:00
|
|
|
|
2012-01-05 00:44:15 +00:00
|
|
|
NSError *error;
|
|
|
|
if (![self.fetchedResultsController performFetch:&error])
|
|
|
|
err(@"Couldn't fetch elements: %@", error);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
|
|
|
|
|
|
|
|
[self.searchDisplayController.searchResultsTableView beginUpdates];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath {
|
|
|
|
|
2012-01-24 23:30:43 +00:00
|
|
|
UITableView *tableView = self.searchDisplayController.searchResultsTableView;
|
2012-01-05 00:44:15 +00:00
|
|
|
switch(type) {
|
|
|
|
|
|
|
|
case NSFetchedResultsChangeInsert:
|
2012-01-24 23:30:43 +00:00
|
|
|
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
|
2012-01-05 00:44:15 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case NSFetchedResultsChangeDelete:
|
2012-01-24 23:30:43 +00:00
|
|
|
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
|
2012-01-05 00:44:15 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case NSFetchedResultsChangeUpdate:
|
2012-01-24 23:30:43 +00:00
|
|
|
[self configureCell:[tableView cellForRowAtIndexPath:indexPath]
|
|
|
|
inTableView:tableView
|
2012-01-05 00:44:15 +00:00
|
|
|
atIndexPath:indexPath];
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NSFetchedResultsChangeMove:
|
2012-01-24 23:30:43 +00:00
|
|
|
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
|
|
|
|
withRowAnimation:UITableViewRowAnimationFade];
|
|
|
|
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]
|
|
|
|
withRowAnimation:UITableViewRowAnimationFade];
|
2012-01-05 00:44:15 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type {
|
|
|
|
|
2012-01-24 23:30:43 +00:00
|
|
|
UITableView *tableView = self.searchDisplayController.searchResultsTableView;
|
2012-01-05 00:44:15 +00:00
|
|
|
switch(type) {
|
|
|
|
|
|
|
|
case NSFetchedResultsChangeInsert:
|
2012-01-24 23:30:43 +00:00
|
|
|
[tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex]
|
|
|
|
withRowAnimation:UITableViewRowAnimationFade];
|
2012-01-05 00:44:15 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case NSFetchedResultsChangeDelete:
|
2012-01-24 23:30:43 +00:00
|
|
|
[tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex]
|
|
|
|
withRowAnimation:UITableViewRowAnimationFade];
|
2012-01-05 00:44:15 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
|
|
|
|
|
|
|
|
[self.searchDisplayController.searchResultsTableView endUpdates];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
|
|
|
|
|
|
|
|
return [[self.fetchedResultsController sections] count] + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
|
|
|
|
|
2012-01-24 23:30:43 +00:00
|
|
|
if (section == [self numberOfSectionsInTableView:tableView] - 1)
|
2012-01-05 00:44:15 +00:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
return [[[self.fetchedResultsController sections] objectAtIndex:section] numberOfObjects];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
|
|
|
|
|
|
|
|
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"OPElementSearch"];
|
2012-01-24 23:30:43 +00:00
|
|
|
if (!cell) {
|
2012-01-05 00:44:15 +00:00
|
|
|
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"OPElementSearch"];
|
2012-01-24 23:30:43 +00:00
|
|
|
UIImageView *backgroundImageView = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:@"ui_list_middle"] resizableImageWithCapInsets:UIEdgeInsetsMake(5, 5, 5, 5)]];
|
|
|
|
backgroundImageView.frame = CGRectMake(-5, 0, 330, 34);
|
|
|
|
UIView *backgroundView = [[UIView alloc] initWithFrame:cell.frame];
|
|
|
|
[backgroundView addSubview:backgroundImageView];
|
|
|
|
|
|
|
|
cell.backgroundView = backgroundView;
|
|
|
|
cell.textLabel.backgroundColor = [UIColor clearColor];
|
|
|
|
cell.textLabel.textColor = [UIColor whiteColor];
|
|
|
|
cell.detailTextLabel.backgroundColor = [UIColor clearColor];
|
|
|
|
cell.detailTextLabel.textColor = [UIColor lightGrayColor];
|
|
|
|
}
|
2012-01-05 00:44:15 +00:00
|
|
|
|
2012-01-24 23:30:43 +00:00
|
|
|
[self configureCell:cell inTableView:tableView atIndexPath:indexPath];
|
2012-01-05 00:44:15 +00:00
|
|
|
|
|
|
|
return cell;
|
|
|
|
}
|
|
|
|
|
2012-01-24 23:30:43 +00:00
|
|
|
- (void)configureCell:(UITableViewCell *)cell inTableView:(UITableView *)tableView atIndexPath:(NSIndexPath *)indexPath {
|
2012-01-05 00:44:15 +00:00
|
|
|
|
2012-01-24 23:30:43 +00:00
|
|
|
if (indexPath.section < [[self.fetchedResultsController sections] count]) {
|
2012-01-05 00:44:15 +00:00
|
|
|
OPElementEntity *element = [self.fetchedResultsController objectAtIndexPath:indexPath];
|
2012-01-19 16:40:39 +00:00
|
|
|
|
2012-01-05 00:44:15 +00:00
|
|
|
cell.textLabel.text = element.name;
|
|
|
|
cell.detailTextLabel.text = [NSString stringWithFormat:@"%d", element.uses];
|
2012-01-24 23:30:43 +00:00
|
|
|
} else {
|
|
|
|
// "New" section
|
|
|
|
cell.textLabel.text = self.searchDisplayController.searchBar.text;
|
|
|
|
cell.detailTextLabel.text = @"New";
|
2012-01-05 00:44:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
|
|
|
|
|
|
|
|
OPElementEntity *element;
|
2012-01-24 23:30:43 +00:00
|
|
|
if (indexPath.section < [[self.fetchedResultsController sections] count])
|
|
|
|
element = [self.fetchedResultsController objectAtIndexPath:indexPath];
|
|
|
|
|
|
|
|
else {
|
|
|
|
// "New" section.
|
2012-01-19 16:40:39 +00:00
|
|
|
element = [NSEntityDescription insertNewObjectForEntityForName:NSStringFromClass([OPElementGeneratedEntity class])
|
|
|
|
inManagedObjectContext:[OPAppDelegate managedObjectContext]];
|
2012-01-29 11:41:48 +00:00
|
|
|
assert([element isKindOfClass:ClassFromOPElementType(element.type)]);
|
|
|
|
|
2012-01-05 00:44:15 +00:00
|
|
|
element.name = self.searchDisplayController.searchBar.text;
|
2012-01-29 11:41:48 +00:00
|
|
|
element.mpHashHex = [OPAppDelegate get].keyPhraseHashHex;
|
2012-01-24 23:30:43 +00:00
|
|
|
}
|
2012-01-05 00:44:15 +00:00
|
|
|
|
|
|
|
[self.delegate didSelectElement:element];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
|
|
|
|
|
|
|
|
if (--section == -1)
|
|
|
|
return @"";
|
|
|
|
|
|
|
|
return [[[self.fetchedResultsController sections] objectAtIndex:section] name];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
|
|
|
|
|
|
|
|
return [self.fetchedResultsController sectionIndexTitles];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
|
|
|
|
|
|
|
|
return [self.fetchedResultsController sectionForSectionIndexTitle:title atIndex:index];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
|
2012-01-19 16:40:39 +00:00
|
|
|
|
2012-01-24 23:30:43 +00:00
|
|
|
if (indexPath.section == [[self.fetchedResultsController sections] count])
|
|
|
|
// "New" section.
|
|
|
|
return;
|
2012-01-19 16:40:39 +00:00
|
|
|
|
2012-01-05 00:44:15 +00:00
|
|
|
if (editingStyle == UITableViewCellEditingStyleDelete) {
|
|
|
|
OPElementEntity *element = [self.fetchedResultsController objectAtIndexPath:indexPath];
|
2012-01-19 16:40:39 +00:00
|
|
|
|
|
|
|
[[OPAppDelegate managedObjectContext] deleteObject:element];
|
2012-01-05 00:44:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@end
|