From b49f33b2d27694c6432ff8a4124f090e4c6c6774 Mon Sep 17 00:00:00 2001 From: Rebecca Putinski Date: Tue, 3 Dec 2013 13:26:47 -0500 Subject: [PATCH 1/5] Use cellClass for WPTableVC instead of newCell MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - WPTableVC registers the cell with a default cell identifier for its subclasses - Clean up WPTableVC and remove all swiping functionality as it wasn’t used --- WordPress/Classes/CommentsViewController.m | 9 +- .../Classes/NotificationsViewController.m | 12 +- WordPress/Classes/PostsViewController.m | 13 +- WordPress/Classes/ReaderPostsViewController.m | 20 +- WordPress/Classes/WPTableViewController.m | 279 +++--------------- .../Classes/WPTableViewControllerSubclass.h | 13 +- 6 files changed, 53 insertions(+), 293 deletions(-) diff --git a/WordPress/Classes/CommentsViewController.m b/WordPress/Classes/CommentsViewController.m index d4d005da7df2..d6b0a6fa6bbf 100644 --- a/WordPress/Classes/CommentsViewController.m +++ b/WordPress/Classes/CommentsViewController.m @@ -209,13 +209,8 @@ - (NSString *)sectionNameKeyPath { return @"status"; } -- (UITableViewCell *)newCell { - static NSString *cellIdentifier = @"CommentCell"; - UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:cellIdentifier]; - if (cell == nil) { - cell = [[NewCommentsTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; - } - return cell; +- (Class)cellClass { + return [NewCommentsTableViewCell class]; } - (void)syncItemsWithSuccess:(void (^)())success failure:(void (^)(NSError *))failure { diff --git a/WordPress/Classes/NotificationsViewController.m b/WordPress/Classes/NotificationsViewController.m index e0b35870d37d..60582ce43bd0 100644 --- a/WordPress/Classes/NotificationsViewController.m +++ b/WordPress/Classes/NotificationsViewController.m @@ -17,7 +17,6 @@ #import "NotificationSettingsViewController.h" #import "WPAccount.h" -NSString * const NotificationsTableViewNoteCellIdentifier = @"NotificationsTableViewCell"; NSString * const NotificationsLastSyncDateKey = @"NotificationsLastSyncDate"; @interface NotificationsViewController () { @@ -60,7 +59,6 @@ - (void)viewDidLoad [WPStyleGuide configureColorsForView:self.view andTableView:self.tableView]; self.infiniteScrollEnabled = YES; - [self.tableView registerClass:[NewNotificationsTableViewCell class] forCellReuseIdentifier:NotificationsTableViewNoteCellIdentifier]; } - (void)viewWillAppear:(BOOL)animated { @@ -221,14 +219,8 @@ - (NSFetchRequest *)fetchRequest { return fetchRequest; } -- (UITableViewCell *)newCell { - NewNotificationsTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:NotificationsTableViewNoteCellIdentifier]; - - if (cell == nil) { - cell = [[NewNotificationsTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:NotificationsTableViewNoteCellIdentifier]; - } - - return cell; +- (Class)cellClass { + return [NewNotificationsTableViewCell class]; } - (void)configureCell:(NewNotificationsTableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath { diff --git a/WordPress/Classes/PostsViewController.m b/WordPress/Classes/PostsViewController.m index 16ca2c63ac9a..4a427ecdcbfd 100644 --- a/WordPress/Classes/PostsViewController.m +++ b/WordPress/Classes/PostsViewController.m @@ -261,17 +261,8 @@ - (void)syncItemsWithSuccess:(void (^)())success failure:(void (^)(NSError *))fa [self.blog syncPostsWithSuccess:success failure:failure loadMore:NO]; } -- (UITableViewCell *)newCell { - static NSString *const cellIdentifier = @"PostCell"; - UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:cellIdentifier]; - if (cell == nil) { - cell = [[NewPostTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; - if (!IS_IOS7) { - UIImageView *imageView = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:@"cell_gradient_bg"] stretchableImageWithLeftCapWidth:0 topCapHeight:1]]; - [cell setBackgroundView:imageView]; - } - } - return cell; +- (Class)cellClass { + return [NewPostTableViewCell class]; } - (void)controller:(NSFetchedResultsController *)controller diff --git a/WordPress/Classes/ReaderPostsViewController.m b/WordPress/Classes/ReaderPostsViewController.m index dc5c07983786..df00e082ab8a 100644 --- a/WordPress/Classes/ReaderPostsViewController.m +++ b/WordPress/Classes/ReaderPostsViewController.m @@ -553,18 +553,8 @@ - (NSString *)sectionNameKeyPath { return nil; } -- (UITableViewCell *)newCell { - NSString *cellIdentifier = @"ReaderPostCell"; - ReaderPostTableViewCell *cell = (ReaderPostTableViewCell *)[self.tableView dequeueReusableCellWithIdentifier:cellIdentifier]; - if (cell == nil) { - cell = [[ReaderPostTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; - [cell.reblogButton addTarget:self action:@selector(reblogAction:) forControlEvents:UIControlEventTouchUpInside]; - [cell.likeButton addTarget:self action:@selector(likeAction:) forControlEvents:UIControlEventTouchUpInside]; - [cell.followButton addTarget:self action:@selector(followAction:) forControlEvents:UIControlEventTouchUpInside]; - [cell.commentButton addTarget:self action:@selector(commentAction:) forControlEvents:UIControlEventTouchUpInside]; - [cell.tagButton addTarget:self action:@selector(tagAction:) forControlEvents:UIControlEventTouchUpInside]; - } - return cell; +- (Class)cellClass { + return [ReaderPostTableViewCell class]; } - (void)configureCell:(UITableViewCell *)aCell atIndexPath:(NSIndexPath *)indexPath { @@ -590,6 +580,12 @@ - (void)configureCell:(UITableViewCell *)aCell atIndexPath:(NSIndexPath *)indexP } }]; } + + [cell.reblogButton addTarget:self action:@selector(reblogAction:) forControlEvents:UIControlEventTouchUpInside]; + [cell.likeButton addTarget:self action:@selector(likeAction:) forControlEvents:UIControlEventTouchUpInside]; + [cell.followButton addTarget:self action:@selector(followAction:) forControlEvents:UIControlEventTouchUpInside]; + [cell.commentButton addTarget:self action:@selector(commentAction:) forControlEvents:UIControlEventTouchUpInside]; + [cell.tagButton addTarget:self action:@selector(tagAction:) forControlEvents:UIControlEventTouchUpInside]; } - (void)setImageForPost:(ReaderPost *)post forCell:(ReaderPostTableViewCell *)cell indexPath:(NSIndexPath *)indexPath { diff --git a/WordPress/Classes/WPTableViewController.m b/WordPress/Classes/WPTableViewController.m index 4e47e29b2e8c..4e6bafa87f92 100644 --- a/WordPress/Classes/WPTableViewController.m +++ b/WordPress/Classes/WPTableViewController.m @@ -17,43 +17,31 @@ NSTimeInterval const WPTableViewControllerRefreshTimeout = 300; // 5 minutes CGFloat const WPTableViewTopMargin = 40; -NSString * const WPBlogRestorationKey = @"WPBlogRestorationKey"; +NSString *const WPBlogRestorationKey = @"WPBlogRestorationKey"; +NSString *const DefaultCellIdentifier = @"DefaultCellIdentifier"; @interface WPTableViewController () @property (nonatomic, strong) NSFetchedResultsController *resultsController; -@property (nonatomic) BOOL swipeActionsEnabled; @property (nonatomic) BOOL infiniteScrollEnabled; @property (nonatomic, strong, readonly) UIView *swipeView; @property (nonatomic, strong) UITableViewCell *swipeCell; @property (nonatomic, strong) UIView *noResultsView; +@property (nonatomic, strong) EditSiteViewController *editSiteViewController; +@property (nonatomic, strong) NSIndexPath *indexPathSelectedBeforeUpdates; +@property (nonatomic, strong) NSIndexPath *indexPathSelectedAfterUpdates; +@property (nonatomic, assign) UISwipeGestureRecognizerDirection swipeDirection; +@property (nonatomic, strong) UIActivityIndicatorView *activityFooter; +@property (nonatomic, assign) BOOL animatingRemovalOfModerationSwipeView; +@property (nonatomic, assign) BOOL didPromptForCredentials; +@property (nonatomic, assign) BOOL isSyncing; +@property (nonatomic, assign) BOOL isLoadingMore; +@property (nonatomic, assign) BOOL didTriggerRefresh; +@property (nonatomic, assign) CGPoint savedScrollOffset; @end -@implementation WPTableViewController { - EditSiteViewController *editSiteViewController; - UIView *noResultsView; - NSIndexPath *_indexPathSelectedBeforeUpdates; - NSIndexPath *_indexPathSelectedAfterUpdates; - UISwipeGestureRecognizer *_leftSwipeGestureRecognizer; - UISwipeGestureRecognizer *_rightSwipeGestureRecognizer; - UISwipeGestureRecognizerDirection _swipeDirection; - UIActivityIndicatorView *_activityFooter; - BOOL _animatingRemovalOfModerationSwipeView; - BOOL didPromptForCredentials; - BOOL _isSyncing; - BOOL _isLoadingMore; - BOOL didTriggerRefresh; - CGPoint savedScrollOffset; -} - -@synthesize blog = _blog; -@synthesize resultsController = _resultsController; -@synthesize swipeActionsEnabled = _swipeActionsEnabled; -@synthesize infiniteScrollEnabled = _infiniteScrollEnabled; -@synthesize swipeView = _swipeView; -@synthesize swipeCell = _swipeCell; -@synthesize noResultsView; +@implementation WPTableViewController + (UIViewController *)viewControllerWithRestorationIdentifierPath:(NSArray *)identifierComponents coder:(NSCoder *)coder { NSString *blogID = [coder decodeObjectForKey:WPBlogRestorationKey]; @@ -90,7 +78,7 @@ - (id)initWithStyle:(UITableViewStyle)style { - (void)dealloc { _resultsController.delegate = nil; - editSiteViewController.delegate = nil; + _editSiteViewController.delegate = nil; } - (void)encodeRestorableStateWithCoder:(NSCoder *)coder { @@ -107,30 +95,20 @@ - (void)viewDidLoad { self.tableView.allowsSelectionDuringEditing = YES; [WPStyleGuide configureColorsForView:self.view andTableView:self.tableView]; - - if (self.swipeActionsEnabled) { - [self enableSwipeGestureRecognizer]; - } + [self.tableView registerClass:[self cellClass] forCellReuseIdentifier:DefaultCellIdentifier]; if (self.infiniteScrollEnabled) { [self enableInfiniteScrolling]; } [self configureNoResultsView]; - - // Remove one-pixel gap resulting from a top-aligned grouped table view - if (IS_IPHONE) { - UIEdgeInsets tableInset = [self.tableView contentInset]; - tableInset.top = -1; - self.tableView.contentInset = tableInset; - } } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; CGSize contentSize = self.tableView.contentSize; - if(contentSize.height > savedScrollOffset.y) { - [self.tableView scrollRectToVisible:CGRectMake(savedScrollOffset.x, savedScrollOffset.y, 0.0, 0.0) animated:NO]; + if (contentSize.height > _savedScrollOffset.y) { + [self.tableView scrollRectToVisible:CGRectMake(_savedScrollOffset.x, _savedScrollOffset.y, 0.0, 0.0) animated:NO]; } else { [self.tableView scrollRectToVisible:CGRectMake(0.0, contentSize.height, 0.0, 0.0) animated:NO]; } @@ -145,7 +123,7 @@ - (void)viewDidAppear:(BOOL)animated { if( appDelegate.connectionAvailable == NO ) return; //do not start auto-synch if connection is down // Don't try to refresh if we just canceled editing credentials - if (didPromptForCredentials) { + if (_didPromptForCredentials) { return; } NSDate *lastSynced = [self lastSyncDate]; @@ -158,7 +136,7 @@ - (void)viewDidAppear:(BOOL)animated { - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; if (IS_IPHONE) { - savedScrollOffset = self.tableView.contentOffset; + _savedScrollOffset = self.tableView.contentOffset; } } @@ -191,43 +169,6 @@ - (void)setBlog:(Blog *)blog { } } -- (void)setSwipeActionsEnabled:(BOOL)swipeActionsEnabled { - if (swipeActionsEnabled == _swipeActionsEnabled) { - return; - } - - _swipeActionsEnabled = swipeActionsEnabled; - if (self.isViewLoaded) { - if (_swipeActionsEnabled) { - [self enableSwipeGestureRecognizer]; - } else { - [self disableSwipeGestureRecognizer]; - } - } -} - -- (BOOL)swipeActionsEnabled { - return _swipeActionsEnabled && !self.editing; -} - -- (UIView *)swipeView { - if (_swipeView) { - return _swipeView; - } - - _swipeView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.bounds.size.width, kCellHeight)]; - _swipeView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background.png"]]; - - UIImage *shadow = [[UIImage imageNamed:@"inner-shadow.png"] stretchableImageWithLeftCapWidth:0 topCapHeight:0]; - UIImageView *shadowImageView = [[UIImageView alloc] initWithFrame:_swipeView.frame]; - shadowImageView.alpha = 0.5; - shadowImageView.image = shadow; - shadowImageView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; - [_swipeView insertSubview:shadowImageView atIndex:0]; - - return _swipeView; -} - - (void)setInfiniteScrollEnabled:(BOOL)infiniteScrollEnabled { if (infiniteScrollEnabled == _infiniteScrollEnabled) { return; @@ -243,10 +184,6 @@ - (void)setInfiniteScrollEnabled:(BOOL)infiniteScrollEnabled { } } -- (BOOL)infiniteScrollEnabled { - return _infiniteScrollEnabled; -} - #pragma mark - Table view data source - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { @@ -265,7 +202,7 @@ - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { - UITableViewCell *cell = [self newCell]; + UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:DefaultCellIdentifier]; if (IS_IPAD || self.tableView.isEditing) { cell.accessoryType = UITableViewCellAccessoryNone; @@ -442,9 +379,9 @@ - (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id #pragma mark - UIRefreshControl Methods - (void)refresh { - didTriggerRefresh = YES; + _didTriggerRefresh = YES; [self syncItemsViaUserInteraction]; - [noResultsView removeFromSuperview]; + [self.noResultsView removeFromSuperview]; } @@ -452,9 +389,6 @@ - (void)refresh { - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView { _isScrolling = YES; - if (self.swipeActionsEnabled) { - [self removeSwipeView:YES]; - } } - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { @@ -527,9 +461,9 @@ - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)butto #pragma mark - SettingsViewControllerDelegate - (void)controllerDidDismiss:(UIViewController *)controller cancelled:(BOOL)cancelled { - if (editSiteViewController == controller) { - didPromptForCredentials = cancelled; - editSiteViewController = nil; + if (self.editSiteViewController == controller) { + _didPromptForCredentials = cancelled; + self.editSiteViewController = nil; } } @@ -564,7 +498,7 @@ - (UIView *)createNoResultsView { - (void)hideRefreshHeader { [self.refreshControl endRefreshing]; - didTriggerRefresh = NO; + _didTriggerRefresh = NO; } - (void)dismissModal:(id)sender { @@ -606,9 +540,9 @@ - (void)syncItemsViaUserInteraction:(BOOL)userInteraction { alertView.tag = 30; [alertView show]; - } else if (error.code == 403 && editSiteViewController == nil) { + } else if (error.code == 403 && self.editSiteViewController == nil) { [self promptForPassword]; - } else if (error.code == 425 && editSiteViewController == nil) { + } else if (error.code == 425 && self.editSiteViewController == nil) { [self promptForPasswordWithMessage:[error localizedDescription]]; } else if (userInteraction) { [WPError showAlertWithError:error title:NSLocalizedString(@"Couldn't sync", @"")]; @@ -639,10 +573,10 @@ - (void)promptForPasswordWithMessage:(NSString *)message { [alertView show]; // bad login/pass combination - editSiteViewController = [[EditSiteViewController alloc] initWithBlog:self.blog]; - editSiteViewController.isCancellable = YES; - editSiteViewController.delegate = self; - UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:editSiteViewController]; + self.editSiteViewController = [[EditSiteViewController alloc] initWithBlog:self.blog]; + self.editSiteViewController.isCancellable = YES; + self.editSiteViewController.delegate = self; + UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:self.editSiteViewController]; navController.navigationBar.translucent = NO; if(IS_IPAD) { @@ -653,143 +587,6 @@ - (void)promptForPasswordWithMessage:(NSString *)message { [self.navigationController presentViewController:navController animated:YES completion:nil]; } -#pragma mark - Swipe gestures - -- (void)enableSwipeGestureRecognizer { - [self disableSwipeGestureRecognizer]; // Disable any existing gesturerecognizers before initing new ones to avoid leaks. - - _leftSwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeLeft:)]; - _leftSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionLeft; - [self.tableView addGestureRecognizer:_leftSwipeGestureRecognizer]; - - _rightSwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRight:)]; - _rightSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionRight; - [self.tableView addGestureRecognizer:_rightSwipeGestureRecognizer]; -} - -- (void)disableSwipeGestureRecognizer { - if (_leftSwipeGestureRecognizer) { - [self.tableView removeGestureRecognizer:_leftSwipeGestureRecognizer]; - _leftSwipeGestureRecognizer = nil; - } - - if (_rightSwipeGestureRecognizer) { - [self.tableView removeGestureRecognizer:_rightSwipeGestureRecognizer]; - _rightSwipeGestureRecognizer = nil; - } -} - -- (void)removeSwipeView:(BOOL)animated { - if (!self.swipeActionsEnabled || !_swipeCell || (self.swipeCell.frame.origin.x == 0 && self.swipeView.superview == nil)) return; - - if (animated) - { - _animatingRemovalOfModerationSwipeView = YES; - [UIView animateWithDuration:0.2 - animations:^{ - if (_swipeDirection == UISwipeGestureRecognizerDirectionRight) - { - self.swipeView.frame = CGRectMake(-self.swipeView.frame.size.width + 5.0,self.swipeView.frame.origin.y, self.swipeView.frame.size.width, self.swipeView.frame.size.height); - self.swipeCell.frame = CGRectMake(5.0, self.swipeCell.frame.origin.y, self.swipeCell.frame.size.width, self.swipeCell.frame.size.height); - } - else - { - self.swipeView.frame = CGRectMake(self.swipeView.frame.size.width - 5.0,self.swipeView.frame.origin.y,self.swipeView.frame.size.width, self.swipeView.frame.size.height); - self.swipeCell.frame = CGRectMake(-5.0, self.swipeCell.frame.origin.y, self.swipeCell.frame.size.width, self.swipeCell.frame.size.height); - } - } - completion:^(BOOL finished) { - [UIView animateWithDuration:0.1 - animations:^{ - if (_swipeDirection == UISwipeGestureRecognizerDirectionRight) - { - self.swipeView.frame = CGRectMake(-self.swipeView.frame.size.width + 10.0,self.swipeView.frame.origin.y,self.swipeView.frame.size.width, self.swipeView.frame.size.height); - self.swipeCell.frame = CGRectMake(10.0, self.swipeCell.frame.origin.y, self.swipeCell.frame.size.width, self.swipeCell.frame.size.height); - } - else - { - self.swipeView.frame = CGRectMake(self.swipeView.frame.size.width - 10.0,self.swipeView.frame.origin.y,self.swipeView.frame.size.width, self.swipeView.frame.size.height); - self.swipeCell.frame = CGRectMake(-10.0, self.swipeCell.frame.origin.y, self.swipeCell.frame.size.width, self.swipeCell.frame.size.height); - } - } completion:^(BOOL finished) { - [UIView animateWithDuration:0.1 - animations:^{ - if (_swipeDirection == UISwipeGestureRecognizerDirectionRight) - { - self.swipeView.frame = CGRectMake(-self.swipeView.frame.size.width ,self.swipeView.frame.origin.y,self.swipeView.frame.size.width, self.swipeView.frame.size.height); - self.swipeCell.frame = CGRectMake(0, self.swipeCell.frame.origin.y, self.swipeCell.frame.size.width, self.swipeCell.frame.size.height); - } - else - { - self.swipeView.frame = CGRectMake(self.swipeView.frame.size.width ,self.swipeView.frame.origin.y,self.swipeView.frame.size.width, self.swipeView.frame.size.height); - self.swipeCell.frame = CGRectMake(0, self.swipeCell.frame.origin.y, self.swipeCell.frame.size.width, self.swipeCell.frame.size.height); - } - } - completion:^(BOOL finished) { - _animatingRemovalOfModerationSwipeView = NO; - self.swipeCell = nil; - [_swipeView removeFromSuperview]; - _swipeView = nil; - }]; - }]; - }]; - } - else - { - [self.swipeView removeFromSuperview]; - _swipeView = nil; - self.swipeCell.frame = CGRectMake(0,self.swipeCell.frame.origin.y,self.swipeCell.frame.size.width, self.swipeCell.frame.size.height); - self.swipeCell = nil; - } -} - -- (void)swipe:(UISwipeGestureRecognizer *)recognizer direction:(UISwipeGestureRecognizerDirection)direction -{ - if (!self.swipeActionsEnabled) { - return; - } - if (recognizer && recognizer.state == UIGestureRecognizerStateEnded) - { - if (_animatingRemovalOfModerationSwipeView) return; - - CGPoint location = [recognizer locationInView:self.tableView]; - NSIndexPath* indexPath = [self.tableView indexPathForRowAtPoint:location]; - UITableViewCell* cell = [self.tableView cellForRowAtIndexPath:indexPath]; - - if (cell.frame.origin.x != 0) - { - [self removeSwipeView:YES]; - return; - } - [self removeSwipeView:NO]; - - if (cell != self.swipeCell) - { - [self configureSwipeView:self.swipeView forIndexPath:indexPath]; - - [self.tableView addSubview:self.swipeView]; - self.swipeCell = cell; - CGRect cellFrame = cell.frame; - _swipeDirection = direction; - self.swipeView.frame = CGRectMake(direction == UISwipeGestureRecognizerDirectionRight ? -cellFrame.size.width : cellFrame.size.width, cellFrame.origin.y, cellFrame.size.width, cellFrame.size.height); - - [UIView animateWithDuration:0.2 animations:^{ - self.swipeView.frame = CGRectMake(0, cellFrame.origin.y, cellFrame.size.width, cellFrame.size.height); - cell.frame = CGRectMake(direction == UISwipeGestureRecognizerDirectionRight ? cellFrame.size.width : -cellFrame.size.width, cellFrame.origin.y, cellFrame.size.width, cellFrame.size.height); - }]; - } - } -} - -- (void)swipeLeft:(UISwipeGestureRecognizer *)recognizer -{ - [self swipe:recognizer direction:UISwipeGestureRecognizerDirectionLeft]; -} - -- (void)swipeRight:(UISwipeGestureRecognizer *)recognizer -{ - [self swipe:recognizer direction:UISwipeGestureRecognizerDirectionRight]; -} #pragma mark - Infinite scrolling @@ -863,14 +660,8 @@ - (BOOL)isSyncing { return _isSyncing; } -- (UITableViewCell *)newCell { - // To comply with apple ownership and naming conventions, returned cell should have a retain count > 0, so retain the dequeued cell. - NSString *cellIdentifier = [NSString stringWithFormat:@"_WPTable_%@_Cell", [self entityName]]; - UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:cellIdentifier]; - if (cell == nil) { - cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; - } - return cell; +- (Class)cellClass { + return [UITableViewCell class]; } - (BOOL)hasMoreContent { diff --git a/WordPress/Classes/WPTableViewControllerSubclass.h b/WordPress/Classes/WPTableViewControllerSubclass.h index d5eba681959a..b954ecbc0ac7 100644 --- a/WordPress/Classes/WPTableViewControllerSubclass.h +++ b/WordPress/Classes/WPTableViewControllerSubclass.h @@ -14,11 +14,6 @@ */ @property (nonatomic,readonly,retain) NSFetchedResultsController *resultsController; -/** - Enables the swipe menu for cells - */ -@property (nonatomic) BOOL swipeActionsEnabled; - /** Enables the infinteScrolling */ @@ -96,13 +91,13 @@ - (NSString *)sectionNameKeyPath; /** - Returns a new (unconfigured) cell for the table view. + Returns the class for the cell being used - Optional. If a subclass doesn't implement this method, a UITableViewCell with the default style is used + Optional. If a subclass doesn't implement this method, the UITableViewCell - @return a new initialized cell ready to be configured + @return the class for the cell to be registered */ -- (UITableViewCell *)newCell; +- (Class)cellClass; /** Configure a table cell for a specific index path From 219812a16583d773e9b51771876e4d3a93a57885 Mon Sep 17 00:00:00 2001 From: Rebecca Putinski Date: Tue, 3 Dec 2013 14:44:49 -0500 Subject: [PATCH 2/5] Use cell registration for more table views --- WordPress/Classes/ActivityLogViewController.m | 60 +++++++++---------- .../Classes/AddUsersBlogsViewController.m | 10 ++-- WordPress/Classes/BlogDetailsViewController.m | 14 ++--- WordPress/Classes/BlogListViewController.m | 14 ++--- 4 files changed, 48 insertions(+), 50 deletions(-) diff --git a/WordPress/Classes/ActivityLogViewController.m b/WordPress/Classes/ActivityLogViewController.m index 05b2d6a05c98..1d3466793cb7 100644 --- a/WordPress/Classes/ActivityLogViewController.m +++ b/WordPress/Classes/ActivityLogViewController.m @@ -11,12 +11,13 @@ #import "ActivityLogDetailViewController.h" #import +static NSString *const ActivityLogCellIdentifier = @"ActivityLogCell"; + @interface ActivityLogViewController () -{ - NSArray *logFiles; - NSDateFormatter *dateFormatter; - DDFileLogger *fileLogger; -} + +@property (nonatomic, strong) NSDateFormatter *dateFormatter; +@property (nonatomic, weak) DDFileLogger *fileLogger; +@property (nonatomic, strong) NSArray *logFiles; @end @@ -28,12 +29,7 @@ - (id)init if (self) { // TODO - Replace this call with an injected value, depending on design conventions already in place WordPressAppDelegate *delegate = (WordPressAppDelegate *)[[UIApplication sharedApplication] delegate]; - fileLogger = delegate.fileLogger; - - dateFormatter = [[NSDateFormatter alloc] init]; - dateFormatter.dateStyle = NSDateFormatterMediumStyle; - dateFormatter.doesRelativeDateFormatting = YES; - dateFormatter.timeStyle = NSDateFormatterShortStyle; + _fileLogger = delegate.fileLogger; self.title = NSLocalizedString(@"Activity Logs", @""); @@ -41,7 +37,7 @@ - (id)init style:UIBarButtonItemStyleBordered target:nil action:nil]; - [[self navigationItem] setBackBarButtonItem:backButton]; + [self.navigationItem setBackBarButtonItem:backButton]; } return self; @@ -53,6 +49,8 @@ - (void)viewDidLoad [WPStyleGuide configureColorsForView:self.view andTableView:self.tableView]; [self loadLogFiles]; + + [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:ActivityLogCellIdentifier]; } - (void)didReceiveMemoryWarning @@ -63,7 +61,18 @@ - (void)didReceiveMemoryWarning - (void)loadLogFiles { - logFiles = fileLogger.logFileManager.sortedLogFileInfos; + self.logFiles = self.fileLogger.logFileManager.sortedLogFileInfos; +} + +- (NSDateFormatter *)dateFormatter { + if (_dateFormatter) { + return _dateFormatter; + } + _dateFormatter = [[NSDateFormatter alloc] init]; + _dateFormatter.dateStyle = NSDateFormatterMediumStyle; + _dateFormatter.doesRelativeDateFormatting = YES; + _dateFormatter.timeStyle = NSDateFormatterShortStyle; + return _dateFormatter; } #pragma mark - Table view data source @@ -76,24 +85,17 @@ - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if (section == 0) - return logFiles.count; + return self.logFiles.count; return 1; } -- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath -{ - static NSString *CellIdentifier = @"ActivityLogCell"; - UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; - - if (cell == nil) { - cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; - } - +- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { + UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ActivityLogCellIdentifier]; if (indexPath.section == 0) { - DDLogFileInfo *logFileInfo = (DDLogFileInfo *)logFiles[indexPath.row]; + DDLogFileInfo *logFileInfo = (DDLogFileInfo *)self.logFiles[indexPath.row]; cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; - cell.textLabel.text = indexPath.row == 0 ? NSLocalizedString(@"Current", @"") : [dateFormatter stringFromDate:logFileInfo.creationDate]; + cell.textLabel.text = indexPath.row == 0 ? NSLocalizedString(@"Current", @"") : [self.dateFormatter stringFromDate:logFileInfo.creationDate]; cell.textLabel.textAlignment = NSTextAlignmentLeft; [WPStyleGuide configureTableViewCell:cell]; } else { @@ -102,8 +104,6 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N cell.textLabel.text = NSLocalizedString(@"Clear Old Activity Logs", @""); [WPStyleGuide configureTableViewActionCell:cell]; } - - return cell; } @@ -132,15 +132,15 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath [tableView deselectRowAtIndexPath:indexPath animated:YES]; if (indexPath.section == 0) { - DDLogFileInfo *logFileInfo = (DDLogFileInfo *)logFiles[indexPath.row]; + DDLogFileInfo *logFileInfo = (DDLogFileInfo *)self.logFiles[indexPath.row]; NSData *logData = [NSData dataWithContentsOfFile:logFileInfo.filePath]; NSString *logText = [[NSString alloc] initWithData:logData encoding:NSUTF8StringEncoding]; ActivityLogDetailViewController *detailViewController = [[ActivityLogDetailViewController alloc] initWithLog:logText - forDateString:[dateFormatter stringFromDate:logFileInfo.creationDate]]; + forDateString:[self.dateFormatter stringFromDate:logFileInfo.creationDate]]; [self.navigationController pushViewController:detailViewController animated:YES]; } else { - for (DDLogFileInfo *logFileInfo in logFiles) { + for (DDLogFileInfo *logFileInfo in self.logFiles) { if (logFileInfo.isArchived) [[NSFileManager defaultManager] removeItemAtPath:logFileInfo.filePath error:nil]; } diff --git a/WordPress/Classes/AddUsersBlogsViewController.m b/WordPress/Classes/AddUsersBlogsViewController.m index 5880053983e8..6edd0fdbfe56 100644 --- a/WordPress/Classes/AddUsersBlogsViewController.m +++ b/WordPress/Classes/AddUsersBlogsViewController.m @@ -22,6 +22,8 @@ #import "Blog.h" #import "WPcomLoginViewController.h" +static NSString *const CellIdentifier = @"Cell"; + @interface AddUsersBlogsViewController () @property (nonatomic, strong) UIView *noblogsView; @@ -84,6 +86,8 @@ - (void)viewDidLoad { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(cancelAddWPcomBlogs) name:@"didCancelWPcomLogin" object:nil]; + + [self.tableView registerClass:[WPTableViewCell class] forCellReuseIdentifier:CellIdentifier]; } - (void)viewWillAppear:(BOOL)animated { @@ -192,12 +196,8 @@ - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSIntege } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { - static NSString *CellIdentifier = @"Cell"; - + UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; - if (cell == nil) { - cell = [[WPTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; - } cell.textLabel.textAlignment = NSTextAlignmentLeft; diff --git a/WordPress/Classes/BlogDetailsViewController.m b/WordPress/Classes/BlogDetailsViewController.m index bfa22ee42c69..20963842de31 100644 --- a/WordPress/Classes/BlogDetailsViewController.m +++ b/WordPress/Classes/BlogDetailsViewController.m @@ -16,6 +16,8 @@ #import "WPTableViewCell.h" #import "ContextManager.h" +static NSString *const BlogDetailsCellIdentifier = @"BlogDetailsCell"; + typedef enum { BlogDetailsRowPosts = 0, BlogDetailsRowPages, @@ -84,6 +86,8 @@ - (void)viewDidLoad { } [WPStyleGuide configureColorsForView:self.view andTableView:self.tableView]; + + [self.tableView registerClass:[WPTableViewCell class] forCellReuseIdentifier:BlogDetailsCellIdentifier]; } - (void)viewWillAppear:(BOOL)animated { @@ -130,14 +134,8 @@ - (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPa } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { - static NSString *CellIdentifier = @"BlogDetailsCell"; - UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; - - if (cell == nil) { - cell = [[WPTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier]; - [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator]; - } - + UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:BlogDetailsCellIdentifier]; + cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; [self configureCell:cell atIndexPath:indexPath]; [WPStyleGuide configureTableViewCell:cell]; diff --git a/WordPress/Classes/BlogListViewController.m b/WordPress/Classes/BlogListViewController.m index 9f06959f39ae..560fab99aedc 100644 --- a/WordPress/Classes/BlogListViewController.m +++ b/WordPress/Classes/BlogListViewController.m @@ -17,10 +17,12 @@ #import "ContextManager.h" #import "Blog.h" +static NSString *const BlogCellIdentifier = @"BlogCell"; CGFloat const blavatarImageSize = 50.f; NSString * const WPBlogListRestorationID = @"WPBlogListID"; @interface BlogListViewController () + @property (nonatomic, strong) NSFetchedResultsController *resultsController; @property (nonatomic, strong) UIBarButtonItem *settingsButton; @@ -97,6 +99,8 @@ - (void)viewDidLoad { // Uncomment the following line to display an Edit button in the navigation bar for this view controller. // self.navigationItem.rightBarButtonItem = self.editButtonItem; + + [self.tableView registerClass:[WPTableViewCell class] forCellReuseIdentifier:BlogCellIdentifier]; } - (void)viewWillAppear:(BOOL)animated { @@ -152,11 +156,8 @@ - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { - NSString *cellIdentifier = @"BlogCell"; - UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:cellIdentifier]; - if (cell == nil) { - cell = [[WPTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier]; - } + + UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:BlogCellIdentifier]; [WPStyleGuide configureTableViewCell:cell]; [self configureCell:cell atIndexPath:indexPath]; @@ -283,8 +284,7 @@ - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPa } -#pragma mark - -#pragma mark NSFetchedResultsController +#pragma mark - NSFetchedResultsController - (NSFetchedResultsController *)resultsController { if (_resultsController) { From d86c14fa5f155404cc653ab5cb9206d2372b1f2a Mon Sep 17 00:00:00 2001 From: Rebecca Putinski Date: Tue, 3 Dec 2013 14:45:31 -0500 Subject: [PATCH 3/5] Use cell registration and clean up creating account --- .../CreateWPComAccountViewController.m | 204 ++++++++---------- .../Classes/WPTableViewSectionFooterView.h | 2 +- 2 files changed, 87 insertions(+), 119 deletions(-) diff --git a/WordPress/Classes/CreateWPComAccountViewController.m b/WordPress/Classes/CreateWPComAccountViewController.m index a2829034d771..acdcf341e875 100644 --- a/WordPress/Classes/CreateWPComAccountViewController.m +++ b/WordPress/Classes/CreateWPComAccountViewController.m @@ -17,49 +17,33 @@ #import "WPTableViewSectionFooterView.h" #import "WPAccount.h" -@interface CreateWPComAccountViewController () < - UITextFieldDelegate> { - - UITableViewTextFieldCell *_usernameCell; - UITableViewTextFieldCell *_passwordCell; - UITableViewTextFieldCell *_emailCell; - UITableViewTextFieldCell *_blogUrlCell; - UITableViewCell *_localeCell; - - UITextField *_usernameTextField; - UITextField *_passwordTextField; - UITextField *_emailTextField; - UITextField *_blogUrlTextField; - - NSString *_buttonText; - NSString *_footerText; - - BOOL _isCreatingAccount; - BOOL _userPressedBackButton; - - NSDictionary *_currentLanguage; - - NSOperationQueue *_operationQueue; -} +static NSString *const TextFieldCellIdentifier = @"TextCell"; +static NSString *const LocaleCellIdentifier = @"LocaleCell"; +static NSString *const FooterViewIdentifier = @"FooterViewIdentifier"; +CGSize const CreateAccountHeaderSize = { 320.0, 70.0 }; -@end +@interface CreateWPComAccountViewController () -@implementation CreateWPComAccountViewController +@property (nonatomic, weak) UITextField *usernameTextField; +@property (nonatomic, weak) UITextField *passwordTextField; +@property (nonatomic, weak) UITextField *emailTextField; +@property (nonatomic, weak) UITextField *blogUrlTextField; +@property (nonatomic, strong) NSString *buttonText; +@property (nonatomic, strong) NSString *footerText; +@property (nonatomic, assign) BOOL isCreatingAccount; +@property (nonatomic, assign) BOOL userPressedBackButton; +@property (nonatomic, strong) NSDictionary *currentLanguage; +@property (nonatomic, strong) NSOperationQueue *operationQueue; -NSUInteger const CreateAccountEmailTextFieldTag = 1; -NSUInteger const CreateAccountUserNameTextFieldTag = 2; -NSUInteger const CreateAccountPasswordTextFieldTag = 3; -NSUInteger const CreateAccountBlogUrlTextFieldTag = 4; +@end -CGSize const CreateAccountHeaderSize = { 320.0, 70.0 }; +@implementation CreateWPComAccountViewController - (id)initWithStyle:(UITableViewStyle)style { self = [super initWithStyle:style]; - if (self) { _currentLanguage = [WPComLanguages currentLanguage]; - _operationQueue = [[NSOperationQueue alloc] init]; } return self; @@ -86,10 +70,17 @@ - (void)viewDidLoad logoImage.autoresizingMask = UIViewAutoresizingFlexibleWidth; logoImage.contentMode = UIViewContentModeCenter; self.tableView.tableHeaderView = logoImage; + + [self.tableView registerClass:[UITableViewTextFieldCell class] forCellReuseIdentifier:TextFieldCellIdentifier]; + [self.tableView registerClass:[WPTableViewSectionFooterView class] forHeaderFooterViewReuseIdentifier:FooterViewIdentifier]; } -- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { - return [super shouldAutorotateToInterfaceOrientation:interfaceOrientation]; +- (NSOperationQueue *)operationQueue { + if (_operationQueue) { + return _operationQueue; + } + _operationQueue = [[NSOperationQueue alloc] init]; + return _operationQueue; } #pragma mark - Table view data source @@ -101,24 +92,26 @@ - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { - if (section == 0) + if (section == 0) { return 5; - else + } else { return 1; + } } - (NSString *)titleForFooterInSection:(NSInteger)section { - if(section == 0) + if(section == 0) { return _footerText; - else + } else { return @""; + } } - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section { - WPTableViewSectionFooterView *header = [[WPTableViewSectionFooterView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), 0)]; - header.title = [self titleForFooterInSection:section]; - return header; + WPTableViewSectionFooterView *footer = [tableView dequeueReusableHeaderFooterViewWithIdentifier:FooterViewIdentifier]; + footer.title = [self titleForFooterInSection:section]; + return footer; } - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section @@ -127,11 +120,8 @@ - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSIntege return [WPTableViewSectionFooterView heightForTitle:title andWidth:CGRectGetWidth(self.view.bounds)]; } -- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath -{ - UITableViewCell *cell = nil; - - if(indexPath.section == 1) { +- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { + if (indexPath.section == 1) { UITableViewActivityCell *activityCell = nil; NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"UITableViewActivityCell" owner:nil options:nil]; for(id currentObject in topLevelObjects) @@ -143,7 +133,7 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N } } - if(_isCreatingAccount) { + if (_isCreatingAccount) { [activityCell.spinner startAnimating]; _buttonText = NSLocalizedString(@"Creating Account...", @""); } else { @@ -159,90 +149,74 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N } [WPStyleGuide configureTableViewActionCell:activityCell]; - cell = activityCell; + return activityCell; } else { if (indexPath.row == 0) { - if (_emailCell == nil) { - _emailCell = [[UITableViewTextFieldCell alloc] initWithStyle:UITableViewCellStyleDefault - reuseIdentifier:@"TextCell"]; - } - _emailCell.textLabel.text = NSLocalizedString(@"Email", @""); - _emailTextField = _emailCell.textField; - _emailTextField.tag = CreateAccountEmailTextFieldTag; + UITableViewTextFieldCell *cell = [self.tableView dequeueReusableCellWithIdentifier:TextFieldCellIdentifier]; + + cell.textLabel.text = NSLocalizedString(@"Email", @""); + _emailTextField = cell.textField; _emailTextField.placeholder = NSLocalizedString(@"user@example.com", @""); _emailTextField.keyboardType = UIKeyboardTypeEmailAddress; _emailTextField.returnKeyType = UIReturnKeyNext; _emailTextField.delegate = self; - [WPStyleGuide configureTableViewTextCell:_emailCell]; - cell = _emailCell; + [WPStyleGuide configureTableViewTextCell:cell]; + return cell; } else if (indexPath.row == 1) { - if (_usernameCell == nil) { - _usernameCell = [[UITableViewTextFieldCell alloc] initWithStyle:UITableViewCellStyleDefault - reuseIdentifier:@"TextCell"]; - } - _usernameCell.textLabel.text = NSLocalizedString(@"Username", @"Label for username field"); - _usernameTextField = _usernameCell.textField; - _usernameTextField.tag = CreateAccountUserNameTextFieldTag; + UITableViewTextFieldCell *cell = [self.tableView dequeueReusableCellWithIdentifier:TextFieldCellIdentifier]; + + cell.textLabel.text = NSLocalizedString(@"Username", @"Label for username field"); + _usernameTextField = cell.textField; _usernameTextField.placeholder = NSLocalizedString(@"Enter username", @"Help user enter username for log in"); _usernameTextField.keyboardType = UIKeyboardTypeEmailAddress; _usernameTextField.returnKeyType = UIReturnKeyNext; _usernameTextField.delegate = self; - [WPStyleGuide configureTableViewTextCell:_usernameCell]; - cell = _usernameCell; + [WPStyleGuide configureTableViewTextCell:cell]; + return cell; } else if (indexPath.row == 2) { - if (_passwordCell == nil) { - _passwordCell = [[UITableViewTextFieldCell alloc] initWithStyle:UITableViewCellStyleDefault - reuseIdentifier:@"TextCell"]; - } - _passwordCell.textLabel.text = NSLocalizedString(@"Password", @"Label for password field"); - _passwordTextField = _passwordCell.textField; - _passwordTextField.tag = CreateAccountPasswordTextFieldTag; + UITableViewTextFieldCell *cell = [self.tableView dequeueReusableCellWithIdentifier:TextFieldCellIdentifier]; + cell.textLabel.text = NSLocalizedString(@"Password", @"Label for password field"); + _passwordTextField = cell.textField; _passwordTextField.placeholder = NSLocalizedString(@"Enter password", @"Help user enter password for log in"); _passwordTextField.keyboardType = UIKeyboardTypeDefault; _passwordTextField.returnKeyType = UIReturnKeyNext; _passwordTextField.secureTextEntry = YES; _passwordTextField.delegate = self; - [WPStyleGuide configureTableViewTextCell:_passwordCell]; - - cell = _passwordCell; + [WPStyleGuide configureTableViewTextCell:cell]; + return cell; } else if (indexPath.row == 3) { - if (_blogUrlCell == nil) { - _blogUrlCell = [[UITableViewTextFieldCell alloc] initWithStyle:UITableViewCellStyleDefault - reuseIdentifier:@"TextCell"]; - } - _blogUrlCell.textLabel.text = NSLocalizedString(@"Site URL", @"Label for Site URL field in the Create a site window"); - _blogUrlTextField = _blogUrlCell.textField; - _blogUrlTextField.tag = CreateAccountBlogUrlTextFieldTag; + UITableViewTextFieldCell *cell = [self.tableView dequeueReusableCellWithIdentifier:TextFieldCellIdentifier]; + cell.textLabel.text = NSLocalizedString(@"Site URL", @"Label for Site URL field in the Create a site window"); + _blogUrlTextField = cell.textField; _blogUrlTextField.placeholder = NSLocalizedString(@"http://(choose-address).wordpress.com", @"Help user enter a URL for their new site"); _blogUrlTextField.keyboardType = UIKeyboardTypeURL; _blogUrlTextField.delegate = self; - [WPStyleGuide configureTableViewTextCell:_blogUrlCell]; - cell = _blogUrlCell; + [WPStyleGuide configureTableViewTextCell:cell]; + return cell; } else if (indexPath.row == 4) { - if (_localeCell == nil) { - _localeCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 - reuseIdentifier:@"LocaleCell"]; + UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:LocaleCellIdentifier]; + if (!cell) { + cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:LocaleCellIdentifier]; } - _localeCell.textLabel.text = @"Language"; - _localeCell.detailTextLabel.text = [_currentLanguage objectForKey:@"name"]; - _localeCell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; - [WPStyleGuide configureTableViewCell:_localeCell]; - cell = _localeCell; + cell.textLabel.text = @"Language"; + cell.detailTextLabel.text = [_currentLanguage objectForKey:@"name"]; + cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; + [WPStyleGuide configureTableViewCell:cell]; + return cell; } } - - return cell; + return nil; } -#pragma mark - -#pragma mark Table view delegate +#pragma mark - Table view delegate - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [tableView deselectRowAtIndexPath:indexPath animated:YES]; - if (_isCreatingAccount) + if (_isCreatingAccount) { return; + } if (indexPath.section == 0) { if (indexPath.row == 4) { @@ -264,18 +238,12 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath - (BOOL)textFieldShouldReturn:(UITextField *)textField { [textField resignFirstResponder]; - switch (textField.tag) { - case CreateAccountEmailTextFieldTag: - [_usernameTextField becomeFirstResponder]; - break; - case CreateAccountUserNameTextFieldTag: - [_passwordTextField becomeFirstResponder]; - break; - case CreateAccountPasswordTextFieldTag: - [_blogUrlTextField becomeFirstResponder]; - break; - default: - break; + if (textField == _emailTextField) { + [_usernameTextField becomeFirstResponder]; + } else if (textField == _usernameTextField) { + [_passwordTextField becomeFirstResponder]; + } else if (textField == _passwordTextField) { + [_blogUrlTextField becomeFirstResponder]; } return YES; @@ -290,7 +258,7 @@ - (BOOL)areFieldsValid - (BOOL)areInputFieldsFilled { - return [[_usernameTextField.text trim] length] != 0 && [[_passwordTextField.text trim] length] != 0 && [[_emailTextField.text trim] length] != 0 && [[_blogUrlTextField.text trim] length] != 0;; + return [[_usernameTextField.text trim] length] != 0 && [[_passwordTextField.text trim] length] != 0 && [[_emailTextField.text trim] length] != 0 && [[_blogUrlTextField.text trim] length] != 0; } - (BOOL)doesUrlHavePeriod @@ -362,7 +330,7 @@ - (void)didMoveToParentViewController:(UIViewController *)parent if (parent == nil) { self.delegate = nil; _userPressedBackButton = YES; - [_operationQueue cancelAllOperations]; + [self.operationQueue cancelAllOperations]; } } @@ -473,11 +441,11 @@ - (void)createUserAndBlog [userCreation addDependency:userValidation]; [blogValidation addDependency:userValidation]; - [_operationQueue addOperation:userValidation]; - [_operationQueue addOperation:blogValidation]; - [_operationQueue addOperation:userCreation]; - [_operationQueue addOperation:userSignIn]; - [_operationQueue addOperation:blogCreation]; + [self.operationQueue addOperation:userValidation]; + [self.operationQueue addOperation:blogValidation]; + [self.operationQueue addOperation:userCreation]; + [self.operationQueue addOperation:userSignIn]; + [self.operationQueue addOperation:blogCreation]; } - (void)processErrorDuringRemoteConnection:(NSError *)error diff --git a/WordPress/Classes/WPTableViewSectionFooterView.h b/WordPress/Classes/WPTableViewSectionFooterView.h index 1a7f71706cb2..3395d719cf2c 100644 --- a/WordPress/Classes/WPTableViewSectionFooterView.h +++ b/WordPress/Classes/WPTableViewSectionFooterView.h @@ -8,7 +8,7 @@ #import -@interface WPTableViewSectionFooterView : UIView +@interface WPTableViewSectionFooterView : UITableViewHeaderFooterView @property (nonatomic, strong) NSString *title; From 8238d9635539a00af03dd6fe1c01ee3ce5767b80 Mon Sep 17 00:00:00 2001 From: Rebecca Putinski Date: Tue, 3 Dec 2013 16:12:00 -0500 Subject: [PATCH 4/5] Use cell registration in note details - Remove other swipe view code from WPTableVC --- ...NotificationsCommentDetailViewController.m | 64 ++++++++----------- WordPress/Classes/WPTableViewController.m | 8 --- .../Classes/WPTableViewControllerSubclass.h | 15 ----- 3 files changed, 26 insertions(+), 61 deletions(-) diff --git a/WordPress/Classes/NotificationsCommentDetailViewController.m b/WordPress/Classes/NotificationsCommentDetailViewController.m index efec5234b21b..04bff42306b9 100644 --- a/WordPress/Classes/NotificationsCommentDetailViewController.m +++ b/WordPress/Classes/NotificationsCommentDetailViewController.m @@ -31,9 +31,9 @@ #define UNSPAM_BUTTON_TAG 6 const CGFloat NotificationsCommentDetailViewControllerReplyTextViewDefaultHeight = 64.f; -NSString * const NotificationsCommentHeaderCellIdentifiter = @"NoteCommentHeaderCell"; -NSString * const NotificationsCommentContentCellIdentifiter = @"NoteCommentContentCell"; -NSString * const NotificationsCommentLoadingCellIdentifiter = @"NoteCommentLoadingCell"; +NSString * const NoteCommentHeaderCellIdentifiter = @"NoteCommentHeaderCell"; +NSString * const NoteCommentContentCellIdentifiter = @"NoteCommentContentCell"; +NSString * const NoteCommentLoadingCellIdentifiter = @"NoteCommentLoadingCell"; NS_ENUM(NSUInteger, NotifcationCommentCellType){ NotificationCommentCellTypeHeader, NotificationCommentCellTypeContent @@ -61,7 +61,6 @@ - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { - // Custom initialization self.title = NSLocalizedString(@"Notification", @"Title for notification detail view"); self.hasScrollBackView = NO; self.contentCache = [[NSCache alloc] init]; @@ -97,19 +96,13 @@ - (void)viewDidLoad target:nil action:nil]; self.toolbar.items = @[self.approveBarButton, spacer, self.trashBarButton, spacer, self.spamBarButton, spacer, self.replyBarButton]; + [self.toolbar setBarTintColor:[WPStyleGuide littleEddieGrey]]; + self.toolbar.translucent = NO; - if (IS_IOS7) { - [self.toolbar setBarTintColor:[WPStyleGuide littleEddieGrey]]; - self.toolbar.translucent = NO; - } - - - if ([self.tableView respondsToSelector:@selector(registerClass:forCellReuseIdentifier:)]) { - [self.tableView registerClass:[NoteCommentCell class] - forCellReuseIdentifier:NotificationsCommentHeaderCellIdentifiter]; - [self.tableView registerClass:[DTAttributedTextCell class] - forCellReuseIdentifier:NotificationsCommentContentCellIdentifiter]; - } + [self.tableView registerClass:[NoteCommentCell class] forCellReuseIdentifier:NoteCommentHeaderCellIdentifiter]; + [self.tableView registerClass:[NoteCommentContentCell class] forCellReuseIdentifier:NoteCommentContentCellIdentifiter]; + [self.tableView registerClass:[NoteCommentLoadingCell class] forCellReuseIdentifier:NoteCommentLoadingCellIdentifiter]; + // create the reply field CGRect replyFrame = self.tableView.bounds; @@ -583,52 +576,47 @@ - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NoteComment *comment = [self.commentThread objectAtIndex:indexPath.section]; - BOOL mainComment = comment == [self.commentThread lastObject]; + BOOL mainComment = (comment == [self.commentThread lastObject]); + UITableViewCell *cell; switch (indexPath.row) { case NotificationCommentCellTypeHeader: { if (comment.isLoaded || mainComment) { - NoteCommentCell *headerCell; - headerCell = [tableView dequeueReusableCellWithIdentifier:NotificationsCommentHeaderCellIdentifiter]; - if (headerCell == nil) { - headerCell = [[NoteCommentCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NotificationsCommentHeaderCellIdentifiter]; - } - if ([comment isParentComment]) + NoteCommentCell *headerCell = [tableView dequeueReusableCellWithIdentifier:NoteCommentHeaderCellIdentifiter]; + if ([comment isParentComment]) { [headerCell displayAsParentComment]; + } headerCell.delegate = self; [self prepareCommentHeaderCell:headerCell forCommment:comment]; cell = headerCell; } else { - NoteCommentLoadingCell *loadingCell; - loadingCell = [tableView dequeueReusableCellWithIdentifier:NotificationsCommentLoadingCellIdentifiter]; - if (loadingCell == nil) { - loadingCell = [[NoteCommentLoadingCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NotificationsCommentLoadingCellIdentifiter]; - } + NoteCommentLoadingCell *loadingCell = [tableView dequeueReusableCellWithIdentifier:NoteCommentLoadingCellIdentifiter]; cell = loadingCell; } break; } case NotificationCommentCellTypeContent: { - NoteCommentContentCell *contentCell; - - contentCell = [self.contentCache objectForKey:comment]; - contentCell.delegate = self; - if (contentCell == nil) { - contentCell = [[NoteCommentContentCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NotificationsCommentContentCellIdentifiter]; + NoteCommentContentCell *contentCell = [tableView dequeueReusableCellWithIdentifier:NoteCommentContentCellIdentifiter]; + if ([self.contentCache objectForKey:comment]) { + contentCell = [self.contentCache objectForKey:comment]; + } else { + [self.contentCache setObject:contentCell forKey:comment]; } - [self.contentCache setObject:contentCell forKey:comment]; + + contentCell.delegate = self; NSString *html = [comment.commentData valueForKey:@"content"]; - if (!html) + if (!html) { html = self.note.commentText; - if (html != nil) { + } else { contentCell.attributedString = [self convertHTMLToAttributedString:html]; contentCell.selectionStyle = UITableViewCellSelectionStyleNone; } - if ([comment isParentComment]) + if ([comment isParentComment]) { [contentCell displayAsParentComment]; + } cell = contentCell; break; } diff --git a/WordPress/Classes/WPTableViewController.m b/WordPress/Classes/WPTableViewController.m index 4e6bafa87f92..40c1b7e54af9 100644 --- a/WordPress/Classes/WPTableViewController.m +++ b/WordPress/Classes/WPTableViewController.m @@ -141,7 +141,6 @@ - (void)viewWillDisappear:(BOOL)animated { } - (void)setEditing:(BOOL)editing animated:(BOOL)animated { - [self removeSwipeView:NO]; [super setEditing:editing animated:animated]; } @@ -265,13 +264,6 @@ - (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPat return NO; } -- (NSIndexPath *)tableView:(UITableView *)theTableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath { - if (!self.editing) { - [self removeSwipeView:YES]; - } - return indexPath; -} - #pragma mark - Fetched results controller - (UITableViewRowAnimation)tableViewRowAnimation { diff --git a/WordPress/Classes/WPTableViewControllerSubclass.h b/WordPress/Classes/WPTableViewControllerSubclass.h index b954ecbc0ac7..ac1664a5728d 100644 --- a/WordPress/Classes/WPTableViewControllerSubclass.h +++ b/WordPress/Classes/WPTableViewControllerSubclass.h @@ -152,21 +152,6 @@ */ - (void)loadMoreWithSuccess:(void (^)())success failure:(void (^)(NSError *error))failure; -/** - Configures the secondary view to show when you swipe on a cell - - Subclasses *MUST* implement this method if swipeActionsEnabled is YES - */ -- (void)configureSwipeView:(UIView *)swipeView forIndexPath:(NSIndexPath *)indexPath; - -/** - Removes the swipe view. - - Subclasses should call this method if one of the swipe actions needs to dismiss the secondary menu - */ -- (void)removeSwipeView:(BOOL)animated; - - /** Create a custom view to display to the user when there are no results to show. From df639fc190e115752cdbaef033b681ea510beeb1 Mon Sep 17 00:00:00 2001 From: Rebecca Putinski Date: Tue, 3 Dec 2013 17:18:25 -0500 Subject: [PATCH 5/5] Register cells in EditSiteVC + cleanup - Remove dead code - save button is hidden unless fields are filled, so validation code not used - Add red colour used to WPStyleGuide instead of macro --- WordPress/Classes/Constants.h | 2 - WordPress/Classes/EditSiteViewController.m | 243 +++++++++------------ WordPress/Classes/WPStyleGuide.h | 1 + WordPress/Classes/WPStyleGuide.m | 5 + 4 files changed, 112 insertions(+), 139 deletions(-) diff --git a/WordPress/Classes/Constants.h b/WordPress/Classes/Constants.h index 9ca9f521cb62..941e6016f1e4 100644 --- a/WordPress/Classes/Constants.h +++ b/WordPress/Classes/Constants.h @@ -72,8 +72,6 @@ #define PENDING_COMMENT_TABLE_VIEW_CELL_BACKGROUND_COLOR [UIColor colorWithRed:1.0 green:1.0 blue:170.0 / 255.0 alpha:1.0] #define PENDING_COMMENT_TABLE_VIEW_CELL_BORDER_COLOR [UIColor colorWithRed:226.0 / 255.0 green:215.0 / 255.0 blue:58.0 / 255.0 alpha:1.0] #define LOAD_MORE_DATA_TEXT_COLOR [UIColor colorWithRed:35.0 / 255.0 green:112.0 / 255.0 blue:216.0 / 255.0 alpha:1.0] -#define WRONG_FIELD_COLOR [UIColor colorWithRed:0.7 green:0.0 blue:0.0 alpha:1.0] -#define GOOD_FIELD_COLOR [UIColor blackColor] #define WP_LINK_COLOR [UIColor colorWithRed:0.0 / 255.0 green:117.0 / 255.0 blue:156.0 / 255.0 alpha:1.0] #define COMMENT_PARENT_BACKGROUND_COLOR [UIColor colorWithRed:222.0 / 255.0 green:222.0 / 255.0 blue:222.0 / 255.0 alpha:1.0] diff --git a/WordPress/Classes/EditSiteViewController.m b/WordPress/Classes/EditSiteViewController.m index f181fd650d76..ecc2eee1a464 100644 --- a/WordPress/Classes/EditSiteViewController.m +++ b/WordPress/Classes/EditSiteViewController.m @@ -19,11 +19,15 @@ #import #import +static NSString *const TextFieldCellIdentifier = @"TextFieldCellIdentifier"; +static NSString *const GeotaggingCellIdentifier = @"GeotaggingCellIdentifier"; +static NSString *const PushNotificationsCellIdentifier = @"PushNotificationsCellIdentifier"; +static NSString *const JetpackConnectedCellIdentifier = @"JetpackConnectedCellIdentifier"; + @interface EditSiteViewController () @property (nonatomic, copy) NSString *startingPwd, *startingUser, *startingUrl; -@property (nonatomic, strong) UITableViewTextFieldCell *urlCell, *usernameCell, *passwordCell; -@property (nonatomic, weak) UITextField *lastTextField; +@property (nonatomic, weak) UITextField *lastTextField, *usernameTextField, *passwordTextField, *urlTextField; @property (nonatomic, strong) UIActivityIndicatorView *savingIndicator; @property (nonatomic, strong) NSMutableDictionary *notificationPreferences; @property (nonatomic, strong) UIAlertView *failureAlertView; @@ -104,6 +108,10 @@ - (void)viewDidLoad { UITapGestureRecognizer *tgr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleViewTapped)]; tgr.cancelsTouchesInView = NO; [self.tableView addGestureRecognizer:tgr]; + + [self.tableView registerClass:[UITableViewTextFieldCell class] forCellReuseIdentifier:TextFieldCellIdentifier]; + [self.tableView registerClass:[WPTableViewCell class] forCellReuseIdentifier:GeotaggingCellIdentifier]; + [self.tableView registerClass:[WPTableViewCell class] forCellReuseIdentifier:PushNotificationsCellIdentifier]; } - (void)viewDidAppear:(BOOL)animated { @@ -164,106 +172,88 @@ - (NSString *)titleForHeaderInSection:(NSInteger)section { } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { - static NSString *const URLCellIdentifer = @"URLCell"; - static NSString *const UsernameCellIdentifier = @"UsernameCell"; - static NSString *const PasswordCellIdentifier = @"PasswordCell"; - if ([indexPath section] == 0) { if (indexPath.row == 0) { - self.urlCell = (UITableViewTextFieldCell *)[tableView dequeueReusableCellWithIdentifier:URLCellIdentifer]; - if (!self.urlCell) { - UITableViewTextFieldCell *urlCell = [[UITableViewTextFieldCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:URLCellIdentifer]; - self.urlCell = urlCell; - self.urlCell.textLabel.text = NSLocalizedString(@"URL", @""); - UITextField *urlField = self.urlCell.textField; - urlField.placeholder = NSLocalizedString(@"http://my-site-address (URL)", @"(placeholder) Help the user enter a URL into the field"); - urlField.keyboardType = UIKeyboardTypeURL; - [urlField addTarget:self action:@selector(showSaveButton) forControlEvents:UIControlEventEditingChanged]; - [self configureTextField:urlField asPassword:NO]; - urlField.keyboardType = UIKeyboardTypeURL; - if (self.blog.url != nil) { - urlField.text = self.blog.url; - - // Make a margin exception for URLs since they're so long - self.urlCell.minimumLabelWidth = 30; - } else { - urlField.text = @""; - } + UITableViewTextFieldCell *cell = [tableView dequeueReusableCellWithIdentifier:TextFieldCellIdentifier]; + self.urlTextField = cell.textField; + cell.textLabel.text = NSLocalizedString(@"URL", @""); + + self.urlTextField.placeholder = NSLocalizedString(@"http://my-site-address (URL)", @"(placeholder) Help the user enter a URL into the field"); + self.urlTextField.keyboardType = UIKeyboardTypeURL; + [self.urlTextField addTarget:self action:@selector(showSaveButton) forControlEvents:UIControlEventEditingChanged]; + [self configureTextField:self.urlTextField asPassword:NO]; + self.urlTextField.keyboardType = UIKeyboardTypeURL; + if (self.blog.url != nil) { + self.urlTextField.text = self.blog.url; - urlField.enabled = [self canEditUsernameAndURL]; - [WPStyleGuide configureTableViewTextCell:self.urlCell]; + // Make a margin exception for URLs since they're so long + cell.minimumLabelWidth = 30; + } else { + self.urlTextField.text = @""; } - return self.urlCell; + self.urlTextField.enabled = [self canEditUsernameAndURL]; + [WPStyleGuide configureTableViewTextCell:cell]; + + + return cell; } else if (indexPath.row == 1) { - self.usernameCell = [tableView dequeueReusableCellWithIdentifier:UsernameCellIdentifier]; - if (self.usernameCell == nil) { - self.usernameCell = [[UITableViewTextFieldCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:UsernameCellIdentifier]; - self.usernameCell.textLabel.text = NSLocalizedString(@"Username", @"Label for entering username in the username field"); - UITextField *usernameField = self.usernameCell.textField; - usernameField = self.usernameCell.textField; - usernameField.placeholder = NSLocalizedString(@"Enter username", @"(placeholder) Help enter WordPress username"); - [usernameField addTarget:self action:@selector(showSaveButton) forControlEvents:UIControlEventEditingChanged]; - [self configureTextField:usernameField asPassword:NO]; - if (self.blog.username != nil) { - usernameField.text = self.blog.username; - } else { - usernameField.text = @""; - } + UITableViewTextFieldCell *cell = [tableView dequeueReusableCellWithIdentifier:TextFieldCellIdentifier]; + + cell.textLabel.text = NSLocalizedString(@"Username", @"Label for entering username in the username field"); + self.usernameTextField = cell.textField; + self.usernameTextField.placeholder = NSLocalizedString(@"Enter username", @"(placeholder) Help enter WordPress username"); + [self.usernameTextField addTarget:self action:@selector(showSaveButton) forControlEvents:UIControlEventEditingChanged]; + [self configureTextField:self.usernameTextField asPassword:NO]; + if (self.blog.username != nil) { + self.usernameTextField.text = self.blog.username; + } else { + self.usernameTextField.text = @""; + } - usernameField.enabled = [self canEditUsernameAndURL]; - [WPStyleGuide configureTableViewTextCell:self.usernameCell]; - } + self.usernameTextField.enabled = [self canEditUsernameAndURL]; + [WPStyleGuide configureTableViewTextCell:cell]; - return self.usernameCell; + return cell; } else if (indexPath.row == 2) { - self.passwordCell = (UITableViewTextFieldCell *)[tableView dequeueReusableCellWithIdentifier:PasswordCellIdentifier]; - if (self.passwordCell == nil) { - self.passwordCell = [[UITableViewTextFieldCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:PasswordCellIdentifier]; - self.passwordCell.textLabel.text = NSLocalizedString(@"Password", @"Label for entering password in password field"); - UITextField *passwordField = self.passwordCell.textField; - passwordField = self.passwordCell.textField; - passwordField.placeholder = NSLocalizedString(@"Enter password", @"(placeholder) Help user enter password in password field"); - [passwordField addTarget:self action:@selector(showSaveButton) forControlEvents:UIControlEventEditingChanged]; - [self configureTextField:passwordField asPassword:YES]; - if (self.password != nil) { - passwordField.text = self.password; - } else { - passwordField.text = @""; - } - [WPStyleGuide configureTableViewTextCell:self.passwordCell]; - - // If the other rows can't be edited, it looks better to align the password to the right as well - if (![self canEditUsernameAndURL]) { - passwordField.textAlignment = NSTextAlignmentRight; - } - } + UITableViewTextFieldCell *cell = [tableView dequeueReusableCellWithIdentifier:TextFieldCellIdentifier]; + + cell.textLabel.text = NSLocalizedString(@"Password", @"Label for entering password in password field"); + self.passwordTextField = cell.textField; + self.passwordTextField.placeholder = NSLocalizedString(@"Enter password", @"(placeholder) Help user enter password in password field"); + [self.passwordTextField addTarget:self action:@selector(showSaveButton) forControlEvents:UIControlEventEditingChanged]; + [self configureTextField:self.passwordTextField asPassword:YES]; + if (self.password != nil) { + self.passwordTextField.text = self.password; + } else { + self.passwordTextField.text = @""; + } + [WPStyleGuide configureTableViewTextCell:cell]; + + // If the other rows can't be edited, it looks better to align the password to the right as well + if (![self canEditUsernameAndURL]) { + self.passwordTextField.textAlignment = NSTextAlignmentRight; + } - return self.passwordCell; + return cell; } } else if(indexPath.section == 1) { - if(indexPath.row == 0) { - UITableViewCell *geotaggingCell = [tableView dequeueReusableCellWithIdentifier:@"GeotaggingCell"]; - if(geotaggingCell == nil) { - geotaggingCell = [[WPTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"GeotaggingCell"]; - geotaggingCell.accessoryView = [[UISwitch alloc] init]; - } - UISwitch *geotaggingSwitch = (UISwitch *)geotaggingCell.accessoryView; + if (indexPath.row == 0) { + UITableViewCell *geotaggingCell = [tableView dequeueReusableCellWithIdentifier:GeotaggingCellIdentifier]; + UISwitch *geotaggingSwitch = [[UISwitch alloc] init]; geotaggingCell.textLabel.text = NSLocalizedString(@"Geotagging", @"Enables geotagging in blog settings (short label)"); geotaggingCell.selectionStyle = UITableViewCellSelectionStyleNone; geotaggingSwitch.on = self.geolocationEnabled; [geotaggingSwitch addTarget:self action:@selector(toggleGeolocation:) forControlEvents:UIControlEventValueChanged]; + geotaggingCell.accessoryView = geotaggingSwitch; [WPStyleGuide configureTableViewCell:geotaggingCell]; return geotaggingCell; + } else if(indexPath.row == 1) { - UITableViewCell *pushCell = [tableView dequeueReusableCellWithIdentifier:@"PushCell"]; - if(pushCell == nil) { - pushCell = [[WPTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"PushCell"]; - pushCell.accessoryView = [[UISwitch alloc] init]; - } - UISwitch *pushSwitch = (UISwitch *)pushCell.accessoryView; + UITableViewCell *pushCell = [tableView dequeueReusableCellWithIdentifier:PushNotificationsCellIdentifier]; + UISwitch *pushSwitch = [[UISwitch alloc] init]; pushCell.textLabel.text = NSLocalizedString(@"Push Notifications", @""); pushCell.selectionStyle = UITableViewCellSelectionStyleNone; pushSwitch.on = [self getBlogPushNotificationsSetting]; @@ -271,10 +261,9 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N return pushCell; } } else if (indexPath.section == 2) { - - UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"]; + UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:JetpackConnectedCellIdentifier]; if (!cell) { - cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"Cell"]; + cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:JetpackConnectedCellIdentifier]; } cell.textLabel.text = NSLocalizedString(@"Configure", @""); if (self.blog.jetpackUsername) { @@ -328,12 +317,12 @@ - (void)textFieldDidBeginEditing:(UITextField *)textField { } - (BOOL)textFieldShouldReturn:(UITextField *)textField { - if (textField == self.urlCell.textField) { - [self.usernameCell.textField becomeFirstResponder]; - } else if (textField == self.usernameCell.textField) { - [self.passwordCell.textField becomeFirstResponder]; - } else if (textField == self.passwordCell.textField) { - [self.passwordCell.textField resignFirstResponder]; + if (textField == self.urlTextField) { + [self.usernameTextField becomeFirstResponder]; + } else if (textField == self.usernameTextField) { + [self.passwordTextField becomeFirstResponder]; + } else if (textField == self.passwordTextField) { + [self.passwordTextField resignFirstResponder]; } return NO; } @@ -349,9 +338,9 @@ - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRang [result replaceCharactersInRange:range withString:string]; if ([result length] == 0) { - cell.textLabel.textColor = WRONG_FIELD_COLOR; + cell.textLabel.textColor = [WPStyleGuide validationErrorRed]; } else { - cell.textLabel.textColor = GOOD_FIELD_COLOR; + cell.textLabel.textColor = [WPStyleGuide whisperGrey]; } return YES; @@ -459,7 +448,7 @@ - (NSString *)getURLToValidate { NSString *urlToValidate = self.url; if (![urlToValidate hasPrefix:@"http"]) { - urlToValidate = [NSString stringWithFormat:@"http://%@", self.url]; + urlToValidate = [NSString stringWithFormat:@"http://%@", urlToValidate]; } NSError *error = nil; @@ -476,7 +465,7 @@ - (NSString *)getURLToValidate { } - (void)validateXmlprcURL:(NSURL *)xmlRpcURL { - WordPressXMLRPCApi *api = [WordPressXMLRPCApi apiWithXMLRPCEndpoint:xmlRpcURL username:self.usernameCell.textField.text password:self.passwordCell.textField.text]; + WordPressXMLRPCApi *api = [WordPressXMLRPCApi apiWithXMLRPCEndpoint:xmlRpcURL username:self.usernameTextField.text password:self.passwordTextField.text]; [api getBlogOptionsWithSuccess:^(id options){ if ([options objectForKey:@"wordpress.com"] != nil) { @@ -494,7 +483,7 @@ - (void)validateXmlprcURL:(NSURL *)xmlRpcURL { } - (void)loginForSiteWithXmlRpcUrl:(NSURL *)xmlRpcURL { - WordPressXMLRPCApi *api = [WordPressXMLRPCApi apiWithXMLRPCEndpoint:xmlRpcURL username:self.usernameCell.textField.text password:self.passwordCell.textField.text]; + WordPressXMLRPCApi *api = [WordPressXMLRPCApi apiWithXMLRPCEndpoint:xmlRpcURL username:self.usernameTextField.text password:self.passwordTextField.text]; [api getBlogsWithSuccess:^(NSArray *blogs) { [SVProgressHUD dismiss]; self.subsites = blogs; @@ -594,46 +583,26 @@ - (void)validationDidFail:(NSError *)error { } } -- (void)validateFields { - self.url = [NSURL IDNEncodedURL:self.urlCell.textField.text]; - DDLogInfo(@"blog url: %@", self.url); - self.username = self.usernameCell.textField.text; - self.password = self.passwordCell.textField.text; - - self.saveButton.enabled = NO; - [self.navigationItem setHidesBackButton:YES animated:NO]; - BOOL validFields = YES; - if ([self.urlCell.textField.text isEqualToString:@""]) { - validFields = NO; - self.urlCell.textLabel.textColor = WRONG_FIELD_COLOR; - } - if ([self.usernameCell.textField.text isEqualToString:@""]) { - validFields = NO; - self.usernameCell.textLabel.textColor = WRONG_FIELD_COLOR; - } - if ([self.passwordCell.textField.text isEqualToString:@""]) { - validFields = NO; - self.passwordCell.textLabel.textColor = WRONG_FIELD_COLOR; - } - - if (validFields) { - if (self.blog) { - // If we are editing an existing blog, use the known XML-RPC URL - // We don't allow editing URL on existing blogs, so XML-RPC shouldn't change - [self validateXmlprcURL:[NSURL URLWithString:self.blog.xmlrpc]]; - } else { - [self checkURL]; - } +- (void)validateUrl { + if (self.blog) { + // If we are editing an existing blog, use the known XML-RPC URL + // We don't allow editing URL on existing blogs, so XML-RPC shouldn't change + [self validateXmlprcURL:[NSURL URLWithString:self.blog.xmlrpc]]; } else { - [self validationDidFail:nil]; + [self checkURL]; } } - (void)save:(id)sender { - [self.urlCell.textField resignFirstResponder]; - [self.usernameCell.textField resignFirstResponder]; - [self.passwordCell.textField resignFirstResponder]; + [self.urlTextField resignFirstResponder]; + [self.usernameTextField resignFirstResponder]; + [self.passwordTextField resignFirstResponder]; + self.url = [NSURL IDNEncodedURL:self.urlTextField.text]; + DDLogInfo(@"blog url: %@", self.url); + self.username = self.usernameTextField.text; + self.password = self.passwordTextField.text; + if (!self.savingIndicator) { self.savingIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]; self.savingIndicator.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin; @@ -653,15 +622,15 @@ - (void)save:(id)sender { [self.blog dataSave]; } if (self.blog == nil || self.blog.username == nil) { - [self validateFields]; + [self validateUrl]; } else { - if ([self.startingUser isEqualToString:self.usernameCell.textField.text] && - [self.startingPwd isEqualToString:self.passwordCell.textField.text] && - [self.startingUrl isEqualToString:self.urlCell.textField.text]) { + if ([self.startingUser isEqualToString:self.usernameTextField.text] && + [self.startingPwd isEqualToString:self.passwordTextField.text] && + [self.startingUrl isEqualToString:self.urlTextField.text]) { // No need to check if nothing changed [self cancel:nil]; } else { - [self validateFields]; + [self validateUrl]; } } } @@ -683,9 +652,9 @@ - (IBAction)cancel:(id)sender { - (void)showSaveButton { BOOL hasContent; - if ([self.urlCell.textField.text isEqualToString:@""] || - [self.usernameCell.textField.text isEqualToString:@""] || - [self.passwordCell.textField.text isEqualToString:@""]) { + if ([self.urlTextField.text isEqualToString:@""] || + [self.usernameTextField.text isEqualToString:@""] || + [self.passwordTextField.text isEqualToString:@""]) { hasContent = NO; } else { hasContent = YES; diff --git a/WordPress/Classes/WPStyleGuide.h b/WordPress/Classes/WPStyleGuide.h index 52b93abdc231..d50b13399913 100644 --- a/WordPress/Classes/WPStyleGuide.h +++ b/WordPress/Classes/WPStyleGuide.h @@ -46,6 +46,7 @@ + (UIColor *)readGrey; + (UIColor *)itsEverywhereGrey; + (UIColor *)darkAsNightGrey; ++ (UIColor *)validationErrorRed; + (UIColor *)tableViewActionColor; + (UIColor *)buttonActionColor; diff --git a/WordPress/Classes/WPStyleGuide.m b/WordPress/Classes/WPStyleGuide.m index 6cf2da6738d5..8a894d8eb4ff 100644 --- a/WordPress/Classes/WPStyleGuide.m +++ b/WordPress/Classes/WPStyleGuide.m @@ -202,6 +202,11 @@ + (UIColor *)darkAsNightGrey return [UIColor colorWithRed:16/255.0f green:16/255.0f blue:16/255.0f alpha:1.0f]; } ++ (UIColor *)validationErrorRed +{ + return [UIColor colorWithRed:0.7 green:0.0 blue:0.0 alpha:1.0]; +} + + (UIColor *)tableViewActionColor { return [WPStyleGuide baseLighterBlue];