2012-01-05 01:44:15 +01:00
|
|
|
//
|
2012-02-05 22:18:38 +01:00
|
|
|
// MPSearchDelegate.m
|
2012-02-03 08:45:09 +01:00
|
|
|
// MasterPassword
|
2012-01-05 01:44:15 +01:00
|
|
|
//
|
|
|
|
// Created by Maarten Billemont on 04/01/12.
|
|
|
|
// Copyright (c) 2012 Lyndir. All rights reserved.
|
|
|
|
//
|
|
|
|
|
2012-02-05 22:18:38 +01:00
|
|
|
#import "MPSearchDelegate.h"
|
2012-05-11 22:45:05 +02:00
|
|
|
#import "MPAppDelegate.h"
|
2012-06-11 23:39:50 +02:00
|
|
|
#import "LocalyticsSession.h"
|
2012-07-29 12:32:26 +02:00
|
|
|
#import "MPAppDelegate_Store.h"
|
2012-01-05 01:44:15 +01:00
|
|
|
|
2012-02-05 22:18:38 +01:00
|
|
|
@interface MPSearchDelegate (Private)
|
2012-01-05 01:44:15 +01:00
|
|
|
|
2012-01-25 00:30:43 +01:00
|
|
|
- (void)configureCell:(UITableViewCell *)cell inTableView:(UITableView *)tableView atIndexPath:(NSIndexPath *)indexPath;
|
2012-01-05 01:44:15 +01:00
|
|
|
|
|
|
|
@end
|
|
|
|
|
2012-02-05 22:18:38 +01:00
|
|
|
@implementation MPSearchDelegate
|
2012-05-15 23:27:05 +02:00
|
|
|
@synthesize tipView;
|
2012-02-26 23:01:17 +01:00
|
|
|
@synthesize query;
|
|
|
|
@synthesize dateFormatter;
|
2012-01-05 01:44:15 +01:00
|
|
|
@synthesize fetchedResultsController;
|
|
|
|
@synthesize delegate;
|
|
|
|
@synthesize searchDisplayController;
|
2012-01-25 00:30:43 +01:00
|
|
|
@synthesize searchTipContainer;
|
2012-01-05 01:44:15 +01:00
|
|
|
|
2012-02-26 23:01:17 +01:00
|
|
|
- (id)init {
|
2012-06-08 23:46:13 +02:00
|
|
|
|
2012-02-26 23:01:17 +01:00
|
|
|
if (!([super init]))
|
|
|
|
return nil;
|
2012-06-08 23:46:13 +02:00
|
|
|
|
|
|
|
self.dateFormatter = [NSDateFormatter new];
|
2012-02-26 23:01:17 +01:00
|
|
|
self.dateFormatter.dateStyle = NSDateFormatterShortStyle;
|
2012-06-08 23:46:13 +02:00
|
|
|
self.query = @"";
|
|
|
|
|
|
|
|
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:NSStringFromClass([MPElementEntity class])];
|
2012-06-10 21:43:18 +02:00
|
|
|
fetchRequest.sortDescriptors = [NSArray arrayWithObject:[[NSSortDescriptor alloc] initWithKey:@"uses_" ascending:NO]];
|
|
|
|
self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
|
|
|
|
managedObjectContext:[MPAppDelegate managedObjectContext]
|
2012-07-16 20:29:48 +02:00
|
|
|
sectionNameKeyPath:nil cacheName:nil];
|
2012-06-10 21:43:18 +02:00
|
|
|
self.fetchedResultsController.delegate = self;
|
2012-06-08 23:46:13 +02:00
|
|
|
|
|
|
|
self.tipView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 170)];
|
|
|
|
self.tipView.textAlignment = UITextAlignmentCenter;
|
|
|
|
self.tipView.backgroundColor = [UIColor clearColor];
|
|
|
|
self.tipView.textColor = [UIColor lightTextColor];
|
|
|
|
self.tipView.shadowColor = [UIColor blackColor];
|
|
|
|
self.tipView.shadowOffset = CGSizeMake(0, -1);
|
|
|
|
self.tipView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight
|
|
|
|
| UIViewAutoresizingFlexibleBottomMargin;
|
|
|
|
self.tipView.numberOfLines = 0;
|
|
|
|
self.tipView.font = [UIFont systemFontOfSize:14];
|
2012-05-15 23:27:05 +02:00
|
|
|
self.tipView.text =
|
2012-06-08 23:46:13 +02:00
|
|
|
@"Tip:\n"
|
|
|
|
@"Name your sites by their domain name:\n"
|
|
|
|
@"apple.com, twitter.com\n\n"
|
|
|
|
@"For email accounts, use the address:\n"
|
|
|
|
@"john@apple.com, john@gmail.com";
|
|
|
|
|
2012-02-26 23:01:17 +01:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
|
2012-06-08 23:46:13 +02:00
|
|
|
|
2012-02-26 23:01:17 +01:00
|
|
|
UITableView *tableView = self.searchDisplayController.searchResultsTableView;
|
2012-05-03 16:49:15 +02:00
|
|
|
for (NSInteger section = 0; section < [self numberOfSectionsInTableView:tableView]; ++section) {
|
|
|
|
NSInteger rowCount = [self tableView:tableView numberOfRowsInSection:section];
|
2012-02-26 23:01:17 +01:00
|
|
|
if (!rowCount)
|
|
|
|
continue;
|
2012-06-08 23:46:13 +02:00
|
|
|
|
2012-02-26 23:01:17 +01:00
|
|
|
if (rowCount == 1)
|
|
|
|
[self tableView:tableView didSelectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:section]];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
|
2012-06-08 23:46:13 +02:00
|
|
|
|
2012-06-11 23:39:50 +02:00
|
|
|
[TestFlight passCheckpoint:MPCheckpointCancelSearch];
|
2012-06-08 23:46:13 +02:00
|
|
|
|
2012-02-26 23:01:17 +01:00
|
|
|
[self.delegate didSelectElement:nil];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
|
2012-06-08 23:46:13 +02:00
|
|
|
|
2012-02-26 23:01:17 +01:00
|
|
|
if (searchBar.searchResultsButtonSelected && !searchText.length)
|
|
|
|
searchBar.text = @" ";
|
2012-06-08 23:46:13 +02:00
|
|
|
|
|
|
|
self.query = [searchBar.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
|
2012-02-26 23:01:17 +01:00
|
|
|
if (!self.query)
|
|
|
|
self.query = @"";
|
|
|
|
}
|
|
|
|
|
2012-01-19 17:40:39 +01:00
|
|
|
- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller {
|
2012-06-08 23:46:13 +02:00
|
|
|
|
2012-07-29 12:32:26 +02:00
|
|
|
controller.searchBar.prompt = @"Enter the site's name:";
|
|
|
|
controller.searchBar.showsScopeBar = controller.searchBar.selectedScopeButtonIndex != MPSearchScopeAll;
|
2012-07-17 22:57:11 +02:00
|
|
|
if (controller.searchBar.showsScopeBar)
|
|
|
|
controller.searchBar.scopeButtonTitles = [NSArray arrayWithObjects:@"All", @"Outdated", nil];
|
|
|
|
else
|
|
|
|
controller.searchBar.scopeButtonTitles = nil;
|
2012-06-08 23:46:13 +02:00
|
|
|
|
2012-01-25 00:30:43 +01:00
|
|
|
[UIView animateWithDuration:0.2f animations:^{
|
|
|
|
self.searchTipContainer.alpha = 0;
|
|
|
|
}];
|
2012-02-26 23:01:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)searchDisplayControllerDidBeginSearch:(UISearchDisplayController *)controller {
|
2012-06-08 23:46:13 +02:00
|
|
|
|
2012-07-29 12:32:26 +02:00
|
|
|
controller.searchBar.text = controller.searchBar.searchResultsButtonSelected? @" ": @"";
|
|
|
|
self.query = @"";
|
2012-01-05 01:44:15 +01:00
|
|
|
}
|
|
|
|
|
2012-01-19 17:40:39 +01:00
|
|
|
- (void)searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller {
|
2012-06-08 23:46:13 +02:00
|
|
|
|
|
|
|
controller.searchBar.prompt = nil;
|
2012-02-26 23:01:17 +01:00
|
|
|
controller.searchBar.searchResultsButtonSelected = NO;
|
2012-07-17 22:57:11 +02:00
|
|
|
controller.searchBar.selectedScopeButtonIndex = MPSearchScopeAll;
|
|
|
|
controller.searchBar.showsScopeBar = NO;
|
2012-01-19 17:40:39 +01:00
|
|
|
}
|
2012-01-05 01:44:15 +01:00
|
|
|
|
2012-01-25 00:30:43 +01:00
|
|
|
- (void)searchDisplayController:(UISearchDisplayController *)controller didLoadSearchResultsTableView:(UITableView *)tableView {
|
2012-06-08 23:46:13 +02:00
|
|
|
|
2012-01-25 00:30:43 +01:00
|
|
|
tableView.backgroundColor = [UIColor blackColor];
|
2012-06-08 23:46:13 +02:00
|
|
|
tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
|
|
|
|
tableView.rowHeight = 48.0f;
|
2012-01-25 00:30:43 +01:00
|
|
|
}
|
|
|
|
|
2012-01-05 01:44:15 +01:00
|
|
|
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
|
2012-06-08 23:46:13 +02:00
|
|
|
|
2012-05-27 14:26:13 +02:00
|
|
|
if (!controller.active)
|
|
|
|
return NO;
|
2012-06-08 23:46:13 +02:00
|
|
|
|
2012-07-17 22:57:11 +02:00
|
|
|
[self fetchData];
|
|
|
|
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption {
|
|
|
|
|
|
|
|
if (!controller.active)
|
|
|
|
return NO;
|
|
|
|
|
|
|
|
[self fetchData];
|
|
|
|
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)fetchData {
|
|
|
|
|
2012-02-26 23:01:17 +01:00
|
|
|
assert(self.query);
|
2012-07-29 12:32:26 +02:00
|
|
|
assert([MPAppDelegate get].activeUser);
|
|
|
|
|
|
|
|
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"user == %@", [MPAppDelegate get].activeUser];
|
|
|
|
if (self.query.length)
|
|
|
|
predicate = [NSCompoundPredicate
|
|
|
|
andPredicateWithSubpredicates:[NSArray arrayWithObjects:[NSPredicate predicateWithFormat:@"name BEGINSWITH[cd] %@", self.query],
|
|
|
|
predicate, nil]];
|
2012-06-08 23:46:13 +02:00
|
|
|
|
2012-07-17 22:57:11 +02:00
|
|
|
switch ((MPSearchScope)self.searchDisplayController.searchBar.selectedScopeButtonIndex) {
|
|
|
|
|
|
|
|
case MPSearchScopeAll:
|
|
|
|
break;
|
|
|
|
case MPSearchScopeOutdated:
|
2012-07-29 12:32:26 +02:00
|
|
|
predicate = [NSCompoundPredicate
|
|
|
|
andPredicateWithSubpredicates:[NSArray arrayWithObjects:[NSPredicate predicateWithFormat:@"requiresExplicitMigration_ == YES"],
|
|
|
|
predicate, nil]];
|
2012-07-17 22:57:11 +02:00
|
|
|
break;
|
|
|
|
}
|
2012-07-29 12:32:26 +02:00
|
|
|
self.fetchedResultsController.fetchRequest.predicate = predicate;
|
2012-06-08 23:46:13 +02:00
|
|
|
|
2012-01-05 01:44:15 +01:00
|
|
|
NSError *error;
|
|
|
|
if (![self.fetchedResultsController performFetch:&error])
|
2012-06-08 23:46:13 +02:00
|
|
|
err(@"Couldn't fetch elements: %@", error);
|
|
|
|
|
2012-05-15 23:27:05 +02:00
|
|
|
NSArray *subviews = self.searchDisplayController.searchBar.superview.subviews;
|
2012-05-20 19:17:22 +02:00
|
|
|
NSUInteger overlayIndex = [subviews indexOfObject:self.searchDisplayController.searchBar] + 1;
|
|
|
|
UIView *overlay = [subviews count] > overlayIndex? [subviews objectAtIndex:overlayIndex]: nil;
|
|
|
|
if (overlay == self.searchDisplayController.searchResultsTableView || ![overlay isKindOfClass:[UIControl class]])
|
|
|
|
overlay = nil;
|
|
|
|
if (self.tipView.superview != overlay) {
|
2012-05-15 23:27:05 +02:00
|
|
|
[self.tipView removeFromSuperview];
|
|
|
|
[overlay addSubview:self.tipView];
|
|
|
|
}
|
2012-01-05 01:44:15 +01:00
|
|
|
}
|
|
|
|
|
2012-05-12 16:04:18 +02:00
|
|
|
// See MP-14, also crashes easily on internal assertions etc..
|
|
|
|
//- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
|
|
|
|
//
|
|
|
|
// [self.searchDisplayController.searchResultsTableView beginUpdates];
|
|
|
|
//}
|
|
|
|
//
|
|
|
|
//- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
|
|
|
|
// atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath {
|
|
|
|
//
|
|
|
|
// UITableView *tableView = self.searchDisplayController.searchResultsTableView;
|
|
|
|
// switch(type) {
|
|
|
|
//
|
|
|
|
// case NSFetchedResultsChangeInsert:
|
|
|
|
// [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
|
|
|
|
// break;
|
|
|
|
//
|
|
|
|
// case NSFetchedResultsChangeDelete:
|
|
|
|
// [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
|
|
|
|
// break;
|
|
|
|
//
|
|
|
|
// case NSFetchedResultsChangeUpdate:
|
|
|
|
// [self configureCell:[tableView cellForRowAtIndexPath:indexPath]
|
|
|
|
// inTableView:tableView atIndexPath:indexPath];
|
|
|
|
// break;
|
|
|
|
//
|
|
|
|
// case NSFetchedResultsChangeMove:
|
|
|
|
// [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
|
|
|
|
// withRowAnimation:UITableViewRowAnimationFade];
|
|
|
|
// [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]
|
|
|
|
// withRowAnimation:UITableViewRowAnimationFade];
|
|
|
|
// break;
|
|
|
|
// }
|
|
|
|
//}
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id<NSFetchedResultsSectionInfo>)sectionInfo
|
|
|
|
// atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type {
|
|
|
|
//
|
|
|
|
// UITableView *tableView = self.searchDisplayController.searchResultsTableView;
|
|
|
|
// switch(type) {
|
|
|
|
//
|
|
|
|
// case NSFetchedResultsChangeInsert:
|
|
|
|
// [tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex]
|
|
|
|
// withRowAnimation:UITableViewRowAnimationFade];
|
|
|
|
// break;
|
|
|
|
//
|
|
|
|
// case NSFetchedResultsChangeDelete:
|
|
|
|
// [tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex]
|
|
|
|
// withRowAnimation:UITableViewRowAnimationFade];
|
|
|
|
// break;
|
|
|
|
// }
|
|
|
|
//}
|
2012-01-05 01:44:15 +01:00
|
|
|
|
|
|
|
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
|
2012-06-08 23:46:13 +02:00
|
|
|
|
2012-07-29 12:32:26 +02:00
|
|
|
dbg(@"controllerDidChangeContent on thread: %@", [NSThread currentThread].name);
|
2012-05-12 16:04:18 +02:00
|
|
|
[self.searchDisplayController.searchResultsTableView reloadData];
|
2012-05-15 23:27:05 +02:00
|
|
|
// [self.searchDisplayController.searchResultsTableView endUpdates];
|
2012-01-05 01:44:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
|
2012-06-08 23:46:13 +02:00
|
|
|
|
2012-05-12 16:04:18 +02:00
|
|
|
NSArray *sections = [self.fetchedResultsController sections];
|
|
|
|
NSUInteger sectionCount = [sections count];
|
2012-06-08 23:46:13 +02:00
|
|
|
|
2012-05-12 16:04:18 +02:00
|
|
|
if ([self.query length]) {
|
|
|
|
__block BOOL hasExactQueryMatch = NO;
|
|
|
|
[sections enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
|
|
|
|
id<NSFetchedResultsSectionInfo> sectionInfo = obj;
|
2012-06-05 00:55:02 +02:00
|
|
|
[[sectionInfo objects] enumerateObjectsUsingBlock:^(id obj_, NSUInteger idx_, BOOL *stop_) {
|
|
|
|
if ([[obj_ name] isEqualToString:self.query]) {
|
2012-05-12 16:04:18 +02:00
|
|
|
hasExactQueryMatch = YES;
|
2012-06-05 00:55:02 +02:00
|
|
|
*stop_ = YES;
|
2012-05-12 16:04:18 +02:00
|
|
|
}
|
|
|
|
}];
|
|
|
|
if (hasExactQueryMatch)
|
2012-06-08 23:46:13 +02:00
|
|
|
*stop = YES;
|
2012-05-12 16:04:18 +02:00
|
|
|
}];
|
|
|
|
if (!hasExactQueryMatch)
|
2012-06-08 23:46:13 +02:00
|
|
|
// Add a section for "new site".
|
2012-05-12 16:04:18 +02:00
|
|
|
++sectionCount;
|
|
|
|
}
|
2012-06-08 23:46:13 +02:00
|
|
|
|
2012-06-05 00:55:02 +02:00
|
|
|
return (NSInteger)sectionCount;
|
2012-01-05 01:44:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
|
2012-06-08 23:46:13 +02:00
|
|
|
|
2012-05-07 01:15:19 +02:00
|
|
|
NSArray *sections = [self.fetchedResultsController sections];
|
2012-06-05 00:55:02 +02:00
|
|
|
if (section < (NSInteger)[sections count])
|
|
|
|
return (NSInteger)[[sections objectAtIndex:(unsigned)section] numberOfObjects];
|
2012-06-08 23:46:13 +02:00
|
|
|
|
2012-02-26 23:01:17 +01:00
|
|
|
return 1;
|
2012-01-05 01:44:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
|
2012-06-08 23:46:13 +02:00
|
|
|
|
2012-02-05 22:18:38 +01:00
|
|
|
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MPElementSearch"];
|
2012-01-25 00:30:43 +01:00
|
|
|
if (!cell) {
|
2012-02-26 23:01:17 +01:00
|
|
|
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MPElementSearch"];
|
2012-06-08 23:46:13 +02:00
|
|
|
|
2012-02-26 23:01:17 +01:00
|
|
|
UIImageView *backgroundImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ui_list_middle"]];
|
2012-06-08 23:46:13 +02:00
|
|
|
backgroundImageView.frame = CGRectMake(-5, 0, 330, 34);
|
2012-02-26 23:01:17 +01:00
|
|
|
backgroundImageView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
|
2012-06-08 23:46:13 +02:00
|
|
|
backgroundImageView.contentStretch = CGRectMake(0.2f, 0.2f, 0.6f, 0.6f);
|
2012-02-26 23:01:17 +01:00
|
|
|
UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 34)];
|
2012-01-25 00:30:43 +01:00
|
|
|
[backgroundView addSubview:backgroundImageView];
|
2012-02-26 23:01:17 +01:00
|
|
|
backgroundView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
|
2012-06-08 23:46:13 +02:00
|
|
|
|
|
|
|
cell.backgroundView = backgroundView;
|
|
|
|
cell.textLabel.backgroundColor = [UIColor clearColor];
|
|
|
|
cell.textLabel.textColor = [UIColor whiteColor];
|
2012-01-25 00:30:43 +01:00
|
|
|
cell.detailTextLabel.backgroundColor = [UIColor clearColor];
|
2012-06-08 23:46:13 +02:00
|
|
|
cell.detailTextLabel.textColor = [UIColor lightGrayColor];
|
|
|
|
cell.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
|
|
|
|
cell.clipsToBounds = YES;
|
2012-01-25 00:30:43 +01:00
|
|
|
}
|
2012-06-08 23:46:13 +02:00
|
|
|
|
2012-01-25 00:30:43 +01:00
|
|
|
[self configureCell:cell inTableView:tableView atIndexPath:indexPath];
|
2012-06-08 23:46:13 +02:00
|
|
|
|
2012-01-05 01:44:15 +01:00
|
|
|
return cell;
|
|
|
|
}
|
|
|
|
|
2012-01-25 00:30:43 +01:00
|
|
|
- (void)configureCell:(UITableViewCell *)cell inTableView:(UITableView *)tableView atIndexPath:(NSIndexPath *)indexPath {
|
2012-06-08 23:46:13 +02:00
|
|
|
|
2012-06-05 00:55:02 +02:00
|
|
|
if (indexPath.section < (NSInteger)[[self.fetchedResultsController sections] count]) {
|
2012-02-05 22:18:38 +01:00
|
|
|
MPElementEntity *element = [self.fetchedResultsController objectAtIndexPath:indexPath];
|
2012-06-08 23:46:13 +02:00
|
|
|
|
|
|
|
cell.textLabel.text = element.name;
|
2012-02-26 23:01:17 +01:00
|
|
|
cell.detailTextLabel.text = [NSString stringWithFormat:@"Used %d times, last on %@",
|
2012-06-08 23:46:13 +02:00
|
|
|
element.uses, [self.dateFormatter stringFromDate:element.lastUsed]];
|
2012-01-25 00:30:43 +01:00
|
|
|
} else {
|
|
|
|
// "New" section
|
2012-06-08 23:46:13 +02:00
|
|
|
cell.textLabel.text = self.query;
|
2012-02-26 23:01:17 +01:00
|
|
|
cell.detailTextLabel.text = @"Create a new site.";
|
2012-01-05 01:44:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
|
2012-06-08 23:46:13 +02:00
|
|
|
|
2012-06-05 00:55:02 +02:00
|
|
|
if (indexPath.section < (NSInteger)[[self.fetchedResultsController sections] count])
|
2012-02-05 00:36:19 +01:00
|
|
|
[self.delegate didSelectElement:[self.fetchedResultsController objectAtIndexPath:indexPath]];
|
2012-06-08 23:46:13 +02:00
|
|
|
|
2012-01-25 00:30:43 +01:00
|
|
|
else {
|
|
|
|
// "New" section.
|
2012-02-26 23:01:17 +01:00
|
|
|
NSString *siteName = self.query;
|
2012-02-27 23:38:28 +01:00
|
|
|
[PearlAlert showAlertWithTitle:@"New Site"
|
2012-06-05 00:55:02 +02:00
|
|
|
message:PearlString(@"Do you want to create a new site named:\n%@", siteName)
|
2012-05-07 01:15:19 +02:00
|
|
|
viewStyle:UIAlertViewStyleDefault
|
2012-06-08 23:46:13 +02:00
|
|
|
initAlert:nil tappedButtonBlock:^(UIAlertView *alert, NSInteger buttonIndex) {
|
|
|
|
[tableView deselectRowAtIndexPath:indexPath animated:YES];
|
|
|
|
|
|
|
|
if (buttonIndex == [alert cancelButtonIndex])
|
|
|
|
return;
|
|
|
|
|
|
|
|
[self.fetchedResultsController.managedObjectContext performBlock:^{
|
2012-06-11 16:15:49 +02:00
|
|
|
MPElementType type = [MPAppDelegate get].activeUser.defaultType;
|
2012-07-17 22:57:11 +02:00
|
|
|
MPElementEntity *element = [NSEntityDescription insertNewObjectForEntityForName:[MPAlgorithmDefault classNameOfType:type]
|
2012-06-08 23:46:13 +02:00
|
|
|
inManagedObjectContext:self.fetchedResultsController.managedObjectContext];
|
2012-06-11 16:15:49 +02:00
|
|
|
assert([MPAppDelegate get].activeUser);
|
2012-06-08 23:46:13 +02:00
|
|
|
|
2012-07-17 22:57:11 +02:00
|
|
|
element.name = siteName;
|
|
|
|
element.user = [MPAppDelegate get].activeUser;
|
|
|
|
element.type = type;
|
|
|
|
element.version = MPAlgorithmDefaultVersion;
|
2012-06-08 23:46:13 +02:00
|
|
|
|
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
|
[self.delegate didSelectElement:element];
|
|
|
|
});
|
|
|
|
}];
|
2012-07-16 20:29:48 +02:00
|
|
|
} cancelTitle:[PearlStrings get].commonButtonCancel otherTitles:[PearlStrings get].commonButtonYes, nil];
|
2012-01-25 00:30:43 +01:00
|
|
|
}
|
2012-01-05 01:44:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
|
2012-06-08 23:46:13 +02:00
|
|
|
|
2012-06-05 00:55:02 +02:00
|
|
|
if (section < (NSInteger)[[self.fetchedResultsController sections] count])
|
2012-05-03 16:49:15 +02:00
|
|
|
return [[[self.fetchedResultsController sections] objectAtIndex:(unsigned)section] name];
|
2012-06-08 23:46:13 +02:00
|
|
|
|
2012-02-26 23:01:17 +01:00
|
|
|
return @"";
|
2012-01-05 01:44:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
|
2012-06-08 23:46:13 +02:00
|
|
|
|
2012-01-05 01:44:15 +01:00
|
|
|
return [self.fetchedResultsController sectionIndexTitles];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
|
2012-06-08 23:46:13 +02:00
|
|
|
|
2012-01-05 01:44:15 +01:00
|
|
|
return [self.fetchedResultsController sectionForSectionIndexTitle:title atIndex:index];
|
|
|
|
}
|
|
|
|
|
2012-06-08 23:46:13 +02:00
|
|
|
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
|
|
|
|
forRowAtIndexPath:(NSIndexPath *)indexPath {
|
|
|
|
|
2012-06-05 00:55:02 +02:00
|
|
|
if (indexPath.section < (NSInteger)[[self.fetchedResultsController sections] count]) {
|
2012-02-26 23:01:17 +01:00
|
|
|
if (editingStyle == UITableViewCellEditingStyleDelete)
|
|
|
|
[self.fetchedResultsController.managedObjectContext performBlock:^{
|
|
|
|
MPElementEntity *element = [self.fetchedResultsController objectAtIndexPath:indexPath];
|
2012-06-14 21:56:54 +02:00
|
|
|
|
|
|
|
inf(@"Deleting element: %@", element.name);
|
2012-02-26 23:01:17 +01:00
|
|
|
[self.fetchedResultsController.managedObjectContext deleteObject:element];
|
2012-06-08 23:46:13 +02:00
|
|
|
|
2012-06-11 23:39:50 +02:00
|
|
|
[TestFlight passCheckpoint:MPCheckpointDeleteElement];
|
|
|
|
[[LocalyticsSession sharedLocalyticsSession] tagEvent:MPCheckpointDeleteElement
|
|
|
|
attributes:[NSDictionary dictionaryWithObjectsAndKeys:
|
2012-07-17 22:57:11 +02:00
|
|
|
element.typeName, @"type",
|
|
|
|
PearlUnsignedInteger(element.version), @"version",
|
2012-06-11 23:39:50 +02:00
|
|
|
nil]];
|
2012-02-26 23:01:17 +01:00
|
|
|
}];
|
|
|
|
}
|
2012-01-05 01:44:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@end
|