Skip to content

Commit

Permalink
Added the ability to long press on products to select them
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Bowes authored and Kevin Bowes committed Jun 21, 2012
1 parent 11cc282 commit fd1a823
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 63 deletions.
6 changes: 4 additions & 2 deletions Classes/DashboardViewController.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@


@interface DashboardViewController : UIViewController <UITableViewDataSource, UITableViewDelegate, ColorPickerViewControllerDelegate> { @interface DashboardViewController : UIViewController <UITableViewDataSource, UITableViewDelegate, ColorPickerViewControllerDelegate> {


Product *selectedProduct; NSArray *selectedProducts;
ASAccount *account; ASAccount *account;
UITableView *productsTableView; UITableView *productsTableView;
UIView *topView; UIView *topView;
Expand All @@ -37,7 +37,7 @@
@property (nonatomic, retain) ASAccount *account; @property (nonatomic, retain) ASAccount *account;
@property (nonatomic, retain) NSArray *products; @property (nonatomic, retain) NSArray *products;
@property (nonatomic, retain) NSArray *visibleProducts; @property (nonatomic, retain) NSArray *visibleProducts;
@property (nonatomic, retain) Product *selectedProduct; @property (nonatomic, retain) NSArray *selectedProducts;
@property (nonatomic, retain) UITableView *productsTableView; @property (nonatomic, retain) UITableView *productsTableView;
@property (nonatomic, retain) UIView *topView; @property (nonatomic, retain) UIView *topView;
@property (nonatomic, retain) UIImageView *shadowView; @property (nonatomic, retain) UIImageView *shadowView;
Expand All @@ -59,4 +59,6 @@
- (void)stopDownload:(id)sender; - (void)stopDownload:(id)sender;
- (UIView *)accessoryViewForRowAtIndexPath:(NSIndexPath *)indexPath; - (UIView *)accessoryViewForRowAtIndexPath:(NSIndexPath *)indexPath;


- (void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer;

@end @end
69 changes: 56 additions & 13 deletions Classes/DashboardViewController.m
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@


@implementation DashboardViewController @implementation DashboardViewController


@synthesize account, products, visibleProducts, selectedProduct; @synthesize account, products, visibleProducts, selectedProducts;
@synthesize productsTableView, topView, shadowView, colorPopover, statusToolbar, stopButtonItem, activityIndicator, statusLabel, progressBar; @synthesize productsTableView, topView, shadowView, colorPopover, statusToolbar, stopButtonItem, activityIndicator, statusLabel, progressBar;
@synthesize activeSheet; @synthesize activeSheet;


Expand All @@ -24,6 +24,7 @@ - (id)initWithAccount:(ASAccount *)anAccount
self = [super initWithNibName:nil bundle:nil]; self = [super initWithNibName:nil bundle:nil];
if (self) { if (self) {
self.account = anAccount; self.account = anAccount;
self.selectedProducts = nil;
self.hidesBottomBarWhenPushed = [[UIDevice currentDevice] userInterfaceIdiom] != UIUserInterfaceIdiomPad; self.hidesBottomBarWhenPushed = [[UIDevice currentDevice] userInterfaceIdiom] != UIUserInterfaceIdiomPad;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(contextDidChange:) name:NSManagedObjectContextObjectsDidChangeNotification object:[account managedObjectContext]]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(contextDidChange:) name:NSManagedObjectContextObjectsDidChangeNotification object:[account managedObjectContext]];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willShowPasscodeLock:) name:ASWillShowPasscodeLockNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willShowPasscodeLock:) name:ASWillShowPasscodeLockNotification object:nil];
Expand Down Expand Up @@ -112,6 +113,7 @@ - (void)loadView
UIEdgeInsets productsTableScrollIndicatorInset = (statusVisible) ? UIEdgeInsetsMake(0, 0, 44, 0) : UIEdgeInsetsMake(0, 0, 0, 0); UIEdgeInsets productsTableScrollIndicatorInset = (statusVisible) ? UIEdgeInsetsMake(0, 0, 44, 0) : UIEdgeInsetsMake(0, 0, 0, 0);
productsTableView.contentInset = productsTableContentInset; productsTableView.contentInset = productsTableContentInset;
productsTableView.scrollIndicatorInsets = productsTableScrollIndicatorInset; productsTableView.scrollIndicatorInsets = productsTableScrollIndicatorInset;
productsTableView.allowsMultipleSelection = YES;


self.view.backgroundColor = [UIColor scrollViewTexturedBackgroundColor]; self.view.backgroundColor = [UIColor scrollViewTexturedBackgroundColor];
[self.view addSubview:self.productsTableView]; [self.view addSubview:self.productsTableView];
Expand Down Expand Up @@ -251,16 +253,16 @@ - (void)reloadData
- (void)reloadTableView - (void)reloadTableView
{ {
//Reload the table view, preserving the current selection: //Reload the table view, preserving the current selection:
NSIndexPath *selectedIndexPath = [self.productsTableView indexPathForSelectedRow];
if (!selectedIndexPath) {
if (self.selectedProduct) {
selectedIndexPath = [NSIndexPath indexPathForRow:[self.products indexOfObject:self.selectedProduct] + 1 inSection:0];
} else {
selectedIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
}
}
[self.productsTableView reloadData]; [self.productsTableView reloadData];
[self.productsTableView selectRowAtIndexPath:selectedIndexPath animated:NO scrollPosition:UITableViewScrollPositionNone]; NSArray *selectedIndexPaths = [self.productsTableView indexPathsForSelectedRows];
if ([selectedIndexPaths count]) {
for (NSIndexPath* selectedIndexPath in selectedIndexPaths) {
[self.productsTableView selectRowAtIndexPath:selectedIndexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
}
} else {
NSIndexPath* selectedIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.productsTableView selectRowAtIndexPath:selectedIndexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
}
} }


- (void)changeColor:(UIButton *)sender - (void)changeColor:(UIButton *)sender
Expand Down Expand Up @@ -330,20 +332,61 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
[cell.colorButton addTarget:self action:@selector(changeColor:) forControlEvents:UIControlEventTouchUpInside]; [cell.colorButton addTarget:self action:@selector(changeColor:) forControlEvents:UIControlEventTouchUpInside];


cell.accessoryView = [self accessoryViewForRowAtIndexPath:indexPath]; cell.accessoryView = [self accessoryViewForRowAtIndexPath:indexPath];


bool t = [self.selectedProducts containsObject:product];
if (t) {
[tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
}

UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc]
initWithTarget:self action:@selector(handleLongPress:)];
[cell addGestureRecognizer:lpgr];
[lpgr release];

return cell; return cell;
} }


- (void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer
{
if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {
DashboardAppCell * cell = ((DashboardAppCell*)gestureRecognizer.view);
int i = [self.visibleProducts indexOfObject:cell.product];
NSIndexPath * indexPath = [NSIndexPath indexPathForRow:i+1 inSection:0];
[self.productsTableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];

if (cell.product) {
if (self.selectedProducts) {
self.selectedProducts = [self.selectedProducts arrayByAddingObject:cell.product];
} else {
self.selectedProducts = [NSArray arrayWithObject:cell.product];
}
} else {
self.selectedProducts = nil;
[self deselectAllRowsInTableView:self.productsTableView exceptForIndexPath:nil];
NSIndexPath * indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.productsTableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
}
}
}

- (UIView *)accessoryViewForRowAtIndexPath:(NSIndexPath *)indexPath - (UIView *)accessoryViewForRowAtIndexPath:(NSIndexPath *)indexPath
{ {
return nil; return nil;
} }


#pragma mark - Table view delegate #pragma mark - Table view delegate


- (void)deselectAllRowsInTableView:(UITableView*)tableView exceptForIndexPath:(NSIndexPath*)indexPath {
for (NSIndexPath * i in [tableView indexPathsForSelectedRows]) {
if ((indexPath.row == i.row) && (indexPath.section == i.section)) continue;
[tableView deselectRowAtIndexPath:i animated:NO];
}
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{ {
self.selectedProduct = [(indexPath.row == 0) ? nil : self.visibleProducts objectAtIndex:indexPath.row - 1]; [self deselectAllRowsInTableView:tableView exceptForIndexPath:indexPath];
self.selectedProducts = (indexPath.row == 0) ? nil : [NSArray arrayWithObject:[self.visibleProducts objectAtIndex:indexPath.row - 1]];
} }


- (void)scrollViewDidScroll:(UIScrollView *)scrollView - (void)scrollViewDidScroll:(UIScrollView *)scrollView
Expand All @@ -363,7 +406,7 @@ - (void)dealloc
[account release]; [account release];
[products release]; [products release];
[visibleProducts release]; [visibleProducts release];
[selectedProduct release]; [selectedProducts release];
[productsTableView release]; [productsTableView release];
[topView release]; [topView release];
[shadowView release]; [shadowView release];
Expand Down
4 changes: 2 additions & 2 deletions Classes/ReviewListViewController.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
@interface ReviewListViewController : UITableViewController <NSFetchedResultsControllerDelegate> { @interface ReviewListViewController : UITableViewController <NSFetchedResultsControllerDelegate> {


ASAccount *account; ASAccount *account;
Product *product; NSArray *products;
NSUInteger rating; NSUInteger rating;


NSFetchedResultsController *fetchedResultsController; NSFetchedResultsController *fetchedResultsController;
Expand All @@ -23,6 +23,6 @@
@property (nonatomic, retain) NSFetchedResultsController *fetchedResultsController; @property (nonatomic, retain) NSFetchedResultsController *fetchedResultsController;
@property (nonatomic, retain) NSManagedObjectContext *managedObjectContext; @property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;


- (id)initWithAccount:(ASAccount *)acc product:(Product *)reviewProduct rating:(NSUInteger)ratingFilter; - (id)initWithAccount:(ASAccount *)acc products:(NSArray *)reviewProducts rating:(NSUInteger)ratingFilter;


@end @end
41 changes: 25 additions & 16 deletions Classes/ReviewListViewController.m
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ @implementation ReviewListViewController


@synthesize fetchedResultsController, managedObjectContext; @synthesize fetchedResultsController, managedObjectContext;


- (id)initWithAccount:(ASAccount *)acc product:(Product *)reviewProduct rating:(NSUInteger)ratingFilter - (id)initWithAccount:(ASAccount *)acc products:(NSArray *)reviewProducts rating:(NSUInteger)ratingFilter
{ {
self = [super initWithStyle:UITableViewStylePlain]; self = [super initWithStyle:UITableViewStylePlain];
if (self) { if (self) {
account = [acc retain]; account = [acc retain];
managedObjectContext = [[account managedObjectContext] retain]; managedObjectContext = [[account managedObjectContext] retain];
rating = ratingFilter; rating = ratingFilter;
product = [reviewProduct retain]; products = [reviewProducts retain];
self.title = NSLocalizedString(@"Reviews", nil); self.title = NSLocalizedString(@"Reviews", nil);


self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"Check.png"] style:UIBarButtonItemStyleBordered target:self action:@selector(markAllAsRead:)] autorelease]; self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"Check.png"] style:UIBarButtonItemStyleBordered target:self action:@selector(markAllAsRead:)] autorelease];
Expand Down Expand Up @@ -114,19 +114,28 @@ - (NSFetchedResultsController *)fetchedResultsController
} }
NSFetchRequest *fetchRequest = [[[NSFetchRequest alloc] init] autorelease]; NSFetchRequest *fetchRequest = [[[NSFetchRequest alloc] init] autorelease];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Review" inManagedObjectContext:self.managedObjectContext]; NSEntityDescription *entity = [NSEntityDescription entityForName:@"Review" inManagedObjectContext:self.managedObjectContext];
if (product) {
if (rating == 0) { NSMutableArray* args = [NSMutableArray arrayWithArray:products];
[fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"product == %@", product]]; NSMutableString* pred = [NSMutableString stringWithString:@""];
} else {
[fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"product == %@ AND rating == %@", product, [NSNumber numberWithInteger:rating]]]; if (products) {
} [pred appendString:@"(product == nil"];
} else { for (Product* p in products) {
if (rating == 0) { [pred appendString:@" OR product == %@"];
[fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"product.account == %@", account]]; }
} else { [pred appendString:@")"];
[fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"product.account == %@ AND rating == %@", account, [NSNumber numberWithInteger:rating]]]; } else {
} [pred appendString:@"product.account == %@"];
} [args addObject:account];
}

if (rating != 0) {
[pred appendString:@" AND rating = %@"];
[args addObject:[NSNumber numberWithInt:rating]];
}

[fetchRequest setPredicate:[NSPredicate predicateWithFormat:pred argumentArray:args]];

[fetchRequest setEntity:entity]; [fetchRequest setEntity:entity];
[fetchRequest setFetchBatchSize:20]; [fetchRequest setFetchBatchSize:20];


Expand Down Expand Up @@ -163,7 +172,7 @@ - (void)controllerDidChangeContent:(NSFetchedResultsController *)controller


- (void)dealloc - (void)dealloc
{ {
[product release]; [products release];
[account release]; [account release];
[fetchedResultsController release]; [fetchedResultsController release];
[managedObjectContext release]; [managedObjectContext release];
Expand Down
43 changes: 36 additions & 7 deletions Classes/ReviewsViewController.m
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -117,10 +117,22 @@ - (NSUInteger)reviewSummaryView:(ReviewSummaryView *)view numberOfReviewsForRati


NSFetchRequest *reviewsCountFetchRequest = [[[NSFetchRequest alloc] init] autorelease]; NSFetchRequest *reviewsCountFetchRequest = [[[NSFetchRequest alloc] init] autorelease];
[reviewsCountFetchRequest setEntity:[NSEntityDescription entityForName:@"Review" inManagedObjectContext:self.account.managedObjectContext]]; [reviewsCountFetchRequest setEntity:[NSEntityDescription entityForName:@"Review" inManagedObjectContext:self.account.managedObjectContext]];
if (!self.selectedProduct) {
[reviewsCountFetchRequest setPredicate:[NSPredicate predicateWithFormat:@"product.account == %@ AND rating == %@", self.account, [NSNumber numberWithInt:rating]]]; NSMutableString * pred = [NSMutableString stringWithString:@"rating == %@"];
NSMutableArray * args = [NSMutableArray arrayWithArray:self.selectedProducts];
[args insertObject:[NSNumber numberWithInt:rating] atIndex:0];

if (![self.selectedProducts count]) {
[pred appendString:@" AND product.account = %@"];
[args addObject:self.account];
[reviewsCountFetchRequest setPredicate:[NSPredicate predicateWithFormat:pred argumentArray:args]];
} else { } else {
[reviewsCountFetchRequest setPredicate:[NSPredicate predicateWithFormat:@"product == %@ AND rating == %@", self.selectedProduct, [NSNumber numberWithInt:rating]]]; [pred appendString:@" AND (product == nil"];
for (Product* p in self.selectedProducts) {
[pred appendString:@" OR product == %@"];
}
[pred appendString:@")"];
[reviewsCountFetchRequest setPredicate:[NSPredicate predicateWithFormat:pred argumentArray:args]];
} }
NSUInteger numberOfReviewsForRating = [self.account.managedObjectContext countForFetchRequest:reviewsCountFetchRequest error:NULL]; NSUInteger numberOfReviewsForRating = [self.account.managedObjectContext countForFetchRequest:reviewsCountFetchRequest error:NULL];
return numberOfReviewsForRating; return numberOfReviewsForRating;
Expand All @@ -132,10 +144,22 @@ - (NSUInteger)reviewSummaryView:(ReviewSummaryView *)view numberOfUnreadReviewsF


NSFetchRequest *reviewsCountFetchRequest = [[[NSFetchRequest alloc] init] autorelease]; NSFetchRequest *reviewsCountFetchRequest = [[[NSFetchRequest alloc] init] autorelease];
[reviewsCountFetchRequest setEntity:[NSEntityDescription entityForName:@"Review" inManagedObjectContext:self.account.managedObjectContext]]; [reviewsCountFetchRequest setEntity:[NSEntityDescription entityForName:@"Review" inManagedObjectContext:self.account.managedObjectContext]];
if (!self.selectedProduct) {
[reviewsCountFetchRequest setPredicate:[NSPredicate predicateWithFormat:@"product.account == %@ AND rating == %@ AND unread = TRUE", self.account, [NSNumber numberWithInt:rating]]]; NSMutableString * pred = [NSMutableString stringWithString:@"rating == %@ AND unread = TRUE"];
NSMutableArray * args = [NSMutableArray arrayWithArray:self.selectedProducts];
[args insertObject:[NSNumber numberWithInt:rating] atIndex:0];

if (![self.selectedProducts count]) {
[pred appendString:@" AND product.account = %@"];
[args addObject:self.account];
[reviewsCountFetchRequest setPredicate:[NSPredicate predicateWithFormat:pred argumentArray:args]];
} else { } else {
[reviewsCountFetchRequest setPredicate:[NSPredicate predicateWithFormat:@"product == %@ AND rating == %@ AND unread = TRUE", self.selectedProduct, [NSNumber numberWithInt:rating]]]; [pred appendString:@" AND (product == nil"];
for (Product* p in self.selectedProducts) {
[pred appendString:@" OR product == %@"];
}
[pred appendString:@")"];
[reviewsCountFetchRequest setPredicate:[NSPredicate predicateWithFormat:pred argumentArray:args]];
} }
NSUInteger numberOfUnreadReviewsForRating = [self.account.managedObjectContext countForFetchRequest:reviewsCountFetchRequest error:NULL]; NSUInteger numberOfUnreadReviewsForRating = [self.account.managedObjectContext countForFetchRequest:reviewsCountFetchRequest error:NULL];
return numberOfUnreadReviewsForRating; return numberOfUnreadReviewsForRating;
Expand All @@ -145,7 +169,7 @@ - (void)reviewSummaryView:(ReviewSummaryView *)view didSelectRating:(NSInteger)r
{ {
if (!self.account) return; if (!self.account) return;


ReviewListViewController *vc = [[[ReviewListViewController alloc] initWithAccount:self.account product:self.selectedProduct rating:rating] autorelease]; ReviewListViewController *vc = [[[ReviewListViewController alloc] initWithAccount:self.account products:self.selectedProducts rating:rating] autorelease];
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) { if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
[self.navigationController pushViewController:vc animated:YES]; [self.navigationController pushViewController:vc animated:YES];
} else { } else {
Expand All @@ -161,6 +185,11 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
[self.reviewSummaryView reloadDataAnimated:YES]; [self.reviewSummaryView reloadDataAnimated:YES];
} }


- (void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer {
[super handleLongPress:gestureRecognizer];
[self.reviewSummaryView reloadDataAnimated:YES];
}

- (void)dealloc - (void)dealloc
{ {
[reviewsPopover release]; [reviewsPopover release];
Expand Down
Loading

0 comments on commit fd1a823

Please sign in to comment.